This repository has been archived by the owner on Jan 2, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
firefly-sync.js
290 lines (251 loc) · 9.2 KB
/
firefly-sync.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
* Copyright (C) 2017-2017 Regents of the University of California.
* @author: Peter Gusev <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* A copy of the GNU Lesser General Public License is in the file COPYING.
*/
fireflySyncDebug = true; // toggle this to turn on / off for global controll
if (fireflySyncDebug) var debug = console.log.bind(window.console);
else var debug = function(){};
/**
* FireflySync provides sync mechanism similar to ChronoSync, only backed by Firestore backend.
* @param {firebase.firestore.Firestore} An initialized Firestore object
* @param syncDoc A dictionary with two keys: appName and syncId which together identify
* client's app sync entity (example: WhatsApp chatroom; appName='whatsapp',
* syncId='chatroom-id').
* @param applicationPrefix Simliar to ChronoSync, this is an application prefix, which
* client code will use for data publishing.
* @param onReceivedSyncState A callback (function({'prefix':<seq-no>})) which will be called
* whenever new updates (sequence numbres) are received.
*/
var FireflySync = function FireflySync(firestoreDb, syncDoc, applicationPrefix, onInitialized, onReceivedSyncState){
this.firestoreDb = firestoreDb;
this.onReceivedSyncState = onReceivedSyncState;
this.onInitialized = onInitialized;
this.applicationDataPrefixUri = applicationPrefix;
this.syncDoc = syncDoc;
this.lockDocName = this.docNameWithoutVersion()+'/lock';
this.syncData = {};
this.syncDocVersion = -1;
this.mySeqNo = -1;
this.initialized = false;
var self = this;
this.firestoreDb.doc('/sync/'+syncDoc.appName).collection(syncDoc.syncId).onSnapshot(function(snapshot){
debug('> firefly-sync: got collection snapshot: ', snapshot);
if (snapshot.empty)
{
debug('> firefly-sync: sync doc does not exist.',);
// self.createLockDoc();
self.checkCallOnInitialized();
}
else
{
// get latest document version
var lastDoc = self.sorted(snapshot.docChanges)[snapshot.docChanges.length-1].doc;
if (lastDoc.id != 'lock')
{
var newDocVersion = parseInt(lastDoc.id);
if (newDocVersion < self.syncDocVersion)
console.error('> firefly-sync: something is wrong, latest doc version less than stored version '+
newDocVersion +' vs '+self.syncDocVersion)
else
{
self.syncDocVersion = newDocVersion;
debug('> firefly-sync: last sync doc version is ', self.syncDocVersion);
self.processNewSyncState(lastDoc);
}
}
}
},function(error){
console.error('> firefly-sync: error getting collection '+syncDoc.syncId+': ', error);
})
};
FireflySync.prototype.processNewSyncState = function(doc){
var delta = {}
var syncData = doc.data();
var self = this;
for (var key in syncData)
{
var decodedKey = decodeURIComponent(key);
var newValue = !(key in self.syncData) && decodedKey != self.applicationDataPrefixUri;
if (!newValue) newValue = (syncData[key] > self.syncData[key]);
if (newValue) delta[key] = syncData[key];
// update our seq no if needed
if (decodedKey == self.applicationDataPrefixUri && self.mySeqNo < syncData[key])
self.mySeqNo = syncData[key];
self.syncData[key] = syncData[key];
}
self.checkCallOnInitialized();
if (Object.keys(delta).length)
{
// self.onReceivedSyncState(delta);
// this if for backward compatibility with old ChronoChat code
// if you don't need this compatibility, use the line above
var syncStates = [];
for (var key in delta)
{
var decodedKey = decodeURIComponent(key);
syncStates.push(new ChronoSync2013.SyncState (decodedKey, 0, delta[key], new Blob()));
}
self.onReceivedSyncState(syncStates);
}
else
debug('> firefly-sync: no sync updates');
}
FireflySync.prototype.sorted = function(docChangesArray){
return docChangesArray.sort(function(a,b){
var aId = parseInt(a.doc.id);
var bId = parseInt(b.doc.id);
if(aId < bId) return -1;
if(aId > bId) return 1;
return 0;
});
}
FireflySync.prototype.sortedDocs = function(docsArray){
return docsArray.sort(function(a,b){
var aId = parseInt(a.id);
var bId = parseInt(b.id);
if(aId < bId) return -1;
if(aId > bId) return 1;
return 0;
});
}
FireflySync.prototype.fullDocName = function(){
return this.docNameWithoutVersion()+'/'+this.syncDocVersion;
}
FireflySync.prototype.docNameWithoutVersion = function() {
return '/sync/'+this.syncDoc.appName+'/'+this.syncDoc.syncId;
};
FireflySync.prototype.createLockDoc = function(){
debug('> firefly-sync: will create lock document', this.lockDocName);
this.firestoreDb.doc(this.lockDocName).set({ version:this.syncDocVersion })
.then(function(){
debug('> firefly-sync: lock doc created');
})
.catch(function(error){
console.error('> firefly-sync: error creating lock doc: ', error);
});
}
FireflySync.prototype.createSyncDoc = function(syncDocName) {
debug('> firefly-sync: will create ', syncDocName);
// setup empty document
var self = this;
this.firestoreDb.doc(syncDocName).set(this.syncData)
.then(function(){
debug('> firefly-sync: new sync doc created: ', syncDocName);
debug('> firefly-sync: contents: ', self.syncData);
self.checkCallOnInitialized();
})
.catch(function(error){
console.error('> firefly-sync: error creating sync doc: ', error);
});
}
FireflySync.prototype.checkCallOnInitialized = function(){
if (!this.initialized)
{
this.initialized = true
this.onInitialized();
}
}
/**
* Simliar to ChronoSync, this will increment current sequence number and notify all other participants
* through updating sync document.
* @return New sequence number
*/
FireflySync.prototype.publishNextSequenceNo = function(){
this.syncDocVersion++;
this.mySeqNo++;
this.syncData[encodeURIComponent(this.applicationDataPrefixUri)] = this.mySeqNo;
this.createSyncDoc(this.fullDocName());
// var lockDocRef = this.firestoreDb.doc(this.lockDocName)
// var self = this;
// var i = 1;
// this.firestoreDb.runTransaction(function(transaction) {
// debug('transation run ', i++);
// return transaction.get(lockDocRef).then(function(lockDoc) {
// self.syncDocVersion = lockDoc.data().version; // get latest version
// // now we need to read sync doc from db to update our syncData
// var syncDocRef = self.firestoreDb.doc(self.fullDocName());
// syncDocRef.get().then(function(doc){
// var newVersion = self.syncDocVersion+1;
// if (doc.exists)
// {
// self.processNewSyncState(doc); // update sync data from new sync doc
// lockDoc.data().version + 1; // bump the version
// transaction.update(syncDocRef, doc.data()); // dummy update to make firebase happy
// }
// else
// transaction.update(syncDocRef, {}); // dummy update to make firebase happy
// transaction.update(lockDocRef, { version: newVersion }); // save new version
// self.syncDocVersion = newVersion;
// self.createSyncDoc(self.fullDocName()); // create new sync doc
// });
// });
// }).then(function() {
// // debug("> firefly-sync: transaction successfully committed!");
// }).catch(function(error) {
// console.error("> firefly-sync: transaction failed: ", error);
// });
return this.mySeqNo;
}
/**
* Returns current sequence number
*/
FireflySync.prototype.getSequenceNo = function(){
return this.mySeqNo;
}
FireflySync.prototype.getSyncStatesDelta = function(syncDocOld, syncDocNew) {
var delta = {};
for (var key in syncDocNew)
{
var newValue = !(key in syncDocOld);
if (!newValue) newValue = (syncDocNew[key] > syncDocOld[key]);
if (newValue) delta[key] = syncDocNew[key];
}
return delta;
};
FireflySync.prototype.getHistoricalSyncStates = function(onSyncStatesFetched) {
var collRef = this.firestoreDb.doc('/sync/'+syncDoc.appName).collection(syncDoc.syncId);
var self = this;
var syncStates = [];
collRef.get().then(function(snap){
var sortedDocs = self.sortedDocs(snap.docs);
if (sortedDocs.length)
sortedDocs.forEach(function(docSnap, idx, arr){
syncStates.push(docSnap.data());
if (idx == arr.length-1) onSyncStatesFetched(syncStates);
});
else
onSyncStatesFetched(syncStates);
});
};
FireflySync.prototype.getHistoricalDeltas = function(onDeltasFetched) {
var self = this;
this.getHistoricalSyncStates(function(syncStates){
var lastSyncState = null
var deltas = []
if (syncStates.length)
syncStates.forEach(function(syncState, idx, arr){
if (lastSyncState)
deltas.push(self.getSyncStatesDelta(lastSyncState, syncState));
else
deltas.push(syncState);
lastSyncState = syncState
if (idx == arr.length-1) onDeltasFetched(deltas);
});
else
onDeltasFetched(deltas);
});
}