-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathThread.js
More file actions
42 lines (33 loc) · 1.25 KB
/
Thread.js
File metadata and controls
42 lines (33 loc) · 1.25 KB
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
function Thread(threadMethod) {
var methodStr = threadMethod.toString().replace(/((\/\/.*$)|(\/\*[\s\S]*?\*\/))/mg, '') //Clear comments
var paramsArray = methodStr.slice(methodStr.indexOf('(') + 1, methodStr.indexOf(')')).match(/([^\s,]+)/g)
var methodBody = methodStr.substring(methodStr.indexOf('{') + 1, methodStr.lastIndexOf('}'));
var args, worker;
if (paramsArray !== null) {
if (paramsArray.length >= 1) {
args = paramsArray[0];
}
}
this.start = function (arguments) {
//Inject custom arguments field
if (arguments != undefined) {
if (paramsArray.length >= 1) {
methodBody = paramsArray[0] + "=" + JSON.stringify(arguments) + ";" + methodBody;
}
}
var objURL = URL.createObjectURL(new Blob([methodBody], { type: 'text/javascript' }));
worker = new Worker(objURL);
worker.onmessage = this.onmessage;
worker.onerror = this.onerror;
}
//Posts a message to the underlying WebWorker
this.postMessage = function (data) {
worker.postMessage(data);
};
//Stops the underlying webworker
this.stop = function () {
if (worker) {
worker.terminate();
}
};
}