-
Notifications
You must be signed in to change notification settings - Fork 3
/
commune.js
155 lines (138 loc) · 4.32 KB
/
commune.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// Generated by CoffeeScript 1.6.3
/*
* Commune.js
* Web workers lose their chains
* 0.2.2
* Easy, DRY, transparent worker threads for your app
* Dan Motzenbecker
* http://oxism.com
* MIT License
*/
(function() {
var Commune, communes, makeBlob, mime, threadSupport;
communes = {};
makeBlob = null;
mime = 'application\/javascript';
Commune = (function() {
function Commune(fnString) {
var lastReturnIndex;
if (fnString.match(/\bthis\b/)) {
if (typeof console !== "undefined" && console !== null) {
console.warn('Commune: Referencing `this` within a worker process might not work as expected.\n`this` will refer to the worker itself or an object created within the worker.');
}
}
if ((lastReturnIndex = fnString.lastIndexOf('return')) === -1) {
throw new Error('Commune: Target function has no return statement.');
}
this.blobUrl = makeBlob((fnString.slice(0, lastReturnIndex) + (" self.postMessage(" + (fnString.substr(lastReturnIndex).replace(/return\s+|;|\}$/g, '')) + ");\n}")).replace(/^function(.+)?\(/, 'function __communeInit(') + 'if (typeof window === \'undefined\') {\n self.addEventListener(\'message\', function(e) {\n __communeInit.apply(this, e.data);\n });\n}');
}
Commune.prototype.spawnWorker = function(args, cb) {
var worker;
worker = new Worker(this.blobUrl);
worker.addEventListener('message', function(e) {
cb(e.data);
return worker.terminate();
});
return worker.postMessage(args);
};
return Commune;
})();
threadSupport = (function() {
var Blob, URL, e, testBlob, testString, testUrl, testWorker;
try {
testBlob = new this.Blob;
Blob = this.Blob;
} catch (_error) {
e = _error;
Blob = this.BlobBuilder || this.WebKitBlobBuilder || this.MozBlobBuilder || false;
}
URL = this.URL || this.webkitURL || this.mozURL || false;
if (!(Blob && URL && this.Worker)) {
return false;
}
testString = 'true';
try {
if (Blob === this.Blob) {
testBlob = new Blob([testString], {
type: mime
});
makeBlob = function(string) {
return URL.createObjectURL(new Blob([string], {
type: mime
}));
};
} else {
testBlob = new Blob;
testBlob.append(testString);
testBlob = testBlob.getBlob(mime);
makeBlob = function(string) {
var blob;
blob = new Blob;
blob.append(string);
return URL.createObjectURL(blob.getBlob(mime));
};
}
testUrl = URL.createObjectURL(testBlob);
testWorker = new Worker(testUrl);
testWorker.terminate();
return true;
} catch (_error) {
e = _error;
if (e.name === 'SECURITY_ERR') {
if (typeof console !== "undefined" && console !== null) {
console.warn('Commune: Cannot provision workers when serving ' + 'via `file://` protocol. Serve over http(s) to use worker threads.');
}
}
return false;
}
})();
this.commune = function(fn, args, cb) {
var commune, fnString;
if (typeof fn !== 'function') {
throw new Error('Commune: Must pass a function as first argument.');
}
if (typeof args === 'function') {
cb = args;
args = [];
}
if (threadSupport) {
fnString = fn.toString();
if (!communes[fnString]) {
if (typeof cb !== 'function') {
throw new Error('Commune: Must pass a callback to use worker result.');
}
commune = communes[fnString] = new Commune(fnString);
} else {
commune = communes[fnString];
}
return commune.spawnWorker(args, cb);
} else {
return setTimeout((function() {
return cb(fn.apply(this, args));
}), 0);
}
};
this.communify = function(fn, args) {
if (args) {
return function(cb) {
return commune(fn, args, cb);
};
} else {
return function(args, cb) {
return commune(fn, args, cb);
};
}
};
this.commune.isThreaded = function() {
return threadSupport;
};
this.commune.disableThreads = function() {
return threadSupport = false;
};
this.commune.enableThreads = function() {
return threadSupport = true;
};
}).call(this);
/*
//@ sourceMappingURL=commune.map
*/