UNPKG

js-uploader

Version:
110 lines 4.13 kB
import { scriptContent as sparkMD5Factory } from './sparkMD5Script'; import { computeMd5 } from '../utils/compute-md5'; import md5WorkerScript from './md5WorkerScript'; import { Logger } from './Logger'; var taskQueue = []; var workers = []; var workerURL = URL.createObjectURL(new Blob([sparkMD5Factory + ";" + md5WorkerScript])); var maxWorkerNum = typeof navigator !== 'undefined' ? navigator.hardwareConcurrency || 4 : 4; var MD5Worker = /** @class */ (function () { function MD5Worker(scriptURL) { this.id = MD5Worker.maxWorkerID++; this.isBusy = false; this.worker = new Worker(scriptURL); } MD5Worker.prototype.execute = function (task) { var _this = this; task = task || taskQueue.pop(); if (task) { this.isBusy = true; var data = task.data, callback_1 = task.callback; task.workerID = this.id; // transferable data instanceof ArrayBuffer ? this.worker.postMessage(data, [data]) : this.worker.postMessage(data); var complete_1 = function (error, md5) { typeof callback_1 === 'function' && callback_1(error, md5); taskQueue.length && _this.execute(taskQueue.pop()); _this.isBusy = false; }; this.worker.onmessage = function (ev) { return complete_1(null, ev.data); }; this.worker.onerror = function (ev) { return complete_1(ev.error, ''); }; } }; MD5Worker.prototype.terminate = function () { this.worker.terminate(); }; MD5Worker.prototype.postMessage = function (message) { this.worker.postMessage(message); }; MD5Worker.maxWorkerID = 1; return MD5Worker; }()); var MD5WorkerPool = /** @class */ (function () { function MD5WorkerPool() { } MD5WorkerPool.prototype.execute = function (data, callback) { var _a; var logid = "computemd5->" + (data instanceof Blob ? data.size : data.byteLength); console.time(logid); var promise = !callback ? new Promise(function (resolve, reject) { callback = function (error, md5) { console.timeEnd(logid); error ? reject(error) : resolve(md5); }; }) : void 0; var task = { data: data, callback: callback }; if (taskQueue.push(task) > maxWorkerNum) { // TODO promise = computeMd5(data); } else { (_a = MD5WorkerPool.getWorker()) === null || _a === void 0 ? void 0 : _a.execute(); } return { promise: promise || null, abort: function () { var index = taskQueue.findIndex(function (tsk) { return tsk === task; }); index !== -1 && taskQueue.splice(index, 1); if (task.workerID) { var worker = workers.find(function (w) { return w.id === task.workerID; }); if (worker) { Logger.warn('abort worker'); worker.postMessage({ action: 'abort' }); worker.execute(); } } }, }; }; MD5WorkerPool.getWorker = function () { var worker = workers.find(function (wk) { return !wk.isBusy; }); if (!worker && workers.length < maxWorkerNum) { workers.push((worker = new MD5Worker(workerURL))); } return worker || null; }; return MD5WorkerPool; }()); export var md5WorkerPool = new MD5WorkerPool(); // input= document.createElement('input') // input.type='file' // input.onchange=function(e){ // tempFile = e.path[0].files[0] // } // input.click() // console.time('s') // tempFile.arrayBuffer().then(bf=>{ // console.warn(bf) // md5WorkerPool.execute(bf,(s)=>{ // console.timeEnd('s') // console.log(s) // }) // }) // console.time('s') // md5WorkerPool.execute(tempFile).then(md5=>{ // console.timeEnd('s') // console.log(md5) // }) //# sourceMappingURL=md5WorkerPool.js.map