UNPKG

7.29 kBJavaScriptView Raw
1"use strict";
2/**
3 * @license
4 * Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
5 * This code may only be used under the BSD style license found at
6 * http://polymer.github.io/LICENSE.txt
7 * The complete set of authors may be found at
8 * http://polymer.github.io/AUTHORS.txt
9 * The complete set of contributors may be found at
10 * http://polymer.github.io/CONTRIBUTORS.txt
11 * Code distributed by Google as part of the polymer project is also
12 * subject to an additional IP rights grant found at
13 * http://polymer.github.io/PATENTS.txt
14 */
15var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
16 return new (P || (P = Promise))(function (resolve, reject) {
17 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
18 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
19 function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
20 step((generator = generator.apply(thisArg, _arguments || [])).next());
21 });
22};
23Object.defineProperty(exports, "__esModule", { value: true });
24const utils_1 = require("./utils");
25class DocumentRecord {
26 static from(from) {
27 return new DocumentRecord(from);
28 }
29 constructor(urlOrFrom) {
30 if (!(urlOrFrom instanceof DocumentRecord)) {
31 this.url = urlOrFrom;
32 this.dependencies = new Set();
33 this.dependants = new Set();
34 this.dependenciesDeferred = new utils_1.Deferred();
35 }
36 else {
37 const from = urlOrFrom;
38 this.url = from.url;
39 this.dependencies = from.dependencies;
40 this.dependants = from.dependants;
41 this.dependenciesDeferred = new utils_1.Deferred();
42 if (from.dependenciesDeferred.resolved) {
43 this.dependenciesDeferred.resolve(this.dependencies);
44 }
45 else if (from.dependenciesDeferred.rejected) {
46 this.dependenciesDeferred.reject(from.dependenciesDeferred.error);
47 }
48 }
49 this.dependenciesDeferred.promise.catch((_) => {
50 // no one listens for document rejections yet,
51 });
52 }
53 get dependenciesKnown() {
54 return this.dependenciesDeferred.promise;
55 }
56 toString() {
57 let s = `${this.url}\n`;
58 for (const dependency of this.dependencies) {
59 s += ` -> ${dependency}\n`;
60 }
61 for (const dependant of this.dependants) {
62 s += ` <- ${dependant}\n`;
63 }
64 return s;
65 }
66}
67/**
68 * Maintains bidirectional indexes of the dependency graph, for quick querying.
69 */
70class DependencyGraph {
71 constructor(from) {
72 this._documents = new Map();
73 if (!from) {
74 return;
75 }
76 // Deep copy of `from`
77 for (const entry of from._documents.entries()) {
78 this._documents.set(entry[0], DocumentRecord.from(entry[1]));
79 }
80 }
81 _getRecordFor(url) {
82 let record = this._documents.get(url);
83 if (record == null) {
84 record = new DocumentRecord(url);
85 this._documents.set(url, record);
86 }
87 return record;
88 }
89 /**
90 * Add dependencies of the given path.
91 *
92 * @param url The url of a document.
93 * @param newDependencies The paths of that document's direct dependencies.
94 */
95 addDocument(url, dependencies) {
96 const record = this._getRecordFor(url);
97 for (const dependency of dependencies) {
98 record.dependencies.add(dependency);
99 const dependencyRecord = this._getRecordFor(dependency);
100 dependencyRecord.dependants.add(url);
101 }
102 record.dependenciesDeferred.resolve(record.dependencies);
103 }
104 rejectDocument(url, error) {
105 this._getRecordFor(url).dependenciesDeferred.reject(error);
106 }
107 /**
108 * Returns a Promise that resolves when the given document and all
109 * of its transitive dependencies have been resolved or rejected. This
110 * Promise never rejects, if the document or any dependencies are rejected,
111 * the Promise still resolves.
112 */
113 whenReady(url) {
114 return __awaiter(this, void 0, void 0, function* () {
115 yield this._whenReady(url, new Set());
116 });
117 }
118 _whenReady(key, visited) {
119 return __awaiter(this, void 0, void 0, function* () {
120 if (visited.has(key)) {
121 return;
122 }
123 visited.add(key);
124 const dependenciesKnown = this._getRecordFor(key).dependenciesKnown;
125 const forgivingDependenciesKnown = dependenciesKnown.catch((_) => []);
126 const deps = yield forgivingDependenciesKnown;
127 for (const dep of deps) {
128 yield this._whenReady(dep, visited);
129 }
130 });
131 }
132 /**
133 * Returns a fork of this graph without the documents at the given paths.
134 */
135 invalidatePaths(paths) {
136 const fork = new DependencyGraph(this);
137 for (const path of paths) {
138 const record = fork._documents.get(path);
139 if (!record) {
140 continue;
141 }
142 // Tell the dependencies that `path` is no longer one of their dependants.
143 for (const dependency of record.dependencies) {
144 const dependencyRecord = fork._documents.get(dependency);
145 if (dependencyRecord) {
146 dependencyRecord.dependants.delete(path);
147 }
148 }
149 fork._documents.delete(path);
150 // If there are dependents on this record, we must preserve them,
151 // as they're only added with an addDocument() call, and there are
152 // never repeated addDocument() calls for the same path.
153 if (record.dependants.size > 0) {
154 const newRecord = fork._getRecordFor(record.url);
155 for (const dependant of record.dependants) {
156 newRecord.dependants.add(dependant);
157 }
158 }
159 }
160 return fork;
161 }
162 /**
163 * Returns the set of transitive dependencies on the given path.
164 *
165 * So if A depends on B which depends on C, then getAllDependentsOf(C) will
166 * be Set([A,B]), and getAllDependantsOf(B) will be Set([A]).
167 */
168 getAllDependantsOf(path) {
169 const result = new Set();
170 this._getAllDependantsOf(path, new Set(), result);
171 return result;
172 }
173 _getAllDependantsOf(path, visited, result) {
174 if (visited.has(path)) {
175 return;
176 }
177 visited.add(path);
178 const record = this._documents.get(path);
179 if (!record) {
180 return;
181 }
182 const dependants = record.dependants;
183 for (const dependant of dependants) {
184 result.add(dependant);
185 this._getAllDependantsOf(dependant, visited, result);
186 }
187 }
188 toString() {
189 return Array.from(this._documents.values())
190 .map((dr) => dr.toString())
191 .join('\n');
192 }
193}
194exports.DependencyGraph = DependencyGraph;
195//# sourceMappingURL=dependency-graph.js.map
\No newline at end of file