zx
zx copied to clipboard
Feature: Abort controller support
As a part of node.js 16. Abort signal stable feature. I would like to propose Abort signal and Abort Controller support.
API:
import { $ } from 'zx';
const controller = new AbortController();
$('some long command', {signal: controller.signal});
if(somethingHapens){ // e.g. ctrl+c
controller.abort();
}
// some long command will be halted.
What about kill() method? It already works.
yes, that's worked, but the reason is resources. When signal comes for promises, it's not stops actually. and resources are not flushed. I mean what's happens when do the snippet:
function delay(ms: number){
return new Promise(res => setTimeout(res, ms)})
}
await Promise.race([
delay(1000),
asyncFnLessThan1000ms(),
])
set timeout will not stops actually, results will be ignored (it takes machine time)
But if we use the signal to stop any async code(setTimeout in this case) - nodejs will real stop setTimeout fn and flush resources.
The same behavior for the child process, This feature will reduce process memory/time management
Native intertop with signal would make the API a lot nicer to use.
Now the API feels awkward:
handle: async ({ signal }) => {
const promise = $`sleep 30`;
const kill = () => {
promise.kill();
};
signal.addEventListener('abort', kill, { once: true });
await promise;
signal.removeEventListener('abort', kill);
},
If zx added native support, it could be simplified down to:
handle: async ({ signal }) => {
await $({signal})`sleep 30`;
},
This API is similar to how sql.type() is used in Slonik, example.
Please show an example with usage with signal vs kill()
Updated