From 8af8c96d7a066ae7228d041aacff8fe4cc6df026 Mon Sep 17 00:00:00 2001 From: ehmicky Date: Thu, 20 Jun 2019 06:35:45 -0700 Subject: [PATCH] Improve/refactor `.cancel()` (#309) --- index.js | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/index.js b/index.js index 9fe95fdcc6..088e787e02 100644 --- a/index.js +++ b/index.js @@ -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) => { @@ -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); @@ -400,7 +407,7 @@ const execa = (file, args, options) => { command, parsed, timedOut: context.timedOut, - isCanceled, + isCanceled: context.isCanceled, killed: spawned.killed }); @@ -431,12 +438,6 @@ const execa = (file, args, options) => { spawned.all = makeAllStream(spawned); - spawned.cancel = () => { - if (spawned.kill()) { - isCanceled = true; - } - }; - return mergePromise(spawned, handlePromise); };