Skip to content

Commit 78690d7

Browse files
committed
fix: Cancel run promise if the exit() is called
1 parent 2d6fae8 commit 78690d7

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

src/createFFmpeg.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@ module.exports = (_options = {}) => {
2121
let Core = null;
2222
let ffmpeg = null;
2323
let runResolve = null;
24+
let runReject = null;
2425
let running = false;
2526
let progress = optProgress;
2627
const detectCompletion = (message) => {
2728
if (message === 'FFMPEG_END' && runResolve !== null) {
2829
runResolve();
2930
runResolve = null;
31+
runReject = null;
3032
running = false;
3133
}
3234
};
@@ -126,9 +128,10 @@ module.exports = (_options = {}) => {
126128
throw Error('ffmpeg.wasm can only run one command at a time');
127129
} else {
128130
running = true;
129-
return new Promise((resolve) => {
131+
return new Promise((resolve, reject) => {
130132
const args = [...defaultArgs, ..._args].filter((s) => s.length !== 0);
131133
runResolve = resolve;
134+
runReject = reject
132135
ffmpeg(...parseArgs(Core, args));
133136
});
134137
}
@@ -177,6 +180,10 @@ module.exports = (_options = {}) => {
177180
if (Core === null) {
178181
throw NO_LOAD;
179182
} else {
183+
// if there's any pending runs, reject them
184+
if(runReject) {
185+
runReject('ffmpeg has exited')
186+
}
180187
running = false;
181188
Core.exit(1);
182189
Core = null;

tests/ffmpeg.test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,16 @@ describe('run()', () => {
4848
}
4949
}, 500);
5050
}).timeout(TIMEOUT);
51+
52+
it('should terminate the run if exit is called', async () => {
53+
const ffmpeg = createFFmpeg(OPTIONS);
54+
await ffmpeg.load();
55+
56+
ffmpeg.run('-h').catch(e=> {
57+
expect(e).to.be.equal('ffmpeg has exited')
58+
});
59+
expect(ffmpeg.exit()).to.throw();
60+
}).timeout(TIMEOUT);
5161
});
5262

5363
describe('FS()', () => {

0 commit comments

Comments
 (0)