UNPKG

1.19 kBJavaScriptView Raw
1"use strict";
2/*
3 * Copyright (c) Jupyter Development Team.
4 * Distributed under the terms of the Modified BSD License.
5 */
6Object.defineProperty(exports, "__esModule", { value: true });
7exports.signalToPromise = void 0;
8const coreutils_1 = require("@lumino/coreutils");
9/**
10 * Convert a signal into a promise for the first emitted value.
11 *
12 * @param signal - The signal we are listening to.
13 * @param timeout - Timeout to wait for signal in ms (not timeout if not defined or 0)
14 *
15 * @returns a Promise that resolves with a `(sender, args)` pair.
16 */
17function signalToPromise(signal, timeout) {
18 const waitForSignal = new coreutils_1.PromiseDelegate();
19 function cleanup() {
20 signal.disconnect(slot);
21 }
22 function slot(sender, args) {
23 cleanup();
24 waitForSignal.resolve([sender, args]);
25 }
26 signal.connect(slot);
27 if ((timeout !== null && timeout !== void 0 ? timeout : 0) > 0) {
28 setTimeout(() => {
29 cleanup();
30 waitForSignal.reject(`Signal not emitted within ${timeout} ms.`);
31 }, timeout);
32 }
33 return waitForSignal.promise;
34}
35exports.signalToPromise = signalToPromise;
36//# sourceMappingURL=signal.js.map
\No newline at end of file