-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsw.js
71 lines (63 loc) · 2.06 KB
/
sw.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
importScripts('bower_components/pouchdb/dist/pouchdb.js');
// PouchDB.debug.enable('*');
let localMainDB = new PouchDB('localMainDB');
let remoteDB = new PouchDB('http://couchadmin:test@localhost:5984/main');
function push() {
console.log('sw pushing');
return localMainDB.replicate.to(remoteDB)
.on('change', function(info) {
console.log('push change: ', info);
}).on('paused', function(err) {
console.log('push paused: ', err);
}).on('active', function() {
console.log('pull active');
}).on('denied', function(err) {
console.log('push denied: ', err);
}).on('complete', function(info) {
console.log('push complete: ', info);
}).on('error', function(err) {
console.log('push error: ', err);
})
.catch(function(err) {
console.log('push promise error: ', err);
});
}
function pull() {
console.log('sw pulling');
return localMainDB.replicate.from(remoteDB)
.on('change', function(info) {
console.log('pull change: ', info);
}).on('paused', function(err) {
console.log('pull paused: ', err);
}).on('active', function() {
console.log('pull active');
}).on('denied', function(err) {
console.log('pull denied: ', err);
}).on('complete', function(info) {
console.log('pull complete: ', info);
}).on('error', function(err) {
console.log('pull error: ', err);
})
.catch(function(err) {
console.log('pull promise error: ', err);
});
}
// self.addEventListener('install', function(event) {
// self.skipWaiting();
// });
self.addEventListener('activate', function(event) {
console.log('sw activate event: ', event);
// event.waitUntil(pull()); // failed with 500 missing
});
self.addEventListener('sync', function(event) {
console.log('sw sync event: ', event);
if (event.tag === 'push') {
event.waitUntil(push());
} else if (event.tag === 'pull') {
event.waitUntil(pull());
}
});
// this.addEventListener('fetch', function(event) {
// console.log('sw fetch event: ', event);
// event.respondWith(fetch(event.request));
// });