2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 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";
|
2024-01-26 17:46:46 -05:00
|
|
|
import { op_signal_bind, op_signal_poll, op_signal_unbind } from "ext:core/ops";
|
2023-02-07 14:22:46 -05:00
|
|
|
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;
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
function bindSignal(signo) {
|
2024-01-10 17:37:25 -05:00
|
|
|
return op_signal_bind(signo);
|
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 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) {
|
2024-01-10 17:37:25 -05:00
|
|
|
op_signal_unbind(rid);
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
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> }`
|
2024-05-22 18:03:35 -04:00
|
|
|
const signalData = { __proto__: null };
|
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 };
|