@verdant-web/store 2.8.5 → 3.0.0

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