define(["require", "exports"], function (require, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
function executeTaskAsync(context, taskFn, callbackFn, data, preventCallback, parent) {
return new WorkerHelper(context, taskFn, callbackFn, data, preventCallback, parent);
}
exports.executeTaskAsync = executeTaskAsync;
var WorkerHelper = (function () {
function WorkerHelper(context, task, defaultListener, taskData, preventCallback, parent) {
this.preventCallback = false;
this.context = context;
this.workerTask = task;
this.defaultListener = defaultListener;
this.workerData = taskData;
this.parent = parent;
if (preventCallback) {
this.preventCallback = true;
}
this.initWorker();
}
WorkerHelper.prototype.terminate = function () {
this.worker.terminate();
URL.revokeObjectURL(this.workerUrl);
};
WorkerHelper.prototype.initWorker = function () {
var taskBlob = new Blob([this.getFnCode()], { type: 'text/javascript' });
this.workerUrl = URL.createObjectURL(taskBlob);
this.worker = new Worker(this.workerUrl);
this.worker.onmessage = this.messageFromWorker.bind(this);
this.worker.onerror = this.onError.bind(this);
if (!this.parent.isVue) {
this.worker.postMessage(this.workerData);
}
else {
var clonedData = JSON.parse(JSON.stringify(this.workerData));
this.worker.postMessage(clonedData);
}
};
WorkerHelper.prototype.messageFromWorker = function (args) {
this.terminate();
this.defaultListener.apply(this.context, [args.data]);
};
WorkerHelper.prototype.onError = function (args) {
this.terminate();
if (args.message && args.message.includes('FormData')) {
this.defaultListener.apply(this.context, [{ isFormDataError: true }]);
}
else {
throw args.message || args;
}
};
WorkerHelper.prototype.getFnCode = function () {
var workerCode = '';
var i;
var keys;
var workerFunction = '';
var isHaveFunction = false;
if (typeof this.workerTask === 'function') {
if (this.workerTask.toString().indexOf('function') < 0) {
workerFunction = 'function ' + this.workerTask.toString();
}
else {
workerFunction = this.workerTask.toString();
isHaveFunction = true;
}
workerCode += ('self.workerTask = ' + workerFunction + '; \n');
}
else {
if (typeof this.workerTask === 'object') {
keys = Object.keys(this.workerTask);
for (i = 0; i < keys.length; i++) {
if (this.workerTask[keys[i]].toString().indexOf('function') < 0) {
workerFunction = 'function ' + this.workerTask[keys[i]].toString();
}
else {
workerFunction = this.workerTask[keys[i]].toString();
isHaveFunction = true;
}
workerCode += ((i === 0 ? 'self.workerTask' : keys[i]) + '= ' + workerFunction + '; \n');
}
}
}
workerCode += 'self.onmessage = ' + (isHaveFunction ? '' : ' function ') +
(this.preventCallback ? this.getMessageFn.toString() : this.getCallbackMessageFn.toString()) + '; \n';
return workerCode;
};
WorkerHelper.prototype.getCallbackMessageFn = function (args) {
postMessage(this.workerTask.apply(this, args.data));
};
WorkerHelper.prototype.getMessageFn = function (args) {
this.workerTask.apply(this, args.data);
};
return WorkerHelper;
}());
});
|