Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

enable cancellation in browser mode #51

Merged
merged 2 commits into from
Feb 2, 2022
Merged
Show file tree
Hide file tree
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
8 changes: 6 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function toArrayBuffer(buf) {

module.exports = function (
file,
{ browserMode, progress, useWorker = true } = {}
{ browserMode, progress, useWorker = true, cancellationToken } = {}
) {
var mp4boxFile;
var trackId;
Expand Down Expand Up @@ -124,7 +124,11 @@ module.exports = function (
reject('File not compatible');
}
buffer.fileStart = offset;
mp4boxFile.appendBuffer(buffer);
if (cancellationToken && cancellationToken.cancelled) {
if (worker) worker.terminate();
else readBlock.stop();
resolve(null);
} else mp4boxFile.appendBuffer(buffer);
};
// var flush = mp4boxFile.flush;
//Try to use a web worker to avoid blocking the browser
Expand Down
15 changes: 11 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,20 @@ You can specify some options in an object as a second argument:
- **browserMode**: Default: _false_. Change behaviour to use in browser. This is optional for debugging reasons
- **useWorker**: Default: _true_. In browser mode, use a web worker to avoid locking the browser. This is optional as it seems to crash on some recent browsers
- **progress**: Pass a function to read the processed percentage updates
- **cancellationToken**: An optional object, containing a cancelled property, that allows for cancelling the extraction process. Currently only supported in browser mode. If cancelled, the extraction process will return null.

```js
const gpmfExtract = require('gpmf-extract');
const progress = percent => console.log(`${percent}% processed`);
gpmfExtract(file, { browserMode: true, progress }).then(res => {
// Do what you want with the data
});
const progress = (percent) => console.log(`${percent}% processed`);
const cancellationToken = { cancelled: false };
gpmfExtract(file, { browserMode: true, progress, cancellationToken }).then(
(res) => {
if (!res) return; //cancelled
// Do what you want with the data
}
);
// Some other processes
cancellationToken.cancelled = true;
```

## About
Expand Down