2023-01-02 16:00:42 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-12-07 08:21:01 -05:00
|
|
|
import { core, primordials } from "ext:core/mod.js";
|
2023-02-07 14:22:46 -05:00
|
|
|
const ops = core.ops;
|
|
|
|
const {
|
2023-04-14 16:23:28 -04:00
|
|
|
SafeSet,
|
2023-02-07 14:22:46 -05:00
|
|
|
SafeSetIterator,
|
2023-04-14 16:23:28 -04:00
|
|
|
SetPrototypeAdd,
|
2023-02-07 14:22:46 -05:00
|
|
|
SetPrototypeDelete,
|
|
|
|
TypeError,
|
|
|
|
} = primordials;
|
2023-12-26 20:30:26 -05:00
|
|
|
const {
|
|
|
|
op_signal_poll,
|
|
|
|
} = core.ensureFastOps();
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
function bindSignal(signo) {
|
|
|
|
return ops.op_signal_bind(signo);
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
function pollSignal(rid) {
|
2023-12-26 20:30:26 -05:00
|
|
|
const promise = op_signal_poll(rid);
|
2023-11-09 15:57:26 -05:00
|
|
|
core.unrefOpPromise(promise);
|
2023-02-07 14:22:46 -05:00
|
|
|
return promise;
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
function unbindSignal(rid) {
|
|
|
|
ops.op_signal_unbind(rid);
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// Stores signal listeners and resource data. This has type of
|
|
|
|
// `Record<string, { rid: number | undefined, listeners: Set<() => void> }`
|
|
|
|
const signalData = {};
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
/** Gets the signal handlers and resource data of the given signal */
|
|
|
|
function getSignalData(signo) {
|
|
|
|
return signalData[signo] ??
|
2023-04-14 16:23:28 -04:00
|
|
|
(signalData[signo] = { rid: undefined, listeners: new SafeSet() });
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
function checkSignalListenerType(listener) {
|
|
|
|
if (typeof listener !== "function") {
|
|
|
|
throw new TypeError(
|
|
|
|
`Signal listener must be a function. "${typeof listener}" is given.`,
|
|
|
|
);
|
2021-10-25 23:03:38 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
function addSignalListener(signo, listener) {
|
|
|
|
checkSignalListenerType(listener);
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const sigData = getSignalData(signo);
|
2023-04-14 16:23:28 -04:00
|
|
|
SetPrototypeAdd(sigData.listeners, listener);
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
if (!sigData.rid) {
|
|
|
|
// If signal resource doesn't exist, create it.
|
|
|
|
// The program starts listening to the signal
|
|
|
|
sigData.rid = bindSignal(signo);
|
|
|
|
loop(sigData);
|
2021-10-25 23:03:38 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
function removeSignalListener(signo, listener) {
|
|
|
|
checkSignalListenerType(listener);
|
2021-10-25 23:03:38 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const sigData = getSignalData(signo);
|
|
|
|
SetPrototypeDelete(sigData.listeners, listener);
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
if (sigData.listeners.size === 0 && sigData.rid) {
|
|
|
|
unbindSignal(sigData.rid);
|
|
|
|
sigData.rid = undefined;
|
2021-10-25 23:03:38 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
async function loop(sigData) {
|
|
|
|
while (sigData.rid) {
|
|
|
|
if (await pollSignal(sigData.rid)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (const listener of new SafeSetIterator(sigData.listeners)) {
|
|
|
|
listener();
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
export { addSignalListener, removeSignalListener };
|