2020-05-09 08:34:47 -04:00
|
|
|
import { MuxAsyncIterator } from "../async/mux_async_iterator.ts";
|
2020-02-07 01:53:15 -05:00
|
|
|
|
2020-04-10 10:05:56 -04:00
|
|
|
export type Disposable = { dispose: () => void };
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates an AsyncIterable which can be awaited on for one or more signals.
|
|
|
|
* `dispose()` can be called when you are finished waiting on the events.
|
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
*
|
|
|
|
* const sig = signal(Deno.Signal.SIGUSR1, Deno.Signal.SIGINT);
|
|
|
|
* setTimeout(() => {}, 5000); // Prevents exiting immediately
|
|
|
|
*
|
|
|
|
* for await (const _ of sig) {
|
|
|
|
* console.log("interrupt or usr1 signal received");
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* // At some other point in your code when finished listening:
|
|
|
|
* sig.dispose();
|
|
|
|
*
|
|
|
|
* @param signos - one or more `Deno.Signal`s to await on
|
|
|
|
*/
|
2020-02-07 01:53:15 -05:00
|
|
|
export function signal(
|
|
|
|
...signos: [number, ...number[]]
|
2020-04-10 10:05:56 -04:00
|
|
|
): AsyncIterable<void> & Disposable {
|
2020-02-07 01:53:15 -05:00
|
|
|
const mux = new MuxAsyncIterator<void>();
|
|
|
|
|
|
|
|
if (signos.length < 1) {
|
|
|
|
throw new Error(
|
2020-07-14 15:24:17 -04:00
|
|
|
"No signals are given. You need to specify at least one signal to create a signal stream.",
|
2020-02-07 01:53:15 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const streams = signos.map(Deno.signal);
|
|
|
|
|
2020-03-28 13:03:49 -04:00
|
|
|
streams.forEach((stream) => {
|
2020-02-07 01:53:15 -05:00
|
|
|
mux.add(stream);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Create dispose method for the muxer of signal streams.
|
|
|
|
const dispose = (): void => {
|
2020-03-28 13:03:49 -04:00
|
|
|
streams.forEach((stream) => {
|
2020-02-07 01:53:15 -05:00
|
|
|
stream.dispose();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
return Object.assign(mux, { dispose });
|
|
|
|
}
|
2020-04-10 10:05:56 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Registers a callback function to be called on triggering of a signal event.
|
|
|
|
*
|
|
|
|
* const handle = onSignal(Deno.Signal.SIGINT, () => {
|
|
|
|
* console.log('Received SIGINT');
|
|
|
|
* handle.dispose(); // de-register from receiving further events
|
|
|
|
* });
|
|
|
|
*
|
|
|
|
* @param signo One of Deno.Signal (e.g. Deno.Signal.SIGINT)
|
|
|
|
* @param callback Callback function triggered upon signal event
|
|
|
|
*/
|
|
|
|
export function onSignal(signo: number, callback: () => void): Disposable {
|
|
|
|
const sig = signal(signo);
|
|
|
|
|
2020-06-07 09:23:52 -04:00
|
|
|
// allows `sig` to be returned before blocking on the await
|
|
|
|
(async (): Promise<void> => {
|
2020-04-10 10:05:56 -04:00
|
|
|
for await (const _ of sig) {
|
|
|
|
callback();
|
|
|
|
}
|
2020-06-07 09:23:52 -04:00
|
|
|
})();
|
2020-04-10 10:05:56 -04:00
|
|
|
|
|
|
|
return sig;
|
|
|
|
}
|