/
DeltaCalculator.js
260 lines (223 loc) Β· 7.28 KB
/
DeltaCalculator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict-local
* @format
*/
'use strict';
const {
initialTraverseDependencies,
reorderGraph,
traverseDependencies,
} = require('./traverseDependencies');
const {EventEmitter} = require('events');
import type DependencyGraph from '../node-haste/DependencyGraph';
import type {DeltaResult, Graph, Options} from './types.flow';
/**
* This class is in charge of calculating the delta of changed modules that
* happen between calls. To do so, it subscribes to file changes, so it can
* traverse the files that have been changed between calls and avoid having to
* traverse the whole dependency tree for trivial small changes.
*/
class DeltaCalculator<T> extends EventEmitter {
_dependencyGraph: DependencyGraph;
_options: Options<T>;
_currentBuildPromise: ?Promise<DeltaResult<T>>;
_deletedFiles: Set<string> = new Set();
_modifiedFiles: Set<string> = new Set();
_graph: Graph<T>;
constructor(
entryPoints: $ReadOnlyArray<string>,
dependencyGraph: DependencyGraph,
options: Options<T>,
) {
super();
this._options = options;
this._dependencyGraph = dependencyGraph;
this._graph = {
dependencies: new Map(),
entryPoints,
importBundleNames: new Set(),
transformOptions: this._options.transformOptions,
};
this._dependencyGraph
.getWatcher()
.on('change', this._handleMultipleFileChanges);
}
/**
* Stops listening for file changes and clears all the caches.
*/
end(): void {
this._dependencyGraph
.getWatcher()
.removeListener('change', this._handleMultipleFileChanges);
this.removeAllListeners();
// Clean up all the cache data structures to deallocate memory.
this._graph = {
dependencies: new Map(),
entryPoints: this._graph.entryPoints,
importBundleNames: new Set(),
transformOptions: this._options.transformOptions,
};
this._modifiedFiles = new Set();
this._deletedFiles = new Set();
}
/**
* Main method to calculate the delta of modules. It returns a DeltaResult,
* which contain the modified/added modules and the removed modules.
*/
async getDelta({
reset,
shallow,
}: {
reset: boolean,
shallow: boolean,
...
}): Promise<DeltaResult<T>> {
// If there is already a build in progress, wait until it finish to start
// processing a new one (delta server doesn't support concurrent builds).
if (this._currentBuildPromise) {
await this._currentBuildPromise;
}
// We don't want the modified files Set to be modified while building the
// bundle, so we isolate them by using the current instance for the bundling
// and creating a new instance for the file watcher.
const modifiedFiles = this._modifiedFiles;
this._modifiedFiles = new Set();
const deletedFiles = this._deletedFiles;
this._deletedFiles = new Set();
// Concurrent requests should reuse the same bundling process. To do so,
// this method stores the promise as an instance variable, and then it's
// removed after it gets resolved.
this._currentBuildPromise = this._getChangedDependencies(
modifiedFiles,
deletedFiles,
);
let result;
const numDependencies = this._graph.dependencies.size;
try {
result = await this._currentBuildPromise;
} catch (error) {
// In case of error, we don't want to mark the modified files as
// processed (since we haven't actually created any delta). If we do not
// do so, asking for a delta after an error will produce an empty Delta,
// which is not correct.
modifiedFiles.forEach((file: string) => this._modifiedFiles.add(file));
deletedFiles.forEach((file: string) => this._deletedFiles.add(file));
// If after an error the number of modules has changed, we could be in
// a weird state. As a safe net we clean the dependency modules to force
// a clean traversal of the graph next time.
if (this._graph.dependencies.size !== numDependencies) {
this._graph.dependencies = new Map();
}
throw error;
} finally {
this._currentBuildPromise = null;
}
// Return all the modules if the client requested a reset delta.
if (reset) {
reorderGraph(this._graph, {shallow});
return {
added: this._graph.dependencies,
modified: new Map(),
deleted: new Set(),
reset: true,
};
}
return result;
}
/**
* Returns the graph with all the dependencies. Each module contains the
* needed information to do the traversing (dependencies, inverseDependencies)
* plus some metadata.
*/
getGraph(): Graph<T> {
return this._graph;
}
_handleMultipleFileChanges = ({eventsQueue}) => {
eventsQueue.forEach(this._handleFileChange);
};
/**
* Handles a single file change. To avoid doing any work before it's needed,
* the listener only stores the modified file, which will then be used later
* when the delta needs to be calculated.
*/
_handleFileChange = ({
type,
filePath,
}: {
type: string,
filePath: string,
...
}): mixed => {
if (type === 'delete') {
this._deletedFiles.add(filePath);
this._modifiedFiles.delete(filePath);
} else {
this._deletedFiles.delete(filePath);
this._modifiedFiles.add(filePath);
}
// Notify users that there is a change in some of the bundle files. This
// way the client can choose to refetch the bundle.
this.emit('change');
};
async _getChangedDependencies(
modifiedFiles: Set<string>,
deletedFiles: Set<string>,
): Promise<DeltaResult<T>> {
if (!this._graph.dependencies.size) {
const {added} = await initialTraverseDependencies(
this._graph,
this._options,
);
return {
added,
modified: new Map(),
deleted: new Set(),
reset: true,
};
}
// If a file has been deleted, we want to invalidate any other file that
// depends on it, so we can process it and correctly return an error.
deletedFiles.forEach((filePath: string) => {
const module = this._graph.dependencies.get(filePath);
if (module) {
module.inverseDependencies.forEach((path: string) => {
// Only mark the inverse dependency as modified if it's not already
// marked as deleted (in that case we can just ignore it).
if (!deletedFiles.has(path)) {
modifiedFiles.add(path);
}
});
}
});
// We only want to process files that are in the bundle.
const modifiedDependencies = Array.from(
modifiedFiles,
).filter((filePath: string) => this._graph.dependencies.has(filePath));
// No changes happened. Return empty delta.
if (modifiedDependencies.length === 0) {
return {
added: new Map(),
modified: new Map(),
deleted: new Set(),
reset: false,
};
}
const {added, modified, deleted} = await traverseDependencies(
modifiedDependencies,
this._graph,
this._options,
);
return {
added,
modified,
deleted,
reset: false,
};
}
}
module.exports = DeltaCalculator;