@nxtedition/deepstream.io-client-js 24.0.5 → 24.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/deepstream.io-client-js",
3
- "version": "24.0.5",
3
+ "version": "24.0.7",
4
4
  "description": "the javascript client for deepstream.io",
5
5
  "homepage": "http://deepstream.io",
6
6
  "bugs": {
@@ -63,6 +63,8 @@ class RecordHandler {
63
63
  this._records = new Map()
64
64
  this._listeners = new Map()
65
65
  this._pruning = new Set()
66
+ this._patching = new Map()
67
+ this._updating = new Map()
66
68
 
67
69
  this._connected = 0
68
70
  this._stats = {
@@ -70,7 +72,6 @@ class RecordHandler {
70
72
  created: 0,
71
73
  destroyed: 0,
72
74
  records: 0,
73
- pending: 0,
74
75
  pruning: 0,
75
76
  patching: 0,
76
77
  }
@@ -122,27 +123,33 @@ class RecordHandler {
122
123
  }
123
124
  }
124
125
 
125
- _onPending(rec, value) {
126
- if (value) {
127
- this._stats.pending += 1
128
- } else {
129
- this._stats.pending -= 1
130
- }
131
- }
132
-
133
126
  _onUpdating(rec, value) {
134
127
  if (value) {
135
128
  this._stats.updating += 1
129
+ this._updating.set(rec, [])
136
130
  } else {
137
131
  this._stats.updating -= 1
132
+
133
+ const callbacks = this._updating.get(rec)
134
+ this._updating.delete(rec)
135
+ for (const callback of callbacks) {
136
+ callback()
137
+ }
138
138
  }
139
139
  }
140
140
 
141
141
  _onPatching(rec, value) {
142
142
  if (value) {
143
143
  this._stats.patching += 1
144
+ this._patching.set(rec, [])
144
145
  } else {
145
146
  this._stats.patching -= 1
147
+
148
+ const callbacks = this._patching.get(rec)
149
+ this._patching.delete(rec)
150
+ for (const callback of callbacks) {
151
+ callback()
152
+ }
146
153
  }
147
154
  }
148
155
 
@@ -211,13 +218,66 @@ class RecordHandler {
211
218
  }
212
219
  }
213
220
 
214
- sync() {
215
- // TODO (fix): Sync patching & updating?
216
- // TODO (fix): Ensure no pending?
217
- return new Promise((resolve) => {
218
- const token = xuid()
219
- this._syncEmitter.once(token, resolve)
220
- this._connection.sendMsg(C.TOPIC.RECORD, C.ACTIONS.SYNC, [token])
221
+ async sync() {
222
+ // TODO (fix): Sync pending? What about VOID state?
223
+
224
+ let patchingTimeout
225
+ await Promise.race([
226
+ Promise.all(
227
+ [...this._patching.values()].map(
228
+ (callbacks) => new Promise((resolve) => callbacks.push(resolve))
229
+ )
230
+ ),
231
+ new Promise((resolve) => {
232
+ patchingTimeout = timers.setTimeout(() => {
233
+ this._client._$onError(
234
+ C.TOPIC.RECORD,
235
+ C.EVENT.TIMEOUT,
236
+ new Error('sync patching timeout')
237
+ )
238
+ resolve(null)
239
+ }, 2 * 60e3)
240
+ }),
241
+ ]).finally(() => {
242
+ timers.clearTimeout(patchingTimeout)
243
+ })
244
+
245
+ let updatingTimeout
246
+ await Promise.race([
247
+ Promise.all(
248
+ [...this._updating.values()].map(
249
+ (callbacks) => new Promise((resolve) => callbacks.push(resolve))
250
+ )
251
+ ),
252
+ new Promise((resolve) => {
253
+ updatingTimeout = timers.setTimeout(() => {
254
+ this._client._$onError(
255
+ C.TOPIC.RECORD,
256
+ C.EVENT.TIMEOUT,
257
+ new Error('sync updating timeout')
258
+ )
259
+ resolve(null)
260
+ }, 2 * 60e3)
261
+ }),
262
+ ]).finally(() => {
263
+ timers.clearTimeout(updatingTimeout)
264
+ })
265
+
266
+ let serverTimeout
267
+ await Promise.race([
268
+ await new Promise((resolve) => {
269
+ const token = xuid()
270
+ this._syncEmitter.once(token, resolve)
271
+ this._connection.sendMsg(C.TOPIC.RECORD, C.ACTIONS.SYNC, [token])
272
+ }),
273
+ new Promise((resolve) => {
274
+ serverTimeout = timers.setTimeout(() => {
275
+ this._client._$onError(C.TOPIC.RECORD, C.EVENT.TIMEOUT, new Error('sync server timeout'))
276
+ resolve(null)
277
+ }, 2 * 60e3)
278
+ }),
279
+ ]).finally(() => {
280
+ timers.clearTimeout(serverTimeout)
221
281
  })
222
282
  }
223
283
 
@@ -21,10 +21,7 @@ class Record {
21
21
  this._emitting = false
22
22
  /** @type Map? */ this._updating = null
23
23
  /** @type Array? */ this._patching = null
24
- this._pending = false
25
24
  this._subscribed = this._sendMsg1(C.ACTIONS.SUBSCRIBE, this._name)
26
-
27
- this._onPending(true)
28
25
  }
29
26
 
30
27
  /** @type {string} */
@@ -270,10 +267,6 @@ class Record {
270
267
  this._subscribed = false
271
268
  }
272
269
 
273
- if (!this._pending) {
274
- this._onPending(true)
275
- }
276
-
277
270
  if (this._state > C.RECORD_STATE.CLIENT) {
278
271
  this._state = C.RECORD_STATE.CLIENT
279
272
  this._emitUpdate()
@@ -284,7 +277,6 @@ class Record {
284
277
  invariant(!this._refs, 'must not have refs')
285
278
  invariant(!this._patching, 'must not have patches')
286
279
  invariant(!this._updating, 'must not have updates')
287
- invariant(!this._pending, 'must not be pending')
288
280
 
289
281
  if (this._subscribed) {
290
282
  this._sendMsg1(C.ACTIONS.UNSUBSCRIBE, this._name)
@@ -364,10 +356,6 @@ class Record {
364
356
  this._state = this._version.charAt(0) === 'I' ? C.RECORD_STATE.STALE : C.RECORD_STATE.SERVER
365
357
  }
366
358
 
367
- if (this._pending) {
368
- this._onPending(false)
369
- }
370
-
371
359
  if (this._state !== prevState || this._data !== prevData || this._version !== prevVersion) {
372
360
  this._emitUpdate()
373
361
  }
@@ -401,16 +389,6 @@ class Record {
401
389
  this._handler._onUpdating(this, value)
402
390
  }
403
391
 
404
- _onPending(value) {
405
- if (value) {
406
- this._pending = true
407
- } else {
408
- this._pending = false
409
- }
410
-
411
- this._handler._onPending(this, value)
412
- }
413
-
414
392
  _onSubscriptionHasProvider([, hasProvider]) {
415
393
  if (this._state < C.RECORD_STATE.SERVER) {
416
394
  return