From 6f17936d95dc1a6c50634ca4e3c16029ba7a00d7 Mon Sep 17 00:00:00 2001 From: Carlos Rodrigues Date: Thu, 6 Jan 2022 12:15:15 +0000 Subject: [PATCH] fix: Cancel run promise if the exit() is called --- src/createFFmpeg.js | 9 ++++++++- tests/ffmpeg.test.js | 10 ++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/createFFmpeg.js b/src/createFFmpeg.js index c5425ce..836cc85 100644 --- a/src/createFFmpeg.js +++ b/src/createFFmpeg.js @@ -21,12 +21,14 @@ module.exports = (_options = {}) => { let Core = null; let ffmpeg = null; let runResolve = null; + let runReject = null; let running = false; let progress = optProgress; const detectCompletion = (message) => { if (message === 'FFMPEG_END' && runResolve !== null) { runResolve(); runResolve = null; + runReject = null; running = false; } }; @@ -126,9 +128,10 @@ module.exports = (_options = {}) => { throw Error('ffmpeg.wasm can only run one command at a time'); } else { running = true; - return new Promise((resolve) => { + return new Promise((resolve, reject) => { const args = [...defaultArgs, ..._args].filter((s) => s.length !== 0); runResolve = resolve; + runReject = reject ffmpeg(...parseArgs(Core, args)); }); } @@ -177,6 +180,10 @@ module.exports = (_options = {}) => { if (Core === null) { throw NO_LOAD; } else { + // if there's any pending runs, reject them + if(runReject) { + runReject('ffmpeg has exited') + } running = false; Core.exit(1); Core = null; diff --git a/tests/ffmpeg.test.js b/tests/ffmpeg.test.js index 2a434cd..270ad3d 100644 --- a/tests/ffmpeg.test.js +++ b/tests/ffmpeg.test.js @@ -48,6 +48,16 @@ describe('run()', () => { } }, 500); }).timeout(TIMEOUT); + + it('should terminate the run if exit is called', async () => { + const ffmpeg = createFFmpeg(OPTIONS); + await ffmpeg.load(); + + ffmpeg.run('-h').catch(e=> { + expect(e).to.be.equal('ffmpeg has exited') + }); + expect(ffmpeg.exit()).to.throw(); + }).timeout(TIMEOUT); }); describe('FS()', () => {