-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
82 lines (73 loc) · 2.19 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>PouchDB in Service Worker</title>
</head>
<body>
<h1>PouchDB in Service Worker</h1>
<script src="bower_components/pouchdb/dist/pouchdb.js"></script>
<script>
PouchDB.debug.enable('*');
const localMainDB = new PouchDB('localMainDB');
const remoteDB = new PouchDB('http://couchadmin:test@localhost:5984/main');
if ('serviceWorker' in navigator) {
function sync() {
console.log('local syncing');
return localMainDB.sync(remoteDB).on('error', function(err) {
console.log('local sync error:', err);
});
}
function push() {
console.log('local pushing');
navigator.serviceWorker.ready
.then(function(reg) {
return reg.sync.register('push');
});
}
function pull() {
console.log('local pulling');
navigator.serviceWorker.ready
.then(function(reg) {
return reg.sync.register('pull');
});
}
function watchLocalDB() {
console.log('watching local DB');
return localMainDB.changes({
since: 'now',
live: true
}).on('change', function(change) {
console.log('local change: ', change);
push();
}).on('error', function(err) {
console.log('local err: ', err);
});
}
function watchRemoteDB() {
console.log('watching remote DB');
return remoteDB.changes({
since: 'now',
live: true
}).on('change', function(change) {
console.log('remote change: ', change);
pull();
}).on('error', function(err) {
console.log('remote err: ', err);
});
}
sync().then(function() {
watchLocalDB();
watchRemoteDB();
});
navigator.serviceWorker.register('/sw.js')
.then(function(reg) {
console.log('Yey!', reg);
})
.catch(function(err) {
console.log('Boo!', err);
});
}
</script>
</body>
</html>