all files / workbook/common/ worker.js

21.88% Statements 14/64
0% Branches 0/24
18.18% Functions 2/11
21.88% Lines 14/64
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96                                                                                                                                                                    
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;
    }());
});