teamplay 0.4.0-alpha.36 → 0.4.0-alpha.37

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.
@@ -389,6 +389,11 @@ console.log(getSubscriptionGcDelay()) // 500
389
389
  When refCount drops to `0`, unsubscribe/destroy is scheduled after this delay.
390
390
  If a new subscribe arrives before timeout, pending destroy is cancelled and the same doc/query instance is reused.
391
391
 
392
+ Compat queries also retain lifecycle ownership of docs they materialize into DataTree.
393
+ This means a doc that arrived through `useQuery` / `useBatchQuery` will stay available
394
+ for immediate `useLocal` / `useModel` reads while that query remains subscribed, even if
395
+ some unrelated `useDoc` subscriber for the same `collection.id` unmounts.
396
+
392
397
  ### set(value) and set(path, value)
393
398
 
394
399
  `SignalCompat` accepts both:
package/orm/Doc.js CHANGED
@@ -149,6 +149,15 @@ export class DocSubscriptions {
149
149
  return doc._subscribing
150
150
  }
151
151
 
152
+ retain ($doc) {
153
+ const segments = [...$doc[SEGMENTS]]
154
+ const hash = hashDoc(segments)
155
+ this.cancelDestroy(hash)
156
+ const count = this.subCount.get(hash) || 0
157
+ this.subCount.set(hash, count + 1)
158
+ this.init($doc)
159
+ }
160
+
152
161
  async unsubscribe ($doc) {
153
162
  const segments = [...$doc[SEGMENTS]]
154
163
  const hash = hashDoc(segments)
@@ -167,6 +176,23 @@ export class DocSubscriptions {
167
176
  await this.scheduleDestroy(segments)
168
177
  }
169
178
 
179
+ async release ($doc) {
180
+ const segments = [...$doc[SEGMENTS]]
181
+ const hash = hashDoc(segments)
182
+ let count = this.subCount.get(hash) || 0
183
+ count -= 1
184
+ if (count < 0) {
185
+ if (ERROR_ON_EXCESSIVE_UNSUBSCRIBES) throw ERRORS.notSubscribed($doc)
186
+ return
187
+ }
188
+ if (count > 0) {
189
+ this.subCount.set(hash, count)
190
+ return
191
+ }
192
+ this.subCount.set(hash, 0)
193
+ await this.scheduleDestroy(segments)
194
+ }
195
+
170
196
  async destroy (segments) {
171
197
  const hash = hashDoc(segments)
172
198
  await this.destroyByHash(hash, { force: true })
package/orm/Query.js CHANGED
@@ -89,7 +89,7 @@ export class Query {
89
89
  const ids = this.shareQuery.results.map(doc => doc.id)
90
90
  for (const docId of ids) {
91
91
  const $doc = getSignal(undefined, [this.collectionName, docId])
92
- docSubscriptions.init($doc)
92
+ docSubscriptions.retain($doc)
93
93
  this.docSignals.add($doc)
94
94
  }
95
95
  _set([QUERIES, this.hash, 'ids'], ids)
@@ -112,7 +112,7 @@ export class Query {
112
112
  const ids = shareDocs.map(doc => doc.id)
113
113
  for (const docId of ids) {
114
114
  const $doc = getSignal(undefined, [this.collectionName, docId])
115
- docSubscriptions.init($doc)
115
+ docSubscriptions.retain($doc)
116
116
  this.docSignals.add($doc)
117
117
  }
118
118
  _get([QUERIES, this.hash, 'ids']).splice(index, 0, ...ids)
@@ -172,6 +172,7 @@ export class Query {
172
172
  const docIds = shareDocs.map(doc => doc.id)
173
173
  for (const docId of docIds) {
174
174
  const $doc = getSignal(undefined, [this.collectionName, docId])
175
+ docSubscriptions.release($doc).catch(ignoreDestroyError)
175
176
  this.docSignals.delete($doc)
176
177
  }
177
178
  const ids = _get([QUERIES, this.hash, 'ids'])
@@ -202,6 +203,9 @@ export class Query {
202
203
  }
203
204
 
204
205
  _removeData () {
206
+ for (const $doc of this.docSignals) {
207
+ docSubscriptions.release($doc).catch(ignoreDestroyError)
208
+ }
205
209
  this.docSignals.clear()
206
210
  _del([QUERIES, this.hash])
207
211
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "teamplay",
3
- "version": "0.4.0-alpha.36",
3
+ "version": "0.4.0-alpha.37",
4
4
  "description": "Full-stack signals ORM with multiplayer",
5
5
  "type": "module",
6
6
  "main": "index.js",
@@ -81,5 +81,5 @@
81
81
  ]
82
82
  },
83
83
  "license": "MIT",
84
- "gitHead": "0f90958b39502634ec8fefc99d7e688f0ac4c4bc"
84
+ "gitHead": "5ef9050360d503f068c90c54fc1a549f7d63ee8d"
85
85
  }