/
tab.js
196 lines (172 loc) · 4.44 KB
/
tab.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import PouchDB from 'pouchdb';
import createClass from '../lang/class';
import iobject from '../lang/iobject';
export default createClass({
constructor: function (localDbName, remoteDbLocation, changesCallback) {
if (!localDbName || typeof localDbName !== 'string') {
throw new Error('missing localDbName parameter');
}
if (!remoteDbLocation || typeof remoteDbLocation !== 'string') {
throw new Error('missing remoteDbLocation parameter');
}
this.remoteDbLocation = remoteDbLocation;
this._onChangesHandler = changesCallback;
this.db = new PouchDB(localDbName);
this.remoteDb = new PouchDB(remoteDbLocation);
},
connect: function () {
var allDocs;
return (
this.replicateFromRemote()
.then(this.fetchAll.bind(this))
.then(function (docs) {
allDocs = docs;
})
.then(function () {
return this.db.info();
}.bind(this))
.then(function (info) {
this._lastSequenceNumber = info.update_seq;
}.bind(this))
.then(this.startSyncing.bind(this))
.then(function () {
return allDocs;
})
);
},
createDoc: function (doc) {
if (doc._id) {
return (
this.db.put(doc)
.then(function (response) {
console.log('db: put doc (created)', response);
})
);
}
return (
this.db.post(doc)
.then(function (response) {
console.log('db: posted doc', response);
})
);
},
updateDoc: function (doc) {
return (
this.db.get(doc._id)
.then(function (fetchedDoc) {
return iobject.merge(doc, {_rev: fetchedDoc._rev});
})
.then(this.db.put.bind(this.db))
.then(function (response) {
console.log('db: put doc (updated)', response);
})
);
},
deleteDoc: function (id) {
return (
this.db.get(id)
.then(function (fetchedDoc) {
return {
_id: id,
_rev: fetchedDoc._rev,
_deleted: true
};
})
.then(this.db.put.bind(this.db))
.then(function (response) {
console.log('db: deleted doc', response);
})
);
},
replicateFromRemote: function () {
console.info('replication start');
return new Promise(function (resolve) {
this.db.replicate.from(this.remoteDb, {
batch_size: 100
})
.on('paused', function () {
console.info('replication paused');
})
.on('active', function () {
console.info('replication active');
})
.on('complete', function () {
console.info('replication complete');
resolve();
})
.on('error', function (err) {
console.error('replication error', err);
// resolve even in error case
// incomplete replication can be handled by next sync
resolve();
});
}.bind(this));
},
fetchAll: function () {
return this.db.allDocs({
include_docs: true,
attachments: true
})
.then(function (result) {
return result.rows.map(function (row) {
return row.doc;
});
});
},
startSyncing: function () {
this.sync();
this._startSyncInterval();
},
/**
* This sets up replication between the local database
* and its remote counterpart
*/
sync: function () {
this._isSyncing = true;
this.syncHandle = this.db.sync(this.remoteDb, {
batch_size: 100
})
.on('error', function (err) {
console.error('replication error', err);
this._isSyncing = false;
this._emitChanges();
}.bind(this))
.on('complete', function () {
this._isSyncing = false;
this._emitChanges();
}.bind(this));
},
cancelSync: function () {
this.syncHandle.cancel();
},
_startSyncInterval: function () {
setInterval(function () {
if (!this._isSyncing) {
this.sync();
}
}.bind(this), 7500);
},
_emitChanges: function () {
this.db.changes({
since: this._lastSequenceNumber,
include_docs: true
})
.on('complete', function (info) {
this._lastSequenceNumber = info.last_seq;
if (!info.results.length) {
return;
}
console.info('db changes', info.results);
this._onChangesHandler(info.results);
}.bind(this))
.on('error', console.error.bind(console));
},
/**
* removes local database without triggering
* events on objects.
*/
destroy: function () {
this.cancelSync();
this.db.destroy();
}
});