2022-01-07 22:09:52 -05:00
|
|
|
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
|
2021-02-04 17:18:32 -05:00
|
|
|
"use strict";
|
2020-07-19 13:49:44 -04:00
|
|
|
|
|
|
|
((window) => {
|
2020-09-16 16:22:43 -04:00
|
|
|
const core = window.Deno.core;
|
2022-08-11 09:56:56 -04:00
|
|
|
const ops = core.ops;
|
2021-07-03 18:17:52 -04:00
|
|
|
const {
|
2022-12-19 21:37:50 -05:00
|
|
|
SafeSetIterator,
|
2021-10-25 23:03:38 -04:00
|
|
|
Set,
|
2022-12-19 21:37:50 -05:00
|
|
|
SetPrototypeDelete,
|
2021-11-25 13:49:09 -05:00
|
|
|
SymbolFor,
|
2021-10-25 23:03:38 -04:00
|
|
|
TypeError,
|
2021-07-03 18:17:52 -04:00
|
|
|
} = window.__bootstrap.primordials;
|
2020-07-19 13:49:44 -04:00
|
|
|
|
|
|
|
function bindSignal(signo) {
|
2022-08-11 09:56:56 -04:00
|
|
|
return ops.op_signal_bind(signo);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function pollSignal(rid) {
|
2021-11-25 13:49:09 -05:00
|
|
|
const promise = core.opAsync("op_signal_poll", rid);
|
|
|
|
core.unrefOp(promise[SymbolFor("Deno.core.internalPromiseId")]);
|
|
|
|
return promise;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function unbindSignal(rid) {
|
2022-08-11 09:56:56 -04:00
|
|
|
ops.op_signal_unbind(rid);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2021-10-25 23:03:38 -04: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
|
|
|
|
2021-10-25 23:03:38 -04:00
|
|
|
/** Gets the signal handlers and resource data of the given signal */
|
|
|
|
function getSignalData(signo) {
|
|
|
|
return signalData[signo] ??
|
|
|
|
(signalData[signo] = { rid: undefined, listeners: new Set() });
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2021-10-25 23:03:38 -04:00
|
|
|
function checkSignalListenerType(listener) {
|
|
|
|
if (typeof listener !== "function") {
|
|
|
|
throw new TypeError(
|
|
|
|
`Signal listener must be a function. "${typeof listener}" is given.`,
|
|
|
|
);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2021-10-25 23:03:38 -04:00
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2021-10-25 23:03:38 -04:00
|
|
|
function addSignalListener(signo, listener) {
|
|
|
|
checkSignalListenerType(listener);
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2021-10-25 23:03:38 -04:00
|
|
|
const sigData = getSignalData(signo);
|
|
|
|
sigData.listeners.add(listener);
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2021-10-25 23:03:38 -04: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);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2021-10-25 23:03:38 -04:00
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2021-10-25 23:03:38 -04:00
|
|
|
function removeSignalListener(signo, listener) {
|
|
|
|
checkSignalListenerType(listener);
|
|
|
|
|
|
|
|
const sigData = getSignalData(signo);
|
2022-12-19 21:37:50 -05:00
|
|
|
SetPrototypeDelete(sigData.listeners, listener);
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2021-10-25 23:03:38 -04:00
|
|
|
if (sigData.listeners.size === 0 && sigData.rid) {
|
|
|
|
unbindSignal(sigData.rid);
|
|
|
|
sigData.rid = undefined;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2021-10-25 23:03:38 -04:00
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2021-10-25 23:03:38 -04:00
|
|
|
async function loop(sigData) {
|
|
|
|
while (sigData.rid) {
|
|
|
|
if (await pollSignal(sigData.rid)) {
|
|
|
|
return;
|
|
|
|
}
|
2022-12-19 21:37:50 -05:00
|
|
|
for (const listener of new SafeSetIterator(sigData.listeners)) {
|
2021-10-25 23:03:38 -04:00
|
|
|
listener();
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
window.__bootstrap.signals = {
|
2021-10-25 23:03:38 -04:00
|
|
|
addSignalListener,
|
|
|
|
removeSignalListener,
|
2020-07-19 13:49:44 -04:00
|
|
|
};
|
|
|
|
})(this);
|