mirror of
https://github.com/denoland/deno.git
synced 2025-08-03 10:33:54 +00:00
feat(unstable): Ability to ref/unref "Child" in "Deno.spawnChild()" API (#15151)
Co-authored-by: Bartek Iwańczuk <biwanczuk@gmail.com> Co-authored-by: Colin Ihrig <cjihrig@gmail.com>
This commit is contained in:
parent
9eb70bdb5f
commit
2bebdc9116
5 changed files with 98 additions and 7 deletions
|
@ -13,10 +13,13 @@
|
|||
TypeError,
|
||||
Uint8Array,
|
||||
PromiseAll,
|
||||
SymbolFor,
|
||||
} = window.__bootstrap.primordials;
|
||||
const { readableStreamForRid, writableStreamForRid } =
|
||||
window.__bootstrap.streamUtils;
|
||||
|
||||
const promiseIdSymbol = SymbolFor("Deno.core.internalPromiseId");
|
||||
|
||||
function spawnChild(command, {
|
||||
args = [],
|
||||
cwd = undefined,
|
||||
|
@ -71,6 +74,7 @@
|
|||
|
||||
class Child {
|
||||
#rid;
|
||||
#waitPromiseId;
|
||||
|
||||
#pid;
|
||||
get pid() {
|
||||
|
@ -85,6 +89,8 @@
|
|||
return this.#stdin;
|
||||
}
|
||||
|
||||
#stdoutPromiseId;
|
||||
#stdoutRid;
|
||||
#stdout = null;
|
||||
get stdout() {
|
||||
if (this.#stdout == null) {
|
||||
|
@ -93,6 +99,8 @@
|
|||
return this.#stdout;
|
||||
}
|
||||
|
||||
#stderrPromiseId;
|
||||
#stderrRid;
|
||||
#stderr = null;
|
||||
get stderr() {
|
||||
if (this.#stderr == null) {
|
||||
|
@ -121,17 +129,25 @@
|
|||
}
|
||||
|
||||
if (stdoutRid !== null) {
|
||||
this.#stdout = readableStreamForRid(stdoutRid);
|
||||
this.#stdoutRid = stdoutRid;
|
||||
this.#stdout = readableStreamForRid(stdoutRid, (promise) => {
|
||||
this.#stdoutPromiseId = promise[promiseIdSymbol];
|
||||
});
|
||||
}
|
||||
|
||||
if (stderrRid !== null) {
|
||||
this.#stderr = readableStreamForRid(stderrRid);
|
||||
this.#stderrRid = stderrRid;
|
||||
this.#stderr = readableStreamForRid(stderrRid, (promise) => {
|
||||
this.#stderrPromiseId = promise[promiseIdSymbol];
|
||||
});
|
||||
}
|
||||
|
||||
const onAbort = () => this.kill("SIGTERM");
|
||||
signal?.[add](onAbort);
|
||||
|
||||
this.#status = core.opAsync("op_spawn_wait", this.#rid).then((res) => {
|
||||
const waitPromise = core.opAsync("op_spawn_wait", this.#rid);
|
||||
this.#waitPromiseId = waitPromise[promiseIdSymbol];
|
||||
this.#status = waitPromise.then((res) => {
|
||||
this.#rid = null;
|
||||
signal?.[remove](onAbort);
|
||||
return res;
|
||||
|
@ -186,6 +202,18 @@
|
|||
}
|
||||
core.opSync("op_kill", this.#pid, signo);
|
||||
}
|
||||
|
||||
ref() {
|
||||
core.refOp(this.#waitPromiseId);
|
||||
if (this.#stdoutPromiseId) core.refOp(this.#stdoutPromiseId);
|
||||
if (this.#stderrPromiseId) core.refOp(this.#stderrPromiseId);
|
||||
}
|
||||
|
||||
unref() {
|
||||
core.unrefOp(this.#waitPromiseId);
|
||||
if (this.#stdoutPromiseId) core.unrefOp(this.#stdoutPromiseId);
|
||||
if (this.#stderrPromiseId) core.unrefOp(this.#stderrPromiseId);
|
||||
}
|
||||
}
|
||||
|
||||
function spawn(command, options) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue