@tldraw/store 5.3.0 → 5.4.0-canary.074b19f2c11f
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/dist-cjs/index.d.ts +39 -8
- package/dist-cjs/index.js +1 -1
- package/dist-cjs/lib/AtomMap.js +41 -0
- package/dist-cjs/lib/AtomMap.js.map +3 -3
- package/dist-cjs/lib/RecordType.js +1 -1
- package/dist-cjs/lib/RecordType.js.map +2 -2
- package/dist-cjs/lib/RecordsDiff.js +45 -9
- package/dist-cjs/lib/RecordsDiff.js.map +3 -3
- package/dist-cjs/lib/Store.js +18 -29
- package/dist-cjs/lib/Store.js.map +2 -2
- package/dist-cjs/lib/StoreQueries.js +7 -54
- package/dist-cjs/lib/StoreQueries.js.map +2 -2
- package/dist-cjs/lib/StoreSchema.js +8 -12
- package/dist-cjs/lib/StoreSchema.js.map +3 -3
- package/dist-cjs/lib/executeQuery.js +5 -2
- package/dist-cjs/lib/executeQuery.js.map +2 -2
- package/dist-cjs/lib/migrate.js +5 -7
- package/dist-cjs/lib/migrate.js.map +2 -2
- package/dist-esm/index.d.mts +39 -8
- package/dist-esm/index.mjs +1 -1
- package/dist-esm/lib/AtomMap.mjs +41 -0
- package/dist-esm/lib/AtomMap.mjs.map +3 -3
- package/dist-esm/lib/RecordType.mjs +1 -1
- package/dist-esm/lib/RecordType.mjs.map +2 -2
- package/dist-esm/lib/RecordsDiff.mjs +45 -9
- package/dist-esm/lib/RecordsDiff.mjs.map +3 -3
- package/dist-esm/lib/Store.mjs +19 -30
- package/dist-esm/lib/Store.mjs.map +2 -2
- package/dist-esm/lib/StoreQueries.mjs +7 -54
- package/dist-esm/lib/StoreQueries.mjs.map +2 -2
- package/dist-esm/lib/StoreSchema.mjs +8 -12
- package/dist-esm/lib/StoreSchema.mjs.map +3 -3
- package/dist-esm/lib/executeQuery.mjs +5 -2
- package/dist-esm/lib/executeQuery.mjs.map +2 -2
- package/dist-esm/lib/migrate.mjs +5 -7
- package/dist-esm/lib/migrate.mjs.map +2 -2
- package/package.json +3 -3
- package/src/lib/AtomMap.test.ts +38 -0
- package/src/lib/AtomMap.ts +44 -0
- package/src/lib/RecordType.ts +1 -1
- package/src/lib/RecordsDiff.ts +66 -10
- package/src/lib/Store.ts +21 -36
- package/src/lib/StoreQueries.test.ts +24 -0
- package/src/lib/StoreQueries.ts +7 -60
- package/src/lib/StoreSchema.ts +11 -17
- package/src/lib/executeQuery.ts +5 -2
- package/src/lib/migrate.ts +8 -12
package/src/lib/RecordsDiff.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { objectMapValues } from '@tldraw/utils'
|
|
1
2
|
import { IdOf, UnknownRecord } from './BaseRecord'
|
|
2
3
|
|
|
3
4
|
/**
|
|
@@ -78,7 +79,7 @@ export function createEmptyRecordsDiff<R extends UnknownRecord>(): RecordsDiff<R
|
|
|
78
79
|
*/
|
|
79
80
|
export function reverseRecordsDiff(diff: RecordsDiff<any>) {
|
|
80
81
|
const result: RecordsDiff<any> = { added: diff.removed, removed: diff.added, updated: {} }
|
|
81
|
-
for (const [from, to] of
|
|
82
|
+
for (const [from, to] of objectMapValues(diff.updated)) {
|
|
82
83
|
result.updated[from.id] = [to, from]
|
|
83
84
|
}
|
|
84
85
|
return result
|
|
@@ -159,11 +160,14 @@ export function squashRecordDiffs<T extends UnknownRecord>(
|
|
|
159
160
|
mutateFirstDiff?: boolean
|
|
160
161
|
}
|
|
161
162
|
): RecordsDiff<T> {
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
163
|
+
if (options?.mutateFirstDiff) {
|
|
164
|
+
const result = diffs[0]
|
|
165
|
+
squashRecordDiffsMutable(result, diffs, 1)
|
|
166
|
+
return result
|
|
167
|
+
}
|
|
165
168
|
|
|
166
|
-
|
|
169
|
+
const result = { added: {}, removed: {}, updated: {} } as RecordsDiff<T>
|
|
170
|
+
squashRecordDiffsMutable(result, diffs)
|
|
167
171
|
return result
|
|
168
172
|
}
|
|
169
173
|
|
|
@@ -205,8 +209,38 @@ export function squashRecordDiffs<T extends UnknownRecord>(
|
|
|
205
209
|
*/
|
|
206
210
|
export function squashRecordDiffsMutable<T extends UnknownRecord>(
|
|
207
211
|
target: RecordsDiff<T>,
|
|
208
|
-
diffs: RecordsDiff<T>[]
|
|
212
|
+
diffs: RecordsDiff<T>[],
|
|
213
|
+
fromIndex = 0
|
|
209
214
|
): void {
|
|
215
|
+
squashRecordDiffsMutableImpl(target, diffs, fromIndex)
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* Applies only records of a given type from an array of diffs to a target diff. Returns the net
|
|
220
|
+
* change in the number of entries in the target.
|
|
221
|
+
*
|
|
222
|
+
* @internal
|
|
223
|
+
*/
|
|
224
|
+
export function squashRecordDiffsMutableByType<
|
|
225
|
+
T extends UnknownRecord,
|
|
226
|
+
TypeName extends T['typeName'],
|
|
227
|
+
>(
|
|
228
|
+
target: RecordsDiff<Extract<T, { typeName: TypeName }>>,
|
|
229
|
+
diffs: RecordsDiff<T>[],
|
|
230
|
+
typeName: TypeName
|
|
231
|
+
): number {
|
|
232
|
+
return squashRecordDiffsMutableImpl(target, diffs, 0, typeName)
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
function squashRecordDiffsMutableImpl<T extends UnknownRecord>(
|
|
236
|
+
target: RecordsDiff<T>,
|
|
237
|
+
diffs: RecordsDiff<T>[],
|
|
238
|
+
fromIndex: number,
|
|
239
|
+
typeName?: T['typeName']
|
|
240
|
+
): number {
|
|
241
|
+
let sizeChange = 0
|
|
242
|
+
const trackSize = typeName !== undefined
|
|
243
|
+
|
|
210
244
|
// This runs on every history interceptor call — e.g. once per input tick while
|
|
211
245
|
// resizing N shapes, with N entries in diff.updated — so the updated loop must not
|
|
212
246
|
// allocate per entry. We use for-in instead of Object.entries, mutate the target's
|
|
@@ -214,7 +248,8 @@ export function squashRecordDiffsMutable<T extends UnknownRecord>(
|
|
|
214
248
|
// know are empty. In-place tuple mutation is safe because the target exclusively
|
|
215
249
|
// owns its updated tuples: they are always created here (never shared with a source
|
|
216
250
|
// diff), and sources are never mutated.
|
|
217
|
-
for (
|
|
251
|
+
for (let i = fromIndex; i < diffs.length; i++) {
|
|
252
|
+
const diff = diffs[i]
|
|
218
253
|
// target.removed can only lose entries before the removed loop below, so a
|
|
219
254
|
// stale `true` is harmless (extra no-op deletes).
|
|
220
255
|
const targetHasRemoved = hasAnyKey(target.removed)
|
|
@@ -222,13 +257,17 @@ export function squashRecordDiffsMutable<T extends UnknownRecord>(
|
|
|
222
257
|
for (const _id in diff.added) {
|
|
223
258
|
const id = _id as IdOf<T>
|
|
224
259
|
const value = diff.added[id]
|
|
260
|
+
if (typeName !== undefined && value.typeName !== typeName) continue
|
|
225
261
|
if (targetHasRemoved && target.removed[id]) {
|
|
226
262
|
const original = target.removed[id]
|
|
227
263
|
delete target.removed[id]
|
|
264
|
+
if (trackSize) sizeChange--
|
|
228
265
|
if (original !== value) {
|
|
229
266
|
target.updated[id] = [original, value]
|
|
267
|
+
if (trackSize) sizeChange++
|
|
230
268
|
}
|
|
231
269
|
} else {
|
|
270
|
+
if (trackSize && !target.added[id]) sizeChange++
|
|
232
271
|
target.added[id] = value
|
|
233
272
|
}
|
|
234
273
|
}
|
|
@@ -239,35 +278,52 @@ export function squashRecordDiffsMutable<T extends UnknownRecord>(
|
|
|
239
278
|
for (const _id in diff.updated) {
|
|
240
279
|
const id = _id as IdOf<T>
|
|
241
280
|
const to = diff.updated[id][1]
|
|
281
|
+
if (typeName !== undefined && to.typeName !== typeName) continue
|
|
242
282
|
if (targetHasAdded && target.added[id]) {
|
|
243
283
|
target.added[id] = to
|
|
284
|
+
if (trackSize && target.updated[id]) sizeChange--
|
|
244
285
|
delete target.updated[id]
|
|
245
|
-
if (targetHasRemoved)
|
|
286
|
+
if (targetHasRemoved) {
|
|
287
|
+
if (trackSize && target.removed[id]) sizeChange--
|
|
288
|
+
delete target.removed[id]
|
|
289
|
+
}
|
|
246
290
|
continue
|
|
247
291
|
}
|
|
248
292
|
const existing = target.updated[id]
|
|
249
293
|
if (existing) {
|
|
250
294
|
existing[1] = to
|
|
251
|
-
if (targetHasRemoved)
|
|
295
|
+
if (targetHasRemoved) {
|
|
296
|
+
if (trackSize && target.removed[id]) sizeChange--
|
|
297
|
+
delete target.removed[id]
|
|
298
|
+
}
|
|
252
299
|
continue
|
|
253
300
|
}
|
|
254
301
|
|
|
255
302
|
// copy the tuple so the target owns it and can mutate it in place later
|
|
256
303
|
target.updated[id] = [diff.updated[id][0], to]
|
|
257
|
-
if (
|
|
304
|
+
if (trackSize) sizeChange++
|
|
305
|
+
if (targetHasRemoved) {
|
|
306
|
+
if (trackSize && target.removed[id]) sizeChange--
|
|
307
|
+
delete target.removed[id]
|
|
308
|
+
}
|
|
258
309
|
}
|
|
259
310
|
|
|
260
311
|
for (const _id in diff.removed) {
|
|
261
312
|
const id = _id as IdOf<T>
|
|
313
|
+
if (typeName !== undefined && diff.removed[id].typeName !== typeName) continue
|
|
262
314
|
// the same record was added in this diff sequence, just drop it
|
|
263
315
|
if (target.added[id]) {
|
|
264
316
|
delete target.added[id]
|
|
317
|
+
if (trackSize) sizeChange--
|
|
265
318
|
} else if (target.updated[id]) {
|
|
266
319
|
target.removed[id] = target.updated[id][0]
|
|
267
320
|
delete target.updated[id]
|
|
268
321
|
} else {
|
|
322
|
+
if (trackSize && !target.removed[id]) sizeChange++
|
|
269
323
|
target.removed[id] = diff.removed[id]
|
|
270
324
|
}
|
|
271
325
|
}
|
|
272
326
|
}
|
|
327
|
+
|
|
328
|
+
return sizeChange
|
|
273
329
|
}
|
package/src/lib/Store.ts
CHANGED
|
@@ -14,7 +14,7 @@ import {
|
|
|
14
14
|
import { AtomMap } from './AtomMap'
|
|
15
15
|
import { IdOf, RecordId, UnknownRecord } from './BaseRecord'
|
|
16
16
|
import { devFreeze } from './devFreeze'
|
|
17
|
-
import {
|
|
17
|
+
import { isRecordsDiffEmpty, RecordsDiff, squashRecordDiffs } from './RecordsDiff'
|
|
18
18
|
import { RecordScope } from './RecordType'
|
|
19
19
|
import { StoreQueries } from './StoreQueries'
|
|
20
20
|
import { SerializedSchema, StoreSchema } from './StoreSchema'
|
|
@@ -504,23 +504,15 @@ export class Store<R extends UnknownRecord = UnknownRecord, Props = unknown> {
|
|
|
504
504
|
},
|
|
505
505
|
{ scheduleEffect: (cb) => (this.cancelHistoryReactor = throttleToNextFrame(cb)) }
|
|
506
506
|
)
|
|
507
|
-
|
|
508
|
-
document: new Set(
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
.map((t) => t.typeName)
|
|
512
|
-
),
|
|
513
|
-
session: new Set(
|
|
514
|
-
objectMapValues(this.schema.types)
|
|
515
|
-
.filter((t) => t.scope === 'session')
|
|
516
|
-
.map((t) => t.typeName)
|
|
517
|
-
),
|
|
518
|
-
presence: new Set(
|
|
519
|
-
objectMapValues(this.schema.types)
|
|
520
|
-
.filter((t) => t.scope === 'presence')
|
|
521
|
-
.map((t) => t.typeName)
|
|
522
|
-
),
|
|
507
|
+
const scopedTypes: { [K in RecordScope]: Set<R['typeName']> } = {
|
|
508
|
+
document: new Set(),
|
|
509
|
+
session: new Set(),
|
|
510
|
+
presence: new Set(),
|
|
523
511
|
}
|
|
512
|
+
for (const type of objectMapValues(this.schema.types)) {
|
|
513
|
+
scopedTypes[type.scope].add(type.typeName)
|
|
514
|
+
}
|
|
515
|
+
this.scopedTypes = scopedTypes
|
|
524
516
|
}
|
|
525
517
|
|
|
526
518
|
public _flushHistory() {
|
|
@@ -528,30 +520,23 @@ export class Store<R extends UnknownRecord = UnknownRecord, Props = unknown> {
|
|
|
528
520
|
if (this.historyAccumulator.hasChanges()) {
|
|
529
521
|
const entries = this.historyAccumulator.flush()
|
|
530
522
|
for (const { changes, source } of entries) {
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
523
|
+
// Filtered diffs are computed at most once per scope per entry, and shared by every
|
|
524
|
+
// listener watching that scope.
|
|
525
|
+
const scopedChanges = new Map<RecordScope, RecordsDiff<R> | null>()
|
|
534
526
|
for (const { onHistory, filters } of this.listeners) {
|
|
535
527
|
if (filters.source !== 'all' && filters.source !== source) {
|
|
536
528
|
continue
|
|
537
529
|
}
|
|
538
|
-
if (filters.scope
|
|
539
|
-
if (filters.scope === 'document') {
|
|
540
|
-
documentChanges ??= this.filterChangesByScope(changes, 'document')
|
|
541
|
-
if (!documentChanges) continue
|
|
542
|
-
onHistory({ changes: documentChanges, source })
|
|
543
|
-
} else if (filters.scope === 'session') {
|
|
544
|
-
instanceChanges ??= this.filterChangesByScope(changes, 'session')
|
|
545
|
-
if (!instanceChanges) continue
|
|
546
|
-
onHistory({ changes: instanceChanges, source })
|
|
547
|
-
} else {
|
|
548
|
-
presenceChanges ??= this.filterChangesByScope(changes, 'presence')
|
|
549
|
-
if (!presenceChanges) continue
|
|
550
|
-
onHistory({ changes: presenceChanges, source })
|
|
551
|
-
}
|
|
552
|
-
} else {
|
|
530
|
+
if (filters.scope === 'all') {
|
|
553
531
|
onHistory({ changes, source })
|
|
532
|
+
continue
|
|
533
|
+
}
|
|
534
|
+
if (!scopedChanges.has(filters.scope)) {
|
|
535
|
+
scopedChanges.set(filters.scope, this.filterChangesByScope(changes, filters.scope))
|
|
554
536
|
}
|
|
537
|
+
const filtered = scopedChanges.get(filters.scope)
|
|
538
|
+
if (!filtered) continue
|
|
539
|
+
onHistory({ changes: filtered, source })
|
|
555
540
|
}
|
|
556
541
|
}
|
|
557
542
|
}
|
|
@@ -573,7 +558,7 @@ export class Store<R extends UnknownRecord = UnknownRecord, Props = unknown> {
|
|
|
573
558
|
updated: filterEntries(change.updated, (_, r) => this.scopedTypes[scope].has(r[1].typeName)),
|
|
574
559
|
removed: filterEntries(change.removed, (_, r) => this.scopedTypes[scope].has(r.typeName)),
|
|
575
560
|
}
|
|
576
|
-
if (
|
|
561
|
+
if (isRecordsDiffEmpty(result)) {
|
|
577
562
|
return null
|
|
578
563
|
}
|
|
579
564
|
return result
|
|
@@ -211,6 +211,30 @@ describe('filtered history (QH)', () => {
|
|
|
211
211
|
},
|
|
212
212
|
])
|
|
213
213
|
})
|
|
214
|
+
|
|
215
|
+
it('[QH2] squashes matching records while ignoring interleaved record types', () => {
|
|
216
|
+
const authorHistory = store.query.filterHistory('author')
|
|
217
|
+
authorHistory.get()
|
|
218
|
+
|
|
219
|
+
const lastChangedEpoch = authorHistory.lastChangedEpoch
|
|
220
|
+
const newAuthor = Author.create({ name: 'Stanley Briggs' })
|
|
221
|
+
const updatedNewAuthor = { ...newAuthor, age: 38 }
|
|
222
|
+
|
|
223
|
+
store.put([newAuthor])
|
|
224
|
+
store.put([updatedNewAuthor, { ...books.lotr, title: 'The Fellowship of the Ring' }])
|
|
225
|
+
store.put([{ ...authors.davidMitchellFunny, age: 38 }])
|
|
226
|
+
store.remove([authors.davidMitchellFunny.id])
|
|
227
|
+
store.remove([authors.tolkein.id])
|
|
228
|
+
store.put([authors.tolkein])
|
|
229
|
+
|
|
230
|
+
expect(authorHistory.getDiffSince(lastChangedEpoch)).toEqual([
|
|
231
|
+
{
|
|
232
|
+
added: { [newAuthor.id]: updatedNewAuthor },
|
|
233
|
+
updated: {},
|
|
234
|
+
removed: { [authors.davidMitchellFunny.id]: authors.davidMitchellFunny },
|
|
235
|
+
},
|
|
236
|
+
])
|
|
237
|
+
})
|
|
214
238
|
})
|
|
215
239
|
|
|
216
240
|
describe('indexes (QI)', () => {
|
package/src/lib/StoreQueries.ts
CHANGED
|
@@ -12,7 +12,7 @@ import { AtomMap } from './AtomMap'
|
|
|
12
12
|
import { IdOf, UnknownRecord } from './BaseRecord'
|
|
13
13
|
import { executeQuery, objectMatchesQuery, QueryExpression } from './executeQuery'
|
|
14
14
|
import { IncrementalSetConstructor } from './IncrementalSetConstructor'
|
|
15
|
-
import { RecordsDiff } from './RecordsDiff'
|
|
15
|
+
import { hasAnyKey, RecordsDiff, squashRecordDiffsMutableByType } from './RecordsDiff'
|
|
16
16
|
import { diffSets } from './setUtils'
|
|
17
17
|
import { CollectionDiff } from './Store'
|
|
18
18
|
|
|
@@ -197,62 +197,9 @@ export class StoreQueries<R extends UnknownRecord> {
|
|
|
197
197
|
if (diff === RESET_VALUE) return this.history.get()
|
|
198
198
|
|
|
199
199
|
const res = { added: {}, removed: {}, updated: {} } as RecordsDiff<S>
|
|
200
|
-
|
|
201
|
-
let numRemoved = 0
|
|
202
|
-
let numUpdated = 0
|
|
200
|
+
const size = squashRecordDiffsMutableByType(res, diff, typeName)
|
|
203
201
|
|
|
204
|
-
|
|
205
|
-
for (const added of objectMapValues(changes.added)) {
|
|
206
|
-
if (added.typeName === typeName) {
|
|
207
|
-
if (res.removed[added.id as IdOf<S>]) {
|
|
208
|
-
const original = res.removed[added.id as IdOf<S>]
|
|
209
|
-
delete res.removed[added.id as IdOf<S>]
|
|
210
|
-
numRemoved--
|
|
211
|
-
if (original !== added) {
|
|
212
|
-
res.updated[added.id as IdOf<S>] = [original, added as S]
|
|
213
|
-
numUpdated++
|
|
214
|
-
}
|
|
215
|
-
} else {
|
|
216
|
-
res.added[added.id as IdOf<S>] = added as S
|
|
217
|
-
numAdded++
|
|
218
|
-
}
|
|
219
|
-
}
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
for (const [from, to] of objectMapValues(changes.updated)) {
|
|
223
|
-
if (to.typeName === typeName) {
|
|
224
|
-
if (res.added[to.id as IdOf<S>]) {
|
|
225
|
-
res.added[to.id as IdOf<S>] = to as S
|
|
226
|
-
} else if (res.updated[to.id as IdOf<S>]) {
|
|
227
|
-
res.updated[to.id as IdOf<S>] = [res.updated[to.id as IdOf<S>][0], to as S]
|
|
228
|
-
} else {
|
|
229
|
-
res.updated[to.id as IdOf<S>] = [from as S, to as S]
|
|
230
|
-
numUpdated++
|
|
231
|
-
}
|
|
232
|
-
}
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
for (const removed of objectMapValues(changes.removed)) {
|
|
236
|
-
if (removed.typeName === typeName) {
|
|
237
|
-
if (res.added[removed.id as IdOf<S>]) {
|
|
238
|
-
// was added during this diff sequence, so just undo the add
|
|
239
|
-
delete res.added[removed.id as IdOf<S>]
|
|
240
|
-
numAdded--
|
|
241
|
-
} else if (res.updated[removed.id as IdOf<S>]) {
|
|
242
|
-
// remove oldest version
|
|
243
|
-
res.removed[removed.id as IdOf<S>] = res.updated[removed.id as IdOf<S>][0]
|
|
244
|
-
delete res.updated[removed.id as IdOf<S>]
|
|
245
|
-
numUpdated--
|
|
246
|
-
numRemoved++
|
|
247
|
-
} else {
|
|
248
|
-
res.removed[removed.id as IdOf<S>] = removed as S
|
|
249
|
-
numRemoved++
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
if (numAdded || numRemoved || numUpdated) {
|
|
202
|
+
if (size) {
|
|
256
203
|
return withDiff(this.history.get(), res)
|
|
257
204
|
} else {
|
|
258
205
|
return lastValue
|
|
@@ -470,7 +417,7 @@ export class StoreQueries<R extends UnknownRecord> {
|
|
|
470
417
|
record<TypeName extends R['typeName']>(
|
|
471
418
|
typeName: TypeName,
|
|
472
419
|
queryCreator: () => QueryExpression<Extract<R, { typeName: TypeName }>> = () => ({}),
|
|
473
|
-
name = 'record:' + typeName +
|
|
420
|
+
name = 'record:' + typeName + ':' + queryCreator.toString()
|
|
474
421
|
): Computed<Extract<R, { typeName: TypeName }> | undefined> {
|
|
475
422
|
type S = Extract<R, { typeName: TypeName }>
|
|
476
423
|
const ids = this.ids(typeName, queryCreator, name)
|
|
@@ -510,7 +457,7 @@ export class StoreQueries<R extends UnknownRecord> {
|
|
|
510
457
|
records<TypeName extends R['typeName']>(
|
|
511
458
|
typeName: TypeName,
|
|
512
459
|
queryCreator: () => QueryExpression<Extract<R, { typeName: TypeName }>> = () => ({}),
|
|
513
|
-
name = 'records:' + typeName +
|
|
460
|
+
name = 'records:' + typeName + ':' + queryCreator.toString()
|
|
514
461
|
): Computed<Array<Extract<R, { typeName: TypeName }>>> {
|
|
515
462
|
type S = Extract<R, { typeName: TypeName }>
|
|
516
463
|
const ids = this.ids(typeName, queryCreator, 'ids:' + name)
|
|
@@ -554,7 +501,7 @@ export class StoreQueries<R extends UnknownRecord> {
|
|
|
554
501
|
ids<TypeName extends R['typeName']>(
|
|
555
502
|
typeName: TypeName,
|
|
556
503
|
queryCreator: () => QueryExpression<Extract<R, { typeName: TypeName }>> = () => ({}),
|
|
557
|
-
name = 'ids:' + typeName +
|
|
504
|
+
name = 'ids:' + typeName + ':' + queryCreator.toString()
|
|
558
505
|
): Computed<
|
|
559
506
|
Set<IdOf<Extract<R, { typeName: TypeName }>>>,
|
|
560
507
|
CollectionDiff<IdOf<Extract<R, { typeName: TypeName }>>>
|
|
@@ -567,7 +514,7 @@ export class StoreQueries<R extends UnknownRecord> {
|
|
|
567
514
|
// deref type history early to allow first incremental update to use diffs
|
|
568
515
|
typeHistory.get()
|
|
569
516
|
const query: QueryExpression<S> = queryCreator()
|
|
570
|
-
if (
|
|
517
|
+
if (!hasAnyKey(query)) {
|
|
571
518
|
return this.getAllIdsForType(typeName)
|
|
572
519
|
}
|
|
573
520
|
|
package/src/lib/StoreSchema.ts
CHANGED
|
@@ -423,16 +423,21 @@ export class StoreSchema<R extends UnknownRecord, P = unknown> {
|
|
|
423
423
|
* @public
|
|
424
424
|
*/
|
|
425
425
|
public getMigrationsSince(persistedSchema: SerializedSchema): Result<Migration[], string> {
|
|
426
|
-
//
|
|
426
|
+
// Every result — success, empty, or error — is cached, so cache once around the whole
|
|
427
|
+
// computation rather than at each of its exit points.
|
|
427
428
|
const cached = this.migrationCache.get(persistedSchema)
|
|
428
429
|
if (cached) {
|
|
429
430
|
return cached
|
|
430
431
|
}
|
|
431
432
|
|
|
433
|
+
const result = this.computeMigrationsSince(persistedSchema)
|
|
434
|
+
this.migrationCache.set(persistedSchema, result)
|
|
435
|
+
return result
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
private computeMigrationsSince(persistedSchema: SerializedSchema): Result<Migration[], string> {
|
|
432
439
|
const upgradeResult = upgradeSchema(persistedSchema)
|
|
433
440
|
if (!upgradeResult.ok) {
|
|
434
|
-
// Cache the error result
|
|
435
|
-
this.migrationCache.set(persistedSchema, upgradeResult)
|
|
436
441
|
return upgradeResult
|
|
437
442
|
}
|
|
438
443
|
const schema = upgradeResult.value
|
|
@@ -449,10 +454,7 @@ export class StoreSchema<R extends UnknownRecord, P = unknown> {
|
|
|
449
454
|
}
|
|
450
455
|
|
|
451
456
|
if (sequenceIdsToInclude.size === 0) {
|
|
452
|
-
|
|
453
|
-
// Cache the empty result
|
|
454
|
-
this.migrationCache.set(persistedSchema, result)
|
|
455
|
-
return result
|
|
457
|
+
return Result.ok([])
|
|
456
458
|
}
|
|
457
459
|
|
|
458
460
|
const allMigrationsToInclude = new Set<MigrationId>()
|
|
@@ -471,10 +473,7 @@ export class StoreSchema<R extends UnknownRecord, P = unknown> {
|
|
|
471
473
|
const idx = this.migrations[sequenceId].sequence.findIndex((m) => m.id === theirVersionId)
|
|
472
474
|
// todo: better error handling
|
|
473
475
|
if (idx === -1) {
|
|
474
|
-
|
|
475
|
-
// Cache the error result
|
|
476
|
-
this.migrationCache.set(persistedSchema, result)
|
|
477
|
-
return result
|
|
476
|
+
return Result.err('Incompatible schema?')
|
|
478
477
|
}
|
|
479
478
|
for (const migration of this.migrations[sequenceId].sequence.slice(idx + 1)) {
|
|
480
479
|
allMigrationsToInclude.add(migration.id)
|
|
@@ -482,12 +481,7 @@ export class StoreSchema<R extends UnknownRecord, P = unknown> {
|
|
|
482
481
|
}
|
|
483
482
|
|
|
484
483
|
// collect any migrations
|
|
485
|
-
|
|
486
|
-
this.sortedMigrations.filter(({ id }) => allMigrationsToInclude.has(id))
|
|
487
|
-
)
|
|
488
|
-
// Cache the result
|
|
489
|
-
this.migrationCache.set(persistedSchema, result)
|
|
490
|
-
return result
|
|
484
|
+
return Result.ok(this.sortedMigrations.filter(({ id }) => allMigrationsToInclude.has(id)))
|
|
491
485
|
}
|
|
492
486
|
|
|
493
487
|
/**
|
package/src/lib/executeQuery.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { objectMapFromEntries, objectMapValues } from '@tldraw/utils'
|
|
1
2
|
import { IdOf, UnknownRecord } from './BaseRecord'
|
|
2
3
|
import { intersectSets } from './setUtils'
|
|
3
4
|
import { StoreQueries } from './StoreQueries'
|
|
@@ -147,7 +148,9 @@ export function executeQuery<R extends UnknownRecord, TypeName extends R['typeNa
|
|
|
147
148
|
const matcherPaths = extractMatcherPaths(query)
|
|
148
149
|
|
|
149
150
|
// Build a set of matching IDs for each path
|
|
150
|
-
const matchIds =
|
|
151
|
+
const matchIds = objectMapFromEntries(
|
|
152
|
+
matcherPaths.map(({ path }) => [path, new Set<IdOf<S>>()] as const)
|
|
153
|
+
)
|
|
151
154
|
|
|
152
155
|
// For each path, use the index to find matching IDs
|
|
153
156
|
for (const { path, matcher } of matcherPaths) {
|
|
@@ -185,5 +188,5 @@ export function executeQuery<R extends UnknownRecord, TypeName extends R['typeNa
|
|
|
185
188
|
}
|
|
186
189
|
|
|
187
190
|
// Intersect all the match sets
|
|
188
|
-
return intersectSets(
|
|
191
|
+
return intersectSets(objectMapValues(matchIds)) as Set<IdOf<S>>
|
|
189
192
|
}
|
package/src/lib/migrate.ts
CHANGED
|
@@ -100,6 +100,8 @@ export function createMigrationIds<
|
|
|
100
100
|
const ID extends string,
|
|
101
101
|
const Versions extends Record<string, number>,
|
|
102
102
|
>(sequenceId: ID, versions: Versions): { [K in keyof Versions]: `${ID}/${Versions[K]}` } {
|
|
103
|
+
// Note: not objectMapFromEntries — `keyof Versions` is not narrowed to `string`, which that
|
|
104
|
+
// helper requires.
|
|
103
105
|
return Object.fromEntries(
|
|
104
106
|
objectMapEntries(versions).map(([key, version]) => [key, `${sequenceId}/${version}`] as const)
|
|
105
107
|
) as any
|
|
@@ -132,18 +134,12 @@ export function createRecordMigrationSequence(opts: {
|
|
|
132
134
|
return createMigrationSequence({
|
|
133
135
|
sequenceId,
|
|
134
136
|
retroactive: opts.retroactive ?? true,
|
|
135
|
-
sequence: opts.sequence.map((m) =>
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
r.typeName === opts.recordType &&
|
|
142
|
-
(m.filter?.(r) ?? true) &&
|
|
143
|
-
(opts.filter?.(r) ?? true),
|
|
144
|
-
}
|
|
145
|
-
: m
|
|
146
|
-
),
|
|
137
|
+
sequence: opts.sequence.map((m) => ({
|
|
138
|
+
...m,
|
|
139
|
+
scope: 'record',
|
|
140
|
+
filter: (r: UnknownRecord) =>
|
|
141
|
+
r.typeName === opts.recordType && (m.filter?.(r) ?? true) && (opts.filter?.(r) ?? true),
|
|
142
|
+
})),
|
|
147
143
|
})
|
|
148
144
|
}
|
|
149
145
|
|