@nxtedition/deepstream.io-client-js 23.4.45 → 23.4.47

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": "23.4.45",
3
+ "version": "23.4.47",
4
4
  "description": "the javascript client for deepstream.io",
5
5
  "homepage": "http://deepstream.io",
6
6
  "bugs": {
@@ -10,6 +10,7 @@ module.exports.CONNECTION_STATE.ERROR = 'ERROR'
10
10
  module.exports.CONNECTION_STATE.RECONNECTING = 'RECONNECTING'
11
11
 
12
12
  module.exports.RECORD_STATE = {}
13
+ module.exports.RECORD_STATE.INIT = -1
13
14
  module.exports.RECORD_STATE.VOID = 0
14
15
  module.exports.RECORD_STATE.CLIENT = 1
15
16
  module.exports.RECORD_STATE.PENDING = 2
@@ -363,6 +363,7 @@ Connection.prototype._setState = function (state) {
363
363
  this._client.emit(C.EVENT.CONNECTED, true)
364
364
  } else if (state === C.CONNECTION_STATE.RECONNECTING || state === C.CONNECTION_STATE.CLOSED) {
365
365
  this._client.emit(C.EVENT.CONNECTED, false)
366
+ this._reset()
366
367
  }
367
368
  }
368
369
 
@@ -58,7 +58,7 @@ class RecordHandler {
58
58
  invariant(rec.refs === 0, 'cannot prune referenced record')
59
59
  invariant(!this._pending.has(rec), 'cannot prune pending record')
60
60
 
61
- rec._unsubscribe()
61
+ rec._$dispose()
62
62
  this._records.delete(rec.name)
63
63
  this._stats.destroyed++
64
64
  }
@@ -77,24 +77,20 @@ class RecordHandler {
77
77
  }
78
78
 
