Skip to content

Commit

Permalink
Improve/refactor .cancel() (#309)
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky authored and sindresorhus committed Jun 20, 2019
1 parent 4503764 commit 8af8c96
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions index.js
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);

const timeoutId = setupTimeout(spawned, parsed.options, context);
const removeExitHandler = setExitHandler(spawned, parsed.options);
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

0 comments on commit 8af8c96

Please sign in to comment.