@verdant-web/store 2.8.5 → 3.0.0
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/bundle/index.js +9 -10
- package/dist/bundle/index.js.map +4 -4
- package/dist/cjs/DocumentManager.d.ts +1 -1
- package/dist/cjs/DocumentManager.js +1 -1
- package/dist/cjs/DocumentManager.js.map +1 -1
- package/dist/cjs/IDBService.d.ts +28 -7
- package/dist/cjs/IDBService.js +50 -13
- package/dist/cjs/IDBService.js.map +1 -1
- package/dist/cjs/UndoHistory.d.ts +1 -1
- package/dist/cjs/UndoHistory.js +6 -2
- package/dist/cjs/UndoHistory.js.map +1 -1
- package/dist/cjs/__tests__/batching.test.js +3 -1
- package/dist/cjs/__tests__/batching.test.js.map +1 -1
- package/dist/cjs/__tests__/documents.test.js +37 -6
- package/dist/cjs/__tests__/documents.test.js.map +1 -1
- package/dist/cjs/__tests__/fixtures/testStorage.d.ts +2 -2
- package/dist/cjs/__tests__/fixtures/testStorage.js +2 -1
- package/dist/cjs/__tests__/fixtures/testStorage.js.map +1 -1
- package/dist/cjs/__tests__/legacyOids.test.js +50 -17
- package/dist/cjs/__tests__/legacyOids.test.js.map +1 -1
- package/dist/cjs/__tests__/mutations.test.js +9 -3
- package/dist/cjs/__tests__/mutations.test.js.map +1 -1
- package/dist/cjs/__tests__/queries.test.js +6 -2
- package/dist/cjs/__tests__/queries.test.js.map +1 -1
- package/dist/cjs/__tests__/setup/indexedDB.d.ts +1 -1
- package/dist/cjs/__tests__/setup/indexedDB.js +8 -1
- package/dist/cjs/__tests__/setup/indexedDB.js.map +1 -1
- package/dist/cjs/__tests__/undo.test.js +16 -9
- package/dist/cjs/__tests__/undo.test.js.map +1 -1
- package/dist/cjs/client/Client.d.ts +1 -1
- package/dist/cjs/client/Client.js +7 -3
- package/dist/cjs/client/Client.js.map +1 -1
- package/dist/cjs/client/ClientDescriptor.js +21 -6
- package/dist/cjs/client/ClientDescriptor.js.map +1 -1
- package/dist/cjs/context.d.ts +10 -1
- package/dist/cjs/entities/Entity.d.ts +106 -178
- package/dist/cjs/entities/Entity.js +558 -376
- package/dist/cjs/entities/Entity.js.map +1 -1
- package/dist/cjs/entities/Entity.test.d.ts +1 -0
- package/dist/cjs/entities/Entity.test.js +194 -0
- package/dist/cjs/entities/Entity.test.js.map +1 -0
- package/dist/cjs/entities/EntityCache.d.ts +15 -0
- package/dist/cjs/entities/EntityCache.js +39 -0
- package/dist/cjs/entities/EntityCache.js.map +1 -0
- package/dist/cjs/entities/EntityMetadata.d.ts +68 -0
- package/dist/cjs/entities/EntityMetadata.js +261 -0
- package/dist/cjs/entities/EntityMetadata.js.map +1 -0
- package/dist/cjs/entities/EntityStore.d.ts +63 -68
- package/dist/cjs/entities/EntityStore.js +294 -438
- package/dist/cjs/entities/EntityStore.js.map +1 -1
- package/dist/cjs/entities/OperationBatcher.d.ts +52 -0
- package/dist/cjs/entities/OperationBatcher.js +165 -0
- package/dist/cjs/entities/OperationBatcher.js.map +1 -0
- package/dist/cjs/entities/types.d.ts +84 -0
- package/dist/cjs/entities/types.js +3 -0
- package/dist/cjs/entities/types.js.map +1 -0
- package/dist/cjs/files/EntityFile.d.ts +5 -2
- package/dist/cjs/files/EntityFile.js +8 -4
- package/dist/cjs/files/EntityFile.js.map +1 -1
- package/dist/cjs/files/FileManager.d.ts +3 -1
- package/dist/cjs/files/FileManager.js +5 -3
- package/dist/cjs/files/FileManager.js.map +1 -1
- package/dist/cjs/files/FileStorage.js +7 -7
- package/dist/cjs/files/FileStorage.js.map +1 -1
- package/dist/cjs/files/utils.d.ts +2 -0
- package/dist/cjs/files/utils.js +5 -2
- package/dist/cjs/files/utils.js.map +1 -1
- package/dist/cjs/idb.d.ts +2 -0
- package/dist/cjs/idb.js +50 -4
- package/dist/cjs/idb.js.map +1 -1
- package/dist/cjs/index.d.ts +1 -1
- package/dist/cjs/metadata/AckInfoStore.js +1 -1
- package/dist/cjs/metadata/AckInfoStore.js.map +1 -1
- package/dist/cjs/metadata/BaselinesStore.d.ts +4 -1
- package/dist/cjs/metadata/BaselinesStore.js +19 -10
- package/dist/cjs/metadata/BaselinesStore.js.map +1 -1
- package/dist/cjs/metadata/LocalReplicaStore.d.ts +1 -1
- package/dist/cjs/metadata/LocalReplicaStore.js +11 -5
- package/dist/cjs/metadata/LocalReplicaStore.js.map +1 -1
- package/dist/cjs/metadata/Metadata.d.ts +26 -5
- package/dist/cjs/metadata/Metadata.js +55 -18
- package/dist/cjs/metadata/Metadata.js.map +1 -1
- package/dist/cjs/metadata/OperationsStore.d.ts +3 -0
- package/dist/cjs/metadata/OperationsStore.js +35 -15
- package/dist/cjs/metadata/OperationsStore.js.map +1 -1
- package/dist/cjs/migration/openDatabase.js +31 -10
- package/dist/cjs/migration/openDatabase.js.map +1 -1
- package/dist/cjs/queries/BaseQuery.js +13 -1
- package/dist/cjs/queries/BaseQuery.js.map +1 -1
- package/dist/cjs/queries/CollectionQueries.js +1 -1
- package/dist/cjs/queries/CollectionQueries.js.map +1 -1
- package/dist/cjs/queries/FindAllQuery.js +1 -0
- package/dist/cjs/queries/FindAllQuery.js.map +1 -1
- package/dist/cjs/queries/QueryCache.d.ts +1 -0
- package/dist/cjs/queries/QueryCache.js +4 -0
- package/dist/cjs/queries/QueryCache.js.map +1 -1
- package/dist/cjs/queries/QueryableStorage.d.ts +20 -0
- package/dist/cjs/queries/QueryableStorage.js +84 -0
- package/dist/cjs/queries/QueryableStorage.js.map +1 -0
- package/dist/cjs/queries/dbQueries.js +13 -3
- package/dist/cjs/queries/dbQueries.js.map +1 -1
- package/dist/cjs/sync/FileSync.d.ts +1 -0
- package/dist/cjs/sync/FileSync.js +1 -0
- package/dist/cjs/sync/FileSync.js.map +1 -1
- package/dist/cjs/sync/PushPullSync.d.ts +2 -1
- package/dist/cjs/sync/PushPullSync.js +7 -1
- package/dist/cjs/sync/PushPullSync.js.map +1 -1
- package/dist/cjs/sync/Sync.d.ts +6 -3
- package/dist/cjs/sync/Sync.js +9 -4
- package/dist/cjs/sync/Sync.js.map +1 -1
- package/dist/cjs/sync/WebSocketSync.d.ts +4 -1
- package/dist/cjs/sync/WebSocketSync.js +41 -11
- package/dist/cjs/sync/WebSocketSync.js.map +1 -1
- package/dist/esm/DocumentManager.d.ts +1 -1
- package/dist/esm/DocumentManager.js +1 -1
- package/dist/esm/DocumentManager.js.map +1 -1
- package/dist/esm/IDBService.d.ts +28 -7
- package/dist/esm/IDBService.js +51 -14
- package/dist/esm/IDBService.js.map +1 -1
- package/dist/esm/UndoHistory.d.ts +1 -1
- package/dist/esm/UndoHistory.js +6 -2
- package/dist/esm/UndoHistory.js.map +1 -1
- package/dist/esm/__tests__/batching.test.js +3 -1
- package/dist/esm/__tests__/batching.test.js.map +1 -1
- package/dist/esm/__tests__/documents.test.js +37 -6
- package/dist/esm/__tests__/documents.test.js.map +1 -1
- package/dist/esm/__tests__/fixtures/testStorage.d.ts +2 -2
- package/dist/esm/__tests__/fixtures/testStorage.js +2 -1
- package/dist/esm/__tests__/fixtures/testStorage.js.map +1 -1
- package/dist/esm/__tests__/legacyOids.test.js +50 -17
- package/dist/esm/__tests__/legacyOids.test.js.map +1 -1
- package/dist/esm/__tests__/mutations.test.js +9 -3
- package/dist/esm/__tests__/mutations.test.js.map +1 -1
- package/dist/esm/__tests__/queries.test.js +6 -2
- package/dist/esm/__tests__/queries.test.js.map +1 -1
- package/dist/esm/__tests__/setup/indexedDB.d.ts +1 -1
- package/dist/esm/__tests__/setup/indexedDB.js +8 -1
- package/dist/esm/__tests__/setup/indexedDB.js.map +1 -1
- package/dist/esm/__tests__/undo.test.js +16 -9
- package/dist/esm/__tests__/undo.test.js.map +1 -1
- package/dist/esm/client/Client.d.ts +1 -1
- package/dist/esm/client/Client.js +7 -3
- package/dist/esm/client/Client.js.map +1 -1
- package/dist/esm/client/ClientDescriptor.js +21 -6
- package/dist/esm/client/ClientDescriptor.js.map +1 -1
- package/dist/esm/context.d.ts +10 -1
- package/dist/esm/entities/Entity.d.ts +106 -178
- package/dist/esm/entities/Entity.js +559 -376
- package/dist/esm/entities/Entity.js.map +1 -1
- package/dist/esm/entities/Entity.test.d.ts +1 -0
- package/dist/esm/entities/Entity.test.js +192 -0
- package/dist/esm/entities/Entity.test.js.map +1 -0
- package/dist/esm/entities/EntityCache.d.ts +15 -0
- package/dist/esm/entities/EntityCache.js +35 -0
- package/dist/esm/entities/EntityCache.js.map +1 -0
- package/dist/esm/entities/EntityMetadata.d.ts +68 -0
- package/dist/esm/entities/EntityMetadata.js +256 -0
- package/dist/esm/entities/EntityMetadata.js.map +1 -0
- package/dist/esm/entities/EntityStore.d.ts +63 -68
- package/dist/esm/entities/EntityStore.js +295 -439
- package/dist/esm/entities/EntityStore.js.map +1 -1
- package/dist/esm/entities/OperationBatcher.d.ts +52 -0
- package/dist/esm/entities/OperationBatcher.js +161 -0
- package/dist/esm/entities/OperationBatcher.js.map +1 -0
- package/dist/esm/entities/types.d.ts +84 -0
- package/dist/esm/entities/types.js +2 -0
- package/dist/esm/entities/types.js.map +1 -0
- package/dist/esm/files/EntityFile.d.ts +5 -2
- package/dist/esm/files/EntityFile.js +8 -4
- package/dist/esm/files/EntityFile.js.map +1 -1
- package/dist/esm/files/FileManager.d.ts +3 -1
- package/dist/esm/files/FileManager.js +5 -3
- package/dist/esm/files/FileManager.js.map +1 -1
- package/dist/esm/files/FileStorage.js +7 -7
- package/dist/esm/files/FileStorage.js.map +1 -1
- package/dist/esm/files/utils.d.ts +2 -0
- package/dist/esm/files/utils.js +4 -2
- package/dist/esm/files/utils.js.map +1 -1
- package/dist/esm/idb.d.ts +2 -0
- package/dist/esm/idb.js +47 -3
- package/dist/esm/idb.js.map +1 -1
- package/dist/esm/index.d.ts +1 -1
- package/dist/esm/metadata/AckInfoStore.js +1 -1
- package/dist/esm/metadata/AckInfoStore.js.map +1 -1
- package/dist/esm/metadata/BaselinesStore.d.ts +4 -1
- package/dist/esm/metadata/BaselinesStore.js +19 -10
- package/dist/esm/metadata/BaselinesStore.js.map +1 -1
- package/dist/esm/metadata/LocalReplicaStore.d.ts +1 -1
- package/dist/esm/metadata/LocalReplicaStore.js +11 -5
- package/dist/esm/metadata/LocalReplicaStore.js.map +1 -1
- package/dist/esm/metadata/Metadata.d.ts +26 -5
- package/dist/esm/metadata/Metadata.js +56 -19
- package/dist/esm/metadata/Metadata.js.map +1 -1
- package/dist/esm/metadata/OperationsStore.d.ts +3 -0
- package/dist/esm/metadata/OperationsStore.js +35 -15
- package/dist/esm/metadata/OperationsStore.js.map +1 -1
- package/dist/esm/migration/openDatabase.js +32 -11
- package/dist/esm/migration/openDatabase.js.map +1 -1
- package/dist/esm/queries/BaseQuery.js +13 -1
- package/dist/esm/queries/BaseQuery.js.map +1 -1
- package/dist/esm/queries/CollectionQueries.js +1 -1
- package/dist/esm/queries/CollectionQueries.js.map +1 -1
- package/dist/esm/queries/FindAllQuery.js +1 -0
- package/dist/esm/queries/FindAllQuery.js.map +1 -1
- package/dist/esm/queries/QueryCache.d.ts +1 -0
- package/dist/esm/queries/QueryCache.js +4 -0
- package/dist/esm/queries/QueryCache.js.map +1 -1
- package/dist/esm/queries/QueryableStorage.d.ts +20 -0
- package/dist/esm/queries/QueryableStorage.js +80 -0
- package/dist/esm/queries/QueryableStorage.js.map +1 -0
- package/dist/esm/queries/dbQueries.js +13 -3
- package/dist/esm/queries/dbQueries.js.map +1 -1
- package/dist/esm/sync/FileSync.d.ts +1 -0
- package/dist/esm/sync/FileSync.js +1 -0
- package/dist/esm/sync/FileSync.js.map +1 -1
- package/dist/esm/sync/PushPullSync.d.ts +2 -1
- package/dist/esm/sync/PushPullSync.js +7 -1
- package/dist/esm/sync/PushPullSync.js.map +1 -1
- package/dist/esm/sync/Sync.d.ts +6 -3
- package/dist/esm/sync/Sync.js +9 -4
- package/dist/esm/sync/Sync.js.map +1 -1
- package/dist/esm/sync/WebSocketSync.d.ts +4 -1
- package/dist/esm/sync/WebSocketSync.js +41 -11
- package/dist/esm/sync/WebSocketSync.js.map +1 -1
- package/dist/tsconfig-cjs.tsbuildinfo +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +8 -7
- package/src/DocumentManager.ts +1 -1
- package/src/IDBService.ts +78 -17
- package/src/UndoHistory.ts +5 -3
- package/src/__tests__/batching.test.ts +5 -2
- package/src/__tests__/documents.test.ts +44 -6
- package/src/__tests__/fixtures/testStorage.ts +3 -0
- package/src/__tests__/legacyOids.test.ts +53 -17
- package/src/__tests__/mutations.test.ts +9 -3
- package/src/__tests__/queries.test.ts +6 -2
- package/src/__tests__/setup/indexedDB.ts +8 -1
- package/src/__tests__/undo.test.ts +17 -9
- package/src/client/Client.ts +7 -3
- package/src/client/ClientDescriptor.ts +24 -8
- package/src/context.ts +16 -1
- package/src/entities/Entity.test.ts +218 -0
- package/src/entities/Entity.ts +696 -616
- package/src/entities/EntityCache.ts +41 -0
- package/src/entities/EntityMetadata.ts +364 -0
- package/src/entities/EntityStore.ts +384 -621
- package/src/entities/OperationBatcher.ts +251 -0
- package/src/entities/types.ts +154 -0
- package/src/files/EntityFile.ts +9 -4
- package/src/files/FileManager.ts +5 -3
- package/src/files/FileStorage.ts +7 -13
- package/src/files/utils.ts +6 -2
- package/src/idb.ts +51 -3
- package/src/index.ts +1 -1
- package/src/metadata/AckInfoStore.ts +1 -1
- package/src/metadata/BaselinesStore.ts +16 -24
- package/src/metadata/LocalReplicaStore.ts +13 -6
- package/src/metadata/Metadata.ts +109 -24
- package/src/metadata/OperationsStore.ts +37 -16
- package/src/migration/openDatabase.ts +32 -10
- package/src/queries/BaseQuery.ts +14 -1
- package/src/queries/CollectionQueries.ts +1 -1
- package/src/queries/FindAllQuery.ts +4 -0
- package/src/queries/QueryCache.ts +5 -0
- package/src/queries/QueryableStorage.ts +107 -0
- package/src/queries/dbQueries.ts +10 -3
- package/src/sync/FileSync.ts +2 -0
- package/src/sync/PushPullSync.ts +8 -1
- package/src/sync/Sync.ts +14 -6
- package/src/sync/WebSocketSync.ts +47 -10
- package/dist/cjs/entities/DocumentFamiliyCache.d.ts +0 -96
- package/dist/cjs/entities/DocumentFamiliyCache.js +0 -287
- package/dist/cjs/entities/DocumentFamiliyCache.js.map +0 -1
- package/dist/esm/entities/DocumentFamiliyCache.d.ts +0 -96
- package/dist/esm/entities/DocumentFamiliyCache.js +0 -283
- package/dist/esm/entities/DocumentFamiliyCache.js.map +0 -1
- package/src/entities/DocumentFamiliyCache.ts +0 -426
- package/src/entities/design.tldr +0 -808
|
@@ -1,731 +1,494 @@
|
|
|
1
1
|
import {
|
|
2
|
+
DocumentBaseline,
|
|
3
|
+
ObjectIdentifier,
|
|
4
|
+
Operation,
|
|
5
|
+
StorageFieldsSchema,
|
|
6
|
+
StorageObjectFieldSchema,
|
|
2
7
|
assert,
|
|
3
|
-
assignIndexValues,
|
|
4
8
|
assignOid,
|
|
5
|
-
assignOidPropertiesToAllSubObjects,
|
|
6
|
-
Batcher,
|
|
7
|
-
cloneDeep,
|
|
8
9
|
decomposeOid,
|
|
9
|
-
DocumentBaseline,
|
|
10
|
-
EventSubscriber,
|
|
11
|
-
generateId,
|
|
12
|
-
getIndexValues,
|
|
13
10
|
getOidRoot,
|
|
14
|
-
getUndoOperations,
|
|
15
11
|
groupBaselinesByRootOid,
|
|
16
|
-
|
|
12
|
+
groupPatchesByOid,
|
|
17
13
|
groupPatchesByRootOid,
|
|
18
|
-
|
|
19
|
-
Operation,
|
|
14
|
+
isRootOid,
|
|
20
15
|
removeOidsFromAllSubObjects,
|
|
21
|
-
StorageCollectionSchema,
|
|
22
|
-
StorageObjectFieldSchema,
|
|
23
16
|
} from '@verdant-web/common';
|
|
24
17
|
import { Context } from '../context.js';
|
|
18
|
+
import { Metadata } from '../metadata/Metadata.js';
|
|
19
|
+
import { Entity } from './Entity.js';
|
|
20
|
+
import { Disposable } from '../utils/Disposable.js';
|
|
21
|
+
import { EntityFamilyMetadata } from './EntityMetadata.js';
|
|
25
22
|
import { FileManager } from '../files/FileManager.js';
|
|
23
|
+
import { OperationBatcher } from './OperationBatcher.js';
|
|
24
|
+
import { QueryableStorage } from '../queries/QueryableStorage.js';
|
|
25
|
+
import { WeakEvent } from 'weak-event';
|
|
26
26
|
import { processValueFiles } from '../files/utils.js';
|
|
27
|
-
import {
|
|
28
|
-
import { Metadata } from '../metadata/Metadata.js';
|
|
29
|
-
import { DocumentFamilyCache } from './DocumentFamiliyCache.js';
|
|
30
|
-
import { TaggedOperation } from '../types.js';
|
|
27
|
+
import { abort } from 'process';
|
|
31
28
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
export interface OperationBatch {
|
|
35
|
-
run: (fn: () => void) => this;
|
|
36
|
-
flush: () => Promise<void>;
|
|
37
|
-
discard: () => void;
|
|
29
|
+
enum AbortReason {
|
|
30
|
+
Reset,
|
|
38
31
|
}
|
|
39
32
|
|
|
40
|
-
export
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
private
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
private
|
|
67
|
-
|
|
33
|
+
export type EntityStoreEventData = {
|
|
34
|
+
oid: ObjectIdentifier;
|
|
35
|
+
operations?: Record<string, Operation[]>;
|
|
36
|
+
baselines?: DocumentBaseline[];
|
|
37
|
+
isLocal: boolean;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
export type EntityStoreEvents = {
|
|
41
|
+
add: WeakEvent<EntityStore, EntityStoreEventData>;
|
|
42
|
+
replace: WeakEvent<EntityStore, EntityStoreEventData>;
|
|
43
|
+
resetAll: WeakEvent<EntityStore, void>;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
type IncomingData = {
|
|
47
|
+
operations?: Operation[];
|
|
48
|
+
baselines?: DocumentBaseline[];
|
|
49
|
+
reset?: boolean;
|
|
50
|
+
isLocal?: boolean;
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
export class EntityStore extends Disposable {
|
|
54
|
+
private ctx;
|
|
55
|
+
private meta;
|
|
56
|
+
private files;
|
|
57
|
+
private batcher;
|
|
58
|
+
private queryableStorage;
|
|
59
|
+
private events: EntityStoreEvents = {
|
|
60
|
+
add: new WeakEvent(),
|
|
61
|
+
replace: new WeakEvent(),
|
|
62
|
+
resetAll: new WeakEvent(),
|
|
63
|
+
};
|
|
64
|
+
private cache = new Map<ObjectIdentifier, WeakRef<Entity>>();
|
|
65
|
+
private pendingEntityPromises = new Map<
|
|
66
|
+
ObjectIdentifier,
|
|
67
|
+
Promise<Entity | null>
|
|
68
|
+
>();
|
|
69
|
+
// halts the current data queue processing
|
|
70
|
+
private abortDataQueueController = new AbortController();
|
|
71
|
+
private ongoingResetPromise: Promise<void> | null = null;
|
|
72
|
+
private entityFinalizationRegistry = new FinalizationRegistry(
|
|
73
|
+
(oid: ObjectIdentifier) => {
|
|
74
|
+
this.ctx.log('debug', 'Entity GC', oid);
|
|
75
|
+
},
|
|
76
|
+
);
|
|
68
77
|
|
|
69
78
|
constructor({
|
|
70
|
-
|
|
79
|
+
ctx,
|
|
71
80
|
meta,
|
|
72
|
-
batchTimeout = 200,
|
|
73
81
|
files,
|
|
74
82
|
}: {
|
|
75
|
-
|
|
83
|
+
ctx: Context;
|
|
76
84
|
meta: Metadata;
|
|
77
85
|
files: FileManager;
|
|
78
|
-
batchTimeout?: number;
|
|
79
86
|
}) {
|
|
80
|
-
|
|
87
|
+
super();
|
|
81
88
|
|
|
82
|
-
this.
|
|
89
|
+
this.ctx = ctx;
|
|
83
90
|
this.meta = meta;
|
|
84
91
|
this.files = files;
|
|
85
|
-
this.
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
key: DEFAULT_BATCH_KEY,
|
|
91
|
-
items: [],
|
|
92
|
-
max: 100,
|
|
93
|
-
timeout: batchTimeout,
|
|
94
|
-
userData: { undoable: true },
|
|
92
|
+
this.queryableStorage = new QueryableStorage({ ctx });
|
|
93
|
+
this.batcher = new OperationBatcher({
|
|
94
|
+
ctx,
|
|
95
|
+
meta,
|
|
96
|
+
entities: this,
|
|
95
97
|
});
|
|
96
|
-
this.unsubscribes.push(this.meta.subscribe('rebase', this.handleRebase));
|
|
97
98
|
}
|
|
98
99
|
|
|
99
|
-
|
|
100
|
-
|
|
100
|
+
// expose batch APIs
|
|
101
|
+
get batch() {
|
|
102
|
+
return this.batcher.batch;
|
|
103
|
+
}
|
|
104
|
+
get flushAllBatches() {
|
|
105
|
+
return this.batcher.flushAll;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// internal-ish API to load remote / stored data
|
|
109
|
+
addData = async (data: IncomingData) => {
|
|
110
|
+
if (this.disposed) {
|
|
111
|
+
this.ctx.log('warn', 'EntityStore is disposed, not adding incoming data');
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
// for resets - abort any other changes, reset everything,
|
|
115
|
+
// then proceed
|
|
116
|
+
if (data.reset) {
|
|
117
|
+
this.ctx.log(
|
|
118
|
+
'info',
|
|
119
|
+
'Resetting local store to replicate remote synced data - dropping any current transactions',
|
|
120
|
+
);
|
|
121
|
+
// cancel any other ongoing data - it will all
|
|
122
|
+
// be replaced by the reset
|
|
123
|
+
this.abortDataQueueController.abort(AbortReason.Reset);
|
|
124
|
+
this.abortDataQueueController = new AbortController();
|
|
125
|
+
this.ongoingResetPromise = this.resetData().finally(() => {
|
|
126
|
+
this.ongoingResetPromise = null;
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// await either the reset we just started, or any that was
|
|
131
|
+
// in progress when this data came in.
|
|
132
|
+
if (this.ongoingResetPromise) {
|
|
133
|
+
this.ctx.log('debug', 'Waiting for ongoing reset to complete');
|
|
134
|
+
await this.ongoingResetPromise;
|
|
135
|
+
this.ctx.log('debug', 'Ongoing reset complete');
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
await this.processData(data);
|
|
101
139
|
};
|
|
102
140
|
|
|
103
|
-
private
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
if (!this.schema.collections[collection]) {
|
|
108
|
-
this.log('warn', `Missing schema for collection: ${collection}`);
|
|
109
|
-
return { schema: null, readonlyKeys: [] };
|
|
141
|
+
private resetData = async () => {
|
|
142
|
+
if (this.disposed) {
|
|
143
|
+
this.ctx.log('warn', 'EntityStore is disposed, not resetting local data');
|
|
144
|
+
return;
|
|
110
145
|
}
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
schema: {
|
|
115
|
-
type: 'object',
|
|
116
|
-
properties: schema.fields as any,
|
|
117
|
-
} as const,
|
|
118
|
-
};
|
|
146
|
+
await this.meta.reset();
|
|
147
|
+
await this.queryableStorage.reset();
|
|
148
|
+
this.events.resetAll.invoke(this);
|
|
119
149
|
};
|
|
120
150
|
|
|
121
|
-
private
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
// avoid writing to disposed db
|
|
127
|
-
if (this._disposed) {
|
|
128
|
-
this.context.log(
|
|
129
|
-
'debug',
|
|
130
|
-
`EntityStore is disposed, not refreshing ${familyCache.oid} cache`,
|
|
151
|
+
private processData = async (data: IncomingData) => {
|
|
152
|
+
if (this.disposed) {
|
|
153
|
+
this.ctx.log(
|
|
154
|
+
'warn',
|
|
155
|
+
'EntityStore is disposed, not processing incoming data',
|
|
131
156
|
);
|
|
132
157
|
return;
|
|
133
158
|
}
|
|
134
159
|
|
|
135
|
-
|
|
136
|
-
const
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
const operations: TaggedOperation[] = [];
|
|
143
|
-
|
|
144
|
-
await Promise.all([
|
|
145
|
-
this.meta.baselines.iterateOverAllForDocument(
|
|
146
|
-
familyCache.oid,
|
|
147
|
-
(baseline) => {
|
|
148
|
-
baselines.push(baseline);
|
|
149
|
-
},
|
|
150
|
-
{
|
|
151
|
-
transaction,
|
|
152
|
-
mode: 'readwrite',
|
|
153
|
-
},
|
|
154
|
-
),
|
|
155
|
-
this.meta.operations.iterateOverAllOperationsForDocument(
|
|
156
|
-
familyCache.oid,
|
|
157
|
-
(op) => {
|
|
158
|
-
(op as TaggedOperation).confirmed = true;
|
|
159
|
-
operations.push(op as TaggedOperation);
|
|
160
|
-
},
|
|
161
|
-
{ transaction, mode: 'readwrite' },
|
|
162
|
-
),
|
|
163
|
-
]);
|
|
164
|
-
familyCache.reset({
|
|
165
|
-
operations,
|
|
166
|
-
baselines,
|
|
167
|
-
dropExistingUnconfirmed: dropUnconfirmed,
|
|
168
|
-
dropAll,
|
|
160
|
+
const baselines = data?.baselines ?? [];
|
|
161
|
+
const operations = data?.operations ?? [];
|
|
162
|
+
|
|
163
|
+
this.ctx.log('debug', 'Processing incoming data', {
|
|
164
|
+
operations: operations.length,
|
|
165
|
+
baselines: baselines.length,
|
|
166
|
+
reset: !!data.reset,
|
|
169
167
|
});
|
|
170
|
-
};
|
|
171
168
|
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
context: this.context,
|
|
182
|
-
});
|
|
169
|
+
const allDocumentOids: ObjectIdentifier[] = Array.from(
|
|
170
|
+
new Set(
|
|
171
|
+
baselines
|
|
172
|
+
.map((b) => getOidRoot(b.oid))
|
|
173
|
+
.concat(operations.map((o) => getOidRoot(o.oid))),
|
|
174
|
+
),
|
|
175
|
+
);
|
|
176
|
+
const baselinesGroupedByOid = groupBaselinesByRootOid(baselines);
|
|
177
|
+
const operationsGroupedByOid = groupPatchesByRootOid(operations);
|
|
183
178
|
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
//
|
|
191
|
-
//
|
|
192
|
-
//
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
179
|
+
this.ctx.log('debug', 'Applying data to live entities');
|
|
180
|
+
// synchronously add/replace data in any open entities via eventing
|
|
181
|
+
for (const oid of allDocumentOids) {
|
|
182
|
+
const baselines = baselinesGroupedByOid[oid];
|
|
183
|
+
const operations = operationsGroupedByOid[oid] ?? [];
|
|
184
|
+
const groupedOperations = groupPatchesByOid(operations);
|
|
185
|
+
// what happens if an entity is being hydrated
|
|
186
|
+
// while this is happening? - we wait for the hydration promise
|
|
187
|
+
// to complete, then invoke the event
|
|
188
|
+
const event = data.reset ? this.events.replace : this.events.add;
|
|
189
|
+
const hydrationPromise = this.pendingEntityPromises.get(oid);
|
|
190
|
+
if (hydrationPromise) {
|
|
191
|
+
hydrationPromise.then(() => {
|
|
192
|
+
event.invoke(this, {
|
|
193
|
+
oid,
|
|
194
|
+
baselines,
|
|
195
|
+
operations: groupedOperations,
|
|
196
|
+
isLocal: false,
|
|
197
|
+
});
|
|
198
|
+
});
|
|
199
|
+
} else {
|
|
200
|
+
if (this.cache.has(oid)) {
|
|
201
|
+
this.ctx.log('debug', 'Cache has', oid, ', an event should follow.');
|
|
202
|
+
}
|
|
203
|
+
event.invoke(this, {
|
|
204
|
+
oid,
|
|
205
|
+
baselines,
|
|
206
|
+
operations: groupedOperations,
|
|
207
|
+
isLocal: false,
|
|
208
|
+
});
|
|
209
|
+
}
|
|
204
210
|
}
|
|
205
|
-
await familyCache.initializedPromise;
|
|
206
211
|
|
|
207
|
-
|
|
208
|
-
|
|
212
|
+
const abortOptions = {
|
|
213
|
+
abort: this.abortDataQueueController.signal,
|
|
214
|
+
};
|
|
209
215
|
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
};
|
|
216
|
+
// then, asynchronously add to the database
|
|
217
|
+
await this.meta.insertData(data, abortOptions);
|
|
213
218
|
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
219
|
+
// FIXME: entities hydrated here are not seeing
|
|
220
|
+
// the operations just inserted above!!
|
|
221
|
+
// IDEA: can we coordinate here with hydrate promises
|
|
222
|
+
// based on affected OIDs?
|
|
223
|
+
|
|
224
|
+
// recompute all affected documents for querying
|
|
225
|
+
const entities = await Promise.all(
|
|
226
|
+
allDocumentOids.map(async (oid) => {
|
|
227
|
+
const entity = await this.hydrate(oid, abortOptions);
|
|
228
|
+
// if the entity is not found, we return a stub that
|
|
229
|
+
// indicates it's deleted and should be cleared
|
|
230
|
+
return (
|
|
231
|
+
entity ?? {
|
|
232
|
+
oid,
|
|
233
|
+
getSnapshot(): any {
|
|
234
|
+
return null;
|
|
235
|
+
},
|
|
236
|
+
}
|
|
237
|
+
);
|
|
238
|
+
}),
|
|
239
|
+
);
|
|
240
|
+
try {
|
|
241
|
+
await this.queryableStorage.saveEntities(entities, abortOptions);
|
|
242
|
+
} catch (err) {
|
|
243
|
+
if (this.disposed) {
|
|
244
|
+
this.ctx.log(
|
|
245
|
+
'warn',
|
|
246
|
+
'Error saving entities to queryable storage - EntityStore is disposed',
|
|
247
|
+
err,
|
|
248
|
+
);
|
|
249
|
+
} else {
|
|
250
|
+
this.ctx.log(
|
|
251
|
+
'error',
|
|
252
|
+
'Error saving entities to queryable storage',
|
|
253
|
+
err,
|
|
254
|
+
);
|
|
255
|
+
}
|
|
218
256
|
}
|
|
219
|
-
|
|
220
|
-
const { id, collection } = decomposeOid(rootOid);
|
|
221
|
-
const entity = await this.get(rootOid);
|
|
257
|
+
};
|
|
222
258
|
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
259
|
+
// internal-ish API for creating Entities from OIDs
|
|
260
|
+
// when query results come in
|
|
261
|
+
hydrate = async (
|
|
262
|
+
oid: string,
|
|
263
|
+
opts?: { abort: AbortSignal },
|
|
264
|
+
): Promise<Entity | null> => {
|
|
265
|
+
if (!isRootOid(oid)) {
|
|
266
|
+
throw new Error('Cannot hydrate non-root entity');
|
|
226
267
|
}
|
|
227
268
|
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
const
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
const store = tx.objectStore(collection);
|
|
237
|
-
await storeRequestPromise(store.put(stored));
|
|
238
|
-
this.log('info', '📝', 'wrote', collection, id, 'to storage', stored);
|
|
239
|
-
} catch (err) {
|
|
240
|
-
// if the document can't be written, something's very wrong :(
|
|
241
|
-
// log the error and move on...
|
|
242
|
-
this.log(
|
|
243
|
-
"⚠️ CRITICAL: possibly corrupt data couldn't be written to queryable storage. This is probably a bug in verdant! Please report at https://github.com/a-type/verdant/issues",
|
|
244
|
-
'\n',
|
|
245
|
-
'Invalid data:',
|
|
246
|
-
JSON.stringify(stored),
|
|
247
|
-
);
|
|
248
|
-
}
|
|
249
|
-
} else {
|
|
250
|
-
try {
|
|
251
|
-
const tx = this.db.transaction(collection, 'readwrite');
|
|
252
|
-
const store = tx.objectStore(collection);
|
|
253
|
-
await storeRequestPromise(store.delete(id));
|
|
254
|
-
this.log('info', '❌', 'deleted', collection, id, 'from storage');
|
|
255
|
-
} catch (err) {
|
|
256
|
-
if (err instanceof Error) {
|
|
257
|
-
// it's ok if the collection doesn't exist or the document
|
|
258
|
-
// doesn't exist.
|
|
259
|
-
if (
|
|
260
|
-
err instanceof DOMException &&
|
|
261
|
-
err.message?.includes('not found')
|
|
262
|
-
) {
|
|
263
|
-
this.log('debug', 'document not found in storage', oid);
|
|
264
|
-
} else {
|
|
265
|
-
throw err;
|
|
269
|
+
if (this.cache.has(oid)) {
|
|
270
|
+
this.ctx.log('debug', 'Hydrating entity from cache', oid);
|
|
271
|
+
const cached = this.cache.get(oid);
|
|
272
|
+
if (cached) {
|
|
273
|
+
const entity = cached.deref();
|
|
274
|
+
if (entity) {
|
|
275
|
+
if (entity.deleted) {
|
|
276
|
+
return null;
|
|
266
277
|
}
|
|
278
|
+
return entity;
|
|
279
|
+
} else {
|
|
280
|
+
this.ctx.log('debug', "Removing GC'd entity from cache", oid);
|
|
281
|
+
this.cache.delete(oid);
|
|
267
282
|
}
|
|
268
283
|
}
|
|
269
284
|
}
|
|
270
|
-
};
|
|
271
|
-
|
|
272
|
-
get = async (oid: ObjectIdentifier) => {
|
|
273
|
-
const familyCache = await this.openFamilyCache(oid);
|
|
274
|
-
const { schema, readonlyKeys } = this.getDocumentSchema(oid);
|
|
275
|
-
if (!schema) {
|
|
276
|
-
return null;
|
|
277
|
-
}
|
|
278
|
-
return familyCache.getEntity({ oid, fieldSchema: schema, readonlyKeys });
|
|
279
|
-
};
|
|
280
285
|
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
const { schema, readonlyKeys } = this.getDocumentSchema(oid);
|
|
290
|
-
if (!schema) {
|
|
286
|
+
// we don't want to hydrate two entities in parallel, so
|
|
287
|
+
// we use a promise to ensure that only one is ever
|
|
288
|
+
// constructed at a time
|
|
289
|
+
const pendingPromise = this.pendingEntityPromises.get(oid);
|
|
290
|
+
if (!pendingPromise) {
|
|
291
|
+
this.ctx.log('debug', 'Hydrating entity from storage', oid);
|
|
292
|
+
const entity = this.constructEntity(oid);
|
|
293
|
+
if (!entity) {
|
|
291
294
|
return null;
|
|
292
295
|
}
|
|
293
|
-
|
|
296
|
+
const pendingPromise = this.loadEntity(entity, opts);
|
|
297
|
+
pendingPromise.finally(() => {
|
|
298
|
+
this.pendingEntityPromises.delete(oid);
|
|
299
|
+
});
|
|
300
|
+
this.pendingEntityPromises.set(oid, pendingPromise);
|
|
301
|
+
return pendingPromise;
|
|
302
|
+
} else {
|
|
303
|
+
this.ctx.log('debug', 'Waiting for entity hydration', oid);
|
|
304
|
+
return pendingPromise;
|
|
294
305
|
}
|
|
295
|
-
return null;
|
|
296
306
|
};
|
|
297
307
|
|
|
308
|
+
destroy = async () => {
|
|
309
|
+
this.dispose();
|
|
310
|
+
await this.batcher.flushAll();
|
|
311
|
+
};
|
|
312
|
+
|
|
313
|
+
// public APIs for manipulating entities
|
|
314
|
+
|
|
298
315
|
/**
|
|
299
|
-
* Creates a new
|
|
300
|
-
* document is submitted to storage and sync.
|
|
316
|
+
* Creates a new Entity with the given initial data.
|
|
301
317
|
*/
|
|
302
318
|
create = async (
|
|
303
319
|
initial: any,
|
|
304
320
|
oid: ObjectIdentifier,
|
|
305
|
-
|
|
321
|
+
{ undoable = true }: { undoable?: boolean } = {},
|
|
306
322
|
) => {
|
|
307
|
-
|
|
323
|
+
this.ctx.log('debug', 'Creating new entity', oid);
|
|
324
|
+
const { collection } = decomposeOid(oid);
|
|
325
|
+
// remove any OID associations from the initial data
|
|
308
326
|
removeOidsFromAllSubObjects(initial);
|
|
309
|
-
//
|
|
327
|
+
// grab files and replace them with refs
|
|
310
328
|
const processed = processValueFiles(initial, this.files.add);
|
|
311
329
|
|
|
312
330
|
assignOid(processed, oid);
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
// we do this so it can be immediately queryable from storage...
|
|
318
|
-
// only holding it in memory would introduce lag before it shows up
|
|
319
|
-
// in other queries.
|
|
320
|
-
await this.submitOperations(operations, options);
|
|
321
|
-
const { schema, readonlyKeys } = this.getDocumentSchema(oid);
|
|
322
|
-
if (!schema) {
|
|
331
|
+
|
|
332
|
+
// creating a new Entity with no data, then preloading the operations
|
|
333
|
+
const entity = this.constructEntity(oid);
|
|
334
|
+
if (!entity) {
|
|
323
335
|
throw new Error(
|
|
324
|
-
`
|
|
325
|
-
decomposeOid(oid).collection
|
|
326
|
-
} collection; it is not defined in the current schema version.`,
|
|
336
|
+
`Could not put new document: no schema exists for collection ${collection}`,
|
|
327
337
|
);
|
|
328
338
|
}
|
|
329
|
-
return familyCache.getEntity({ oid, fieldSchema: schema, readonlyKeys });
|
|
330
|
-
};
|
|
331
339
|
|
|
332
|
-
|
|
333
|
-
operations
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
const operationsByOid = groupPatchesByRootOid(operations);
|
|
337
|
-
const oids = Object.keys(operationsByOid);
|
|
338
|
-
oids.forEach((oid) => {
|
|
339
|
-
const familyCache = this.documentFamilyCaches.get(oid);
|
|
340
|
-
if (familyCache) {
|
|
341
|
-
this.log(
|
|
342
|
-
'adding',
|
|
343
|
-
info.confirmed ? 'confirmed' : 'unconfirmed',
|
|
344
|
-
'operations to cache',
|
|
345
|
-
oid,
|
|
346
|
-
operationsByOid[oid].length,
|
|
347
|
-
);
|
|
348
|
-
if (info.isLocal) {
|
|
349
|
-
familyCache.insertLocalOperations(operationsByOid[oid]);
|
|
350
|
-
} else {
|
|
351
|
-
familyCache.insertOperations(operationsByOid[oid], info);
|
|
352
|
-
}
|
|
353
|
-
}
|
|
340
|
+
const operations = this.meta.patchCreator.createInitialize(processed, oid);
|
|
341
|
+
await this.batcher.commitOperations(operations, {
|
|
342
|
+
undoable: !!undoable,
|
|
343
|
+
source: entity,
|
|
354
344
|
});
|
|
355
|
-
};
|
|
356
345
|
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
info: { isLocal: boolean },
|
|
360
|
-
) => {
|
|
361
|
-
const baselinesByOid = groupBaselinesByRootOid(baselines);
|
|
362
|
-
const oids = Object.keys(baselinesByOid);
|
|
363
|
-
oids.forEach((oid) => {
|
|
364
|
-
const cache = this.documentFamilyCaches.get(oid);
|
|
365
|
-
if (cache) {
|
|
366
|
-
this.log(
|
|
367
|
-
'adding',
|
|
368
|
-
'baselines to cache',
|
|
369
|
-
oid,
|
|
370
|
-
baselinesByOid[oid].length,
|
|
371
|
-
);
|
|
372
|
-
cache.insertBaselines(baselinesByOid[oid], info);
|
|
373
|
-
}
|
|
374
|
-
});
|
|
375
|
-
};
|
|
346
|
+
// TODO: what happens if you create an entity with an OID that already
|
|
347
|
+
// exists?
|
|
376
348
|
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
}) => {
|
|
388
|
-
const baselinesByDocumentOid = groupBaselinesByRootOid(baselines);
|
|
389
|
-
const operationsByDocumentOid = groupPatchesByRootOid(operations);
|
|
390
|
-
const allDocumentOids = Array.from(
|
|
391
|
-
new Set(
|
|
392
|
-
Object.keys(baselinesByDocumentOid).concat(
|
|
393
|
-
Object.keys(operationsByDocumentOid),
|
|
394
|
-
),
|
|
395
|
-
),
|
|
396
|
-
);
|
|
397
|
-
for (const oid of allDocumentOids) {
|
|
398
|
-
const familyCache = this.documentFamilyCaches.get(oid);
|
|
399
|
-
if (familyCache) {
|
|
400
|
-
familyCache.addData({
|
|
401
|
-
operations: operationsByDocumentOid[oid] || [],
|
|
402
|
-
baselines: baselinesByDocumentOid[oid] || [],
|
|
403
|
-
reset,
|
|
404
|
-
isLocal,
|
|
405
|
-
});
|
|
406
|
-
this.log(
|
|
407
|
-
'debug',
|
|
408
|
-
'Added data to cache for',
|
|
409
|
-
oid,
|
|
410
|
-
operationsByDocumentOid[oid]?.length ?? 0,
|
|
411
|
-
'operations',
|
|
412
|
-
baselinesByDocumentOid[oid]?.length ?? 0,
|
|
413
|
-
'baselines',
|
|
414
|
-
);
|
|
415
|
-
} else {
|
|
416
|
-
this.log(
|
|
417
|
-
'debug',
|
|
418
|
-
'Could not add data to cache for',
|
|
419
|
-
oid,
|
|
420
|
-
'because it is not open',
|
|
421
|
-
);
|
|
422
|
-
}
|
|
423
|
-
}
|
|
349
|
+
// we still need to synchronously add the initial operations to the Entity
|
|
350
|
+
// even though they are flowing through the system
|
|
351
|
+
// TODO: this could be better aligned to avoid grouping here
|
|
352
|
+
const operationsGroupedByOid = groupPatchesByOid(operations);
|
|
353
|
+
this.events.add.invoke(this, {
|
|
354
|
+
operations: operationsGroupedByOid,
|
|
355
|
+
isLocal: true,
|
|
356
|
+
oid,
|
|
357
|
+
});
|
|
358
|
+
this.cache.set(oid, this.ctx.weakRef(entity));
|
|
424
359
|
|
|
425
|
-
return
|
|
360
|
+
return entity;
|
|
426
361
|
};
|
|
427
362
|
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
if (this._disposed) {
|
|
438
|
-
this.log('warn', 'EntityStore is disposed, not adding data');
|
|
439
|
-
return;
|
|
440
|
-
}
|
|
441
|
-
// convert operations to tagged operations with confirmed = false
|
|
442
|
-
// while we process and store them. this is in-place so as to
|
|
443
|
-
// not allocate a bunch of objects...
|
|
444
|
-
const taggedOperations = operations as TaggedOperation[];
|
|
445
|
-
for (const op of taggedOperations) {
|
|
446
|
-
op.confirmed = false;
|
|
447
|
-
}
|
|
448
|
-
|
|
449
|
-
let allDocumentOids: string[] = [];
|
|
450
|
-
// in a reset scenario, it only makes things confusing if we
|
|
451
|
-
// optimistically apply incoming operations, since the local
|
|
452
|
-
// history is out of sync
|
|
453
|
-
if (reset) {
|
|
454
|
-
this.log(
|
|
455
|
-
'Resetting local store to replicate remote synced data',
|
|
456
|
-
baselines.length,
|
|
457
|
-
'baselines, and',
|
|
458
|
-
operations.length,
|
|
459
|
-
'operations',
|
|
460
|
-
);
|
|
461
|
-
await this.meta.reset();
|
|
462
|
-
await this.resetStoredDocuments();
|
|
463
|
-
allDocumentOids = Array.from(
|
|
464
|
-
new Set(
|
|
465
|
-
baselines
|
|
466
|
-
.map((b) => getOidRoot(b.oid))
|
|
467
|
-
.concat(operations.map((o) => getOidRoot(o.oid))),
|
|
468
|
-
),
|
|
469
|
-
);
|
|
470
|
-
} else {
|
|
471
|
-
// first, synchronously add data to any open caches for immediate change propagation
|
|
472
|
-
allDocumentOids = this.addDataToOpenCaches({
|
|
473
|
-
operations: taggedOperations,
|
|
474
|
-
baselines,
|
|
475
|
-
reset,
|
|
476
|
-
});
|
|
477
|
-
}
|
|
478
|
-
|
|
479
|
-
// then, asynchronously add data to storage
|
|
480
|
-
await this.meta.insertRemoteBaselines(baselines);
|
|
481
|
-
await this.meta.insertRemoteOperations(operations);
|
|
482
|
-
|
|
483
|
-
if (reset) {
|
|
484
|
-
await this.refreshAllCaches(true, true);
|
|
485
|
-
}
|
|
486
|
-
|
|
487
|
-
// recompute all affected documents for querying
|
|
488
|
-
for (const oid of allDocumentOids) {
|
|
489
|
-
await this.writeDocumentToStorage(oid);
|
|
490
|
-
}
|
|
363
|
+
deleteAll = async (
|
|
364
|
+
oids: ObjectIdentifier[],
|
|
365
|
+
options?: { undoable?: boolean },
|
|
366
|
+
) => {
|
|
367
|
+
this.ctx.log('info', 'Deleting documents', oids);
|
|
368
|
+
assert(
|
|
369
|
+
oids.every((oid) => oid === getOidRoot(oid)),
|
|
370
|
+
'Only root documents may be deleted via client methods',
|
|
371
|
+
);
|
|
491
372
|
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
),
|
|
373
|
+
const allOids = await Promise.all(
|
|
374
|
+
oids.flatMap(async (oid) => {
|
|
375
|
+
const entity = await this.hydrate(oid);
|
|
376
|
+
return entity?.__getFamilyOids__() ?? [];
|
|
377
|
+
}),
|
|
497
378
|
);
|
|
498
|
-
this.context.log('changes to collections', affectedCollections);
|
|
499
|
-
this.context.entityEvents.emit('collectionsChanged', affectedCollections);
|
|
500
|
-
};
|
|
501
379
|
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
confirmed: false,
|
|
380
|
+
// remove the entities from cache
|
|
381
|
+
oids.forEach((oid) => {
|
|
382
|
+
this.cache.delete(oid);
|
|
383
|
+
this.ctx.log('debug', 'Deleted document from cache', oid);
|
|
507
384
|
});
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
385
|
+
|
|
386
|
+
// create the delete patches and wait for them to be applied
|
|
387
|
+
const operations = this.meta.patchCreator.createDeleteAll(allOids.flat());
|
|
388
|
+
await this.batcher.commitOperations(operations, {
|
|
389
|
+
undoable: options?.undoable === undefined ? true : options.undoable,
|
|
511
390
|
});
|
|
512
391
|
};
|
|
513
392
|
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
}
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
fn();
|
|
539
|
-
this.currentBatchKey = DEFAULT_BATCH_KEY;
|
|
540
|
-
return externalApi;
|
|
541
|
-
},
|
|
542
|
-
flush: async () => {
|
|
543
|
-
// before running a batch, the default operations must be flushed
|
|
544
|
-
// this better preserves undo history behavior...
|
|
545
|
-
// if we left the default batch open while flushing a named batch,
|
|
546
|
-
// then the default batch would be flushed after the named batch,
|
|
547
|
-
// and the default batch could contain operations both prior and
|
|
548
|
-
// after the named batch. this would result in a confusing undo
|
|
549
|
-
// history where the first undo might reverse changes before and
|
|
550
|
-
// after a set of other changes.
|
|
551
|
-
await this.operationBatcher.flush(DEFAULT_BATCH_KEY);
|
|
552
|
-
return internalBatch.flush();
|
|
553
|
-
},
|
|
554
|
-
discard: () => {
|
|
555
|
-
this.operationBatcher.discard(batchName);
|
|
393
|
+
delete = async (oid: ObjectIdentifier, options?: { undoable?: boolean }) => {
|
|
394
|
+
return this.deleteAll([oid], options);
|
|
395
|
+
};
|
|
396
|
+
|
|
397
|
+
private getCollectionSchema = (
|
|
398
|
+
collectionName: string,
|
|
399
|
+
): {
|
|
400
|
+
schema: StorageObjectFieldSchema | null;
|
|
401
|
+
readonlyKeys: string[];
|
|
402
|
+
} => {
|
|
403
|
+
const schema = this.ctx.schema.collections[collectionName];
|
|
404
|
+
if (!schema) {
|
|
405
|
+
this.ctx.log('warn', `Missing schema for collection: ${collectionName}`);
|
|
406
|
+
return {
|
|
407
|
+
schema: null,
|
|
408
|
+
readonlyKeys: [],
|
|
409
|
+
};
|
|
410
|
+
}
|
|
411
|
+
return {
|
|
412
|
+
// convert to object schema for compatibility
|
|
413
|
+
schema: {
|
|
414
|
+
type: 'object',
|
|
415
|
+
nullable: false,
|
|
416
|
+
properties: schema.fields as any,
|
|
556
417
|
},
|
|
418
|
+
readonlyKeys: [schema.primaryKey],
|
|
557
419
|
};
|
|
558
|
-
return externalApi;
|
|
559
420
|
};
|
|
560
421
|
|
|
561
422
|
/**
|
|
562
|
-
*
|
|
423
|
+
* Constructs an entity from an OID, but does not load it.
|
|
563
424
|
*/
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
flushAllBatches = async () => {
|
|
569
|
-
await Promise.all(this.operationBatcher.flushAll());
|
|
570
|
-
};
|
|
425
|
+
private constructEntity = (oid: string): Entity | null => {
|
|
426
|
+
const { collection } = decomposeOid(oid);
|
|
427
|
+
const { schema, readonlyKeys } = this.getCollectionSchema(collection);
|
|
571
428
|
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
batchKey: string,
|
|
575
|
-
meta: { undoable?: boolean },
|
|
576
|
-
) => {
|
|
577
|
-
if (!operations.length) return;
|
|
578
|
-
|
|
579
|
-
this.log('Flushing operations', operations.length, 'to storage / sync');
|
|
580
|
-
// rewrite timestamps of all operations to now - this preserves
|
|
581
|
-
// the linear history of operations which are sent to the server.
|
|
582
|
-
// even if multiple batches are spun up in parallel and flushed
|
|
583
|
-
// after delay, the final operations in each one should reflect
|
|
584
|
-
// when the batch flushed, not when the changes were made.
|
|
585
|
-
// This also corresponds to user-observed behavior, since unconfirmed
|
|
586
|
-
// operations are applied universally after confirmed operations locally,
|
|
587
|
-
// so even operations which were made before a remote operation but
|
|
588
|
-
// have not been confirmed yet will appear to come after the remote one
|
|
589
|
-
// despite the provisional timestamp being earlier (see DocumentFamilyCache#computeView)
|
|
590
|
-
for (const op of operations) {
|
|
591
|
-
op.timestamp = this.meta.now;
|
|
429
|
+
if (!schema) {
|
|
430
|
+
return null;
|
|
592
431
|
}
|
|
593
|
-
await this.submitOperations(operations, meta);
|
|
594
|
-
};
|
|
595
432
|
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
{ undoable = true }: { undoable?: boolean } = {},
|
|
599
|
-
) => {
|
|
600
|
-
if (undoable) {
|
|
601
|
-
// FIXME: this is too slow and needs to be optimized.
|
|
602
|
-
this.undoHistory.addUndo(await this.createUndo(operations));
|
|
433
|
+
if (this.disposed) {
|
|
434
|
+
throw new Error('Cannot hydrate entity after store has been disposed');
|
|
603
435
|
}
|
|
604
|
-
await this.meta.insertLocalOperation(operations);
|
|
605
|
-
|
|
606
|
-
// confirm the operations
|
|
607
|
-
this.addDataToOpenCaches({ operations, baselines: [] });
|
|
608
436
|
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
await this.writeDocumentToStorage(oid);
|
|
615
|
-
}
|
|
616
|
-
|
|
617
|
-
// TODO: find a more efficient and straightforward way to update affected
|
|
618
|
-
// queries. Move to Metadata?
|
|
619
|
-
const affectedCollections = new Set(
|
|
620
|
-
operations.map(({ oid }) => decomposeOid(oid).collection),
|
|
621
|
-
);
|
|
622
|
-
this.context.log('changes to collections', affectedCollections);
|
|
623
|
-
this.context.entityEvents.emit(
|
|
624
|
-
'collectionsChanged',
|
|
625
|
-
Array.from(affectedCollections),
|
|
626
|
-
);
|
|
627
|
-
};
|
|
437
|
+
const metadataFamily = new EntityFamilyMetadata({
|
|
438
|
+
ctx: this.ctx,
|
|
439
|
+
onPendingOperations: this.onPendingOperations,
|
|
440
|
+
rootOid: oid,
|
|
441
|
+
});
|
|
628
442
|
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
443
|
+
// this is created synchronously so it's immediately available
|
|
444
|
+
// to begin capturing incoming data.
|
|
445
|
+
return new Entity({
|
|
446
|
+
ctx: this.ctx,
|
|
447
|
+
oid,
|
|
448
|
+
schema,
|
|
449
|
+
readonlyKeys,
|
|
450
|
+
files: this.files,
|
|
451
|
+
metadataFamily: metadataFamily,
|
|
452
|
+
patchCreator: this.meta.patchCreator,
|
|
453
|
+
events: this.events,
|
|
454
|
+
});
|
|
640
455
|
};
|
|
641
456
|
|
|
642
|
-
private
|
|
643
|
-
|
|
644
|
-
return async () => {
|
|
645
|
-
const redo = await this.createUndo(inverseOps);
|
|
646
|
-
await this.submitOperations(
|
|
647
|
-
inverseOps.map((op) => {
|
|
648
|
-
op.timestamp = this.meta.now;
|
|
649
|
-
return op;
|
|
650
|
-
}),
|
|
651
|
-
// undos should not generate their own undo operations
|
|
652
|
-
// since they already calculate redo as the inverse.
|
|
653
|
-
{ undoable: false },
|
|
654
|
-
);
|
|
655
|
-
return redo;
|
|
656
|
-
};
|
|
457
|
+
private onPendingOperations = (operations: Operation[]) => {
|
|
458
|
+
this.batcher.addOperations(operations);
|
|
657
459
|
};
|
|
658
460
|
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
const
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
};
|
|
670
|
-
|
|
671
|
-
deleteAll = async (
|
|
672
|
-
oids: ObjectIdentifier[],
|
|
673
|
-
options?: { undoable?: boolean },
|
|
674
|
-
) => {
|
|
675
|
-
const allOids = await Promise.all(
|
|
676
|
-
oids.map((oid) => this.meta.getAllDocumentRelatedOids(oid)),
|
|
461
|
+
/**
|
|
462
|
+
* Loads initial Entity data from storage
|
|
463
|
+
*/
|
|
464
|
+
private loadEntity = async (
|
|
465
|
+
entity: Entity,
|
|
466
|
+
opts?: { abort: AbortSignal },
|
|
467
|
+
): Promise<Entity | null> => {
|
|
468
|
+
const { operations, baselines } = await this.meta.getDocumentData(
|
|
469
|
+
entity.oid,
|
|
470
|
+
opts,
|
|
677
471
|
);
|
|
678
|
-
const patches = this.meta.patchCreator.createDeleteAll(allOids.flat());
|
|
679
|
-
// don't enqueue these, submit as distinct operation
|
|
680
|
-
await this.submitOperations(patches, options);
|
|
681
|
-
};
|
|
682
|
-
|
|
683
|
-
reset = async () => {
|
|
684
|
-
this.context.log('warn', 'Resetting local database');
|
|
685
|
-
await this.resetStoredDocuments();
|
|
686
|
-
await this.refreshAllCaches(true);
|
|
687
|
-
// this.context.entityEvents.emit(
|
|
688
|
-
// 'collectionsChanged',
|
|
689
|
-
// Object.keys(this.schema.collections),
|
|
690
|
-
// );
|
|
691
|
-
};
|
|
692
472
|
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
unsubscribe();
|
|
697
|
-
}
|
|
698
|
-
for (const cache of this.documentFamilyCaches.values()) {
|
|
699
|
-
cache.dispose();
|
|
473
|
+
if (!baselines.length && !Object.keys(operations).length) {
|
|
474
|
+
this.ctx.log('debug', 'No data found for entity', entity.oid);
|
|
475
|
+
return null;
|
|
700
476
|
}
|
|
701
|
-
this.documentFamilyCaches.clear();
|
|
702
|
-
await this.flushAllBatches();
|
|
703
|
-
};
|
|
704
477
|
|
|
705
|
-
|
|
706
|
-
this.log('debug', 'Reacting to rebases', baselines.length);
|
|
707
|
-
// update any open caches with new baseline. this will automatically
|
|
708
|
-
// drop operations before the baseline.
|
|
709
|
-
this.addBaselinesToOpenCaches(baselines, { isLocal: true });
|
|
710
|
-
};
|
|
478
|
+
this.ctx.log('debug', 'Loaded entity from storage', entity.oid);
|
|
711
479
|
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
const store = tx.objectStore(collection);
|
|
719
|
-
await storeRequestPromise(store.clear());
|
|
720
|
-
}
|
|
721
|
-
};
|
|
480
|
+
this.events.replace.invoke(this, {
|
|
481
|
+
oid: entity.oid,
|
|
482
|
+
baselines,
|
|
483
|
+
operations,
|
|
484
|
+
isLocal: false,
|
|
485
|
+
});
|
|
722
486
|
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
}
|
|
487
|
+
// only set the cache after loading.
|
|
488
|
+
// TODO: is this cache/promise stuff redundant?
|
|
489
|
+
this.cache.set(entity.oid, this.ctx.weakRef(entity));
|
|
490
|
+
this.entityFinalizationRegistry.register(entity, entity.oid);
|
|
491
|
+
|
|
492
|
+
return entity;
|
|
730
493
|
};
|
|
731
494
|
}
|