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