forked from hotpi/frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
syncer.js
364 lines (311 loc) · 9.64 KB
/
syncer.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/**
* TO BE IMPLEMENTED: Filter functionality
* Create a filter function that goes through the history and gets all the operations
* related to one specific index
*/
import 'isomorphic-fetch';
import Emitter from 'component-emitter';
import request from 'superagent';
import Throttle from 'superagent-throttle';
import * as fromDatabase from './helpers/databaseStorage';
import * as fromFakeBackend from './fakeBackend';
import xformT from './helpers/xformT';
import {
translateActionToOperation,
translateOperationToAction
} from './helpers/actionTranslator';
// TODO: change this logic
import { connectionMonitor } from './index';
// eslint-disable-next-line
const connectionEmitter = new Emitter;
// eslint-disable-next-line
const ROOT_URL = __API_ROOT_URL__;
const BROADCAST_URL = ROOT_URL + 'sync/status/';
const SEND_OP_URL = ROOT_URL + 'sync/sendOp';
const SUBSCRIBE_URL = ROOT_URL + 'sync/subscribe';
const INITIAL_STATE_URL = ROOT_URL + 'sync/initialState';
const superagentThrottle = new Throttle({
// set false to pause queue
active: true,
// how many requests can be sent every `ratePer`
rate: 30,
// number of ms in which `rate` requests may be sent
ratePer: 5000,
// how many requests can be sent concurrently
concurrent: 10
});
class Syncer {
constructor() {
this.hasAcknowledge = true;
this.longPolledRequests = [];
this.newOperationRequests = [];
this.inflightOp = null;
this.inFlight = false;
this.buffer = [];
this.revisionNr = 0;
// TODO: is it better to store them as operations are generated or get them from server?
this.history = [];
this.uid = 0;
this.disconnectionListener = connectionMonitor.listenToEvent(
'disconnected',
[
this.onDisconnectDo.bind(this, this.longPolledRequests, this.newOperationRequests)
]
);
this.reconnectionListener = connectionMonitor.listenToEvent(
'reconnected',
[
this.listen.bind(this),
this.sendToServer.bind(this)
]
);
if (!this.subscribe()) {
throw new Error('Unable to fetch uid');
}
this.listen();
}
onDisconnectDo(queuedLongPollingRequests, queuedOperationRequests) {
// this.hasAcknowledge = false;
// Abort all queued long-polled requests that are open.
queuedLongPollingRequests.map(longPolledRequest => {
console.log('>>>>> listen request aborted')
if (typeof longPolledRequest.abort === 'function') {
longPolledRequest.abort();
}
});
queuedLongPollingRequests.length = 0;
// Abort all queued new operation requests that are open.
queuedOperationRequests.map(newOperationRequest => {
if (typeof newOperationRequest.abort === 'function') {
newOperationRequest.abort();
}
});
}
newAction(action) {
let translatedOperation = translateActionToOperation(action, this.store);
if (typeof translatedOperation !== 'undefined') {
if (translatedOperation.length === 2) {
let operation = {
// import uid
origin: this.uid,
type: translatedOperation[0][0],
accessPath: translatedOperation[0][1],
node: translatedOperation[0][2],
action: translatedOperation[0][3]
};
this.generateOperation(operation);
translatedOperation = translatedOperation[1];
}
let operation = {
// import uid
origin: this.uid,
type: translatedOperation[0],
accessPath: translatedOperation[1],
node: translatedOperation[2],
action: translatedOperation[3]
};
this.generateOperation(operation);
}
}
generateOperation(newOp) {
this.store.dispatch({ type: 'NEW_OPERATION', operation: newOp });
if (this.inFlight) {
this.buffer.push(newOp);
} else {
this.inFlight = true;
this.inflightOp = newOp;
if (connectionMonitor.getConnectionStatus() === 'up') {
this.sendToServer();
}
}
}
transform(operations, receivedOp) {
receivedOp = operations.reduce((prev, curr) => {
return xformT(prev[0], curr);
}, [ receivedOp, {} ]);
return receivedOp;
}
apply(operationFunction) {
if (typeof operationFunction !== 'function') {
throw new Error('First argument of apply must be a function');
}
return operationFunction;
}
// Fetch from server localhost:3001/sendOp
sendToServer() {
// console.log('Sending operation: ', this.inflightOp, 'revisionNr: ', this.revisionNr);
if (this.inFlight && this.hasAcknowledge) {
this.hasAcknowledge = false;
let newOperationRequest = request
.post(SEND_OP_URL)
.set('Accept', 'application/json')
.set('Content-Type', 'application/json; charset=utf-8')
.send({
operation: this.inflightOp
})
.send({
revisionNr: this.revisionNr
})
.then(
(res) => {
if (res.ok) {
// console.log(res.body)
}
return true;
},
(err) => {
connectionMonitor.emit('disconnected');
throw new Error('Something went wrong..', err);
// console.log('Something went wrong..', err)
}
);
this.newOperationRequests.push(newOperationRequest);
}
if (!this.hasAcknowledge) {
setTimeout(() => {
this.sendToServer();
}, 3000);
}
return;
}
getHistory() {
return this.history;
}
// Fetch uid
subscribe() {
return request
.get(SUBSCRIBE_URL)
.then(
(res) => {
this.uid = res.body.uid;
this.revisionNr = res.body.revisionNr;
return true;
},
(err) => {
connectionMonitor.emit('disconnected');
throw new Error('Something went wrong..', err);
}
);
}
// Fetch current status
listen() {
if (this.uid === 0 || connectionMonitor.getConnectionStatus() === 'down') {
return setTimeout(() => this.listen(), 500);
}
if (connectionMonitor.getConnectionStatus() === 'up') {
// let requestNumber = this.longPolledRequests.length;
console.log('>>> listen request');
let nextRequest = request
.get(BROADCAST_URL + this.uid + '/' + this.revisionNr)
.use(superagentThrottle.plugin())
.then(
(res) => {
console.log('>>>>>>> listen request success');
// find a way to delete entry in array elegantly
// this.longPolledRequests[requestNumber].pop();
if (!res.body.empty) {
this.opReceived(res.body);
} else {
this.hasAcknowledge = true;
}
this.listen();
return res.body;
},
(err) => {
if (err !== null) {
connectionMonitor.emit('disconnected');
throw new Error('Something went wrong..', err);
}
}
);
this.longPolledRequests.push(nextRequest);
}
return null;
}
// if inflightop was affected through the transformation
// should it be sent again and cancel the last one?
opReceived(receivedOp) {
// console.log('Operation received: ', receivedOp, 'current revisionNr: ', this.revisionNr)
this.revisionNr = this.revisionNr + 1;
if (typeof receivedOp.acknowledge !== 'undefined') {
this.hasAcknowledge = true;
if (this.buffer.length > 0) {
this.inflightOp = this.buffer.shift();
this.sendToServer();
} else {
this.inflightOp = null;
this.inFlight = false;
}
} else {
if (this.inflightOp) {
let transformFirst = xformT(receivedOp, this.inflightOp);
receivedOp = transformFirst[0];
this.inflightOp = transformFirst[1];
receivedOp = this.transform(this.buffer, receivedOp)[0];
}
this.store.dispatch({ type: 'NEW_OPERATION', operation: receivedOp });
this.apply(this.store.dispatch)(translateOperationToAction(receivedOp, this.store));
}
}
setStore(store) {
this.store = store;
// this.store.subscribe(throttle(() => {
// this.saveCurrentStateIntoIndexedDB()
// }, 1000))
}
initialLoad() {
// if connection === up get the initialLoad from server
if (connectionMonitor.getConnectionStatus() === 'up') {
return request
.get(INITIAL_STATE_URL)
.then(
(res) => {
// console.log(res.body)
return {
entities: res.body
};
},
(err) => {
connectionMonitor.emit('disconnected');
throw new Error('Something went wrong fetching initial state..' + err);
}
);
}
return fromDatabase.loadState().then(
state => {
return {
entities: state
};
},
error => {
throw new Error('Initial load from database failed: ', error);
}
)
.catch((err) => {
throw new Error('Something went wrong..' + err);
});
}
saveCurrentStateIntoIndexedDB() {
fromDatabase.saveState(this.store.getState().entities);
}
saveOperationIntoQueue(operation) {
fromDatabase.saveOperationIntoQueue(operation);
}
fetchState() {
if (connectionMonitor.getConnectionStatus() === 'up') {
return this.delay(500).then(() => {
return fromFakeBackend.fakeBackend;
})
.catch((err) => {
throw new Error('Error getting state from server: ' + err);
});
}
return this.delay(0).then(() => {
return fromDatabase.loadState().then(state => state);
})
.catch((err) => {
throw new Error('Error getting state from indexed Db: ' + err);
});
}
}
export default Syncer;