79
79
  _onRef(rec) {
80
- if (rec.refs === 0 && !this._pending.has(rec)) {
80
+ if (rec.refs === 0) {
81
81
  this._pruning.add(rec)
82
82
  } else if (rec.refs === 1) {
83
83
  this._pruning.delete(rec)
84
84
  }
85
85
  }
86
86
 
87
- _onPending(rec) {
88
- if (rec.state > C.RECORD_STATE.PENDING) {
89
- this._pending.delete(rec)
90
- if (rec.refs === 0) {
91
- this._pruning.add(rec)
92
- }
93
- } else {
87
+ _onState(rec, prevState) {
88
+ // TODO (perf): avoid pending.has
89
+ if (rec.state < Record.STATE.SERVER && !this._pending.has(rec)) {
94
90
  this._pending.add(rec)
95
- if (rec.refs === 0) {
96
- this._pruning.delete(rec)
97
- }
91
+ rec.ref()
92
+ } else if (this._pending.delete(rec)) {
93
+ rec.unref()
98
94
  }
99
95
  }
100
96
 
@@ -179,11 +175,15 @@ class RecordHandler {
179
175
  }
180
176
  }
181
177
 
178
+ const xs = new Set()
179
+
182
180
  const onUpdate = (rec) => {
183
181
  if (rec.state < C.RECORD_STATE.SERVER) {
184
182
  return
185
183
  }
186
184
 
185
+ xs.delete(rec.name)
186
+
187
187
  rec.unsubscribe(onUpdate)
188
188
  rec.unref()
189
189
  counter -= 1
@@ -193,8 +193,9 @@ class RecordHandler {
193
193
 
194
194
  for (const rec of this._pending) {
195
195
  if (rec.state < C.RECORD_STATE.SERVER) {
196
- rec.ref()
196
+ xs.add(rec.name)
197
197
  rec.subscribe(onUpdate)
198
+ rec.ref()
198
199
  counter += 1
199
200
  }
200
201
  }
@@ -23,7 +23,7 @@ class Record {
23
23
  this._updating = null
24
24
  this._patches = null
25
25
 
26
- this._handler._onPending(this)
26
+ this._handler._onState(this, Record.STATE.INIT)
27
27
 
28
28
  this._subscribe()
29
29
  }
@@ -49,22 +49,26 @@ class Record {
49
49
  }
50
50
 
51
51
  ref() {
52
+ const prevRefs = this._refs
53
+
52
54
  this._refs += 1
53
- if (this._refs === 1) {
55
+ if (this._refs === 1 && !this._subscribed) {
54
56
  this._subscribe()
55
57
  }
56
58
 
57
- this._handler._onRef(this)
59
+ this._handler._onRef(this, prevRefs)
58
60
 
59
61
  return this
60
62
  }
61
63
 
62
64
  unref() {
63
- invariant(this._refs > 0, this._name + ' missing refs')
65
+ invariant(this._refs > 0, 'missing refs')
66
+
67
+ const prevRefs = this._refs
64
68
 
65
69
  this._refs -= 1
66
70
 
67
- this._handler._onRef(this)
71
+ this._handler._onRef(this, prevRefs)
68
72
 
69
73
  return this
70
74
  }
@@ -99,7 +103,11 @@ class Record {
99
103
  }
100
104
 
101
105
  set(pathOrData, dataOrNil) {
102
- invariant(this._refs > 0, this._name + ' missing refs')
106
+ const prevState = this._state
107
+ const prevData = this._data
108
+ const prevVersion = this._version
109
+
110
+ invariant(this._refs > 0, 'missing refs')
103
111
 
104
112
  if (this._version.charAt(0) === 'I' || this._name.startsWith('_')) {
105
113
  this._error(C.EVENT.USER_ERROR, 'cannot set')
@@ -123,20 +131,22 @@ class Record {
123
131
  throw new Error('invalid argument: path')
124
132
  }
125
133
 
134
+ this._update(jsonPath.set(this._data, path, data, false))
135
+
126
136
  if (this._state < Record.STATE.SERVER) {
127
137
  this._patches = path && this._patches ? this._patches : []
128
138
  this._patches.push(path, cloneDeep(data))
129
139
  this._state = Record.STATE.PENDING
130
- }
131
-
132
- if (this._update(jsonPath.set(this._data, path, data, false))) {
140
+ this._handler._onState(this, prevState)
141
+ this._emitUpdate()
142
+ } else if (this._data !== prevData || this._version !== prevVersion) {
133
143
  this._emitUpdate()
134
144
  }
135
145
  }
136
146
 
137
147
  // TODO (fix): timeout + signal
138
148
  when(stateOrNull) {
139
- invariant(this._refs > 0, this._name + ' missing refs')
149
+ invariant(this._refs > 0, 'missing refs')
140
150
 
141
151
  const state = stateOrNull == null ? Record.STATE.SERVER : stateOrNull
142
152
 
@@ -167,7 +177,7 @@ class Record {
167
177
  }
168
178
 
169
179
  update(pathOrUpdater, updaterOrNil) {
170
- invariant(this._refs > 0, this._name + ' missing refs')
180
+ invariant(this._refs > 0, 'missing refs')
171
181
 
172
182
  if (this._version.charAt(0) === 'I') {
173
183
  this._handler._client._$onError(C.TOPIC.RECORD, C.EVENT.UPDATE_ERROR, 'cannot update', [
@@ -205,17 +215,6 @@ class Record {
205
215
  })
206
216
  }
207
217
 
208
- _emitUpdate() {
209
- try {
210
- this._subscriptionsEmitting = true
211
- for (const fn of this._subscriptions) {
212
- fn(this)
213
- }
214
- } finally {
215
- this._subscriptionsEmitting = false
216
- }
217
- }
218
-
219
218
  _$onMessage(message) {
220
219
  if (message.action === C.ACTIONS.UPDATE) {
221
220
  this._onUpdate(message.data)
@@ -231,6 +230,8 @@ class Record {
231
230
  _$onConnectionStateChange() {
232
231
  const connection = this._handler._connection
233
232
  if (connection.connected) {
233
+ invariant(!this._subscribed, 'must not be subscribed')
234
+
234
235
  if (this._refs > 0) {
235
236
  this._subscribe()
236
237
  }
@@ -241,19 +242,20 @@ class Record {
241
242
  }
242
243
  }
243
244
  } else {
244
- this._subscribed = false
245
- if (this._state > Record.STATE.CLIENT) {
246
- this._state = Record.STATE.CLIENT
247
- this._handler._onPending(this)
248
- this._emitUpdate()
249
- }
245
+ this._unsubscribe()
250
246
  }
251
247
  }
252
248
 
253
- _subscribe() {
254
- invariant(this._refs, this._name + ' missing refs')
249
+ _$dispose() {
250
+ this._unsubscribe()
251
+ }
255
252
 
253
+ _subscribe() {
256
254
  const connection = this._handler._connection
255
+
256
+ invariant(this._refs, 'missing refs')
257
+ invariant(!this._subscribed, 'must not be subscribed')
258
+
257
259
  if (!this._subscribed && connection.connected) {
258
260
  connection.sendMsg1(C.TOPIC.RECORD, C.ACTIONS.SUBSCRIBE, this._name)
259
261
  this._subscribed = true
@@ -261,18 +263,21 @@ class Record {
261
263
  }
262
264
 
263
265
  _unsubscribe() {
264
- invariant(!this._refs, this._name + ' must not have refs')
265
- invariant(!this._patches, this._name + ' must not have patches')
266
-
266
+ const prevState = this._state
267
267
  const connection = this._handler._connection
268
+
269
+ invariant(!connection.connected || !this._refs, 'must not have refs')
270
+ invariant(!connection.connected || !this._patches, 'must not have patches')
271
+ invariant(!connection.connected || this._subscribed, 'must be subscribed')
272
+
268
273
  if (this._subscribed && connection.connected) {
269
274
  connection.sendMsg1(C.TOPIC.RECORD, C.ACTIONS.UNSUBSCRIBE, this._name)
275
+ this._subscribed = false
270
276
  }
271
277
 
272
- this._subscribed = false
273
278
  if (this._state > Record.STATE.CLIENT) {
274
279
  this._state = Record.STATE.CLIENT
275
- this._handler._onPending(this)
280
+ this._handler._onState(this, prevState)
276
281
  this._emitUpdate()
277
282
  }
278
283
  }
@@ -304,6 +309,7 @@ class Record {
304
309
  }
305
310
 
306
311
  _onUpdate([, version, data]) {
312
+ const prevState = this._state
307
313
  const prevData = this._data
308
314
  const prevVersion = this._version
309
315
 
@@ -334,7 +340,7 @@ class Record {
334
340
 
335
341
  if (this._state < Record.STATE.SERVER) {
336
342
  this._state = this._version.charAt(0) === 'I' ? Record.STATE.STALE : Record.STATE.SERVER
337
- this._handler._onPending(this)
343
+ this._handler._onState(this, prevState)
338
344
  this._emitUpdate()
339
345
  } else if (this._data !== prevData || this._version !== prevVersion) {
340
346
  this._emitUpdate()
@@ -342,19 +348,19 @@ class Record {
342
348
  }
343
349
 
344
350
  _onSubscriptionHasProvider([, hasProvider]) {
345
- if (this._state < Record.STATE.SERVER) {
346
- return
347
- }
348
-
349
- const provided = hasProvider && messageParser.convertTyped(hasProvider, this._handler._client)
350
- const state = provided
351
- ? Record.STATE.PROVIDER
352
- : this._version.charAt(0) === 'I'
353
- ? Record.STATE.STALE
354
- : Record.STATE.SERVER
355
-
356
- if (this._state !== state) {
357
- this._state = state
351
+ const prevState = this._state
352
+
353
+ this._state =
354
+ hasProvider &&
355
+ messageParser.convertTyped(hasProvider, this._handler._client) &&
356
+ this._state >= Record.STATE.SERVER
357
+ ? Record.STATE.PROVIDER
358
+ : this._version.charAt(0) === 'I'
359
+ ? Record.STATE.STALE
360
+ : Record.STATE.SERVER
361
+
362
+ if (this._state !== prevState) {
363
+ this._handler._onState(this, prevState)
358
364
  this._emitUpdate()
359
365
  }
360
366
  }
@@ -376,6 +382,17 @@ class Record {
376
382
  }
377
383
  return `${start}-${revid}`
378
384
  }
385
+
386
+ _emitUpdate() {
387
+ this._subscriptionsEmitting = true
388
+ try {
389
+ for (const fn of this._subscriptions) {
390
+ fn(this)
391
+ }
392
+ } finally {
393
+ this._subscriptionsEmitting = false
394
+ }
395
+ }
379
396
  }
380
397
 
381
398
  // Compat