-
Notifications
You must be signed in to change notification settings - Fork 16
/
service-worker.js
49 lines (41 loc) · 1.02 KB
/
service-worker.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
var connect = require("../can-connect");
var makeDeferred = require("../helpers/deferred");
var canLog = require("can-log");
/**
* @module can-connect/service-worker
* @parent can-connect.behaviors
* @hide
*/
module.exports = connect.behavior("service-worker",function(baseConnection){
var worker = new Worker(this.workerURL);
var requestId = 0;
var requestDeferreds = {};
var isReady = makeDeferred();
var makeRequest = function(data){
var reqId = requestId++;
var def = makeDeferred();
requestDeferreds[reqId] = def;
isReady.promise.then(function(){
worker.postMessage({
request: data,
requestId: reqId
});
});
return def.promise;
};
worker.onmessage = function(ev){
canLog.log("MAIN - got message", ev.data.type);
if(ev.data.type === "ready"){
isReady.resolve();
} else if(ev.data.type === "response") {
requestDeferreds[ev.data.requestId].resolve(ev.data.response);
}
};
return {
getListData: function(params){
return makeRequest({
params: params
});
}
};
});