1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | 'use strict';
|
11 | var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
12 | return new (P || (P = Promise))(function (resolve, reject) {
|
13 | function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
14 | function rejected(value) { try { step(generator.throw(value)); } catch (e) { reject(e); } }
|
15 | function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
|
16 | step((generator = generator.apply(thisArg, _arguments)).next());
|
17 | });
|
18 | };
|
19 | const interruptHandlers = [];
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 | function onInterrupt(handler) {
|
27 | interruptHandlers.push(handler);
|
28 | }
|
29 | exports.onInterrupt = onInterrupt;
|
30 |
|
31 |
|
32 |
|
33 | function promiseAllStrict(promises) {
|
34 | return __awaiter(this, void 0, Promise, function* () {
|
35 | let errors = yield Promise.all(promises.map((p) => p.then(() => null, (e) => e)));
|
36 | let firstError = errors.find((e) => e != null);
|
37 | if (firstError) {
|
38 | throw firstError;
|
39 | }
|
40 | });
|
41 | }
|
42 | exports.promiseAllStrict = promiseAllStrict;
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 | function close() {
|
49 | return __awaiter(this, void 0, Promise, function* () {
|
50 | const promises = interruptHandlers.map((handler) => handler());
|
51 |
|
52 | interruptHandlers.length = 0;
|
53 | yield promiseAllStrict(promises);
|
54 | });
|
55 | }
|
56 | exports.close = close;
|
57 | let interrupted = false;
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 | function interrupt() {
|
68 | if (interrupted) {
|
69 | console.log('\nGot multiple interrupts, exiting immediately!');
|
70 | return process.exit(1);
|
71 | }
|
72 | interrupted = true;
|
73 | close().then(() => process.exit(0), (error) => {
|
74 | error = error || 'cleankill interrupt handler failed without a message';
|
75 | console.error(error.stack || error.message || error);
|
76 | process.exit(2);
|
77 | });
|
78 | console.log('\nShutting down. Press ctrl-c again to kill immediately.');
|
79 | }
|
80 | exports.interrupt = interrupt;
|
81 | process.on('SIGINT', interrupt);
|