-
Notifications
You must be signed in to change notification settings - Fork 120
/
catiline.js
769 lines (731 loc) · 23.5 KB
/
catiline.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
/*! catiline 2.4.2 2013-08-08*/
/*!©2013 Calvin Metcalf @license MIT https://github.com/calvinmetcalf/catiline */
if (typeof document === 'undefined') {
self._noTransferable=true;
self.onmessage=function(e){
/*jslint evil: true */
eval(e.data);
};
} else {
(function(global){
'use strict';
/*!From setImmediate Copyright (c) 2012 Barnesandnoble.com,llc, Donavon West, and Domenic Denicola @license MIT https://github.com/NobleJS/setImmediate */
(function(attachTo,global) {
var tasks = (function () {
function Task(handler, args) {
this.handler = handler;
this.args = args;
}
Task.prototype.run = function () {
// See steps in section 5 of the spec.
if (typeof this.handler === 'function') {
// Choice of `thisArg` is not in the setImmediate spec; `undefined` is in the setTimeout spec though:
// http://www.whatwg.org/specs/web-apps/current-work/multipage/timers.html
this.handler.apply(undefined, this.args);
} else {
var scriptSource = '' + this.handler;
/*jshint evil: true */
eval(scriptSource);
}
};
var nextHandle = 1; // Spec says greater than zero
var tasksByHandle = {};
var currentlyRunningATask = false;
return {
addFromSetImmediateArguments: function (args) {
var handler = args[0];
var argsToHandle = Array.prototype.slice.call(args, 1);
var task = new Task(handler, argsToHandle);
var thisHandle = nextHandle++;
tasksByHandle[thisHandle] = task;
return thisHandle;
},
runIfPresent: function (handle) {
// From the spec: 'Wait until any invocations of this algorithm started before this one have completed.'
// So if we're currently running a task, we'll need to delay this invocation.
if (!currentlyRunningATask) {
var task = tasksByHandle[handle];
if (task) {
currentlyRunningATask = true;
try {
task.run();
} finally {
delete tasksByHandle[handle];
currentlyRunningATask = false;
}
}
} else {
// Delay by doing a setTimeout. setImmediate was tried instead, but in Firefox 7 it generated a
// 'too much recursion' error.
global.setTimeout(function () {
tasks.runIfPresent(handle);
}, 0);
}
},
remove: function (handle) {
delete tasksByHandle[handle];
}
};
}());
// Installs an event handler on `global` for the `message` event: see
// * https://developer.mozilla.org/en/DOM/window.postMessage
// * http://www.whatwg.org/specs/web-apps/current-work/multipage/comms.html#crossDocumentMessages
var MESSAGE_PREFIX = 'com.catilinejs.setImmediate' + Math.random();
function isStringAndStartsWith(string, putativeStart) {
return typeof string === 'string' && string.substring(0, putativeStart.length) === putativeStart;
}
function onGlobalMessage(event) {
// This will catch all incoming messages (even from other windows!), so we need to try reasonably hard to
// avoid letting anyone else trick us into firing off. We test the origin is still this window, and that a
// (randomly generated) unpredictable identifying prefix is present.
if (event.source === global && isStringAndStartsWith(event.data, MESSAGE_PREFIX)) {
var handle = event.data.substring(MESSAGE_PREFIX.length);
tasks.runIfPresent(handle);
}
}
if (global.addEventListener) {
global.addEventListener('message', onGlobalMessage, false);
} else {
global.attachEvent('onmessage', onGlobalMessage);
}
attachTo.setImmediate = function () {
var handle = tasks.addFromSetImmediateArguments(arguments);
// Make `global` post a message to itself with the handle and identifying prefix, thus asynchronously
// invoking our onGlobalMessage listener above.
global.postMessage(MESSAGE_PREFIX + handle, '*');
return handle;
};
})(catiline,global);
/*! Promiscuous ©2013 Ruben Verborgh @license MIT https://github.com/RubenVerborgh/promiscuous*/
(function (exports,tick) {
var func = 'function';
// Creates a deferred: an object with a promise and corresponding resolve/reject methods
function Deferred() {
// The `handler` variable points to the function that will
// 1) handle a .then(onFulfilled, onRejected) call
// 2) handle a .resolve or .reject call (if not fulfilled)
// Before 2), `handler` holds a queue of callbacks.
// After 2), `handler` is a simple .then handler.
// We use only one function to save memory and complexity.
var handler = function (onFulfilled, onRejected, value) {
// Case 1) handle a .then(onFulfilled, onRejected) call
var createdDeffered;
if (onFulfilled !== handler) {
createdDeffered = createDeferred();
handler.queue.push({ deferred: createdDeffered, resolve: onFulfilled, reject: onRejected });
return createdDeffered.promise;
}
// Case 2) handle a .resolve or .reject call
// (`onFulfilled` acts as a sentinel)
// The actual function signature is
// .re[ject|solve](sentinel, success, value)
var action = onRejected ? 'resolve' : 'reject',queue,deferred,callback;
for (var i = 0, l = handler.queue.length; i < l; i++) {
queue = handler.queue[i];
deferred = queue.deferred;
callback = queue[action];
if (typeof callback !== func) {
deferred[action](value);
} else {
execute(callback, value, deferred);
}
}
// Replace this handler with a simple resolved or rejected handler
handler = createHandler(promise, value, onRejected);
};
function Promise(){
this.then=function (onFulfilled, onRejected) {
return handler(onFulfilled, onRejected);
};
}
var promise = new Promise();
this.promise = promise;
// The queue of deferreds
handler.queue = [];
this.resolve = function (value) {
handler.queue && handler(handler, true, value);
};
this.reject = function (reason) {
handler.queue && handler(handler, false, reason);
};
}
function createDeferred(){
return new Deferred();
}
// Creates a fulfilled or rejected .then function
function createHandler(promise, value, success) {
return function (onFulfilled, onRejected) {
var callback = success ? onFulfilled : onRejected, result;
if (typeof callback !== func) {
return promise;
}
execute(callback, value, result = createDeferred());
return result.promise;
};
}
// Executes the callback with the specified value,
// resolving or rejecting the deferred
function execute(callback, value, deferred) {
tick(function () {
var result;
try {
result = callback(value);
if (result && typeof result.then === func) {
result.then(deferred.resolve, deferred.reject);
} else {
deferred.resolve(result);
}
}
catch (error) {
deferred.reject(error);
}
});
}
// Returns a resolved promise
exports.resolve= function (value) {
var promise = {};
promise.then = createHandler(promise, value, true);
return promise;
};
// Returns a rejected promise
exports.reject= function (reason) {
var promise = {};
promise.then = createHandler(promise, reason, false);
return promise;
};
// Returns a deferred
exports.deferred= createDeferred;
exports.all=function(array){
var promise = exports.deferred();
var len = array.length;
var resolved=0;
var out = [];
var onSuccess=function(n){
return function(v){
out[n]=v;
resolved++;
if(resolved===len){
promise.resolve(out);
}
};
};
array.forEach(function(v,i){
v.then(onSuccess(i),function(a){
promise.reject(a);
});
});
return promise.promise;
};
})(catiline,catiline.setImmediate);
catiline._hasWorker = typeof Worker !== 'undefined'&&typeof fakeLegacy === 'undefined';
catiline.URL = window.URL || window.webkitURL;
catiline._noTransferable=!catiline.URL;
//regex out the importScript call and move it up to the top out of the function.
function regexImports(string){
var rest=string,
match = true,
matches = {},
loopFunc = function(a,b){
if(b){
'importScripts('+b.split(',').forEach(function(cc){
matches[catiline.makeUrl(cc.match(/\s*[\'\"](\S*)[\'\"]\s*/)[1])]=true; // trim whitespace, add to matches
})+');\n';
}
};
while(match){
match = rest.match(/(importScripts\(.*?\);?)/);
rest = rest.replace(/(importScripts\(\s*(?:[\'\"].*?[\'\"])?\s*\);?)/,'\n');
if(match){
match[0].replace(/importScripts\(\s*([\'\"].*?[\'\"])?\s*\);?/g,loopFunc);
}
}
matches = Object.keys(matches);
return [matches,rest];
}
function moveImports(string){
var str = regexImports(string);
var matches = str[0];
var rest = str[1];
if(matches.length>0){
return 'importScripts(\''+matches.join('\',\'')+'\');\n'+rest;
}else{
return rest;
}
}
function moveIimports(string){
var str = regexImports(string);
var matches = str[0];
var rest = str[1];
if(matches.length>0){
return 'importScripts(\''+matches.join('\',\'')+'\');eval(__scripts__);\n'+rest;
}else{
return rest;
}
}
function getPath(){
if(typeof SHIM_WORKER_PATH !== 'undefined'){
return SHIM_WORKER_PATH;
}else if('SHIM_WORKER_PATH' in catiline){
return catiline.SHIM_WORKER_PATH;
}
var scripts = document.getElementsByTagName('script');
var len = scripts.length;
var i = 0;
while(i<len){
if(/catiline(\.min)?\.js/.test(scripts[i].src)){
return scripts[i].src;
}
i++;
}
}
function appendScript(iDoc,text){
var iScript = iDoc.createElement('script');
if (typeof iScript.text !== 'undefined') {
iScript.text = text;
} else {
iScript.innerHTML = text;
}
if(iDoc.readyState==='complete'){
iDoc.documentElement.appendChild(iScript);
}else{
iDoc.onreadystatechange=function(){
if(iDoc.readyState==='complete'){
iDoc.documentElement.appendChild(iScript);
}
};
}
}
//much of the iframe stuff inspired by https://github.com/padolsey/operative
//mos tthings besides the names have since been changed
function actualMakeI(script,codeword){
var iFrame = document.createElement('iframe');
iFrame.style.display = 'none';
document.body.appendChild(iFrame);
var iWin = iFrame.contentWindow;
var iDoc = iWin.document;
var text=['try{ ',
'var __scripts__=\'\';function importScripts(scripts){',
' if(Array.isArray(scripts)&&scripts.length>0){',
' scripts.forEach(function(url){',
' var ajax = new XMLHttpRequest();',
' ajax.open(\'GET\',url,false);',
' ajax.send();__scripts__+=ajax.responseText;',
' __scripts__+=\'\\n;\';',
' });',
' }',
'};',
script,
'}catch(e){',
' window.parent.postMessage([\''+codeword+'\',\'error\'],\'*\')',
'}'].join('\n');
appendScript(iDoc,text);
return iFrame;
}
function makeIframe(script,codeword){
var promise = catiline.deferred();
if(document.readyState==='complete'){
promise.resolve(actualMakeI(script,codeword));
}else{
window.addEventListener('load',function(){
promise.resolve(actualMakeI(script,codeword));
},false);
}
return promise.promise;
}
catiline.makeIWorker = function (strings,codeword){
var script =moveIimports(strings.join(''));
var worker = {onmessage:function(){}};
var ipromise = makeIframe(script,codeword);
window.addEventListener('message',function(e){
if(typeof e.data ==='string'&&e.data.length>codeword.length&&e.data.slice(0,codeword.length)===codeword){
worker.onmessage({data:JSON.parse(e.data.slice(codeword.length))});
}
});
worker.postMessage=function(data){
ipromise.then(function(iFrame){
iFrame.contentWindow.postMessage(JSON.stringify(data),'*');
});
};
worker.terminate=function(){
ipromise.then(function(iFrame){
document.body.removeChild(iFrame);
});
};
return worker;
};
//accepts an array of strings, joins them, and turns them into a worker.
function makeFallbackWorker(script){
catiline._noTransferable=true;
var worker = new Worker(getPath());
worker.postMessage(script);
return worker;
}
catiline.makeWorker = function (strings, codeword){
if(!catiline._hasWorker){
return catiline.makeIWorker(strings,codeword);
}
var worker;
var script =moveImports(strings.join(''));
if(catiline._noTransferable){
return makeFallbackWorker(script);
}
try{
worker= new Worker(catiline.URL.createObjectURL(new Blob([script],{type: 'text/javascript'})));
}catch(e){
try{
worker=makeFallbackWorker(script);
}catch(ee){
worker = catiline.makeIWorker(strings,codeword);
}
}finally{
return worker;
}
};
catiline.makeUrl = function (fileName) {
var link = document.createElement('link');
link.href = fileName;
return link.href;
};
catiline.Worker = function Catiline(obj) {
if(typeof obj === 'function'){
obj = {
data:obj
};
}
var __codeWord__='com.catilinejs.'+(catiline._hasWorker?'iframe':'worker')+Math.random();
var listeners = {};
var self = this;
self.on = function (eventName, func, scope) {
scope = scope || self;
if (eventName.indexOf(' ') > 0) {
eventName.split(' ').map(function (v) {
return self.on(v, func, scope);
}, this);
return self;
}
if (!(eventName in listeners)) {
listeners[eventName] = [];
}
listeners[eventName].push(function (a) {
func.call(scope, a);
});
return self;
};
function _fire(eventName, data) {
if (eventName.indexOf(' ') > 0) {
eventName.split(' ').forEach(function (v) {
_fire(v, data);
});
return self;
}
if (!(eventName in listeners)) {
return self;
}
listeners[eventName].forEach(function (v) {
v(data);
});
return self;
}
self.fire = function (eventName, data, transfer) {
if(catiline._noTransferable){
worker.postMessage([[eventName], data]);
}else{
worker.postMessage([[eventName], data], transfer);
}
return self;
};
self.off = function (eventName, func) {
if (eventName.indexOf(' ') > 0) {
eventName.split(' ').map(function (v) {
return self.off(v, func);
});
return self;
}
if (!(eventName in listeners)) {
return self;
}
else if (!func) {
delete listeners[eventName];
}
else {
if (listeners[eventName].indexOf(func) > -1) {
if (listeners[eventName].length > 1) {
delete listeners[eventName];
}
else {
listeners[eventName].splice(listeners[eventName].indexOf(func), 1);
}
}
}
return self;
};
var i = 0;
var promises = [];
var rejectPromises = function (msg) {
if (typeof msg !== 'string' && 'preventDefault' in msg) {
msg.preventDefault();
msg = msg.message;
}
promises.forEach(function (p) {
if (p) {
p.reject(msg);
}
});
};
obj.__codeWord__='"'+__codeWord__+'"';
if (!('initialize' in obj)) {
if ('init' in obj) {
obj.initialize = obj.init;
}
else {
obj.initialize = function () {};
}
}
var fObj = '{\n\t';
var keyFunc = function (key) {
var out = function (data, transfer) {
var i = promises.length;
promises[i] = catiline.deferred();
if(catiline._noTransferable){
worker.postMessage([[__codeWord__, i], key, data]);
}else{
worker.postMessage([[__codeWord__, i], key, data], transfer);
}
return promises[i].promise;
};
return out;
};
for (var key in obj) {
if (i !== 0) {
fObj = fObj + ',\n\t';
}
else {
i++;
}
fObj = fObj + key + ':' + obj[key].toString();
self[key] = keyFunc(key);
}
fObj = fObj + '}';
var worker = catiline.makeWorker(['\'use strict\';\n\nvar _db = ',fObj,';\nvar listeners = {};\nvar __iFrame__ = typeof document!=="undefined";\nvar __self__={onmessage:function(e){\n _fire("messege",e.data[1]);\n if(e.data[0][0]===_db.__codeWord__){\n return regMsg(e);\n }else{\n _fire(e.data[0][0],e.data[1]);\n }\n}};\nif(__iFrame__){\n window.onmessage=function(e){\n if(typeof e.data === "string"){\n e ={data: JSON.parse(e.data)};\n }\n __self__.onmessage(e);\n };\n}else{\n self.onmessage=__self__.onmessage;\n}\n__self__.postMessage=function(rawData, transfer){\n var data;\n if(!self._noTransferable&&!__iFrame__){\n self.postMessage(rawData, transfer);\n }else if(__iFrame__){\n data = _db.__codeWord__+JSON.stringify(rawData);\n window.parent.postMessage(data,"*");\n }else if(self._noTransferable){\n self.postMessage(rawData);\n }\n};\n_db.on = function (eventName, func, scope) {\n if(eventName.indexOf(" ")>0){\n return eventName.split(" ").map(function(v){\n return _db.on(v,func,scope);\n },_db);\n }\n scope = scope || _db;\n if (!(eventName in listeners)) {\n listeners[eventName] = [];\n }\n listeners[eventName].push(function (a) {\n func.call(scope, a, _db);\n });\n};\nfunction _fire(eventName,data){\n if(eventName.indexOf(" ")>0){\n eventName.split(" ").forEach(function(v){\n _fire(v,data);\n });\n return;\n }\n if (!(eventName in listeners)) {\n return;\n }\n listeners[eventName].forEach(function (v) {\n v(data);\n });\n}\n\n_db.fire = function (eventName, data, transfer) {\n __self__.postMessage([[eventName], data], transfer);\n};\n_db.off=function(eventName,func){\n if(eventName.indexOf(" ")>0){\n return eventName.split(" ").map(function(v){\n return _db.off(v,func);\n });\n }\n if(!(eventName in listeners)){\n return;\n }else if(!func){\n delete listeners[eventName];\n }else{\n if(listeners[eventName].indexOf(func)>-1){\n if(listeners[eventName].length>1){\n delete listeners[eventName];\n }else{\n listeners[eventName].splice(listeners[eventName].indexOf(func),1);\n }\n }\n }\n};\nvar console={};\nfunction makeConsole(method){\n return function(){\n var len = arguments.length;\n var out =[];\n var i = 0;\n while (i<len){\n out.push(arguments[i]);\n i++;\n }\n _db.fire("console",[method,out]);\n };\n}\n["log", "debug", "error", "info", "warn", "time", "timeEnd"].forEach(function(v){\n console[v]=makeConsole(v);\n});\nvar regMsg = function(e){\n var cb=function(data,transfer){\n __self__.postMessage([e.data[0],data],transfer);\n };\n var result;\n if(__iFrame__){\n try{\n result = _db[e.data[1]](e.data[2],cb,_db);\n }catch(e){\n _db.fire("error",JSON.stringify(e));\n }\n }else{\n result = _db[e.data[1]](e.data[2],cb,_db);\n }\n if(typeof result !== "undefined"){\n cb(result);\n }\n};\n_db.initialize(_db);\n'],__codeWord__);
worker.onmessage = function (e) {
_fire('message', e.data[1]);
if (e.data[0][0] === __codeWord__) {
promises[e.data[0][1]].resolve(e.data[1]);
promises[e.data[0][1]] = 0;
}
else {
_fire(e.data[0][0], e.data[1]);
}
};
self.on('error',rejectPromises);
worker.onerror = function (e) {
_fire('error', e);
};
self.on('console', function (msg) {
console[msg[0]].apply(console, msg[1]);
});
self._close = function () {
worker.terminate();
rejectPromises('closed');
return catiline.resolve();
};
if (!('close' in self)) {
self.close = self._close;
}
};
catiline.worker = function (obj){
return new catiline.Worker(obj);
};
catiline.Queue = function CatilineQueue(obj, n, dumb) {
var self = this;
self.__batchcb__ = {};
self.__batchtcb__ = {};
self.batch = function (cb) {
if (typeof cb === 'function') {
self.__batchcb__.__cb__ = cb;
return self.__batchcb__;
}
else {
return clearQueue(cb);
}
};
self.batchTransfer = function (cb) {
if (typeof cb === 'function') {
self.__batchtcb__.__cb__ = cb;
return self.__batchtcb__;
}
else {
return clearQueue(cb);
}
};
var workers = new Array(n);
var numIdle = 0;
var idle = [];
var que = [];
var queueLen = 0;
while (numIdle < n) {
workers[numIdle] = new catiline.Worker(obj);
idle.push(numIdle);
numIdle++;
}
self.on = function (eventName, func, context) {
workers.forEach(function (worker) {
worker.on(eventName, func, context);
});
return self;
};
self.off = function (eventName, func, context) {
workers.forEach(function (worker) {
worker.off(eventName, func, context);
});
return self;
};
var batchFire = function (eventName, data) {
workers.forEach(function (worker) {
worker.fire(eventName, data);
});
return self;
};
self.fire = function (eventName, data) {
workers[~~ (Math.random() * n)].fire(eventName, data);
return self;
};
self.batch.fire = batchFire;
self.batchTransfer.fire = batchFire;
function clearQueue(mgs) {
mgs = mgs || 'canceled';
queueLen = 0;
var oQ = que;
que = [];
oQ.forEach(function (p) {
p[3].reject(mgs);
});
return self;
}
function keyFunc(k) {
return function (data, transfer) {
return doStuff(k, data, transfer);
};
}
function keyFuncBatch(k) {
return function (array) {
return catiline.all(array.map(function (data) {
return doStuff(k, data);
}));
};
}
function keyFuncBatchCB(k) {
return function (array) {
var self = this;
return catiline.all(array.map(function (data) {
return doStuff(k, data).then(self.__cb__);
}));
};
}
function keyFuncBatchTransfer(k) {
return function (array) {
return catiline.all(array.map(function (data) {
return doStuff(k, data[0], data[1]);
}));
};
}
function keyFuncBatchTransferCB(k) {
return function (array) {
var self = this;
return catiline.all(array.map(function (data) {
return doStuff(k, data[0], data[1]).then(self.__cb__);
}));
};
}
for (var key in obj) {
self[key] = keyFunc(key);
self.batch[key] = keyFuncBatch(key);
self.__batchcb__[key] = keyFuncBatchCB(key);
self.batchTransfer[key] = keyFuncBatchTransfer(key);
self.__batchtcb__[key] = keyFuncBatchTransferCB(key);
}
function done(num) {
var data;
if (queueLen) {
data = que.shift();
queueLen--;
workers[num][data[0]](data[1], data[2]).then(function (d) {
done(num);
data[3].resolve(d);
}, function (d) {
done(num);
data[3].reject(d);
});
}
else {
numIdle++;
idle.push(num);
}
}
function doStuff(key, data, transfer) { //srsly better name!
if (dumb) {
return workers[~~ (Math.random() * n)][key](data, transfer);
}
var promise = catiline.deferred(),
num;
if (!queueLen && numIdle) {
num = idle.pop();
numIdle--;
workers[num][key](data, transfer).then(function (d) {
done(num);
promise.resolve(d);
}, function (d) {
done(num);
promise.reject(d);
});
}
else if (queueLen || !numIdle) {
queueLen = que.push([key, data, transfer, promise]);
}
return promise.promise;
}
self._close = function () {
return catiline.all(workers.map(function (w) {
return w._close();
}));
};
if (!('close' in self)) {
self.close = self._close;
}
};
catiline.queue = function (obj, n, dumb) {
return new catiline.Queue(obj, n, dumb);
};
function catiline(object,queueLength,unmanaged){
if(arguments.length === 1 || !queueLength || queueLength <= 1){
return new catiline.Worker(object);
}else{
return new catiline.Queue(object,queueLength,unmanaged);
}
}
function initBrowser(catiline){
var origCW = global.cw;
catiline.noConflict=function(newName){
global.cw = origCW;
if(newName){
global[newName]=catiline;
}
};
global.catiline = catiline;
global.cw = catiline;
if(!('communist' in global)){
global.communist=catiline;
}
}
if(typeof define === 'function'){
define(function(require){
catiline.SHIM_WORKER_PATH=require.toUrl('./catiline.js');
return catiline;
});
}else if(typeof module === 'undefined' || !('exports' in module)){
initBrowser(catiline);
} else {
module.exports=catiline;
}
catiline.version = '2.4.2';
})(this);}