Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Improve/refactor .cancel() #309

Merged
merged 4 commits into from
Jun 20, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,14 @@ const getForceKillAfterTimeout = ({forceKillAfterTimeout = true}) => {
return forceKillAfterTimeout;
};

const spawnedCancel = (spawned, context) => {
const killResult = spawned.kill();

if (killResult) {
context.isCanceled = true;
}
};

const handleSpawned = (spawned, context) => {
return new Promise((resolve, reject) => {
spawned.on('exit', (code, signal) => {
Expand Down Expand Up @@ -374,11 +382,10 @@ const execa = (file, args, options) => {
);
}

const kill = spawned.kill.bind(spawned);
spawned.kill = spawnedKill.bind(null, kill);
const context = {timedOut: false, isCanceled: false};

const context = {timedOut: false};
let isCanceled = false;
spawned.kill = spawnedKill.bind(null, spawned.kill.bind(spawned));
spawned.cancel = spawnedCancel.bind(null, spawned, context);

setExitHandler(spawned, parsed.options, context);
setupTimeout(spawned, parsed.options, context);
Expand All @@ -400,7 +407,7 @@ const execa = (file, args, options) => {
command,
parsed,
timedOut: context.timedOut,
isCanceled,
isCanceled: context.isCanceled,
killed: spawned.killed
});

Expand Down Expand Up @@ -431,12 +438,6 @@ const execa = (file, args, options) => {

spawned.all = makeAllStream(spawned);

spawned.cancel = () => {
if (spawned.kill()) {
isCanceled = true;
}
};

return mergePromise(spawned, handlePromise);
};

Expand Down