2021-01-11 12:13:41 -05:00
|
|
|
// Copyright 2018-2021 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;
|
2021-07-03 18:17:52 -04:00
|
|
|
const {
|
2021-10-25 23:03:38 -04:00
|
|
|
Set,
|
|
|
|
TypeError,
|
2021-07-03 18:17:52 -04:00
|
|
|
} = window.__bootstrap.primordials;
|
2020-07-19 13:49:44 -04:00
|
|
|
|
|
|
|
function bindSignal(signo) {
|
2021-04-12 15:55:05 -04:00
|
|
|
return core.opSync("op_signal_bind", signo);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function pollSignal(rid) {
|
2021-04-12 15:55:05 -04:00
|
|
|
return core.opAsync("op_signal_poll", rid);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function unbindSignal(rid) {
|
2021-04-12 15:55:05 -04:00
|
|
|
core.opSync("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);
|
|
|
|
sigData.listeners.delete(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;
|
|
|
|
}
|
|
|
|
for (const listener of sigData.listeners) {
|
|
|
|
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);
|