-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
94 lines (87 loc) · 2.38 KB
/
index.js
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
(function () {
var functionToObjectURL = function (fn) {
var blob, stringFunc = fn.toString();
stringFunc = stringFunc.substring(stringFunc.indexOf('{') + 1, stringFunc.lastIndexOf('}'));
try {
blob = new Blob([stringFunc], {'type': 'text/javascript'});
} catch (error) { // Backwards-compatibility
var BlobBuilder = window.BlobBuilder || window.WebKitBlobBuilder || window.MozBlobBuilder;
blob = new BlobBuilder();
blob.append(stringFunc);
blob = blob.getBlob();
}
return (window.URL || window.webkitURL).createObjectURL(blob);
},
fnUrl = functionToObjectURL(function () {
self.onmessage = function (event) {
var message = event.data,
args = message.args.map(function (arg) {
if (arg && arg.indexOf && !arg.indexOf('!FN!')) {
return eval('(' + arg.substr(4) + ')');
}
return arg;
});
try {
self.postMessage({
id: message.id,
result: eval('(' + message.func + ')').apply(null, args)
});
} catch (e) {
self.postMessage({id: message.id, error: e.message});
}
}
}),
createAndListen = function () {
var worker = new Worker(fnUrl);
worker.queue = {index: 0};
worker.addEventListener('message', function (e) {
if (e.data.hasOwnProperty('result')) {
this.queue[e.data.id].resolve(e.data.result);
}
else {
this.queue[e.data.id].reject(e.data.error);
}
delete this.queue[e.data.id];
});
return worker;
};
window.Worker.wrap = function (fn) {
var worker = createAndListen(),
retFunc = function () {
var args = Array.prototype.slice.call(arguments).map(function (arg) {
if (typeof arg === 'function') {
return '!FN!' + arg.toString();
}
return arg;
});
if (!worker) {
return Promise.reject("Worker has been terminated. Call `.restart()` before trying again.");
}
worker.postMessage({
id: ++worker.queue.index,
func: fn.toString(),
args: args
});
return (function (index) {
return new Promise(function (resolve, reject) {
worker.queue[index] = {
resolve: resolve,
reject: reject
};
});
})(worker.queue.index);
};
retFunc.terminate = function () {
if (!worker) {
return false;
}
worker.terminate();
worker = null;
return true;
};
retFunc.restart = function () {
worker = createAndListen();
};
return retFunc;
};
})();