UNPKG

3.4 kBJavaScriptView Raw
1/**
2 * @license
3 * Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
4 * This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
5 * The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
6 * The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
7 * Code distributed by Google as part of the polymer project is also
8 * subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
9 */
10'use strict';
11var __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};
19const interruptHandlers = [];
20/**
21 * Register a handler to occur on SIGINT. The handler must return a promise if
22 * it has any asynchronous work to do. The process will be terminated once
23 * all handlers complete. If a handler throws or the promise it returns rejects
24 * then the process will be terminated immediately.
25 */
26function onInterrupt(handler) {
27 interruptHandlers.push(handler);
28}
29exports.onInterrupt = onInterrupt;
30/**
31 * Waits for all promises to settle, then rejects with the first error, if any.
32 */
33function 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}
42exports.promiseAllStrict = promiseAllStrict;
43/**
44 * Call all interrupt handlers, and call the callback when they all complete.
45 *
46 * Clears the list of interrupt handlers.
47 */
48function close() {
49 return __awaiter(this, void 0, Promise, function* () {
50 const promises = interruptHandlers.map((handler) => handler());
51 // Empty the array in place. Looks weird, totally works.
52 interruptHandlers.length = 0;
53 yield promiseAllStrict(promises);
54 });
55}
56exports.close = close;
57let interrupted = false;
58/**
59 * Calls all interrupt handlers, then exits with exit code 0.
60 *
61 * If called more than once it skips waiting for the interrupt handlers to
62 * finish and exits with exit code 1. If there are any errors with interrupt
63 * handlers, the process exits immediately with exit code 2.
64 *
65 * This function is called when a SIGINT is received.
66 */
67function 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}
80exports.interrupt = interrupt;
81process.on('SIGINT', interrupt);