Skip to content

Commit 3fcd57d

Browse files
committed
feat: options.useWebWorker default set to true
1 parent b4c7a76 commit 3fcd57d

File tree

4 files changed

+15
-7
lines changed

4 files changed

+15
-7
lines changed

lib/image-compression.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
* @param {Object} options
1717
* @param {number} [options.maxSizeMB=Number.POSITIVE_INFINITY]
1818
* @param {number} [options.maxWidthOrHeight=undefined]
19-
* @param {boolean} [options.useWebWorker=false]
19+
* @param {boolean} [options.useWebWorker=true]
2020
* @param {number} [options.maxIteration=10]
2121
* @param {number} [options.exifOrientation] - default to be the exif orientation from the image file
2222
* @param {Function} [options.onProgress] - a function takes one progress argument (progress from 0 to 100)

lib/index.d.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
// Type definitions for browser-image-compression 1.0
1+
// Type definitions for browser-image-compression 2.0
22
// Project: https://github.com/Donaldcwl/browser-image-compression
33
// Definitions by: Donald <https://github.com/Donaldcwl> & Jamie Haywood <https://github.com/jamiehaywood>
4-
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
54

65
interface Options {
76
/** @default Number.POSITIVE_INFINITY */
87
maxSizeMB?: number;
98
/** @default undefined */
109
maxWidthOrHeight?: number;
11-
/** @default false */
10+
/** @default true */
1211
useWebWorker?: boolean;
1312
/** @default 10 */
1413
maxIteration?: number;

lib/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ import compressOnWebWorker from './web-worker';
2222
* Compress an image file.
2323
*
2424
* @param {File} file
25-
* @param {Object} opts - { maxSizeMB=Number.POSITIVE_INFINITY, maxWidthOrHeight, useWebWorker=false, maxIteration = 10, exifOrientation, fileType }
25+
* @param {Object} options - { maxSizeMB=Number.POSITIVE_INFINITY, maxWidthOrHeight, useWebWorker=false, maxIteration = 10, exifOrientation, fileType }
2626
* @param {number} [options.maxSizeMB=Number.POSITIVE_INFINITY]
2727
* @param {number} [options.maxWidthOrHeight=undefined]
28-
* @param {boolean} [options.useWebWorker=false]
28+
* @param {boolean} [options.useWebWorker=true]
2929
* @param {number} [options.maxIteration=10]
3030
* @param {number} [options.exifOrientation] - default to be the exif orientation from the image file
3131
* @param {Function} [options.onProgress] - a function takes one progress argument (progress from 0 to 100)

lib/web-worker.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,9 @@ export default function compressOnWebWorker(file, options) {
358358

359359
function handler(e) {
360360
if (e.data.id === id) {
361+
if (options.signal && options.signal.aborted) {
362+
return;
363+
}
361364
if (e.data.progress !== undefined) {
362365
options.onProgress(e.data.progress);
363366
return;
@@ -372,12 +375,18 @@ export default function compressOnWebWorker(file, options) {
372375

373376
worker.addEventListener('message', handler);
374377
worker.addEventListener('error', reject);
378+
if (options.signal) {
379+
options.signal.addEventListener('abort', () => {
380+
worker.terminate();
381+
reject(options.signal.reason);
382+
});
383+
}
375384

376385
worker.postMessage({
377386
file,
378387
id,
379388
imageCompressionLibUrl,
380-
options: { ...options, onProgress: undefined },
389+
options: { ...options, onProgress: undefined, signal: undefined },
381390
});
382391
});
383392
}

0 commit comments

Comments
 (0)