mirror of
https://github.com/denoland/std.git
synced 2024-11-21 20:50:22 +00:00
BREAKING: remove std/signal
sub-module (#4105)
This commit is contained in:
parent
dba0e31a17
commit
bdf41e5b8b
@ -96,7 +96,6 @@ Check out the documentation [here](https://deno.land/std?doc).
|
||||
| permissions | Deprecated |
|
||||
| regexp | Unstable |
|
||||
| semver | Unstable |
|
||||
| signal | Deprecated |
|
||||
| streams | Unstable |
|
||||
| testing | Stable |
|
||||
| toml | Stable |
|
||||
|
@ -113,7 +113,6 @@ deps["path"] = await check("path", "needs clean up");
|
||||
deps["permissions"] = await check("permissions", "deprecated");
|
||||
deps["regexp"] = await check("regexp", "not ready");
|
||||
deps["semver"] = await check("semver", "not ready");
|
||||
deps["signal"] = await check("signal", "deprecated");
|
||||
deps["streams"] = await check("streams", "needs clean up");
|
||||
deps["testing"] = await check("testing", "ready", [
|
||||
"bdd.ts",
|
||||
|
@ -1,86 +0,0 @@
|
||||
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
||||
/**
|
||||
* Higher level API for dealing with OS signals.
|
||||
*
|
||||
* @module
|
||||
* @deprecated (will be removed in 0.212.0) Use the {@link https://docs.deno.com/runtime/tutorials/os_signals|Deno Signals API} directly instead.
|
||||
*/
|
||||
|
||||
import { MuxAsyncIterator } from "../async/mux_async_iterator.ts";
|
||||
|
||||
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:
|
||||
*
|
||||
* ```ts
|
||||
* import { signal } from "https://deno.land/std@$STD_VERSION/signal/mod.ts";
|
||||
*
|
||||
* const sig = signal("SIGUSR1", "SIGINT");
|
||||
* setTimeout(() => {}, 5000); // Prevents exiting immediately
|
||||
*
|
||||
* for await (const _ of sig) {
|
||||
* // ..
|
||||
* }
|
||||
*
|
||||
* // At some other point in your code when finished listening:
|
||||
* sig.dispose();
|
||||
* ```
|
||||
*
|
||||
* @param signals - one or more signals to listen to
|
||||
*
|
||||
* @deprecated (will be removed in 0.212.0) Use the {@link https://docs.deno.com/runtime/tutorials/os_signals|Deno Signals API} directly instead.
|
||||
*/
|
||||
export function signal(
|
||||
...signals: [Deno.Signal, ...Deno.Signal[]]
|
||||
): AsyncIterable<void> & Disposable {
|
||||
const mux = new MuxAsyncIterator<void>();
|
||||
|
||||
if (signals.length < 1) {
|
||||
throw new Error(
|
||||
"No signals are given. You need to specify at least one signal to create a signal stream.",
|
||||
);
|
||||
}
|
||||
|
||||
const streams = signals.map(createSignalStream);
|
||||
|
||||
streams.forEach((stream) => {
|
||||
mux.add(stream);
|
||||
});
|
||||
|
||||
// Create dispose method for the muxer of signal streams.
|
||||
const dispose = () => {
|
||||
streams.forEach((stream) => {
|
||||
stream.dispose();
|
||||
});
|
||||
};
|
||||
|
||||
return Object.assign(mux, { dispose });
|
||||
}
|
||||
|
||||
function createSignalStream(
|
||||
signal: Deno.Signal,
|
||||
): AsyncIterable<void> & Disposable {
|
||||
let streamContinues = Promise.withResolvers<boolean>();
|
||||
const handler = () => {
|
||||
streamContinues.resolve(true);
|
||||
};
|
||||
Deno.addSignalListener(signal, handler);
|
||||
|
||||
const gen = async function* () {
|
||||
while (await streamContinues.promise) {
|
||||
streamContinues = Promise.withResolvers<boolean>();
|
||||
yield undefined;
|
||||
}
|
||||
};
|
||||
|
||||
return Object.assign(gen(), {
|
||||
dispose() {
|
||||
streamContinues.resolve(false);
|
||||
Deno.removeSignalListener(signal, handler);
|
||||
},
|
||||
});
|
||||
}
|
@ -1,87 +0,0 @@
|
||||
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
||||
import { assert, assertEquals, assertThrows } from "../assert/mod.ts";
|
||||
import { delay } from "../async/delay.ts";
|
||||
import { signal } from "./mod.ts";
|
||||
|
||||
const isWindows = Deno.build.os === "windows";
|
||||
|
||||
Deno.test({
|
||||
name: "signal() throws when called with empty signals",
|
||||
ignore: isWindows,
|
||||
fn() {
|
||||
assertThrows(
|
||||
() => {
|
||||
// deno-lint-ignore no-explicit-any
|
||||
(signal as any)();
|
||||
},
|
||||
Error,
|
||||
"No signals are given. You need to specify at least one signal to create a signal stream.",
|
||||
);
|
||||
},
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "signal() iterates for multiple signals",
|
||||
ignore: isWindows,
|
||||
fn: async () => {
|
||||
// This prevents the program from exiting.
|
||||
const t = setInterval(() => {}, 1000);
|
||||
|
||||
let c = 0;
|
||||
const sig = signal(
|
||||
"SIGUSR1",
|
||||
"SIGUSR2",
|
||||
);
|
||||
|
||||
setTimeout(async () => {
|
||||
await delay(20);
|
||||
Deno.kill(Deno.pid, "SIGUSR2");
|
||||
await delay(20);
|
||||
Deno.kill(Deno.pid, "SIGUSR1");
|
||||
await delay(20);
|
||||
Deno.kill(Deno.pid, "SIGUSR2");
|
||||
await delay(20);
|
||||
Deno.kill(Deno.pid, "SIGUSR1");
|
||||
await delay(20);
|
||||
sig.dispose();
|
||||
});
|
||||
|
||||
for await (const _ of sig) {
|
||||
console.log(c);
|
||||
c += 1;
|
||||
}
|
||||
|
||||
assertEquals(c, 4);
|
||||
|
||||
clearTimeout(t);
|
||||
},
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "signal(), multiple .next() results don't resolve at the same time",
|
||||
ignore: isWindows,
|
||||
async fn() {
|
||||
// This prevents the program from exiting.
|
||||
const t = setInterval(() => {}, 1000);
|
||||
const sig = signal("SIGUSR1");
|
||||
const sigIter = sig[Symbol.asyncIterator]();
|
||||
let done0 = false;
|
||||
let done1 = false;
|
||||
sigIter.next().then(() => {
|
||||
done0 = true;
|
||||
});
|
||||
sigIter.next().then(() => {
|
||||
done1 = true;
|
||||
});
|
||||
Deno.kill(Deno.pid, "SIGUSR1");
|
||||
await delay(20);
|
||||
assert(done0);
|
||||
assert(!done1);
|
||||
Deno.kill(Deno.pid, "SIGUSR1");
|
||||
await delay(20);
|
||||
assert(done0);
|
||||
assert(done1);
|
||||
sig.dispose();
|
||||
clearTimeout(t);
|
||||
},
|
||||
});
|
Loading…
Reference in New Issue
Block a user