-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroamer.js
51 lines (44 loc) · 1.13 KB
/
roamer.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
var Roamer = (function() {
var _bindings;
var serviceWorker;
return {
bind: bind,
init: init
};
function sendMessage(message) {
var messageChannel = new MessageChannel();
messageChannel.port1.onmessage = function(event) {
console.log(event);
if (event.data.error) {
reject(event.data.error);
} else {
resolve(event.data);
}
};
serviceWorker.postMessage(message, [messageChannel.port2]);
}
// this would be passed to ServiceWorker using an API call
function init(callback) {
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('/roamersw.js', {scope: '/'})
.then(function(reg) {
serviceWorker = reg.active;
callback();
})
.catch(function(err) {
// registration failed :(
callback(err);
});
}
}
function bind(bindings, finishCallback) {
console.log('putting bindings');
if (typeof(bindings) != 'object') {
throw TypeError("Bindings passed to init need to be of object type.");
}
sendMessage({
command: 'bind',
payload: bindings
});
}
})();