@verdant-web/store 2.7.6 → 2.7.7-next.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 -9
- package/dist/bundle/index.js.map +4 -4
- package/dist/cjs/AckInfoStore.d.ts +10 -0
- package/dist/cjs/AckInfoStore.js +26 -0
- package/dist/cjs/AckInfoStore.js.map +1 -0
- package/dist/cjs/BackoffScheduler.d.ts +19 -19
- package/dist/cjs/BackoffScheduler.js +44 -44
- package/dist/cjs/BaselinesStore.d.ts +10 -0
- package/dist/cjs/BaselinesStore.js +36 -0
- package/dist/cjs/BaselinesStore.js.map +1 -0
- package/dist/cjs/DocumentManager.d.ts +31 -28
- package/dist/cjs/DocumentManager.js +44 -44
- package/dist/cjs/Entity.d.ts +96 -0
- package/dist/cjs/Entity.js +345 -0
- package/dist/cjs/Entity.js.map +1 -0
- package/dist/cjs/EntityStore.d.ts +36 -0
- package/dist/cjs/EntityStore.js +172 -0
- package/dist/cjs/EntityStore.js.map +1 -0
- package/dist/cjs/FakeWeakRef.d.ts +11 -11
- package/dist/cjs/FakeWeakRef.js +18 -18
- package/dist/cjs/IDBService.d.ts +9 -9
- package/dist/cjs/IDBService.js +63 -63
- package/dist/cjs/LocalHistoryStore.d.ts +15 -0
- package/dist/cjs/LocalHistoryStore.js +53 -0
- package/dist/cjs/LocalHistoryStore.js.map +1 -0
- package/dist/cjs/LocalReplicaStore.d.ts +13 -0
- package/dist/cjs/LocalReplicaStore.js +49 -0
- package/dist/cjs/LocalReplicaStore.js.map +1 -0
- package/dist/cjs/MessageCreator.d.ts +19 -0
- package/dist/cjs/MessageCreator.js +104 -0
- package/dist/cjs/MessageCreator.js.map +1 -0
- package/dist/cjs/Metadata.d.ts +91 -0
- package/dist/cjs/Metadata.js +250 -0
- package/dist/cjs/Metadata.js.map +1 -0
- package/dist/cjs/OperationsStore.d.ts +38 -0
- package/dist/cjs/OperationsStore.js +146 -0
- package/dist/cjs/OperationsStore.js.map +1 -0
- package/dist/cjs/PresenceManager.d.ts +30 -0
- package/dist/cjs/PresenceManager.js +89 -0
- package/dist/cjs/PresenceManager.js.map +1 -0
- package/dist/cjs/Query.d.ts +26 -0
- package/dist/cjs/Query.js +86 -0
- package/dist/cjs/Query.js.map +1 -0
- package/dist/cjs/QueryMaker.d.ts +18 -0
- package/dist/cjs/QueryMaker.js +98 -0
- package/dist/cjs/QueryMaker.js.map +1 -0
- package/dist/cjs/QueryStore.d.ts +38 -0
- package/dist/cjs/QueryStore.js +146 -0
- package/dist/cjs/QueryStore.js.map +1 -0
- package/dist/cjs/SchemaStore.d.ts +9 -0
- package/dist/cjs/SchemaStore.js +35 -0
- package/dist/cjs/SchemaStore.js.map +1 -0
- package/dist/cjs/Storage.d.ts +147 -0
- package/dist/cjs/Storage.js +346 -0
- package/dist/cjs/Storage.js.map +1 -0
- package/dist/cjs/Sync.d.ts +117 -0
- package/dist/cjs/Sync.js +561 -0
- package/dist/cjs/Sync.js.map +1 -0
- package/dist/cjs/SyncHarness.d.ts +45 -0
- package/dist/cjs/SyncHarness.js +110 -0
- package/dist/cjs/SyncHarness.js.map +1 -0
- package/dist/cjs/UndoHistory.d.ts +16 -16
- package/dist/cjs/UndoHistory.js +50 -50
- package/dist/cjs/__tests__/batching.test.d.ts +1 -1
- package/dist/cjs/__tests__/batching.test.js +44 -44
- package/dist/cjs/__tests__/documents.test.d.ts +1 -1
- package/dist/cjs/__tests__/documents.test.js +415 -415
- package/dist/cjs/__tests__/fixtures/testStorage.d.ts +123 -122
- package/dist/cjs/__tests__/fixtures/testStorage.js +117 -115
- package/dist/cjs/__tests__/fixtures/testStorage.js.map +1 -1
- package/dist/cjs/__tests__/legacyOids.test.d.ts +1 -1
- package/dist/cjs/__tests__/legacyOids.test.js +309 -123
- package/dist/cjs/__tests__/legacyOids.test.js.map +1 -1
- package/dist/cjs/__tests__/mutations.test.d.ts +1 -1
- package/dist/cjs/__tests__/mutations.test.js +41 -41
- package/dist/cjs/__tests__/queries.test.d.ts +1 -1
- package/dist/cjs/__tests__/queries.test.js +318 -318
- package/dist/cjs/__tests__/setup/indexedDB.d.ts +1 -1
- package/dist/cjs/__tests__/setup/indexedDB.js +3 -3
- package/dist/cjs/__tests__/undo.test.d.ts +1 -1
- package/dist/cjs/__tests__/undo.test.js +86 -86
- package/dist/cjs/client/Client.d.ts +98 -99
- package/dist/cjs/client/Client.js +227 -227
- package/dist/cjs/client/ClientDescriptor.d.ts +76 -74
- package/dist/cjs/client/ClientDescriptor.js +197 -194
- package/dist/cjs/client/ClientDescriptor.js.map +1 -1
- package/dist/cjs/client/constants.d.ts +1 -0
- package/dist/cjs/client/constants.js +5 -0
- package/dist/cjs/client/constants.js.map +1 -0
- package/dist/cjs/constants.d.ts +1 -1
- package/dist/cjs/constants.js +4 -4
- package/dist/cjs/context.d.ts +29 -29
- package/dist/cjs/context.js +2 -2
- package/dist/cjs/entities/DocumentFamiliyCache.d.ts +90 -90
- package/dist/cjs/entities/DocumentFamiliyCache.js +282 -282
- package/dist/cjs/entities/Entity.d.ts +206 -206
- package/dist/cjs/entities/Entity.js +520 -520
- package/dist/cjs/entities/EntityStore.d.ts +85 -85
- package/dist/cjs/entities/EntityStore.js +479 -479
- package/dist/cjs/files/EntityFile.d.ts +31 -31
- package/dist/cjs/files/EntityFile.js +76 -76
- package/dist/cjs/files/FileManager.d.ts +42 -42
- package/dist/cjs/files/FileManager.js +152 -152
- package/dist/cjs/files/FileStorage.d.ts +28 -28
- package/dist/cjs/files/FileStorage.js +104 -104
- package/dist/cjs/files/utils.d.ts +8 -8
- package/dist/cjs/files/utils.js +55 -55
- package/dist/cjs/files/utils.test.d.ts +1 -1
- package/dist/cjs/files/utils.test.js +80 -80
- package/dist/cjs/idb.d.ts +11 -11
- package/dist/cjs/idb.js +111 -111
- package/dist/cjs/index.d.ts +21 -21
- package/dist/cjs/index.js +23 -23
- package/dist/cjs/indexes.d.ts +3 -0
- package/dist/cjs/indexes.js +20 -0
- package/dist/cjs/indexes.js.map +1 -0
- package/dist/cjs/metadata/AckInfoStore.d.ts +10 -10
- package/dist/cjs/metadata/AckInfoStore.js +25 -25
- package/dist/cjs/metadata/BaselinesStore.d.ts +33 -33
- package/dist/cjs/metadata/BaselinesStore.js +89 -89
- package/dist/cjs/metadata/LocalHistoryStore.d.ts +15 -0
- package/dist/cjs/metadata/LocalHistoryStore.js +53 -0
- package/dist/cjs/metadata/LocalHistoryStore.js.map +1 -0
- package/dist/cjs/metadata/LocalReplicaStore.d.ts +17 -17
- package/dist/cjs/metadata/LocalReplicaStore.js +47 -47
- package/dist/cjs/metadata/MessageCreator.d.ts +19 -19
- package/dist/cjs/metadata/MessageCreator.js +127 -127
- package/dist/cjs/metadata/Metadata.d.ts +112 -112
- package/dist/cjs/metadata/Metadata.js +352 -352
- package/dist/cjs/metadata/OperationsStore.d.ts +53 -53
- package/dist/cjs/metadata/OperationsStore.js +152 -152
- package/dist/cjs/metadata/SchemaStore.d.ts +9 -9
- package/dist/cjs/metadata/SchemaStore.js +38 -38
- package/dist/cjs/metadata/openMetadataDatabase.d.ts +19 -17
- package/dist/cjs/metadata/openMetadataDatabase.js +219 -158
- package/dist/cjs/metadata/openMetadataDatabase.js.map +1 -1
- package/dist/cjs/migration/db.d.ts +8 -8
- package/dist/cjs/migration/db.js +111 -111
- package/dist/cjs/migration/errors.d.ts +5 -5
- package/dist/cjs/migration/errors.js +11 -11
- package/dist/cjs/migration/openDatabase.d.ts +20 -20
- package/dist/cjs/migration/openDatabase.js +428 -428
- package/dist/cjs/migration/paths.d.ts +6 -6
- package/dist/cjs/migration/paths.js +52 -52
- package/dist/cjs/migration/paths.test.d.ts +1 -1
- package/dist/cjs/migration/paths.test.js +90 -90
- package/dist/cjs/openDocumentDatabase.d.ts +12 -0
- package/dist/cjs/openDocumentDatabase.js +432 -0
- package/dist/cjs/openDocumentDatabase.js.map +1 -0
- package/dist/cjs/queries/BaseQuery.d.ts +53 -53
- package/dist/cjs/queries/BaseQuery.js +135 -135
- package/dist/cjs/queries/CollectionQueries.d.ts +55 -55
- package/dist/cjs/queries/CollectionQueries.js +91 -91
- package/dist/cjs/queries/FindAllQuery.d.ts +12 -12
- package/dist/cjs/queries/FindAllQuery.js +42 -42
- package/dist/cjs/queries/FindInfiniteQuery.d.ts +19 -19
- package/dist/cjs/queries/FindInfiniteQuery.js +69 -69
- package/dist/cjs/queries/FindOneQuery.d.ts +12 -12
- package/dist/cjs/queries/FindOneQuery.js +42 -42
- package/dist/cjs/queries/FindPageQuery.d.ts +24 -24
- package/dist/cjs/queries/FindPageQuery.js +76 -76
- package/dist/cjs/queries/GetQuery.d.ts +10 -10
- package/dist/cjs/queries/GetQuery.js +29 -29
- package/dist/cjs/queries/LiveQuery.d.ts +28 -0
- package/dist/cjs/queries/LiveQuery.js +96 -0
- package/dist/cjs/queries/LiveQuery.js.map +1 -0
- package/dist/cjs/queries/LiveQueryMaker.d.ts +15 -0
- package/dist/cjs/queries/LiveQueryMaker.js +42 -0
- package/dist/cjs/queries/LiveQueryMaker.js.map +1 -0
- package/dist/cjs/queries/LiveQueryStore.d.ts +33 -0
- package/dist/cjs/queries/LiveQueryStore.js +85 -0
- package/dist/cjs/queries/LiveQueryStore.js.map +1 -0
- package/dist/cjs/queries/Query.d.ts +18 -0
- package/dist/cjs/queries/Query.js +36 -0
- package/dist/cjs/queries/Query.js.map +1 -0
- package/dist/cjs/queries/QueryCache.d.ts +16 -16
- package/dist/cjs/queries/QueryCache.js +41 -41
- package/dist/cjs/queries/QueryMaker.d.ts +14 -0
- package/dist/cjs/queries/QueryMaker.js +42 -0
- package/dist/cjs/queries/QueryMaker.js.map +1 -0
- package/dist/cjs/queries/QueryStore.d.ts +35 -0
- package/dist/cjs/queries/QueryStore.js +130 -0
- package/dist/cjs/queries/QueryStore.js.map +1 -0
- package/dist/cjs/queries/dbQueries.d.ts +22 -22
- package/dist/cjs/queries/dbQueries.js +125 -125
- package/dist/cjs/queries/keys.d.ts +10 -10
- package/dist/cjs/queries/keys.js +33 -33
- package/dist/cjs/queries/ranges.d.ts +2 -2
- package/dist/cjs/queries/ranges.js +69 -69
- package/dist/cjs/queries/types.d.ts +6 -6
- package/dist/cjs/queries/types.js +2 -2
- package/dist/cjs/queries/utils.d.ts +3 -3
- package/dist/cjs/queries/utils.js +24 -24
- package/dist/cjs/queries2/BaseQuery.d.ts +39 -0
- package/dist/cjs/queries2/BaseQuery.js +99 -0
- package/dist/cjs/queries2/BaseQuery.js.map +1 -0
- package/dist/cjs/queries2/CollectionQueries.d.ts +55 -0
- package/dist/cjs/queries2/CollectionQueries.js +83 -0
- package/dist/cjs/queries2/CollectionQueries.js.map +1 -0
- package/dist/cjs/queries2/FindAllQuery.d.ts +11 -0
- package/dist/cjs/queries2/FindAllQuery.js +34 -0
- package/dist/cjs/queries2/FindAllQuery.js.map +1 -0
- package/dist/cjs/queries2/FindInfiniteQuery.d.ts +18 -0
- package/dist/cjs/queries2/FindInfiniteQuery.js +61 -0
- package/dist/cjs/queries2/FindInfiniteQuery.js.map +1 -0
- package/dist/cjs/queries2/FindOneQuery.d.ts +11 -0
- package/dist/cjs/queries2/FindOneQuery.js +34 -0
- package/dist/cjs/queries2/FindOneQuery.js.map +1 -0
- package/dist/cjs/queries2/FindPageQuery.d.ts +23 -0
- package/dist/cjs/queries2/FindPageQuery.js +68 -0
- package/dist/cjs/queries2/FindPageQuery.js.map +1 -0
- package/dist/cjs/queries2/GetQuery.d.ts +10 -0
- package/dist/cjs/queries2/GetQuery.js +30 -0
- package/dist/cjs/queries2/GetQuery.js.map +1 -0
- package/dist/cjs/queries2/QueryCache.d.ts +16 -0
- package/dist/cjs/queries2/QueryCache.js +40 -0
- package/dist/cjs/queries2/QueryCache.js.map +1 -0
- package/dist/cjs/queries2/dbQueries.d.ts +22 -0
- package/dist/cjs/queries2/dbQueries.js +126 -0
- package/dist/cjs/queries2/dbQueries.js.map +1 -0
- package/dist/cjs/queries2/keys.d.ts +10 -0
- package/dist/cjs/queries2/keys.js +34 -0
- package/dist/cjs/queries2/keys.js.map +1 -0
- package/dist/cjs/queries2/ranges.d.ts +2 -0
- package/dist/cjs/queries2/ranges.js +70 -0
- package/dist/cjs/queries2/ranges.js.map +1 -0
- package/dist/cjs/queries2/types.d.ts +6 -0
- package/dist/cjs/queries2/types.js +3 -0
- package/dist/cjs/queries2/types.js.map +1 -0
- package/dist/cjs/queries2/utils.d.ts +1 -0
- package/dist/cjs/queries2/utils.js +20 -0
- package/dist/cjs/queries2/utils.js.map +1 -0
- package/dist/cjs/reactives/DocumentFamiliyCache.d.ts +47 -0
- package/dist/cjs/reactives/DocumentFamiliyCache.js +211 -0
- package/dist/cjs/reactives/DocumentFamiliyCache.js.map +1 -0
- package/dist/cjs/reactives/Entity.d.ts +191 -0
- package/dist/cjs/reactives/Entity.js +463 -0
- package/dist/cjs/reactives/Entity.js.map +1 -0
- package/dist/cjs/reactives/EntityStore.d.ts +84 -0
- package/dist/cjs/reactives/EntityStore.js +422 -0
- package/dist/cjs/reactives/EntityStore.js.map +1 -0
- package/dist/cjs/reactives/FakeWeakRef.d.ts +11 -0
- package/dist/cjs/reactives/FakeWeakRef.js +19 -0
- package/dist/cjs/reactives/FakeWeakRef.js.map +1 -0
- package/dist/cjs/sync/FileSync.d.ts +23 -23
- package/dist/cjs/sync/FileSync.js +84 -84
- package/dist/cjs/sync/Heartbeat.d.ts +25 -25
- package/dist/cjs/sync/Heartbeat.js +63 -63
- package/dist/cjs/sync/PresenceManager.d.ts +43 -43
- package/dist/cjs/sync/PresenceManager.js +119 -119
- package/dist/cjs/sync/PushPullSync.d.ts +36 -36
- package/dist/cjs/sync/PushPullSync.js +143 -143
- package/dist/cjs/sync/ServerSyncEndpointProvider.d.ts +27 -27
- package/dist/cjs/sync/ServerSyncEndpointProvider.js +64 -64
- package/dist/cjs/sync/Sync.d.ts +146 -146
- package/dist/cjs/sync/Sync.js +270 -270
- package/dist/cjs/sync/Sync.js.map +1 -1
- package/dist/cjs/sync/WebSocketSync.d.ts +41 -41
- package/dist/cjs/sync/WebSocketSync.js +190 -190
- package/dist/cjs/types.d.ts +12 -12
- package/dist/cjs/types.js +2 -2
- package/dist/cjs/utils/Disposable.d.ts +6 -6
- package/dist/cjs/utils/Disposable.js +18 -18
- package/dist/cjs/utils/Resolvable.d.ts +8 -8
- package/dist/cjs/utils/Resolvable.js +22 -22
- package/dist/cjs/v2/AckInfoStore.d.ts +10 -0
- package/dist/cjs/v2/AckInfoStore.js +26 -0
- package/dist/cjs/v2/AckInfoStore.js.map +1 -0
- package/dist/cjs/v2/BaselinesStore.d.ts +10 -0
- package/dist/cjs/v2/BaselinesStore.js +36 -0
- package/dist/cjs/v2/BaselinesStore.js.map +1 -0
- package/dist/cjs/v2/DocumentManager.d.ts +19 -0
- package/dist/cjs/v2/DocumentManager.js +47 -0
- package/dist/cjs/v2/DocumentManager.js.map +1 -0
- package/dist/cjs/v2/Entity.d.ts +87 -0
- package/dist/cjs/v2/Entity.js +305 -0
- package/dist/cjs/v2/Entity.js.map +1 -0
- package/dist/cjs/v2/EntityStore.d.ts +37 -0
- package/dist/cjs/v2/EntityStore.js +165 -0
- package/dist/cjs/v2/EntityStore.js.map +1 -0
- package/dist/cjs/v2/IDBService.d.ts +7 -0
- package/dist/cjs/v2/IDBService.js +26 -0
- package/dist/cjs/v2/IDBService.js.map +1 -0
- package/dist/cjs/v2/LocalHistoryStore.d.ts +15 -0
- package/dist/cjs/v2/LocalHistoryStore.js +53 -0
- package/dist/cjs/v2/LocalHistoryStore.js.map +1 -0
- package/dist/cjs/v2/LocalReplicaStore.d.ts +14 -0
- package/dist/cjs/v2/LocalReplicaStore.js +49 -0
- package/dist/cjs/v2/LocalReplicaStore.js.map +1 -0
- package/dist/cjs/v2/MessageCreator.d.ts +19 -0
- package/dist/cjs/v2/MessageCreator.js +99 -0
- package/dist/cjs/v2/MessageCreator.js.map +1 -0
- package/dist/cjs/v2/Metadata.d.ts +90 -0
- package/dist/cjs/v2/Metadata.js +251 -0
- package/dist/cjs/v2/Metadata.js.map +1 -0
- package/dist/cjs/v2/OperationsStore.d.ts +38 -0
- package/dist/cjs/v2/OperationsStore.js +146 -0
- package/dist/cjs/v2/OperationsStore.js.map +1 -0
- package/dist/cjs/v2/PresenceManager.d.ts +24 -0
- package/dist/cjs/v2/PresenceManager.js +73 -0
- package/dist/cjs/v2/PresenceManager.js.map +1 -0
- package/dist/cjs/v2/Query.d.ts +24 -0
- package/dist/cjs/v2/Query.js +79 -0
- package/dist/cjs/v2/Query.js.map +1 -0
- package/dist/cjs/v2/QueryMaker.d.ts +16 -0
- package/dist/cjs/v2/QueryMaker.js +84 -0
- package/dist/cjs/v2/QueryMaker.js.map +1 -0
- package/dist/cjs/v2/QueryStore.d.ts +26 -0
- package/dist/cjs/v2/QueryStore.js +136 -0
- package/dist/cjs/v2/QueryStore.js.map +1 -0
- package/dist/cjs/v2/SchemaStore.d.ts +9 -0
- package/dist/cjs/v2/SchemaStore.js +35 -0
- package/dist/cjs/v2/SchemaStore.js.map +1 -0
- package/dist/cjs/v2/Storage.d.ts +80 -0
- package/dist/cjs/v2/Storage.js +154 -0
- package/dist/cjs/v2/Storage.js.map +1 -0
- package/dist/cjs/v2/Sync.d.ts +42 -0
- package/dist/cjs/v2/Sync.js +98 -0
- package/dist/cjs/v2/Sync.js.map +1 -0
- package/dist/cjs/v2/SyncHarness.d.ts +45 -0
- package/dist/cjs/v2/SyncHarness.js +110 -0
- package/dist/cjs/v2/SyncHarness.js.map +1 -0
- package/dist/cjs/v2/__tests__/documents.test.d.ts +1 -0
- package/dist/cjs/v2/__tests__/documents.test.js +185 -0
- package/dist/cjs/v2/__tests__/documents.test.js.map +1 -0
- package/dist/cjs/v2/__tests__/fixtures/testStorage.d.ts +189 -0
- package/dist/cjs/v2/__tests__/fixtures/testStorage.js +93 -0
- package/dist/cjs/v2/__tests__/fixtures/testStorage.js.map +1 -0
- package/dist/cjs/v2/__tests__/queries.test.d.ts +1 -0
- package/dist/cjs/v2/__tests__/queries.test.js +92 -0
- package/dist/cjs/v2/__tests__/queries.test.js.map +1 -0
- package/dist/cjs/v2/__tests__/setup/indexedDB.d.ts +1 -0
- package/dist/cjs/v2/__tests__/setup/indexedDB.js +4 -0
- package/dist/cjs/v2/__tests__/setup/indexedDB.js.map +1 -0
- package/dist/cjs/v2/constants.d.ts +1 -0
- package/dist/cjs/v2/constants.js +5 -0
- package/dist/cjs/v2/constants.js.map +1 -0
- package/dist/cjs/v2/idb.d.ts +6 -0
- package/dist/cjs/v2/idb.js +71 -0
- package/dist/cjs/v2/idb.js.map +1 -0
- package/dist/cjs/v2/index.d.ts +6 -0
- package/dist/cjs/v2/index.js +14 -0
- package/dist/cjs/v2/index.js.map +1 -0
- package/dist/cjs/v2/indexes.d.ts +3 -0
- package/dist/cjs/v2/indexes.js +20 -0
- package/dist/cjs/v2/indexes.js.map +1 -0
- package/dist/cjs/v2/openDocumentDatabase.d.ts +9 -0
- package/dist/cjs/v2/openDocumentDatabase.js +100 -0
- package/dist/cjs/v2/openDocumentDatabase.js.map +1 -0
- package/dist/cjs/v2/types.d.ts +3 -0
- package/dist/cjs/v2/types.js +3 -0
- package/dist/cjs/v2/types.js.map +1 -0
- package/dist/cjs/vanilla.d.ts +1 -1
- package/dist/cjs/vanilla.js +27 -27
- package/dist/esm/AckInfoStore.d.ts +10 -0
- package/dist/esm/AckInfoStore.js +22 -0
- package/dist/esm/AckInfoStore.js.map +1 -0
- package/dist/esm/BackoffScheduler.d.ts +19 -19
- package/dist/esm/BackoffScheduler.js +39 -39
- package/dist/esm/BaselinesStore.d.ts +10 -0
- package/dist/esm/BaselinesStore.js +32 -0
- package/dist/esm/BaselinesStore.js.map +1 -0
- package/dist/esm/DocumentManager.d.ts +28 -28
- package/dist/esm/DocumentManager.js +40 -40
- package/dist/esm/Entity.d.ts +96 -0
- package/dist/esm/Entity.js +337 -0
- package/dist/esm/Entity.js.map +1 -0
- package/dist/esm/EntityStore.d.ts +36 -0
- package/dist/esm/EntityStore.js +168 -0
- package/dist/esm/EntityStore.js.map +1 -0
- package/dist/esm/FakeWeakRef.d.ts +11 -11
- package/dist/esm/FakeWeakRef.js +14 -14
- package/dist/esm/IDBService.d.ts +9 -9
- package/dist/esm/IDBService.js +59 -59
- package/dist/esm/LocalHistoryStore.d.ts +15 -0
- package/dist/esm/LocalHistoryStore.js +49 -0
- package/dist/esm/LocalHistoryStore.js.map +1 -0
- package/dist/esm/LocalReplicaStore.d.ts +13 -0
- package/dist/esm/LocalReplicaStore.js +42 -0
- package/dist/esm/LocalReplicaStore.js.map +1 -0
- package/dist/esm/MessageCreator.d.ts +19 -0
- package/dist/esm/MessageCreator.js +97 -0
- package/dist/esm/MessageCreator.js.map +1 -0
- package/dist/esm/Metadata.d.ts +91 -0
- package/dist/esm/Metadata.js +245 -0
- package/dist/esm/Metadata.js.map +1 -0
- package/dist/esm/OperationsStore.d.ts +38 -0
- package/dist/esm/OperationsStore.js +142 -0
- package/dist/esm/OperationsStore.js.map +1 -0
- package/dist/esm/PresenceManager.d.ts +30 -0
- package/dist/esm/PresenceManager.js +85 -0
- package/dist/esm/PresenceManager.js.map +1 -0
- package/dist/esm/Query.d.ts +26 -0
- package/dist/esm/Query.js +82 -0
- package/dist/esm/Query.js.map +1 -0
- package/dist/esm/QueryMaker.d.ts +18 -0
- package/dist/esm/QueryMaker.js +94 -0
- package/dist/esm/QueryMaker.js.map +1 -0
- package/dist/esm/QueryStore.d.ts +38 -0
- package/dist/esm/QueryStore.js +142 -0
- package/dist/esm/QueryStore.js.map +1 -0
- package/dist/esm/SchemaStore.d.ts +9 -0
- package/dist/esm/SchemaStore.js +31 -0
- package/dist/esm/SchemaStore.js.map +1 -0
- package/dist/esm/Storage.d.ts +147 -0
- package/dist/esm/Storage.js +341 -0
- package/dist/esm/Storage.js.map +1 -0
- package/dist/esm/Sync.d.ts +117 -0
- package/dist/esm/Sync.js +553 -0
- package/dist/esm/Sync.js.map +1 -0
- package/dist/esm/SyncHarness.d.ts +45 -0
- package/dist/esm/SyncHarness.js +105 -0
- package/dist/esm/SyncHarness.js.map +1 -0
- package/dist/esm/UndoHistory.d.ts +16 -16
- package/dist/esm/UndoHistory.js +46 -46
- package/dist/esm/__tests__/batching.test.d.ts +1 -1
- package/dist/esm/__tests__/batching.test.js +42 -42
- package/dist/esm/__tests__/documents.test.d.ts +1 -1
- package/dist/esm/__tests__/documents.test.js +413 -413
- package/dist/esm/__tests__/fixtures/testStorage.d.ts +123 -122
- package/dist/esm/__tests__/fixtures/testStorage.js +113 -111
- package/dist/esm/__tests__/fixtures/testStorage.js.map +1 -1
- package/dist/esm/__tests__/legacyOids.test.d.ts +1 -1
- package/dist/esm/__tests__/legacyOids.test.js +307 -121
- package/dist/esm/__tests__/legacyOids.test.js.map +1 -1
- package/dist/esm/__tests__/mutations.test.d.ts +1 -1
- package/dist/esm/__tests__/mutations.test.js +39 -39
- package/dist/esm/__tests__/queries.test.d.ts +1 -1
- package/dist/esm/__tests__/queries.test.js +316 -316
- package/dist/esm/__tests__/setup/indexedDB.d.ts +1 -1
- package/dist/esm/__tests__/setup/indexedDB.js +1 -1
- package/dist/esm/__tests__/undo.test.d.ts +1 -1
- package/dist/esm/__tests__/undo.test.js +84 -84
- package/dist/esm/client/Client.d.ts +99 -99
- package/dist/esm/client/Client.js +223 -223
- package/dist/esm/client/ClientDescriptor.d.ts +76 -74
- package/dist/esm/client/ClientDescriptor.js +193 -190
- package/dist/esm/client/ClientDescriptor.js.map +1 -1
- package/dist/esm/client/constants.d.ts +1 -0
- package/dist/esm/client/constants.js +2 -0
- package/dist/esm/client/constants.js.map +1 -0
- package/dist/esm/constants.d.ts +1 -1
- package/dist/esm/constants.js +1 -1
- package/dist/esm/context.d.ts +29 -29
- package/dist/esm/context.js +1 -1
- package/dist/esm/entities/DocumentFamiliyCache.d.ts +90 -90
- package/dist/esm/entities/DocumentFamiliyCache.js +278 -278
- package/dist/esm/entities/Entity.d.ts +206 -206
- package/dist/esm/entities/Entity.js +515 -515
- package/dist/esm/entities/EntityStore.d.ts +85 -85
- package/dist/esm/entities/EntityStore.js +475 -475
- package/dist/esm/files/EntityFile.d.ts +31 -31
- package/dist/esm/files/EntityFile.js +72 -72
- package/dist/esm/files/FileManager.d.ts +42 -42
- package/dist/esm/files/FileManager.js +148 -148
- package/dist/esm/files/FileStorage.d.ts +28 -28
- package/dist/esm/files/FileStorage.js +99 -99
- package/dist/esm/files/utils.d.ts +8 -8
- package/dist/esm/files/utils.js +46 -46
- package/dist/esm/files/utils.test.d.ts +1 -1
- package/dist/esm/files/utils.test.js +78 -78
- package/dist/esm/idb.d.ts +11 -11
- package/dist/esm/idb.js +100 -100
- package/dist/esm/index.d.ts +21 -21
- package/dist/esm/index.js +12 -12
- package/dist/esm/indexes.d.ts +3 -0
- package/dist/esm/indexes.js +15 -0
- package/dist/esm/indexes.js.map +1 -0
- package/dist/esm/metadata/AckInfoStore.d.ts +10 -10
- package/dist/esm/metadata/AckInfoStore.js +21 -21
- package/dist/esm/metadata/BaselinesStore.d.ts +33 -33
- package/dist/esm/metadata/BaselinesStore.js +85 -85
- package/dist/esm/metadata/LocalHistoryStore.d.ts +15 -0
- package/dist/esm/metadata/LocalHistoryStore.js +49 -0
- package/dist/esm/metadata/LocalHistoryStore.js.map +1 -0
- package/dist/esm/metadata/LocalReplicaStore.d.ts +17 -17
- package/dist/esm/metadata/LocalReplicaStore.js +40 -40
- package/dist/esm/metadata/MessageCreator.d.ts +19 -19
- package/dist/esm/metadata/MessageCreator.js +123 -123
- package/dist/esm/metadata/Metadata.d.ts +112 -112
- package/dist/esm/metadata/Metadata.js +348 -348
- package/dist/esm/metadata/OperationsStore.d.ts +53 -53
- package/dist/esm/metadata/OperationsStore.js +148 -148
- package/dist/esm/metadata/SchemaStore.d.ts +9 -9
- package/dist/esm/metadata/SchemaStore.js +34 -34
- package/dist/esm/metadata/openMetadataDatabase.d.ts +19 -17
- package/dist/esm/metadata/openMetadataDatabase.js +214 -153
- package/dist/esm/metadata/openMetadataDatabase.js.map +1 -1
- package/dist/esm/migration/db.d.ts +8 -8
- package/dist/esm/migration/db.js +103 -103
- package/dist/esm/migration/errors.d.ts +5 -5
- package/dist/esm/migration/errors.js +7 -7
- package/dist/esm/migration/openDatabase.d.ts +20 -20
- package/dist/esm/migration/openDatabase.js +423 -423
- package/dist/esm/migration/paths.d.ts +6 -6
- package/dist/esm/migration/paths.js +48 -48
- package/dist/esm/migration/paths.test.d.ts +1 -1
- package/dist/esm/migration/paths.test.js +88 -88
- package/dist/esm/openDocumentDatabase.d.ts +12 -0
- package/dist/esm/openDocumentDatabase.js +428 -0
- package/dist/esm/openDocumentDatabase.js.map +1 -0
- package/dist/esm/queries/BaseQuery.d.ts +53 -53
- package/dist/esm/queries/BaseQuery.js +131 -131
- package/dist/esm/queries/CollectionQueries.d.ts +55 -55
- package/dist/esm/queries/CollectionQueries.js +87 -87
- package/dist/esm/queries/FindAllQuery.d.ts +12 -12
- package/dist/esm/queries/FindAllQuery.js +38 -38
- package/dist/esm/queries/FindInfiniteQuery.d.ts +19 -19
- package/dist/esm/queries/FindInfiniteQuery.js +65 -65
- package/dist/esm/queries/FindOneQuery.d.ts +12 -12
- package/dist/esm/queries/FindOneQuery.js +38 -38
- package/dist/esm/queries/FindPageQuery.d.ts +24 -24
- package/dist/esm/queries/FindPageQuery.js +72 -72
- package/dist/esm/queries/GetQuery.d.ts +10 -10
- package/dist/esm/queries/GetQuery.js +25 -25
- package/dist/esm/queries/LiveQuery.d.ts +28 -0
- package/dist/esm/queries/LiveQuery.js +92 -0
- package/dist/esm/queries/LiveQuery.js.map +1 -0
- package/dist/esm/queries/LiveQueryMaker.d.ts +15 -0
- package/dist/esm/queries/LiveQueryMaker.js +38 -0
- package/dist/esm/queries/LiveQueryMaker.js.map +1 -0
- package/dist/esm/queries/LiveQueryStore.d.ts +33 -0
- package/dist/esm/queries/LiveQueryStore.js +81 -0
- package/dist/esm/queries/LiveQueryStore.js.map +1 -0
- package/dist/esm/queries/Query.d.ts +18 -0
- package/dist/esm/queries/Query.js +32 -0
- package/dist/esm/queries/Query.js.map +1 -0
- package/dist/esm/queries/QueryCache.d.ts +16 -16
- package/dist/esm/queries/QueryCache.js +37 -37
- package/dist/esm/queries/QueryMaker.d.ts +14 -0
- package/dist/esm/queries/QueryMaker.js +38 -0
- package/dist/esm/queries/QueryMaker.js.map +1 -0
- package/dist/esm/queries/QueryStore.d.ts +35 -0
- package/dist/esm/queries/QueryStore.js +126 -0
- package/dist/esm/queries/QueryStore.js.map +1 -0
- package/dist/esm/queries/dbQueries.d.ts +22 -22
- package/dist/esm/queries/dbQueries.js +119 -119
- package/dist/esm/queries/keys.d.ts +10 -10
- package/dist/esm/queries/keys.js +29 -29
- package/dist/esm/queries/ranges.d.ts +2 -2
- package/dist/esm/queries/ranges.js +65 -65
- package/dist/esm/queries/types.d.ts +6 -6
- package/dist/esm/queries/types.js +1 -1
- package/dist/esm/queries/utils.d.ts +3 -3
- package/dist/esm/queries/utils.js +19 -19
- package/dist/esm/queries2/BaseQuery.d.ts +39 -0
- package/dist/esm/queries2/BaseQuery.js +95 -0
- package/dist/esm/queries2/BaseQuery.js.map +1 -0
- package/dist/esm/queries2/CollectionQueries.d.ts +55 -0
- package/dist/esm/queries2/CollectionQueries.js +79 -0
- package/dist/esm/queries2/CollectionQueries.js.map +1 -0
- package/dist/esm/queries2/FindAllQuery.d.ts +11 -0
- package/dist/esm/queries2/FindAllQuery.js +30 -0
- package/dist/esm/queries2/FindAllQuery.js.map +1 -0
- package/dist/esm/queries2/FindInfiniteQuery.d.ts +18 -0
- package/dist/esm/queries2/FindInfiniteQuery.js +57 -0
- package/dist/esm/queries2/FindInfiniteQuery.js.map +1 -0
- package/dist/esm/queries2/FindOneQuery.d.ts +11 -0
- package/dist/esm/queries2/FindOneQuery.js +30 -0
- package/dist/esm/queries2/FindOneQuery.js.map +1 -0
- package/dist/esm/queries2/FindPageQuery.d.ts +23 -0
- package/dist/esm/queries2/FindPageQuery.js +64 -0
- package/dist/esm/queries2/FindPageQuery.js.map +1 -0
- package/dist/esm/queries2/GetQuery.d.ts +10 -0
- package/dist/esm/queries2/GetQuery.js +26 -0
- package/dist/esm/queries2/GetQuery.js.map +1 -0
- package/dist/esm/queries2/QueryCache.d.ts +16 -0
- package/dist/esm/queries2/QueryCache.js +36 -0
- package/dist/esm/queries2/QueryCache.js.map +1 -0
- package/dist/esm/queries2/dbQueries.d.ts +22 -0
- package/dist/esm/queries2/dbQueries.js +120 -0
- package/dist/esm/queries2/dbQueries.js.map +1 -0
- package/dist/esm/queries2/keys.d.ts +10 -0
- package/dist/esm/queries2/keys.js +30 -0
- package/dist/esm/queries2/keys.js.map +1 -0
- package/dist/esm/queries2/ranges.d.ts +2 -0
- package/dist/esm/queries2/ranges.js +66 -0
- package/dist/esm/queries2/ranges.js.map +1 -0
- package/dist/esm/queries2/types.d.ts +6 -0
- package/dist/esm/queries2/types.js +2 -0
- package/dist/esm/queries2/types.js.map +1 -0
- package/dist/esm/queries2/utils.d.ts +1 -0
- package/dist/esm/queries2/utils.js +16 -0
- package/dist/esm/queries2/utils.js.map +1 -0
- package/dist/esm/reactives/DocumentFamiliyCache.d.ts +47 -0
- package/dist/esm/reactives/DocumentFamiliyCache.js +207 -0
- package/dist/esm/reactives/DocumentFamiliyCache.js.map +1 -0
- package/dist/esm/reactives/Entity.d.ts +191 -0
- package/dist/esm/reactives/Entity.js +458 -0
- package/dist/esm/reactives/Entity.js.map +1 -0
- package/dist/esm/reactives/EntityStore.d.ts +84 -0
- package/dist/esm/reactives/EntityStore.js +418 -0
- package/dist/esm/reactives/EntityStore.js.map +1 -0
- package/dist/esm/reactives/FakeWeakRef.d.ts +11 -0
- package/dist/esm/reactives/FakeWeakRef.js +15 -0
- package/dist/esm/reactives/FakeWeakRef.js.map +1 -0
- package/dist/esm/sync/FileSync.d.ts +23 -23
- package/dist/esm/sync/FileSync.js +80 -80
- package/dist/esm/sync/Heartbeat.d.ts +25 -25
- package/dist/esm/sync/Heartbeat.js +59 -59
- package/dist/esm/sync/PresenceManager.d.ts +43 -43
- package/dist/esm/sync/PresenceManager.js +115 -115
- package/dist/esm/sync/PushPullSync.d.ts +36 -36
- package/dist/esm/sync/PushPullSync.js +139 -139
- package/dist/esm/sync/ServerSyncEndpointProvider.d.ts +27 -27
- package/dist/esm/sync/ServerSyncEndpointProvider.js +57 -57
- package/dist/esm/sync/Sync.d.ts +146 -146
- package/dist/esm/sync/Sync.js +265 -265
- package/dist/esm/sync/Sync.js.map +1 -1
- package/dist/esm/sync/{WebSocketSync.d.ts → WebsocketSync.d.ts} +41 -41
- package/dist/esm/sync/{WebSocketSync.js → WebsocketSync.js} +186 -186
- package/dist/esm/types.d.ts +12 -12
- package/dist/esm/types.js +1 -1
- package/dist/esm/utils/Disposable.d.ts +6 -6
- package/dist/esm/utils/Disposable.js +14 -14
- package/dist/esm/utils/Resolvable.d.ts +8 -8
- package/dist/esm/utils/Resolvable.js +18 -18
- package/dist/esm/v2/AckInfoStore.d.ts +10 -0
- package/dist/esm/v2/AckInfoStore.js +22 -0
- package/dist/esm/v2/AckInfoStore.js.map +1 -0
- package/dist/esm/v2/BaselinesStore.d.ts +10 -0
- package/dist/esm/v2/BaselinesStore.js +32 -0
- package/dist/esm/v2/BaselinesStore.js.map +1 -0
- package/dist/esm/v2/DocumentManager.d.ts +19 -0
- package/dist/esm/v2/DocumentManager.js +43 -0
- package/dist/esm/v2/DocumentManager.js.map +1 -0
- package/dist/esm/v2/Entity.d.ts +91 -0
- package/dist/esm/v2/Entity.js +305 -0
- package/dist/esm/v2/Entity.js.map +1 -0
- package/dist/esm/v2/EntityStore.d.ts +37 -0
- package/dist/esm/v2/EntityStore.js +166 -0
- package/dist/esm/v2/EntityStore.js.map +1 -0
- package/dist/esm/v2/IDBService.d.ts +7 -0
- package/dist/esm/v2/IDBService.js +22 -0
- package/dist/esm/v2/IDBService.js.map +1 -0
- package/dist/esm/v2/LocalHistoryStore.d.ts +15 -0
- package/dist/esm/v2/LocalHistoryStore.js +49 -0
- package/dist/esm/v2/LocalHistoryStore.js.map +1 -0
- package/dist/esm/v2/LocalReplicaStore.d.ts +14 -0
- package/dist/esm/v2/LocalReplicaStore.js +42 -0
- package/dist/esm/v2/LocalReplicaStore.js.map +1 -0
- package/dist/esm/v2/MessageCreator.d.ts +19 -0
- package/dist/esm/v2/MessageCreator.js +92 -0
- package/dist/esm/v2/MessageCreator.js.map +1 -0
- package/dist/esm/v2/Metadata.d.ts +90 -0
- package/dist/esm/v2/Metadata.js +246 -0
- package/dist/esm/v2/Metadata.js.map +1 -0
- package/dist/esm/v2/OperationsStore.d.ts +38 -0
- package/dist/esm/v2/OperationsStore.js +142 -0
- package/dist/esm/v2/OperationsStore.js.map +1 -0
- package/dist/esm/v2/PresenceManager.d.ts +24 -0
- package/dist/esm/v2/PresenceManager.js +69 -0
- package/dist/esm/v2/PresenceManager.js.map +1 -0
- package/dist/esm/v2/Query.d.ts +24 -0
- package/dist/esm/v2/Query.js +75 -0
- package/dist/esm/v2/Query.js.map +1 -0
- package/dist/esm/v2/QueryMaker.d.ts +16 -0
- package/dist/esm/v2/QueryMaker.js +80 -0
- package/dist/esm/v2/QueryMaker.js.map +1 -0
- package/dist/esm/v2/QueryStore.d.ts +26 -0
- package/dist/esm/v2/QueryStore.js +132 -0
- package/dist/esm/v2/QueryStore.js.map +1 -0
- package/dist/esm/v2/SchemaStore.d.ts +9 -0
- package/dist/esm/v2/SchemaStore.js +31 -0
- package/dist/esm/v2/SchemaStore.js.map +1 -0
- package/dist/esm/v2/Storage.d.ts +80 -0
- package/dist/esm/v2/Storage.js +149 -0
- package/dist/esm/v2/Storage.js.map +1 -0
- package/dist/esm/v2/Sync.d.ts +42 -0
- package/dist/esm/v2/Sync.js +94 -0
- package/dist/esm/v2/Sync.js.map +1 -0
- package/dist/esm/v2/SyncHarness.d.ts +45 -0
- package/dist/esm/v2/SyncHarness.js +105 -0
- package/dist/esm/v2/SyncHarness.js.map +1 -0
- package/dist/esm/v2/__tests__/documents.test.d.ts +1 -0
- package/dist/esm/v2/__tests__/documents.test.js +208 -0
- package/dist/esm/v2/__tests__/documents.test.js.map +1 -0
- package/dist/esm/v2/__tests__/fixtures/testStorage.d.ts +223 -0
- package/dist/esm/v2/__tests__/fixtures/testStorage.js +106 -0
- package/dist/esm/v2/__tests__/fixtures/testStorage.js.map +1 -0
- package/dist/esm/v2/__tests__/queries.test.d.ts +1 -0
- package/dist/esm/v2/__tests__/queries.test.js +90 -0
- package/dist/esm/v2/__tests__/queries.test.js.map +1 -0
- package/dist/esm/v2/__tests__/setup/indexedDB.d.ts +1 -0
- package/dist/esm/v2/__tests__/setup/indexedDB.js +2 -0
- package/dist/esm/v2/__tests__/setup/indexedDB.js.map +1 -0
- package/dist/esm/v2/constants.d.ts +1 -0
- package/dist/esm/v2/constants.js +2 -0
- package/dist/esm/v2/constants.js.map +1 -0
- package/dist/esm/v2/idb.d.ts +6 -0
- package/dist/esm/v2/idb.js +65 -0
- package/dist/esm/v2/idb.js.map +1 -0
- package/dist/esm/v2/index.d.ts +6 -0
- package/dist/esm/v2/index.js +5 -0
- package/dist/esm/v2/index.js.map +1 -0
- package/dist/esm/v2/indexes.d.ts +3 -0
- package/dist/esm/v2/indexes.js +15 -0
- package/dist/esm/v2/indexes.js.map +1 -0
- package/dist/esm/v2/openDocumentDatabase.d.ts +9 -0
- package/dist/esm/v2/openDocumentDatabase.js +96 -0
- package/dist/esm/v2/openDocumentDatabase.js.map +1 -0
- package/dist/esm/v2/types.d.ts +3 -0
- package/dist/esm/v2/types.js +2 -0
- package/dist/esm/v2/types.js.map +1 -0
- package/dist/esm/vanilla.d.ts +1 -1
- package/dist/esm/vanilla.js +2 -2
- package/dist/tsconfig-cjs.tsbuildinfo +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +2 -2
- package/src/__tests__/fixtures/testStorage.ts +8 -1
- package/src/__tests__/legacyOids.test.ts +240 -49
- package/src/client/ClientDescriptor.ts +6 -0
- package/src/client/constants.ts +1 -0
- package/src/metadata/openMetadataDatabase.ts +65 -2
- package/src/sync/Sync.ts +1 -0
- /package/dist/esm/sync/{WebSocketSync.js.map → WebsocketSync.js.map} +0 -0
|
@@ -1,476 +1,476 @@
|
|
|
1
|
-
import { assert, assignOid, Batcher, decomposeOid, generateId, getIndexValues, getOidRoot, getUndoOperations, groupBaselinesByRootOid, groupPatchesByIdentifier, groupPatchesByRootOid, removeOidsFromAllSubObjects, } from '@verdant-web/common';
|
|
2
|
-
import { processValueFiles } from '../files/utils.js';
|
|
3
|
-
import { storeRequestPromise } from '../idb.js';
|
|
4
|
-
import { DocumentFamilyCache } from './DocumentFamiliyCache.js';
|
|
5
|
-
const DEFAULT_BATCH_KEY = '@@default';
|
|
6
|
-
export class EntityStore {
|
|
7
|
-
get log() {
|
|
8
|
-
return this.context.log;
|
|
9
|
-
}
|
|
10
|
-
get db() {
|
|
11
|
-
return this.context.documentDb;
|
|
12
|
-
}
|
|
13
|
-
get undoHistory() {
|
|
14
|
-
return this.context.undoHistory;
|
|
15
|
-
}
|
|
16
|
-
get schema() {
|
|
17
|
-
return this.context.schema;
|
|
18
|
-
}
|
|
19
|
-
constructor({ context, meta, batchTimeout = 200, files, }) {
|
|
20
|
-
this.documentFamilyCaches = new Map();
|
|
21
|
-
this.unsubscribes = [];
|
|
22
|
-
this._disposed = false;
|
|
23
|
-
this.currentBatchKey = DEFAULT_BATCH_KEY;
|
|
24
|
-
this.setContext = (context) => {
|
|
25
|
-
this.context = context;
|
|
26
|
-
};
|
|
27
|
-
this.getDocumentSchema = (oid) => {
|
|
28
|
-
const { collection } = decomposeOid(oid);
|
|
29
|
-
if (!this.schema.collections[collection]) {
|
|
30
|
-
this.log('warn', `Missing schema for collection: ${collection}`);
|
|
31
|
-
return { schema: null, readonlyKeys: [] };
|
|
32
|
-
}
|
|
33
|
-
const schema = this.schema.collections[collection];
|
|
34
|
-
return {
|
|
35
|
-
readonlyKeys: [schema.primaryKey],
|
|
36
|
-
schema: {
|
|
37
|
-
type: 'object',
|
|
38
|
-
properties: schema.fields,
|
|
39
|
-
},
|
|
40
|
-
};
|
|
41
|
-
};
|
|
42
|
-
this.refreshFamilyCache = async (familyCache, dropUnconfirmed = false, dropAll = false) => {
|
|
43
|
-
// avoid writing to disposed db
|
|
44
|
-
if (this._disposed) {
|
|
45
|
-
this.context.log('debug', `EntityStore is disposed, not refreshing ${familyCache.oid} cache`);
|
|
46
|
-
return;
|
|
47
|
-
}
|
|
48
|
-
// metadata must be loaded from database to initialize family cache
|
|
49
|
-
const transaction = this.meta.createTransaction([
|
|
50
|
-
'baselines',
|
|
51
|
-
'operations',
|
|
52
|
-
]);
|
|
53
|
-
const baselines = [];
|
|
54
|
-
const operations = [];
|
|
55
|
-
await Promise.all([
|
|
56
|
-
this.meta.baselines.iterateOverAllForDocument(familyCache.oid, (baseline) => {
|
|
57
|
-
baselines.push(baseline);
|
|
58
|
-
}, {
|
|
59
|
-
transaction,
|
|
60
|
-
mode: 'readwrite',
|
|
61
|
-
}),
|
|
62
|
-
this.meta.operations.iterateOverAllOperationsForDocument(familyCache.oid, (op) => {
|
|
63
|
-
op.confirmed = true;
|
|
64
|
-
operations.push(op);
|
|
65
|
-
}, { transaction, mode: 'readwrite' }),
|
|
66
|
-
]);
|
|
67
|
-
familyCache.reset({
|
|
68
|
-
operations,
|
|
69
|
-
baselines,
|
|
70
|
-
dropExistingUnconfirmed: dropUnconfirmed,
|
|
71
|
-
dropAll,
|
|
72
|
-
});
|
|
73
|
-
};
|
|
74
|
-
this.openFamilyCache = async (oid) => {
|
|
75
|
-
const documentOid = getOidRoot(oid);
|
|
76
|
-
let familyCache = this.documentFamilyCaches.get(documentOid);
|
|
77
|
-
if (!familyCache) {
|
|
78
|
-
this.context.log('debug', 'opening family cache for', documentOid);
|
|
79
|
-
// metadata must be loaded from database to initialize family cache
|
|
80
|
-
familyCache = new DocumentFamilyCache({
|
|
81
|
-
oid: documentOid,
|
|
82
|
-
store: this,
|
|
83
|
-
context: this.context,
|
|
84
|
-
});
|
|
85
|
-
// PROBLEM: because the next line is async, it yields to
|
|
86
|
-
// queued promises which may need data from this cache,
|
|
87
|
-
// but the cache is empty. But if we move the set to
|
|
88
|
-
// after the async, we can clobber an existing cache
|
|
89
|
-
// with race conditions...
|
|
90
|
-
// So as an attempt to fix that, I've added a promise
|
|
91
|
-
// on DocumentFamilyCache which I manually resolve
|
|
92
|
-
// with setInitialized, then await initializedPromise
|
|
93
|
-
// further down even if there was a cache hit.
|
|
94
|
-
// Surely there is a better pattern for this.
|
|
95
|
-
// FIXME:
|
|
96
|
-
this.documentFamilyCaches.set(documentOid, familyCache);
|
|
97
|
-
await this.refreshFamilyCache(familyCache);
|
|
98
|
-
familyCache.setInitialized();
|
|
99
|
-
// this.unsubscribes.push(
|
|
100
|
-
// familyCache.subscribe('change:*', this.onEntityChange),
|
|
101
|
-
// );
|
|
102
|
-
// TODO: cleanup cache when all documents are disposed
|
|
103
|
-
}
|
|
104
|
-
await familyCache.initializedPromise;
|
|
105
|
-
return familyCache;
|
|
106
|
-
};
|
|
107
|
-
this.onEntityChange = async (oid) => {
|
|
108
|
-
// queueMicrotask(() => this.writeDocumentToStorage(oid));
|
|
109
|
-
};
|
|
110
|
-
this.writeDocumentToStorage = async (oid) => {
|
|
111
|
-
if (this._disposed) {
|
|
112
|
-
this.log('warn', 'EntityStore is disposed, not writing to storage');
|
|
113
|
-
return;
|
|
114
|
-
}
|
|
115
|
-
const rootOid = getOidRoot(oid);
|
|
116
|
-
const { id, collection } = decomposeOid(rootOid);
|
|
117
|
-
const entity = await this.get(rootOid);
|
|
118
|
-
if (this._disposed) {
|
|
119
|
-
this.log('warn', 'EntityStore is disposed, not writing to storage');
|
|
120
|
-
return;
|
|
121
|
-
}
|
|
122
|
-
const snapshot = entity === null || entity === void 0 ? void 0 : entity.getSnapshot();
|
|
123
|
-
if (snapshot) {
|
|
124
|
-
const stored = getIndexValues(this.schema.collections[collection], snapshot);
|
|
125
|
-
try {
|
|
126
|
-
const tx = this.db.transaction(collection, 'readwrite');
|
|
127
|
-
const store = tx.objectStore(collection);
|
|
128
|
-
await storeRequestPromise(store.put(stored));
|
|
129
|
-
this.log('info', '📝', 'wrote', collection, id, 'to storage', stored);
|
|
130
|
-
}
|
|
131
|
-
catch (err) {
|
|
132
|
-
// if the document can't be written, something's very wrong :(
|
|
133
|
-
// log the error and move on...
|
|
134
|
-
this.log("⚠️ 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", '\n', 'Invalid data:', JSON.stringify(stored));
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
else {
|
|
138
|
-
const tx = this.db.transaction(collection, 'readwrite');
|
|
139
|
-
const store = tx.objectStore(collection);
|
|
140
|
-
await storeRequestPromise(store.delete(id));
|
|
141
|
-
this.log('info', '❌', 'deleted', collection, id, 'from storage');
|
|
142
|
-
}
|
|
143
|
-
};
|
|
144
|
-
this.get = async (oid) => {
|
|
145
|
-
const familyCache = await this.openFamilyCache(oid);
|
|
146
|
-
const { schema, readonlyKeys } = this.getDocumentSchema(oid);
|
|
147
|
-
if (!schema) {
|
|
148
|
-
return null;
|
|
149
|
-
}
|
|
150
|
-
return familyCache.getEntity(oid, schema, undefined, readonlyKeys);
|
|
151
|
-
};
|
|
152
|
-
/**
|
|
153
|
-
* Advanced usage!
|
|
154
|
-
* Immediately returns an entity if it exists in the memory cache. An
|
|
155
|
-
* entity would be cached if it has been retrieved by a live query.
|
|
156
|
-
*/
|
|
157
|
-
this.getCached = (oid) => {
|
|
158
|
-
const cache = this.documentFamilyCaches.get(oid);
|
|
159
|
-
if (cache) {
|
|
160
|
-
const { schema, readonlyKeys } = this.getDocumentSchema(oid);
|
|
161
|
-
if (!schema) {
|
|
162
|
-
return null;
|
|
163
|
-
}
|
|
164
|
-
return cache.getEntity(oid, schema, undefined, readonlyKeys);
|
|
165
|
-
}
|
|
166
|
-
return null;
|
|
167
|
-
};
|
|
168
|
-
/**
|
|
169
|
-
* Creates a new document and returns an Entity for it. The created
|
|
170
|
-
* document is submitted to storage and sync.
|
|
171
|
-
*/
|
|
172
|
-
this.create = async (initial, oid, options) => {
|
|
173
|
-
// remove all OID associations from initial data
|
|
174
|
-
removeOidsFromAllSubObjects(initial);
|
|
175
|
-
// first grab any file and replace them with refs
|
|
176
|
-
const processed = processValueFiles(initial, this.files.add);
|
|
177
|
-
assignOid(processed, oid);
|
|
178
|
-
const operations = this.meta.patchCreator.createInitialize(processed, oid);
|
|
179
|
-
const familyCache = await this.openFamilyCache(oid);
|
|
180
|
-
familyCache.insertLocalOperations(operations);
|
|
181
|
-
// don't enqueue these, submit as distinct operation.
|
|
182
|
-
// we do this so it can be immediately queryable from storage...
|
|
183
|
-
// only holding it in memory would introduce lag before it shows up
|
|
184
|
-
// in other queries.
|
|
185
|
-
await this.submitOperations(operations, options);
|
|
186
|
-
const { schema, readonlyKeys } = this.getDocumentSchema(oid);
|
|
187
|
-
if (!schema) {
|
|
188
|
-
throw new Error(`Cannot create a document in the ${decomposeOid(oid).collection} collection; it is not defined in the current schema version.`);
|
|
189
|
-
}
|
|
190
|
-
return familyCache.getEntity(oid, schema, undefined, readonlyKeys);
|
|
191
|
-
};
|
|
192
|
-
this.addOperationsToOpenCaches = async (operations, info) => {
|
|
193
|
-
const operationsByOid = groupPatchesByRootOid(operations);
|
|
194
|
-
const oids = Object.keys(operationsByOid);
|
|
195
|
-
oids.forEach((oid) => {
|
|
196
|
-
const familyCache = this.documentFamilyCaches.get(oid);
|
|
197
|
-
if (familyCache) {
|
|
198
|
-
this.log('adding', info.confirmed ? 'confirmed' : 'unconfirmed', 'operations to cache', oid, operationsByOid[oid].length);
|
|
199
|
-
if (info.isLocal) {
|
|
200
|
-
familyCache.insertLocalOperations(operationsByOid[oid]);
|
|
201
|
-
}
|
|
202
|
-
else {
|
|
203
|
-
familyCache.insertOperations(operationsByOid[oid], info);
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
});
|
|
207
|
-
};
|
|
208
|
-
this.addBaselinesToOpenCaches = async (baselines, info) => {
|
|
209
|
-
const baselinesByOid = groupBaselinesByRootOid(baselines);
|
|
210
|
-
const oids = Object.keys(baselinesByOid);
|
|
211
|
-
oids.forEach((oid) => {
|
|
212
|
-
const cache = this.documentFamilyCaches.get(oid);
|
|
213
|
-
if (cache) {
|
|
214
|
-
this.log('adding', 'baselines to cache', oid, baselinesByOid[oid].length);
|
|
215
|
-
cache.insertBaselines(baselinesByOid[oid], info);
|
|
216
|
-
}
|
|
217
|
-
});
|
|
218
|
-
};
|
|
219
|
-
this.addDataToOpenCaches = ({ baselines, operations, reset, isLocal, }) => {
|
|
220
|
-
var _a, _b, _c, _d;
|
|
221
|
-
const baselinesByDocumentOid = groupBaselinesByRootOid(baselines);
|
|
222
|
-
const operationsByDocumentOid = groupPatchesByRootOid(operations);
|
|
223
|
-
const allDocumentOids = Array.from(new Set(Object.keys(baselinesByDocumentOid).concat(Object.keys(operationsByDocumentOid))));
|
|
224
|
-
for (const oid of allDocumentOids) {
|
|
225
|
-
const familyCache = this.documentFamilyCaches.get(oid);
|
|
226
|
-
if (familyCache) {
|
|
227
|
-
familyCache.addData({
|
|
228
|
-
operations: operationsByDocumentOid[oid] || [],
|
|
229
|
-
baselines: baselinesByDocumentOid[oid] || [],
|
|
230
|
-
reset,
|
|
231
|
-
isLocal,
|
|
232
|
-
});
|
|
233
|
-
this.log('debug', 'Added data to cache for', oid, (_b = (_a = operationsByDocumentOid[oid]) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0, 'operations', (_d = (_c = baselinesByDocumentOid[oid]) === null || _c === void 0 ? void 0 : _c.length) !== null && _d !== void 0 ? _d : 0, 'baselines');
|
|
234
|
-
}
|
|
235
|
-
else {
|
|
236
|
-
this.log('debug', 'Could not add data to cache for', oid, 'because it is not open');
|
|
237
|
-
}
|
|
238
|
-
}
|
|
239
|
-
return allDocumentOids;
|
|
240
|
-
};
|
|
241
|
-
this.addData = async ({ operations, baselines, reset, }) => {
|
|
242
|
-
if (this._disposed) {
|
|
243
|
-
this.log('warn', 'EntityStore is disposed, not adding data');
|
|
244
|
-
return;
|
|
245
|
-
}
|
|
246
|
-
// convert operations to tagged operations with confirmed = false
|
|
247
|
-
// while we process and store them. this is in-place so as to
|
|
248
|
-
// not allocate a bunch of objects...
|
|
249
|
-
const taggedOperations = operations;
|
|
250
|
-
for (const op of taggedOperations) {
|
|
251
|
-
op.confirmed = false;
|
|
252
|
-
}
|
|
253
|
-
let allDocumentOids = [];
|
|
254
|
-
// in a reset scenario, it only makes things confusing if we
|
|
255
|
-
// optimistically apply incoming operations, since the local
|
|
256
|
-
// history is out of sync
|
|
257
|
-
if (reset) {
|
|
258
|
-
this.log('Resetting local store to replicate remote synced data', baselines.length, 'baselines, and', operations.length, 'operations');
|
|
259
|
-
await this.meta.reset();
|
|
260
|
-
await this.resetStoredDocuments();
|
|
261
|
-
allDocumentOids = Array.from(new Set(baselines
|
|
262
|
-
.map((b) => getOidRoot(b.oid))
|
|
263
|
-
.concat(operations.map((o) => getOidRoot(o.oid)))));
|
|
264
|
-
}
|
|
265
|
-
else {
|
|
266
|
-
// first, synchronously add data to any open caches for immediate change propagation
|
|
267
|
-
allDocumentOids = this.addDataToOpenCaches({
|
|
268
|
-
operations: taggedOperations,
|
|
269
|
-
baselines,
|
|
270
|
-
reset,
|
|
271
|
-
});
|
|
272
|
-
}
|
|
273
|
-
// then, asynchronously add data to storage
|
|
274
|
-
await this.meta.insertRemoteBaselines(baselines);
|
|
275
|
-
await this.meta.insertRemoteOperations(operations);
|
|
276
|
-
if (reset) {
|
|
277
|
-
await this.refreshAllCaches(true, true);
|
|
278
|
-
}
|
|
279
|
-
// recompute all affected documents for querying
|
|
280
|
-
for (const oid of allDocumentOids) {
|
|
281
|
-
await this.writeDocumentToStorage(oid);
|
|
282
|
-
}
|
|
283
|
-
// notify active queries
|
|
284
|
-
const affectedCollections = Array.from(new Set(allDocumentOids.map((oid) => decomposeOid(oid).collection)));
|
|
285
|
-
this.context.log('changes to collections', affectedCollections);
|
|
286
|
-
this.context.entityEvents.emit('collectionsChanged', affectedCollections);
|
|
287
|
-
};
|
|
288
|
-
this.addLocalOperations = async (operations) => {
|
|
289
|
-
this.log('Adding local operations', operations.length);
|
|
290
|
-
this.addOperationsToOpenCaches(operations, {
|
|
291
|
-
isLocal: true,
|
|
292
|
-
confirmed: false,
|
|
293
|
-
});
|
|
294
|
-
this.operationBatcher.add({
|
|
295
|
-
key: this.currentBatchKey,
|
|
296
|
-
items: operations,
|
|
297
|
-
});
|
|
298
|
-
};
|
|
299
|
-
this.batch = ({ undoable = true, batchName = generateId(), max = null, timeout = this.defaultBatchTimeout, } = {}) => {
|
|
300
|
-
const internalBatch = this.operationBatcher.add({
|
|
301
|
-
key: batchName,
|
|
302
|
-
max,
|
|
303
|
-
timeout,
|
|
304
|
-
items: [],
|
|
305
|
-
userData: { undoable },
|
|
306
|
-
});
|
|
307
|
-
const externalApi = {
|
|
308
|
-
run: (fn) => {
|
|
309
|
-
// while the provided function runs, operations are forwarded
|
|
310
|
-
// to the new batch instead of default. this relies on the function
|
|
311
|
-
// being synchronous.
|
|
312
|
-
this.currentBatchKey = batchName;
|
|
313
|
-
fn();
|
|
314
|
-
this.currentBatchKey = DEFAULT_BATCH_KEY;
|
|
315
|
-
return externalApi;
|
|
316
|
-
},
|
|
317
|
-
flush: async () => {
|
|
318
|
-
// before running a batch, the default operations must be flushed
|
|
319
|
-
// this better preserves undo history behavior...
|
|
320
|
-
// if we left the default batch open while flushing a named batch,
|
|
321
|
-
// then the default batch would be flushed after the named batch,
|
|
322
|
-
// and the default batch could contain operations both prior and
|
|
323
|
-
// after the named batch. this would result in a confusing undo
|
|
324
|
-
// history where the first undo might reverse changes before and
|
|
325
|
-
// after a set of other changes.
|
|
326
|
-
await this.operationBatcher.flush(DEFAULT_BATCH_KEY);
|
|
327
|
-
return internalBatch.flush();
|
|
328
|
-
},
|
|
329
|
-
discard: () => {
|
|
330
|
-
this.operationBatcher.discard(batchName);
|
|
331
|
-
},
|
|
332
|
-
};
|
|
333
|
-
return externalApi;
|
|
334
|
-
};
|
|
335
|
-
/**
|
|
336
|
-
* @deprecated use `batch` instead
|
|
337
|
-
*/
|
|
338
|
-
this.flushPatches = async () => {
|
|
339
|
-
await this.operationBatcher.flush(this.currentBatchKey);
|
|
340
|
-
};
|
|
341
|
-
this.flushAllBatches = async () => {
|
|
342
|
-
await Promise.all(this.operationBatcher.flushAll());
|
|
343
|
-
};
|
|
344
|
-
this.flushOperations = async (operations, batchKey, meta) => {
|
|
345
|
-
if (!operations.length)
|
|
346
|
-
return;
|
|
347
|
-
this.log('Flushing operations', operations.length, 'to storage / sync');
|
|
348
|
-
// rewrite timestamps of all operations to now - this preserves
|
|
349
|
-
// the linear history of operations which are sent to the server.
|
|
350
|
-
// even if multiple batches are spun up in parallel and flushed
|
|
351
|
-
// after delay, the final operations in each one should reflect
|
|
352
|
-
// when the batch flushed, not when the changes were made.
|
|
353
|
-
// This also corresponds to user-observed behavior, since unconfirmed
|
|
354
|
-
// operations are applied universally after confirmed operations locally,
|
|
355
|
-
// so even operations which were made before a remote operation but
|
|
356
|
-
// have not been confirmed yet will appear to come after the remote one
|
|
357
|
-
// despite the provisional timestamp being earlier (see DocumentFamilyCache#computeView)
|
|
358
|
-
for (const op of operations) {
|
|
359
|
-
op.timestamp = this.meta.now;
|
|
360
|
-
}
|
|
361
|
-
await this.submitOperations(operations, meta);
|
|
362
|
-
};
|
|
363
|
-
this.submitOperations = async (operations, { undoable = true } = {}) => {
|
|
364
|
-
if (undoable) {
|
|
365
|
-
// FIXME: this is too slow and needs to be optimized.
|
|
366
|
-
this.undoHistory.addUndo(await this.createUndo(operations));
|
|
367
|
-
}
|
|
368
|
-
await this.meta.insertLocalOperation(operations);
|
|
369
|
-
// confirm the operations
|
|
370
|
-
this.addDataToOpenCaches({ operations, baselines: [] });
|
|
371
|
-
// recompute all affected documents for querying
|
|
372
|
-
const allDocumentOids = Array.from(new Set(operations.map((op) => getOidRoot(op.oid))));
|
|
373
|
-
for (const oid of allDocumentOids) {
|
|
374
|
-
await this.writeDocumentToStorage(oid);
|
|
375
|
-
}
|
|
376
|
-
// TODO: find a more efficient and straightforward way to update affected
|
|
377
|
-
// queries. Move to Metadata?
|
|
378
|
-
const affectedCollections = new Set(operations.map(({ oid }) => decomposeOid(oid).collection));
|
|
379
|
-
this.context.log('changes to collections', affectedCollections);
|
|
380
|
-
this.context.entityEvents.emit('collectionsChanged', Array.from(affectedCollections));
|
|
381
|
-
};
|
|
382
|
-
this.getInverseOperations = async (ops) => {
|
|
383
|
-
const grouped = groupPatchesByIdentifier(ops);
|
|
384
|
-
const inverseOps = [];
|
|
385
|
-
const getNow = () => this.meta.now;
|
|
386
|
-
for (const [oid, patches] of Object.entries(grouped)) {
|
|
387
|
-
const familyCache = await this.openFamilyCache(oid);
|
|
388
|
-
let { view, deleted } = familyCache.computeConfirmedView(oid);
|
|
389
|
-
const inverse = getUndoOperations(oid, view, patches, getNow);
|
|
390
|
-
inverseOps.unshift(...inverse);
|
|
391
|
-
}
|
|
392
|
-
return inverseOps;
|
|
393
|
-
};
|
|
394
|
-
this.createUndo = async (ops) => {
|
|
395
|
-
const inverseOps = await this.getInverseOperations(ops);
|
|
396
|
-
return async () => {
|
|
397
|
-
const redo = await this.createUndo(inverseOps);
|
|
398
|
-
await this.submitOperations(inverseOps.map((op) => {
|
|
399
|
-
op.timestamp = this.meta.now;
|
|
400
|
-
return op;
|
|
401
|
-
}),
|
|
402
|
-
// undos should not generate their own undo operations
|
|
403
|
-
// since they already calculate redo as the inverse.
|
|
404
|
-
{ undoable: false });
|
|
405
|
-
return redo;
|
|
406
|
-
};
|
|
407
|
-
};
|
|
408
|
-
this.delete = async (oid, options) => {
|
|
409
|
-
assert(oid === getOidRoot(oid), 'Only root documents may be deleted via client methods');
|
|
410
|
-
// we need to get all sub-object oids to delete alongside the root
|
|
411
|
-
const allOids = await this.meta.getAllDocumentRelatedOids(oid);
|
|
412
|
-
const patches = this.meta.patchCreator.createDeleteAll(allOids);
|
|
413
|
-
// don't enqueue these, submit as distinct operation
|
|
414
|
-
await this.submitOperations(patches, options);
|
|
415
|
-
};
|
|
416
|
-
this.deleteAll = async (oids, options) => {
|
|
417
|
-
const allOids = await Promise.all(oids.map((oid) => this.meta.getAllDocumentRelatedOids(oid)));
|
|
418
|
-
const patches = this.meta.patchCreator.createDeleteAll(allOids.flat());
|
|
419
|
-
// don't enqueue these, submit as distinct operation
|
|
420
|
-
await this.submitOperations(patches, options);
|
|
421
|
-
};
|
|
422
|
-
this.reset = async () => {
|
|
423
|
-
this.context.log('warn', 'Resetting local database');
|
|
424
|
-
await this.resetStoredDocuments();
|
|
425
|
-
await this.refreshAllCaches(true);
|
|
426
|
-
// this.context.entityEvents.emit(
|
|
427
|
-
// 'collectionsChanged',
|
|
428
|
-
// Object.keys(this.schema.collections),
|
|
429
|
-
// );
|
|
430
|
-
};
|
|
431
|
-
this.destroy = async () => {
|
|
432
|
-
this._disposed = true;
|
|
433
|
-
for (const unsubscribe of this.unsubscribes) {
|
|
434
|
-
unsubscribe();
|
|
435
|
-
}
|
|
436
|
-
for (const cache of this.documentFamilyCaches.values()) {
|
|
437
|
-
cache.dispose();
|
|
438
|
-
}
|
|
439
|
-
this.documentFamilyCaches.clear();
|
|
440
|
-
await this.flushAllBatches();
|
|
441
|
-
};
|
|
442
|
-
this.handleRebase = (baselines) => {
|
|
443
|
-
this.log('debug', 'Reacting to rebases', baselines.length);
|
|
444
|
-
// update any open caches with new baseline. this will automatically
|
|
445
|
-
// drop operations before the baseline.
|
|
446
|
-
this.addBaselinesToOpenCaches(baselines, { isLocal: true });
|
|
447
|
-
};
|
|
448
|
-
this.resetStoredDocuments = async () => {
|
|
449
|
-
const tx = this.db.transaction(Object.keys(this.schema.collections), 'readwrite');
|
|
450
|
-
for (const collection of Object.keys(this.schema.collections)) {
|
|
451
|
-
const store = tx.objectStore(collection);
|
|
452
|
-
await storeRequestPromise(store.clear());
|
|
453
|
-
}
|
|
454
|
-
};
|
|
455
|
-
this.refreshAllCaches = async (dropUnconfirmed = false, dropAll = false) => {
|
|
456
|
-
for (const [_, cache] of this.documentFamilyCaches) {
|
|
457
|
-
await this.refreshFamilyCache(cache, dropUnconfirmed, dropAll);
|
|
458
|
-
}
|
|
459
|
-
};
|
|
460
|
-
this.context = context;
|
|
461
|
-
this.defaultBatchTimeout = batchTimeout;
|
|
462
|
-
this.meta = meta;
|
|
463
|
-
this.files = files;
|
|
464
|
-
this.operationBatcher = new Batcher(this.flushOperations);
|
|
465
|
-
// initialize default batch
|
|
466
|
-
this.operationBatcher.add({
|
|
467
|
-
key: DEFAULT_BATCH_KEY,
|
|
468
|
-
items: [],
|
|
469
|
-
max: 100,
|
|
470
|
-
timeout: batchTimeout,
|
|
471
|
-
userData: { undoable: true },
|
|
472
|
-
});
|
|
473
|
-
this.unsubscribes.push(this.meta.subscribe('rebase', this.handleRebase));
|
|
474
|
-
}
|
|
475
|
-
}
|
|
1
|
+
import { assert, assignOid, Batcher, decomposeOid, generateId, getIndexValues, getOidRoot, getUndoOperations, groupBaselinesByRootOid, groupPatchesByIdentifier, groupPatchesByRootOid, removeOidsFromAllSubObjects, } from '@verdant-web/common';
|
|
2
|
+
import { processValueFiles } from '../files/utils.js';
|
|
3
|
+
import { storeRequestPromise } from '../idb.js';
|
|
4
|
+
import { DocumentFamilyCache } from './DocumentFamiliyCache.js';
|
|
5
|
+
const DEFAULT_BATCH_KEY = '@@default';
|
|
6
|
+
export class EntityStore {
|
|
7
|
+
get log() {
|
|
8
|
+
return this.context.log;
|
|
9
|
+
}
|
|
10
|
+
get db() {
|
|
11
|
+
return this.context.documentDb;
|
|
12
|
+
}
|
|
13
|
+
get undoHistory() {
|
|
14
|
+
return this.context.undoHistory;
|
|
15
|
+
}
|
|
16
|
+
get schema() {
|
|
17
|
+
return this.context.schema;
|
|
18
|
+
}
|
|
19
|
+
constructor({ context, meta, batchTimeout = 200, files, }) {
|
|
20
|
+
this.documentFamilyCaches = new Map();
|
|
21
|
+
this.unsubscribes = [];
|
|
22
|
+
this._disposed = false;
|
|
23
|
+
this.currentBatchKey = DEFAULT_BATCH_KEY;
|
|
24
|
+
this.setContext = (context) => {
|
|
25
|
+
this.context = context;
|
|
26
|
+
};
|
|
27
|
+
this.getDocumentSchema = (oid) => {
|
|
28
|
+
const { collection } = decomposeOid(oid);
|
|
29
|
+
if (!this.schema.collections[collection]) {
|
|
30
|
+
this.log('warn', `Missing schema for collection: ${collection}`);
|
|
31
|
+
return { schema: null, readonlyKeys: [] };
|
|
32
|
+
}
|
|
33
|
+
const schema = this.schema.collections[collection];
|
|
34
|
+
return {
|
|
35
|
+
readonlyKeys: [schema.primaryKey],
|
|
36
|
+
schema: {
|
|
37
|
+
type: 'object',
|
|
38
|
+
properties: schema.fields,
|
|
39
|
+
},
|
|
40
|
+
};
|
|
41
|
+
};
|
|
42
|
+
this.refreshFamilyCache = async (familyCache, dropUnconfirmed = false, dropAll = false) => {
|
|
43
|
+
// avoid writing to disposed db
|
|
44
|
+
if (this._disposed) {
|
|
45
|
+
this.context.log('debug', `EntityStore is disposed, not refreshing ${familyCache.oid} cache`);
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
// metadata must be loaded from database to initialize family cache
|
|
49
|
+
const transaction = this.meta.createTransaction([
|
|
50
|
+
'baselines',
|
|
51
|
+
'operations',
|
|
52
|
+
]);
|
|
53
|
+
const baselines = [];
|
|
54
|
+
const operations = [];
|
|
55
|
+
await Promise.all([
|
|
56
|
+
this.meta.baselines.iterateOverAllForDocument(familyCache.oid, (baseline) => {
|
|
57
|
+
baselines.push(baseline);
|
|
58
|
+
}, {
|
|
59
|
+
transaction,
|
|
60
|
+
mode: 'readwrite',
|
|
61
|
+
}),
|
|
62
|
+
this.meta.operations.iterateOverAllOperationsForDocument(familyCache.oid, (op) => {
|
|
63
|
+
op.confirmed = true;
|
|
64
|
+
operations.push(op);
|
|
65
|
+
}, { transaction, mode: 'readwrite' }),
|
|
66
|
+
]);
|
|
67
|
+
familyCache.reset({
|
|
68
|
+
operations,
|
|
69
|
+
baselines,
|
|
70
|
+
dropExistingUnconfirmed: dropUnconfirmed,
|
|
71
|
+
dropAll,
|
|
72
|
+
});
|
|
73
|
+
};
|
|
74
|
+
this.openFamilyCache = async (oid) => {
|
|
75
|
+
const documentOid = getOidRoot(oid);
|
|
76
|
+
let familyCache = this.documentFamilyCaches.get(documentOid);
|
|
77
|
+
if (!familyCache) {
|
|
78
|
+
this.context.log('debug', 'opening family cache for', documentOid);
|
|
79
|
+
// metadata must be loaded from database to initialize family cache
|
|
80
|
+
familyCache = new DocumentFamilyCache({
|
|
81
|
+
oid: documentOid,
|
|
82
|
+
store: this,
|
|
83
|
+
context: this.context,
|
|
84
|
+
});
|
|
85
|
+
// PROBLEM: because the next line is async, it yields to
|
|
86
|
+
// queued promises which may need data from this cache,
|
|
87
|
+
// but the cache is empty. But if we move the set to
|
|
88
|
+
// after the async, we can clobber an existing cache
|
|
89
|
+
// with race conditions...
|
|
90
|
+
// So as an attempt to fix that, I've added a promise
|
|
91
|
+
// on DocumentFamilyCache which I manually resolve
|
|
92
|
+
// with setInitialized, then await initializedPromise
|
|
93
|
+
// further down even if there was a cache hit.
|
|
94
|
+
// Surely there is a better pattern for this.
|
|
95
|
+
// FIXME:
|
|
96
|
+
this.documentFamilyCaches.set(documentOid, familyCache);
|
|
97
|
+
await this.refreshFamilyCache(familyCache);
|
|
98
|
+
familyCache.setInitialized();
|
|
99
|
+
// this.unsubscribes.push(
|
|
100
|
+
// familyCache.subscribe('change:*', this.onEntityChange),
|
|
101
|
+
// );
|
|
102
|
+
// TODO: cleanup cache when all documents are disposed
|
|
103
|
+
}
|
|
104
|
+
await familyCache.initializedPromise;
|
|
105
|
+
return familyCache;
|
|
106
|
+
};
|
|
107
|
+
this.onEntityChange = async (oid) => {
|
|
108
|
+
// queueMicrotask(() => this.writeDocumentToStorage(oid));
|
|
109
|
+
};
|
|
110
|
+
this.writeDocumentToStorage = async (oid) => {
|
|
111
|
+
if (this._disposed) {
|
|
112
|
+
this.log('warn', 'EntityStore is disposed, not writing to storage');
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
const rootOid = getOidRoot(oid);
|
|
116
|
+
const { id, collection } = decomposeOid(rootOid);
|
|
117
|
+
const entity = await this.get(rootOid);
|
|
118
|
+
if (this._disposed) {
|
|
119
|
+
this.log('warn', 'EntityStore is disposed, not writing to storage');
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
const snapshot = entity === null || entity === void 0 ? void 0 : entity.getSnapshot();
|
|
123
|
+
if (snapshot) {
|
|
124
|
+
const stored = getIndexValues(this.schema.collections[collection], snapshot);
|
|
125
|
+
try {
|
|
126
|
+
const tx = this.db.transaction(collection, 'readwrite');
|
|
127
|
+
const store = tx.objectStore(collection);
|
|
128
|
+
await storeRequestPromise(store.put(stored));
|
|
129
|
+
this.log('info', '📝', 'wrote', collection, id, 'to storage', stored);
|
|
130
|
+
}
|
|
131
|
+
catch (err) {
|
|
132
|
+
// if the document can't be written, something's very wrong :(
|
|
133
|
+
// log the error and move on...
|
|
134
|
+
this.log("⚠️ 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", '\n', 'Invalid data:', JSON.stringify(stored));
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
else {
|
|
138
|
+
const tx = this.db.transaction(collection, 'readwrite');
|
|
139
|
+
const store = tx.objectStore(collection);
|
|
140
|
+
await storeRequestPromise(store.delete(id));
|
|
141
|
+
this.log('info', '❌', 'deleted', collection, id, 'from storage');
|
|
142
|
+
}
|
|
143
|
+
};
|
|
144
|
+
this.get = async (oid) => {
|
|
145
|
+
const familyCache = await this.openFamilyCache(oid);
|
|
146
|
+
const { schema, readonlyKeys } = this.getDocumentSchema(oid);
|
|
147
|
+
if (!schema) {
|
|
148
|
+
return null;
|
|
149
|
+
}
|
|
150
|
+
return familyCache.getEntity(oid, schema, undefined, readonlyKeys);
|
|
151
|
+
};
|
|
152
|
+
/**
|
|
153
|
+
* Advanced usage!
|
|
154
|
+
* Immediately returns an entity if it exists in the memory cache. An
|
|
155
|
+
* entity would be cached if it has been retrieved by a live query.
|
|
156
|
+
*/
|
|
157
|
+
this.getCached = (oid) => {
|
|
158
|
+
const cache = this.documentFamilyCaches.get(oid);
|
|
159
|
+
if (cache) {
|
|
160
|
+
const { schema, readonlyKeys } = this.getDocumentSchema(oid);
|
|
161
|
+
if (!schema) {
|
|
162
|
+
return null;
|
|
163
|
+
}
|
|
164
|
+
return cache.getEntity(oid, schema, undefined, readonlyKeys);
|
|
165
|
+
}
|
|
166
|
+
return null;
|
|
167
|
+
};
|
|
168
|
+
/**
|
|
169
|
+
* Creates a new document and returns an Entity for it. The created
|
|
170
|
+
* document is submitted to storage and sync.
|
|
171
|
+
*/
|
|
172
|
+
this.create = async (initial, oid, options) => {
|
|
173
|
+
// remove all OID associations from initial data
|
|
174
|
+
removeOidsFromAllSubObjects(initial);
|
|
175
|
+
// first grab any file and replace them with refs
|
|
176
|
+
const processed = processValueFiles(initial, this.files.add);
|
|
177
|
+
assignOid(processed, oid);
|
|
178
|
+
const operations = this.meta.patchCreator.createInitialize(processed, oid);
|
|
179
|
+
const familyCache = await this.openFamilyCache(oid);
|
|
180
|
+
familyCache.insertLocalOperations(operations);
|
|
181
|
+
// don't enqueue these, submit as distinct operation.
|
|
182
|
+
// we do this so it can be immediately queryable from storage...
|
|
183
|
+
// only holding it in memory would introduce lag before it shows up
|
|
184
|
+
// in other queries.
|
|
185
|
+
await this.submitOperations(operations, options);
|
|
186
|
+
const { schema, readonlyKeys } = this.getDocumentSchema(oid);
|
|
187
|
+
if (!schema) {
|
|
188
|
+
throw new Error(`Cannot create a document in the ${decomposeOid(oid).collection} collection; it is not defined in the current schema version.`);
|
|
189
|
+
}
|
|
190
|
+
return familyCache.getEntity(oid, schema, undefined, readonlyKeys);
|
|
191
|
+
};
|
|
192
|
+
this.addOperationsToOpenCaches = async (operations, info) => {
|
|
193
|
+
const operationsByOid = groupPatchesByRootOid(operations);
|
|
194
|
+
const oids = Object.keys(operationsByOid);
|
|
195
|
+
oids.forEach((oid) => {
|
|
196
|
+
const familyCache = this.documentFamilyCaches.get(oid);
|
|
197
|
+
if (familyCache) {
|
|
198
|
+
this.log('adding', info.confirmed ? 'confirmed' : 'unconfirmed', 'operations to cache', oid, operationsByOid[oid].length);
|
|
199
|
+
if (info.isLocal) {
|
|
200
|
+
familyCache.insertLocalOperations(operationsByOid[oid]);
|
|
201
|
+
}
|
|
202
|
+
else {
|
|
203
|
+
familyCache.insertOperations(operationsByOid[oid], info);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
});
|
|
207
|
+
};
|
|
208
|
+
this.addBaselinesToOpenCaches = async (baselines, info) => {
|
|
209
|
+
const baselinesByOid = groupBaselinesByRootOid(baselines);
|
|
210
|
+
const oids = Object.keys(baselinesByOid);
|
|
211
|
+
oids.forEach((oid) => {
|
|
212
|
+
const cache = this.documentFamilyCaches.get(oid);
|
|
213
|
+
if (cache) {
|
|
214
|
+
this.log('adding', 'baselines to cache', oid, baselinesByOid[oid].length);
|
|
215
|
+
cache.insertBaselines(baselinesByOid[oid], info);
|
|
216
|
+
}
|
|
217
|
+
});
|
|
218
|
+
};
|
|
219
|
+
this.addDataToOpenCaches = ({ baselines, operations, reset, isLocal, }) => {
|
|
220
|
+
var _a, _b, _c, _d;
|
|
221
|
+
const baselinesByDocumentOid = groupBaselinesByRootOid(baselines);
|
|
222
|
+
const operationsByDocumentOid = groupPatchesByRootOid(operations);
|
|
223
|
+
const allDocumentOids = Array.from(new Set(Object.keys(baselinesByDocumentOid).concat(Object.keys(operationsByDocumentOid))));
|
|
224
|
+
for (const oid of allDocumentOids) {
|
|
225
|
+
const familyCache = this.documentFamilyCaches.get(oid);
|
|
226
|
+
if (familyCache) {
|
|
227
|
+
familyCache.addData({
|
|
228
|
+
operations: operationsByDocumentOid[oid] || [],
|
|
229
|
+
baselines: baselinesByDocumentOid[oid] || [],
|
|
230
|
+
reset,
|
|
231
|
+
isLocal,
|
|
232
|
+
});
|
|
233
|
+
this.log('debug', 'Added data to cache for', oid, (_b = (_a = operationsByDocumentOid[oid]) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0, 'operations', (_d = (_c = baselinesByDocumentOid[oid]) === null || _c === void 0 ? void 0 : _c.length) !== null && _d !== void 0 ? _d : 0, 'baselines');
|
|
234
|
+
}
|
|
235
|
+
else {
|
|
236
|
+
this.log('debug', 'Could not add data to cache for', oid, 'because it is not open');
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
return allDocumentOids;
|
|
240
|
+
};
|
|
241
|
+
this.addData = async ({ operations, baselines, reset, }) => {
|
|
242
|
+
if (this._disposed) {
|
|
243
|
+
this.log('warn', 'EntityStore is disposed, not adding data');
|
|
244
|
+
return;
|
|
245
|
+
}
|
|
246
|
+
// convert operations to tagged operations with confirmed = false
|
|
247
|
+
// while we process and store them. this is in-place so as to
|
|
248
|
+
// not allocate a bunch of objects...
|
|
249
|
+
const taggedOperations = operations;
|
|
250
|
+
for (const op of taggedOperations) {
|
|
251
|
+
op.confirmed = false;
|
|
252
|
+
}
|
|
253
|
+
let allDocumentOids = [];
|
|
254
|
+
// in a reset scenario, it only makes things confusing if we
|
|
255
|
+
// optimistically apply incoming operations, since the local
|
|
256
|
+
// history is out of sync
|
|
257
|
+
if (reset) {
|
|
258
|
+
this.log('Resetting local store to replicate remote synced data', baselines.length, 'baselines, and', operations.length, 'operations');
|
|
259
|
+
await this.meta.reset();
|
|
260
|
+
await this.resetStoredDocuments();
|
|
261
|
+
allDocumentOids = Array.from(new Set(baselines
|
|
262
|
+
.map((b) => getOidRoot(b.oid))
|
|
263
|
+
.concat(operations.map((o) => getOidRoot(o.oid)))));
|
|
264
|
+
}
|
|
265
|
+
else {
|
|
266
|
+
// first, synchronously add data to any open caches for immediate change propagation
|
|
267
|
+
allDocumentOids = this.addDataToOpenCaches({
|
|
268
|
+
operations: taggedOperations,
|
|
269
|
+
baselines,
|
|
270
|
+
reset,
|
|
271
|
+
});
|
|
272
|
+
}
|
|
273
|
+
// then, asynchronously add data to storage
|
|
274
|
+
await this.meta.insertRemoteBaselines(baselines);
|
|
275
|
+
await this.meta.insertRemoteOperations(operations);
|
|
276
|
+
if (reset) {
|
|
277
|
+
await this.refreshAllCaches(true, true);
|
|
278
|
+
}
|
|
279
|
+
// recompute all affected documents for querying
|
|
280
|
+
for (const oid of allDocumentOids) {
|
|
281
|
+
await this.writeDocumentToStorage(oid);
|
|
282
|
+
}
|
|
283
|
+
// notify active queries
|
|
284
|
+
const affectedCollections = Array.from(new Set(allDocumentOids.map((oid) => decomposeOid(oid).collection)));
|
|
285
|
+
this.context.log('changes to collections', affectedCollections);
|
|
286
|
+
this.context.entityEvents.emit('collectionsChanged', affectedCollections);
|
|
287
|
+
};
|
|
288
|
+
this.addLocalOperations = async (operations) => {
|
|
289
|
+
this.log('Adding local operations', operations.length);
|
|
290
|
+
this.addOperationsToOpenCaches(operations, {
|
|
291
|
+
isLocal: true,
|
|
292
|
+
confirmed: false,
|
|
293
|
+
});
|
|
294
|
+
this.operationBatcher.add({
|
|
295
|
+
key: this.currentBatchKey,
|
|
296
|
+
items: operations,
|
|
297
|
+
});
|
|
298
|
+
};
|
|
299
|
+
this.batch = ({ undoable = true, batchName = generateId(), max = null, timeout = this.defaultBatchTimeout, } = {}) => {
|
|
300
|
+
const internalBatch = this.operationBatcher.add({
|
|
301
|
+
key: batchName,
|
|
302
|
+
max,
|
|
303
|
+
timeout,
|
|
304
|
+
items: [],
|
|
305
|
+
userData: { undoable },
|
|
306
|
+
});
|
|
307
|
+
const externalApi = {
|
|
308
|
+
run: (fn) => {
|
|
309
|
+
// while the provided function runs, operations are forwarded
|
|
310
|
+
// to the new batch instead of default. this relies on the function
|
|
311
|
+
// being synchronous.
|
|
312
|
+
this.currentBatchKey = batchName;
|
|
313
|
+
fn();
|
|
314
|
+
this.currentBatchKey = DEFAULT_BATCH_KEY;
|
|
315
|
+
return externalApi;
|
|
316
|
+
},
|
|
317
|
+
flush: async () => {
|
|
318
|
+
// before running a batch, the default operations must be flushed
|
|
319
|
+
// this better preserves undo history behavior...
|
|
320
|
+
// if we left the default batch open while flushing a named batch,
|
|
321
|
+
// then the default batch would be flushed after the named batch,
|
|
322
|
+
// and the default batch could contain operations both prior and
|
|
323
|
+
// after the named batch. this would result in a confusing undo
|
|
324
|
+
// history where the first undo might reverse changes before and
|
|
325
|
+
// after a set of other changes.
|
|
326
|
+
await this.operationBatcher.flush(DEFAULT_BATCH_KEY);
|
|
327
|
+
return internalBatch.flush();
|
|
328
|
+
},
|
|
329
|
+
discard: () => {
|
|
330
|
+
this.operationBatcher.discard(batchName);
|
|
331
|
+
},
|
|
332
|
+
};
|
|
333
|
+
return externalApi;
|
|
334
|
+
};
|
|
335
|
+
/**
|
|
336
|
+
* @deprecated use `batch` instead
|
|
337
|
+
*/
|
|
338
|
+
this.flushPatches = async () => {
|
|
339
|
+
await this.operationBatcher.flush(this.currentBatchKey);
|
|
340
|
+
};
|
|
341
|
+
this.flushAllBatches = async () => {
|
|
342
|
+
await Promise.all(this.operationBatcher.flushAll());
|
|
343
|
+
};
|
|
344
|
+
this.flushOperations = async (operations, batchKey, meta) => {
|
|
345
|
+
if (!operations.length)
|
|
346
|
+
return;
|
|
347
|
+
this.log('Flushing operations', operations.length, 'to storage / sync');
|
|
348
|
+
// rewrite timestamps of all operations to now - this preserves
|
|
349
|
+
// the linear history of operations which are sent to the server.
|
|
350
|
+
// even if multiple batches are spun up in parallel and flushed
|
|
351
|
+
// after delay, the final operations in each one should reflect
|
|
352
|
+
// when the batch flushed, not when the changes were made.
|
|
353
|
+
// This also corresponds to user-observed behavior, since unconfirmed
|
|
354
|
+
// operations are applied universally after confirmed operations locally,
|
|
355
|
+
// so even operations which were made before a remote operation but
|
|
356
|
+
// have not been confirmed yet will appear to come after the remote one
|
|
357
|
+
// despite the provisional timestamp being earlier (see DocumentFamilyCache#computeView)
|
|
358
|
+
for (const op of operations) {
|
|
359
|
+
op.timestamp = this.meta.now;
|
|
360
|
+
}
|
|
361
|
+
await this.submitOperations(operations, meta);
|
|
362
|
+
};
|
|
363
|
+
this.submitOperations = async (operations, { undoable = true } = {}) => {
|
|
364
|
+
if (undoable) {
|
|
365
|
+
// FIXME: this is too slow and needs to be optimized.
|
|
366
|
+
this.undoHistory.addUndo(await this.createUndo(operations));
|
|
367
|
+
}
|
|
368
|
+
await this.meta.insertLocalOperation(operations);
|
|
369
|
+
// confirm the operations
|
|
370
|
+
this.addDataToOpenCaches({ operations, baselines: [] });
|
|
371
|
+
// recompute all affected documents for querying
|
|
372
|
+
const allDocumentOids = Array.from(new Set(operations.map((op) => getOidRoot(op.oid))));
|
|
373
|
+
for (const oid of allDocumentOids) {
|
|
374
|
+
await this.writeDocumentToStorage(oid);
|
|
375
|
+
}
|
|
376
|
+
// TODO: find a more efficient and straightforward way to update affected
|
|
377
|
+
// queries. Move to Metadata?
|
|
378
|
+
const affectedCollections = new Set(operations.map(({ oid }) => decomposeOid(oid).collection));
|
|
379
|
+
this.context.log('changes to collections', affectedCollections);
|
|
380
|
+
this.context.entityEvents.emit('collectionsChanged', Array.from(affectedCollections));
|
|
381
|
+
};
|
|
382
|
+
this.getInverseOperations = async (ops) => {
|
|
383
|
+
const grouped = groupPatchesByIdentifier(ops);
|
|
384
|
+
const inverseOps = [];
|
|
385
|
+
const getNow = () => this.meta.now;
|
|
386
|
+
for (const [oid, patches] of Object.entries(grouped)) {
|
|
387
|
+
const familyCache = await this.openFamilyCache(oid);
|
|
388
|
+
let { view, deleted } = familyCache.computeConfirmedView(oid);
|
|
389
|
+
const inverse = getUndoOperations(oid, view, patches, getNow);
|
|
390
|
+
inverseOps.unshift(...inverse);
|
|
391
|
+
}
|
|
392
|
+
return inverseOps;
|
|
393
|
+
};
|
|
394
|
+
this.createUndo = async (ops) => {
|
|
395
|
+
const inverseOps = await this.getInverseOperations(ops);
|
|
396
|
+
return async () => {
|
|
397
|
+
const redo = await this.createUndo(inverseOps);
|
|
398
|
+
await this.submitOperations(inverseOps.map((op) => {
|
|
399
|
+
op.timestamp = this.meta.now;
|
|
400
|
+
return op;
|
|
401
|
+
}),
|
|
402
|
+
// undos should not generate their own undo operations
|
|
403
|
+
// since they already calculate redo as the inverse.
|
|
404
|
+
{ undoable: false });
|
|
405
|
+
return redo;
|
|
406
|
+
};
|
|
407
|
+
};
|
|
408
|
+
this.delete = async (oid, options) => {
|
|
409
|
+
assert(oid === getOidRoot(oid), 'Only root documents may be deleted via client methods');
|
|
410
|
+
// we need to get all sub-object oids to delete alongside the root
|
|
411
|
+
const allOids = await this.meta.getAllDocumentRelatedOids(oid);
|
|
412
|
+
const patches = this.meta.patchCreator.createDeleteAll(allOids);
|
|
413
|
+
// don't enqueue these, submit as distinct operation
|
|
414
|
+
await this.submitOperations(patches, options);
|
|
415
|
+
};
|
|
416
|
+
this.deleteAll = async (oids, options) => {
|
|
417
|
+
const allOids = await Promise.all(oids.map((oid) => this.meta.getAllDocumentRelatedOids(oid)));
|
|
418
|
+
const patches = this.meta.patchCreator.createDeleteAll(allOids.flat());
|
|
419
|
+
// don't enqueue these, submit as distinct operation
|
|
420
|
+
await this.submitOperations(patches, options);
|
|
421
|
+
};
|
|
422
|
+
this.reset = async () => {
|
|
423
|
+
this.context.log('warn', 'Resetting local database');
|
|
424
|
+
await this.resetStoredDocuments();
|
|
425
|
+
await this.refreshAllCaches(true);
|
|
426
|
+
// this.context.entityEvents.emit(
|
|
427
|
+
// 'collectionsChanged',
|
|
428
|
+
// Object.keys(this.schema.collections),
|
|
429
|
+
// );
|
|
430
|
+
};
|
|
431
|
+
this.destroy = async () => {
|
|
432
|
+
this._disposed = true;
|
|
433
|
+
for (const unsubscribe of this.unsubscribes) {
|
|
434
|
+
unsubscribe();
|
|
435
|
+
}
|
|
436
|
+
for (const cache of this.documentFamilyCaches.values()) {
|
|
437
|
+
cache.dispose();
|
|
438
|
+
}
|
|
439
|
+
this.documentFamilyCaches.clear();
|
|
440
|
+
await this.flushAllBatches();
|
|
441
|
+
};
|
|
442
|
+
this.handleRebase = (baselines) => {
|
|
443
|
+
this.log('debug', 'Reacting to rebases', baselines.length);
|
|
444
|
+
// update any open caches with new baseline. this will automatically
|
|
445
|
+
// drop operations before the baseline.
|
|
446
|
+
this.addBaselinesToOpenCaches(baselines, { isLocal: true });
|
|
447
|
+
};
|
|
448
|
+
this.resetStoredDocuments = async () => {
|
|
449
|
+
const tx = this.db.transaction(Object.keys(this.schema.collections), 'readwrite');
|
|
450
|
+
for (const collection of Object.keys(this.schema.collections)) {
|
|
451
|
+
const store = tx.objectStore(collection);
|
|
452
|
+
await storeRequestPromise(store.clear());
|
|
453
|
+
}
|
|
454
|
+
};
|
|
455
|
+
this.refreshAllCaches = async (dropUnconfirmed = false, dropAll = false) => {
|
|
456
|
+
for (const [_, cache] of this.documentFamilyCaches) {
|
|
457
|
+
await this.refreshFamilyCache(cache, dropUnconfirmed, dropAll);
|
|
458
|
+
}
|
|
459
|
+
};
|
|
460
|
+
this.context = context;
|
|
461
|
+
this.defaultBatchTimeout = batchTimeout;
|
|
462
|
+
this.meta = meta;
|
|
463
|
+
this.files = files;
|
|
464
|
+
this.operationBatcher = new Batcher(this.flushOperations);
|
|
465
|
+
// initialize default batch
|
|
466
|
+
this.operationBatcher.add({
|
|
467
|
+
key: DEFAULT_BATCH_KEY,
|
|
468
|
+
items: [],
|
|
469
|
+
max: 100,
|
|
470
|
+
timeout: batchTimeout,
|
|
471
|
+
userData: { undoable: true },
|
|
472
|
+
});
|
|
473
|
+
this.unsubscribes.push(this.meta.subscribe('rebase', this.handleRebase));
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
476
|
//# sourceMappingURL=EntityStore.js.map
|