1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | var __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 | };
|
23 | Object.defineProperty(exports, "__esModule", { value: true });
|
24 | const utils_1 = require("./utils");
|
25 | class 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 |
|
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 |
|
69 |
|
70 | class DependencyGraph {
|
71 | constructor(from) {
|
72 | this._documents = new Map();
|
73 | if (!from) {
|
74 | return;
|
75 | }
|
76 |
|
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 |
|
91 |
|
92 |
|
93 |
|
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 |
|
109 |
|
110 |
|
111 |
|
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 |
|
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 |
|
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 |
|
151 |
|
152 |
|
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 |
|
164 |
|
165 |
|
166 |
|
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 | }
|
194 | exports.DependencyGraph = DependencyGraph;
|
195 |
|
\ | No newline at end of file |