1 | var __extends = (this && this.__extends) || (function () {
|
2 | var extendStatics = function (d, b) {
|
3 | extendStatics = Object.setPrototypeOf ||
|
4 | ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
5 | function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
6 | return extendStatics(d, b);
|
7 | };
|
8 | return function (d, b) {
|
9 | if (typeof b !== "function" && b !== null)
|
10 | throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
11 | extendStatics(d, b);
|
12 | function __() { this.constructor = d; }
|
13 | d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
14 | };
|
15 | })();
|
16 | var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
17 | function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
18 | return new (P || (P = Promise))(function (resolve, reject) {
|
19 | function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
20 | function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
21 | function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
22 | step((generator = generator.apply(thisArg, _arguments || [])).next());
|
23 | });
|
24 | };
|
25 | var __generator = (this && this.__generator) || function (thisArg, body) {
|
26 | var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
27 | return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
28 | function verb(n) { return function (v) { return step([n, v]); }; }
|
29 | function step(op) {
|
30 | if (f) throw new TypeError("Generator is already executing.");
|
31 | while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
32 | if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
33 | if (y = 0, t) op = [op[0] & 2, t.value];
|
34 | switch (op[0]) {
|
35 | case 0: case 1: t = op; break;
|
36 | case 4: _.label++; return { value: op[1], done: false };
|
37 | case 5: _.label++; y = op[1]; op = [0]; continue;
|
38 | case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
39 | default:
|
40 | if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
41 | if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
42 | if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
43 | if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
44 | if (t[2]) _.ops.pop();
|
45 | _.trys.pop(); continue;
|
46 | }
|
47 | op = body.call(thisArg, _);
|
48 | } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
49 | if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
50 | }
|
51 | };
|
52 | var __values = (this && this.__values) || function(o) {
|
53 | var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
|
54 | if (m) return m.call(o);
|
55 | if (o && typeof o.length === "number") return {
|
56 | next: function () {
|
57 | if (o && i >= o.length) o = void 0;
|
58 | return { value: o && o[i++], done: !o };
|
59 | }
|
60 | };
|
61 | throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
62 | };
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 | var BackgroundProcessManager = (function () {
|
76 | |
77 |
|
78 |
|
79 |
|
80 |
|
81 | function BackgroundProcessManager() {
|
82 | |
83 |
|
84 |
|
85 |
|
86 |
|
87 | this._state = BackgroundProcessManagerState.Open;
|
88 | |
89 |
|
90 |
|
91 | this.jobs = new Set();
|
92 | }
|
93 | BackgroundProcessManager.prototype.add = function (jobOrDescription, optionalDescription) {
|
94 | var job;
|
95 | var description;
|
96 | if (typeof jobOrDescription === 'string') {
|
97 | job = undefined;
|
98 | description = jobOrDescription;
|
99 | }
|
100 | else {
|
101 | job = jobOrDescription;
|
102 | description = optionalDescription;
|
103 | }
|
104 | var error = this.closedFailure(description);
|
105 | if (error)
|
106 | return error;
|
107 | if (job === undefined) {
|
108 | return this.addHook(description);
|
109 | }
|
110 | else if (typeof job === 'function') {
|
111 | return this.addFunction(job, description);
|
112 | }
|
113 | else if (job instanceof BackgroundProcessManager) {
|
114 | return this.addManager(job, description);
|
115 | }
|
116 | else {
|
117 | throw new Error('If `job` is provided, it must be an Observable, Function, or BackgroundProcessManager.');
|
118 | }
|
119 | };
|
120 | |
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 | BackgroundProcessManager.prototype.addCleaner = function (clean, description) {
|
131 | var _this = this;
|
132 | var _a = this.addHook(description), resolve = _a.resolve, onTerminate = _a.onTerminate;
|
133 | var proxy = function () { return __awaiter(_this, void 0, void 0, function () {
|
134 | return __generator(this, function (_a) {
|
135 | switch (_a.label) {
|
136 | case 0: return [4 , clean()];
|
137 | case 1:
|
138 | _a.sent();
|
139 | resolve();
|
140 | return [2 ];
|
141 | }
|
142 | });
|
143 | }); };
|
144 | onTerminate.then(proxy);
|
145 | return proxy;
|
146 | };
|
147 | BackgroundProcessManager.prototype.addFunction = function (job, description) {
|
148 |
|
149 | var terminate;
|
150 |
|
151 | var onTerminate = new Promise(function (resolve) {
|
152 | terminate = resolve;
|
153 | });
|
154 |
|
155 | var jobResult = job(onTerminate);
|
156 |
|
157 |
|
158 | if (typeof (jobResult === null || jobResult === void 0 ? void 0 : jobResult.then) === 'function') {
|
159 | this.registerPromise(jobResult, terminate, description);
|
160 | }
|
161 |
|
162 |
|
163 |
|
164 | return jobResult;
|
165 | };
|
166 | BackgroundProcessManager.prototype.addManager = function (manager, description) {
|
167 | var _this = this;
|
168 | this.addCleaner(function () { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) {
|
169 | switch (_a.label) {
|
170 | case 0: return [4 , manager.close()];
|
171 | case 1: return [2 , _a.sent()];
|
172 | }
|
173 | }); }); }, description);
|
174 | };
|
175 | |
176 |
|
177 |
|
178 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 |
|
184 |
|
185 | BackgroundProcessManager.prototype.addHook = function (description) {
|
186 |
|
187 |
|
188 | var resolve;
|
189 | var reject;
|
190 |
|
191 |
|
192 | var promise = new Promise(function (res, rej) {
|
193 | resolve = res;
|
194 | reject = rej;
|
195 | });
|
196 |
|
197 | var terminate;
|
198 |
|
199 | var onTerminate = new Promise(function (resolveTerminate) {
|
200 | terminate = resolveTerminate;
|
201 | });
|
202 | this.registerPromise(promise, terminate, description);
|
203 | return {
|
204 | resolve: resolve,
|
205 | reject: reject,
|
206 | onTerminate: onTerminate,
|
207 | };
|
208 | };
|
209 | |
210 |
|
211 |
|
212 |
|
213 |
|
214 |
|
215 |
|
216 |
|
217 |
|
218 |
|
219 |
|
220 | BackgroundProcessManager.prototype.registerPromise = function (promise, terminate, description) {
|
221 | var _this = this;
|
222 | var jobEntry = { promise: promise, terminate: terminate, description: description };
|
223 | this.jobs.add(jobEntry);
|
224 |
|
225 |
|
226 |
|
227 |
|
228 |
|
229 |
|
230 |
|
231 |
|
232 |
|
233 |
|
234 | promise
|
235 | .then(function () {
|
236 | _this.jobs.delete(jobEntry);
|
237 | })
|
238 | .catch(function () {
|
239 | _this.jobs.delete(jobEntry);
|
240 | });
|
241 | };
|
242 | Object.defineProperty(BackgroundProcessManager.prototype, "length", {
|
243 | |
244 |
|
245 |
|
246 |
|
247 |
|
248 |
|
249 |
|
250 |
|
251 | get: function () {
|
252 | return this.jobs.size;
|
253 | },
|
254 | enumerable: false,
|
255 | configurable: true
|
256 | });
|
257 | Object.defineProperty(BackgroundProcessManager.prototype, "state", {
|
258 | |
259 |
|
260 |
|
261 |
|
262 |
|
263 |
|
264 |
|
265 | get: function () {
|
266 | return this._state;
|
267 | },
|
268 | enumerable: false,
|
269 | configurable: true
|
270 | });
|
271 | Object.defineProperty(BackgroundProcessManager.prototype, "pending", {
|
272 | |
273 |
|
274 |
|
275 |
|
276 |
|
277 | get: function () {
|
278 | return Array.from(this.jobs).map(function (job) { return job.description; });
|
279 | },
|
280 | enumerable: false,
|
281 | configurable: true
|
282 | });
|
283 | Object.defineProperty(BackgroundProcessManager.prototype, "isOpen", {
|
284 | |
285 |
|
286 |
|
287 | get: function () {
|
288 | return this._state === BackgroundProcessManagerState.Open;
|
289 | },
|
290 | enumerable: false,
|
291 | configurable: true
|
292 | });
|
293 | Object.defineProperty(BackgroundProcessManager.prototype, "isClosing", {
|
294 | |
295 |
|
296 |
|
297 |
|
298 | get: function () {
|
299 | return this._state === BackgroundProcessManagerState.Closing;
|
300 | },
|
301 | enumerable: false,
|
302 | configurable: true
|
303 | });
|
304 | Object.defineProperty(BackgroundProcessManager.prototype, "isClosed", {
|
305 | |
306 |
|
307 |
|
308 |
|
309 | get: function () {
|
310 | return this._state === BackgroundProcessManagerState.Closed;
|
311 | },
|
312 | enumerable: false,
|
313 | configurable: true
|
314 | });
|
315 | BackgroundProcessManager.prototype.closedFailure = function (description) {
|
316 | if (!this.isOpen) {
|
317 | return Promise.reject(new BackgroundManagerNotOpenError([
|
318 | "The manager is ".concat(this.state, "."),
|
319 | "You tried to add \"".concat(description, "\"."),
|
320 | "Pending jobs: [\n".concat(this.pending
|
321 | .map(function (t) { return ' ' + t; })
|
322 | .join(',\n'), "\n]"),
|
323 | ].join('\n')));
|
324 | }
|
325 | };
|
326 | |
327 |
|
328 |
|
329 |
|
330 |
|
331 |
|
332 |
|
333 |
|
334 |
|
335 |
|
336 |
|
337 |
|
338 |
|
339 |
|
340 |
|
341 |
|
342 |
|
343 | BackgroundProcessManager.prototype.close = function () {
|
344 | return __awaiter(this, void 0, void 0, function () {
|
345 | var _a, _b, job;
|
346 | var e_1, _c;
|
347 | return __generator(this, function (_d) {
|
348 | switch (_d.label) {
|
349 | case 0:
|
350 | if (!this.isOpen) return [3 , 2];
|
351 | this._state = BackgroundProcessManagerState.Closing;
|
352 | try {
|
353 | for (_a = __values(Array.from(this.jobs)), _b = _a.next(); !_b.done; _b = _a.next()) {
|
354 | job = _b.value;
|
355 | try {
|
356 | job.terminate();
|
357 | }
|
358 | catch (error) {
|
359 |
|
360 |
|
361 |
|
362 | console.warn("Failed to send termination signal to job. Error: ".concat(error.message), job);
|
363 | }
|
364 | }
|
365 | }
|
366 | catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
367 | finally {
|
368 | try {
|
369 | if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
|
370 | }
|
371 | finally { if (e_1) throw e_1.error; }
|
372 | }
|
373 |
|
374 |
|
375 | this._closingPromise = Promise.allSettled(Array.from(this.jobs).map(function (j) { return j.promise; }));
|
376 | return [4 , this._closingPromise];
|
377 | case 1:
|
378 | _d.sent();
|
379 | this._state = BackgroundProcessManagerState.Closed;
|
380 | _d.label = 2;
|
381 | case 2: return [2 , this._closingPromise];
|
382 | }
|
383 | });
|
384 | });
|
385 | };
|
386 | |
387 |
|
388 |
|
389 |
|
390 |
|
391 |
|
392 |
|
393 |
|
394 |
|
395 |
|
396 |
|
397 | BackgroundProcessManager.prototype.open = function () {
|
398 | return __awaiter(this, void 0, void 0, function () {
|
399 | return __generator(this, function (_a) {
|
400 | switch (_a.label) {
|
401 | case 0:
|
402 | if (!this.isClosing) return [3 , 2];
|
403 | return [4 , this.close()];
|
404 | case 1:
|
405 | _a.sent();
|
406 | _a.label = 2;
|
407 | case 2:
|
408 | this._state = BackgroundProcessManagerState.Open;
|
409 | return [2 ];
|
410 | }
|
411 | });
|
412 | });
|
413 | };
|
414 | return BackgroundProcessManager;
|
415 | }());
|
416 | export { BackgroundProcessManager };
|
417 |
|
418 |
|
419 |
|
420 | var BackgroundManagerNotOpenError = (function (_super) {
|
421 | __extends(BackgroundManagerNotOpenError, _super);
|
422 | function BackgroundManagerNotOpenError(message) {
|
423 | return _super.call(this, "BackgroundManagerNotOpenError: ".concat(message)) || this;
|
424 | }
|
425 | return BackgroundManagerNotOpenError;
|
426 | }(Error));
|
427 | export { BackgroundManagerNotOpenError };
|
428 |
|
429 |
|
430 |
|
431 | export var BackgroundProcessManagerState;
|
432 | (function (BackgroundProcessManagerState) {
|
433 | |
434 |
|
435 |
|
436 | BackgroundProcessManagerState["Open"] = "Open";
|
437 | |
438 |
|
439 |
|
440 | BackgroundProcessManagerState["Closing"] = "Closing";
|
441 | |
442 |
|
443 |
|
444 | BackgroundProcessManagerState["Closed"] = "Closed";
|
445 | })(BackgroundProcessManagerState || (BackgroundProcessManagerState = {}));
|