-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsingle.js
35 lines (34 loc) · 853 Bytes
/
single.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
//special case of worker only being called once, instead of sending the data
//we can bake the data into the worker when we make it.
cw.single = function (fun,data){
if(typeof Worker === 'undefined'||typeof fakeLegacy !== 'undefined'){
return cw.worker(fun).data(data);
}
var promise = cw.deferred();
var obj = {
fun:fun,
data:JSON.stringify(data),
init:function(){
var that = this;
var data = JSON.parse(this.data);
var cb = function(data,trans){
that.fire('done',data,trans);
};
var resp = that.fun(data,cb);
if(typeof resp !== 'undefined'){
cb(resp);
}
}
};
var worker = communist.worker(obj);
worker.on('done',function(e){
promise.resolve(e);
worker.close();
});
worker.on('error',function(e){
e.preventDefault();
promise.reject(e.message);
worker.close();
});
return promise.promise;
};