@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
@@ -2,494 +2,350 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.EntityStore = void 0;
4
4
  const common_1 = require("@verdant-web/common");
5
+ const Entity_js_1 = require("./Entity.js");
6
+ const Disposable_js_1 = require("../utils/Disposable.js");
7
+ const EntityMetadata_js_1 = require("./EntityMetadata.js");
8
+ const OperationBatcher_js_1 = require("./OperationBatcher.js");
9
+ const QueryableStorage_js_1 = require("../queries/QueryableStorage.js");
10
+ const weak_event_1 = require("weak-event");
5
11
  const utils_js_1 = require("../files/utils.js");
6
- const idb_js_1 = require("../idb.js");
7
- const DocumentFamiliyCache_js_1 = require("./DocumentFamiliyCache.js");
8
- const DEFAULT_BATCH_KEY = '@@default';
9
- class EntityStore {
10
- get log() {
11
- return this.context.log;
12
- }
13
- get db() {
14
- return this.context.documentDb;
15
- }
16
- get undoHistory() {
17
- return this.context.undoHistory;
18
- }
19
- get schema() {
20
- return this.context.schema;
21
- }
22
- constructor({ context, meta, batchTimeout = 200, files, }) {
23
- this.documentFamilyCaches = new Map();
24
- this.unsubscribes = [];
25
- this._disposed = false;
26
- this.currentBatchKey = DEFAULT_BATCH_KEY;
27
- this.setContext = (context) => {
28
- this.context = context;
12
+ var AbortReason;
13
+ (function (AbortReason) {
14
+ AbortReason[AbortReason["Reset"] = 0] = "Reset";
15
+ })(AbortReason || (AbortReason = {}));
16
+ class EntityStore extends Disposable_js_1.Disposable {
17
+ constructor({ ctx, meta, files, }) {
18
+ super();
19
+ this.events = {
20
+ add: new weak_event_1.WeakEvent(),
21
+ replace: new weak_event_1.WeakEvent(),
22
+ resetAll: new weak_event_1.WeakEvent(),
29
23
  };
30
- this.getDocumentSchema = (oid) => {
31
- const { collection } = (0, common_1.decomposeOid)(oid);
32
- if (!this.schema.collections[collection]) {
33
- this.log('warn', `Missing schema for collection: ${collection}`);
34
- return { schema: null, readonlyKeys: [] };
35
- }
36
- const schema = this.schema.collections[collection];
37
- return {
38
- readonlyKeys: [schema.primaryKey],
39
- schema: {
40
- type: 'object',
41
- properties: schema.fields,
42
- },
43
- };
44
- };
45
- this.refreshFamilyCache = async (familyCache, dropUnconfirmed = false, dropAll = false) => {
46
- // avoid writing to disposed db
47
- if (this._disposed) {
48
- this.context.log('debug', `EntityStore is disposed, not refreshing ${familyCache.oid} cache`);
24
+ this.cache = new Map();
25
+ this.pendingEntityPromises = new Map();
26
+ // halts the current data queue processing
27
+ this.abortDataQueueController = new AbortController();
28
+ this.ongoingResetPromise = null;
29
+ this.entityFinalizationRegistry = new FinalizationRegistry((oid) => {
30
+ this.ctx.log('debug', 'Entity GC', oid);
31
+ });
32
+ // internal-ish API to load remote / stored data
33
+ this.addData = async (data) => {
34
+ if (this.disposed) {
35
+ this.ctx.log('warn', 'EntityStore is disposed, not adding incoming data');
49
36
  return;
50
37
  }
51
- // metadata must be loaded from database to initialize family cache
52
- const transaction = this.meta.createTransaction([
53
- 'baselines',
54
- 'operations',
55
- ]);
56
- const baselines = [];
57
- const operations = [];
58
- await Promise.all([
59
- this.meta.baselines.iterateOverAllForDocument(familyCache.oid, (baseline) => {
60
- baselines.push(baseline);
61
- }, {
62
- transaction,
63
- mode: 'readwrite',
64
- }),
65
- this.meta.operations.iterateOverAllOperationsForDocument(familyCache.oid, (op) => {
66
- op.confirmed = true;
67
- operations.push(op);
68
- }, { transaction, mode: 'readwrite' }),
69
- ]);
70
- familyCache.reset({
71
- operations,
72
- baselines,
73
- dropExistingUnconfirmed: dropUnconfirmed,
74
- dropAll,
75
- });
76
- };
77
- this.openFamilyCache = async (oid) => {
78
- const documentOid = (0, common_1.getOidRoot)(oid);
79
- let familyCache = this.documentFamilyCaches.get(documentOid);
80
- if (!familyCache) {
81
- this.context.log('debug', 'opening family cache for', documentOid);
82
- // metadata must be loaded from database to initialize family cache
83
- familyCache = new DocumentFamiliyCache_js_1.DocumentFamilyCache({
84
- oid: documentOid,
85
- store: this,
86
- context: this.context,
38
+ // for resets - abort any other changes, reset everything,
39
+ // then proceed
40
+ if (data.reset) {
41
+ this.ctx.log('info', 'Resetting local store to replicate remote synced data - dropping any current transactions');
42
+ // cancel any other ongoing data - it will all
43
+ // be replaced by the reset
44
+ this.abortDataQueueController.abort(AbortReason.Reset);
45
+ this.abortDataQueueController = new AbortController();
46
+ this.ongoingResetPromise = this.resetData().finally(() => {
47
+ this.ongoingResetPromise = null;
87
48
  });
88
- // PROBLEM: because the next line is async, it yields to
89
- // queued promises which may need data from this cache,
90
- // but the cache is empty. But if we move the set to
91
- // after the async, we can clobber an existing cache
92
- // with race conditions...
93
- // So as an attempt to fix that, I've added a promise
94
- // on DocumentFamilyCache which I manually resolve
95
- // with setInitialized, then await initializedPromise
96
- // further down even if there was a cache hit.
97
- // Surely there is a better pattern for this.
98
- // FIXME:
99
- this.documentFamilyCaches.set(documentOid, familyCache);
100
- await this.refreshFamilyCache(familyCache);
101
- familyCache.setInitialized();
102
- // this.unsubscribes.push(
103
- // familyCache.subscribe('change:*', this.onEntityChange),
104
- // );
105
- // TODO: cleanup cache when all documents are disposed
106
49
  }
107
- await familyCache.initializedPromise;
108
- return familyCache;
109
- };
110
- this.onEntityChange = async (oid) => {
111
- // queueMicrotask(() => this.writeDocumentToStorage(oid));
50
+ // await either the reset we just started, or any that was
51
+ // in progress when this data came in.
52
+ if (this.ongoingResetPromise) {
53
+ this.ctx.log('debug', 'Waiting for ongoing reset to complete');
54
+ await this.ongoingResetPromise;
55
+ this.ctx.log('debug', 'Ongoing reset complete');
56
+ }
57
+ await this.processData(data);
112
58
  };
113
- this.writeDocumentToStorage = async (oid) => {
114
- var _a;
115
- if (this._disposed) {
116
- this.log('warn', 'EntityStore is disposed, not writing to storage');
59
+ this.resetData = async () => {
60
+ if (this.disposed) {
61
+ this.ctx.log('warn', 'EntityStore is disposed, not resetting local data');
117
62
  return;
118
63
  }
119
- const rootOid = (0, common_1.getOidRoot)(oid);
120
- const { id, collection } = (0, common_1.decomposeOid)(rootOid);
121
- const entity = await this.get(rootOid);
122
- if (this._disposed) {
123
- this.log('warn', 'EntityStore is disposed, not writing to storage');
64
+ await this.meta.reset();
65
+ await this.queryableStorage.reset();
66
+ this.events.resetAll.invoke(this);
67
+ };
68
+ this.processData = async (data) => {
69
+ var _a, _b, _c;
70
+ if (this.disposed) {
71
+ this.ctx.log('warn', 'EntityStore is disposed, not processing incoming data');
124
72
  return;
125
73
  }
126
- const snapshot = entity === null || entity === void 0 ? void 0 : entity.getSnapshot();
127
- if (snapshot) {
128
- const stored = (0, common_1.getIndexValues)(this.schema.collections[collection], snapshot);
129
- try {
130
- const tx = this.db.transaction(collection, 'readwrite');
131
- const store = tx.objectStore(collection);
132
- await (0, idb_js_1.storeRequestPromise)(store.put(stored));
133
- this.log('info', '📝', 'wrote', collection, id, 'to storage', stored);
74
+ const baselines = (_a = data === null || data === void 0 ? void 0 : data.baselines) !== null && _a !== void 0 ? _a : [];
75
+ const operations = (_b = data === null || data === void 0 ? void 0 : data.operations) !== null && _b !== void 0 ? _b : [];
76
+ this.ctx.log('debug', 'Processing incoming data', {
77
+ operations: operations.length,
78
+ baselines: baselines.length,
79
+ reset: !!data.reset,
80
+ });
81
+ const allDocumentOids = Array.from(new Set(baselines
82
+ .map((b) => (0, common_1.getOidRoot)(b.oid))
83
+ .concat(operations.map((o) => (0, common_1.getOidRoot)(o.oid)))));
84
+ const baselinesGroupedByOid = (0, common_1.groupBaselinesByRootOid)(baselines);
85
+ const operationsGroupedByOid = (0, common_1.groupPatchesByRootOid)(operations);
86
+ this.ctx.log('debug', 'Applying data to live entities');
87
+ // synchronously add/replace data in any open entities via eventing
88
+ for (const oid of allDocumentOids) {
89
+ const baselines = baselinesGroupedByOid[oid];
90
+ const operations = (_c = operationsGroupedByOid[oid]) !== null && _c !== void 0 ? _c : [];
91
+ const groupedOperations = (0, common_1.groupPatchesByOid)(operations);
92
+ // what happens if an entity is being hydrated
93
+ // while this is happening? - we wait for the hydration promise
94
+ // to complete, then invoke the event
95
+ const event = data.reset ? this.events.replace : this.events.add;
96
+ const hydrationPromise = this.pendingEntityPromises.get(oid);
97
+ if (hydrationPromise) {
98
+ hydrationPromise.then(() => {
99
+ event.invoke(this, {
100
+ oid,
101
+ baselines,
102
+ operations: groupedOperations,
103
+ isLocal: false,
104
+ });
105
+ });
134
106
  }
135
- catch (err) {
136
- // if the document can't be written, something's very wrong :(
137
- // log the error and move on...
138
- 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
+ else {
108
+ if (this.cache.has(oid)) {
109
+ this.ctx.log('debug', 'Cache has', oid, ', an event should follow.');
110
+ }
111
+ event.invoke(this, {
112
+ oid,
113
+ baselines,
114
+ operations: groupedOperations,
115
+ isLocal: false,
116
+ });
139
117
  }
140
118
  }
141
- else {
142
- try {
143
- const tx = this.db.transaction(collection, 'readwrite');
144
- const store = tx.objectStore(collection);
145
- await (0, idb_js_1.storeRequestPromise)(store.delete(id));
146
- this.log('info', '❌', 'deleted', collection, id, 'from storage');
119
+ const abortOptions = {
120
+ abort: this.abortDataQueueController.signal,
121
+ };
122
+ // then, asynchronously add to the database
123
+ await this.meta.insertData(data, abortOptions);
124
+ // FIXME: entities hydrated here are not seeing
125
+ // the operations just inserted above!!
126
+ // IDEA: can we coordinate here with hydrate promises
127
+ // based on affected OIDs?
128
+ // recompute all affected documents for querying
129
+ const entities = await Promise.all(allDocumentOids.map(async (oid) => {
130
+ const entity = await this.hydrate(oid, abortOptions);
131
+ // if the entity is not found, we return a stub that
132
+ // indicates it's deleted and should be cleared
133
+ return (entity !== null && entity !== void 0 ? entity : {
134
+ oid,
135
+ getSnapshot() {
136
+ return null;
137
+ },
138
+ });
139
+ }));
140
+ try {
141
+ await this.queryableStorage.saveEntities(entities, abortOptions);
142
+ }
143
+ catch (err) {
144
+ if (this.disposed) {
145
+ this.ctx.log('warn', 'Error saving entities to queryable storage - EntityStore is disposed', err);
147
146
  }
148
- catch (err) {
149
- if (err instanceof Error) {
150
- // it's ok if the collection doesn't exist or the document
151
- // doesn't exist.
152
- if (err instanceof DOMException &&
153
- ((_a = err.message) === null || _a === void 0 ? void 0 : _a.includes('not found'))) {
154
- this.log('debug', 'document not found in storage', oid);
155
- }
156
- else {
157
- throw err;
158
- }
159
- }
147
+ else {
148
+ this.ctx.log('error', 'Error saving entities to queryable storage', err);
160
149
  }
161
150
  }
162
151
  };
163
- this.get = async (oid) => {
164
- const familyCache = await this.openFamilyCache(oid);
165
- const { schema, readonlyKeys } = this.getDocumentSchema(oid);
166
- if (!schema) {
167
- return null;
152
+ // internal-ish API for creating Entities from OIDs
153
+ // when query results come in
154
+ this.hydrate = async (oid, opts) => {
155
+ if (!(0, common_1.isRootOid)(oid)) {
156
+ throw new Error('Cannot hydrate non-root entity');
168
157
  }
169
- return familyCache.getEntity({ oid, fieldSchema: schema, readonlyKeys });
170
- };
171
- /**
172
- * Advanced usage!
173
- * Immediately returns an entity if it exists in the memory cache. An
174
- * entity would be cached if it has been retrieved by a live query.
175
- */
176
- this.getCached = (oid) => {
177
- const cache = this.documentFamilyCaches.get(oid);
178
- if (cache) {
179
- const { schema, readonlyKeys } = this.getDocumentSchema(oid);
180
- if (!schema) {
158
+ if (this.cache.has(oid)) {
159
+ this.ctx.log('debug', 'Hydrating entity from cache', oid);
160
+ const cached = this.cache.get(oid);
161
+ if (cached) {
162
+ const entity = cached.deref();
163
+ if (entity) {
164
+ if (entity.deleted) {
165
+ return null;
166
+ }
167
+ return entity;
168
+ }
169
+ else {
170
+ this.ctx.log('debug', "Removing GC'd entity from cache", oid);
171
+ this.cache.delete(oid);
172
+ }
173
+ }
174
+ }
175
+ // we don't want to hydrate two entities in parallel, so
176
+ // we use a promise to ensure that only one is ever
177
+ // constructed at a time
178
+ const pendingPromise = this.pendingEntityPromises.get(oid);
179
+ if (!pendingPromise) {
180
+ this.ctx.log('debug', 'Hydrating entity from storage', oid);
181
+ const entity = this.constructEntity(oid);
182
+ if (!entity) {
181
183
  return null;
182
184
  }
183
- return cache.getEntity({ oid, fieldSchema: schema, readonlyKeys });
185
+ const pendingPromise = this.loadEntity(entity, opts);
186
+ pendingPromise.finally(() => {
187
+ this.pendingEntityPromises.delete(oid);
188
+ });
189
+ this.pendingEntityPromises.set(oid, pendingPromise);
190
+ return pendingPromise;
191
+ }
192
+ else {
193
+ this.ctx.log('debug', 'Waiting for entity hydration', oid);
194
+ return pendingPromise;
184
195
  }
185
- return null;
186
196
  };
197
+ this.destroy = async () => {
198
+ this.dispose();
199
+ await this.batcher.flushAll();
200
+ };
201
+ // public APIs for manipulating entities
187
202
  /**
188
- * Creates a new document and returns an Entity for it. The created
189
- * document is submitted to storage and sync.
203
+ * Creates a new Entity with the given initial data.
190
204
  */
191
- this.create = async (initial, oid, options) => {
192
- // remove all OID associations from initial data
205
+ this.create = async (initial, oid, { undoable = true } = {}) => {
206
+ this.ctx.log('debug', 'Creating new entity', oid);
207
+ const { collection } = (0, common_1.decomposeOid)(oid);
208
+ // remove any OID associations from the initial data
193
209
  (0, common_1.removeOidsFromAllSubObjects)(initial);
194
- // first grab any file and replace them with refs
210
+ // grab files and replace them with refs
195
211
  const processed = (0, utils_js_1.processValueFiles)(initial, this.files.add);
196
212
  (0, common_1.assignOid)(processed, oid);
197
- const operations = this.meta.patchCreator.createInitialize(processed, oid);
198
- const familyCache = await this.openFamilyCache(oid);
199
- familyCache.insertLocalOperations(operations);
200
- // don't enqueue these, submit as distinct operation.
201
- // we do this so it can be immediately queryable from storage...
202
- // only holding it in memory would introduce lag before it shows up
203
- // in other queries.
204
- await this.submitOperations(operations, options);
205
- const { schema, readonlyKeys } = this.getDocumentSchema(oid);
206
- if (!schema) {
207
- throw new Error(`Cannot create a document in the ${(0, common_1.decomposeOid)(oid).collection} collection; it is not defined in the current schema version.`);
213
+ // creating a new Entity with no data, then preloading the operations
214
+ const entity = this.constructEntity(oid);
215
+ if (!entity) {
216
+ throw new Error(`Could not put new document: no schema exists for collection ${collection}`);
208
217
  }
209
- return familyCache.getEntity({ oid, fieldSchema: schema, readonlyKeys });
210
- };
211
- this.addOperationsToOpenCaches = async (operations, info) => {
212
- const operationsByOid = (0, common_1.groupPatchesByRootOid)(operations);
213
- const oids = Object.keys(operationsByOid);
214
- oids.forEach((oid) => {
215
- const familyCache = this.documentFamilyCaches.get(oid);
216
- if (familyCache) {
217
- this.log('adding', info.confirmed ? 'confirmed' : 'unconfirmed', 'operations to cache', oid, operationsByOid[oid].length);
218
- if (info.isLocal) {
219
- familyCache.insertLocalOperations(operationsByOid[oid]);
220
- }
221
- else {
222
- familyCache.insertOperations(operationsByOid[oid], info);
223
- }
224
- }
218
+ const operations = this.meta.patchCreator.createInitialize(processed, oid);
219
+ await this.batcher.commitOperations(operations, {
220
+ undoable: !!undoable,
221
+ source: entity,
225
222
  });
223
+ // TODO: what happens if you create an entity with an OID that already
224
+ // exists?
225
+ // we still need to synchronously add the initial operations to the Entity
226
+ // even though they are flowing through the system
227
+ // TODO: this could be better aligned to avoid grouping here
228
+ const operationsGroupedByOid = (0, common_1.groupPatchesByOid)(operations);
229
+ this.events.add.invoke(this, {
230
+ operations: operationsGroupedByOid,
231
+ isLocal: true,
232
+ oid,
233
+ });
234
+ this.cache.set(oid, this.ctx.weakRef(entity));
235
+ return entity;
226
236
  };
227
- this.addBaselinesToOpenCaches = async (baselines, info) => {
228
- const baselinesByOid = (0, common_1.groupBaselinesByRootOid)(baselines);
229
- const oids = Object.keys(baselinesByOid);
237
+ this.deleteAll = async (oids, options) => {
238
+ this.ctx.log('info', 'Deleting documents', oids);
239
+ (0, common_1.assert)(oids.every((oid) => oid === (0, common_1.getOidRoot)(oid)), 'Only root documents may be deleted via client methods');
240
+ const allOids = await Promise.all(oids.flatMap(async (oid) => {
241
+ var _a;
242
+ const entity = await this.hydrate(oid);
243
+ return (_a = entity === null || entity === void 0 ? void 0 : entity.__getFamilyOids__()) !== null && _a !== void 0 ? _a : [];
244
+ }));
245
+ // remove the entities from cache
230
246
  oids.forEach((oid) => {
231
- const cache = this.documentFamilyCaches.get(oid);
232
- if (cache) {
233
- this.log('adding', 'baselines to cache', oid, baselinesByOid[oid].length);
234
- cache.insertBaselines(baselinesByOid[oid], info);
235
- }
247
+ this.cache.delete(oid);
248
+ this.ctx.log('debug', 'Deleted document from cache', oid);
249
+ });
250
+ // create the delete patches and wait for them to be applied
251
+ const operations = this.meta.patchCreator.createDeleteAll(allOids.flat());
252
+ await this.batcher.commitOperations(operations, {
253
+ undoable: (options === null || options === void 0 ? void 0 : options.undoable) === undefined ? true : options.undoable,
236
254
  });
237
255
  };
238
- this.addDataToOpenCaches = ({ baselines, operations, reset, isLocal, }) => {
239
- var _a, _b, _c, _d;
240
- const baselinesByDocumentOid = (0, common_1.groupBaselinesByRootOid)(baselines);
241
- const operationsByDocumentOid = (0, common_1.groupPatchesByRootOid)(operations);
242
- const allDocumentOids = Array.from(new Set(Object.keys(baselinesByDocumentOid).concat(Object.keys(operationsByDocumentOid))));
243
- for (const oid of allDocumentOids) {
244
- const familyCache = this.documentFamilyCaches.get(oid);
245
- if (familyCache) {
246
- familyCache.addData({
247
- operations: operationsByDocumentOid[oid] || [],
248
- baselines: baselinesByDocumentOid[oid] || [],
249
- reset,
250
- isLocal,
251
- });
252
- this.log('debug', 'Added data to cache for', oid, (_b = (_a = operationsByDocumentOid[oid]) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0, 'operations', (_d = (_c = baselinesByDocumentOid[oid]) === null || _c === void 0 ? void 0 : _c.length) !== null && _d !== void 0 ? _d : 0, 'baselines');
253
- }
254
- else {
255
- this.log('debug', 'Could not add data to cache for', oid, 'because it is not open');
256
- }
257
- }
258
- return allDocumentOids;
256
+ this.delete = async (oid, options) => {
257
+ return this.deleteAll([oid], options);
259
258
  };
260
- this.addData = async ({ operations, baselines, reset, }) => {
261
- if (this._disposed) {
262
- this.log('warn', 'EntityStore is disposed, not adding data');
263
- return;
264
- }
265
- // convert operations to tagged operations with confirmed = false
266
- // while we process and store them. this is in-place so as to
267
- // not allocate a bunch of objects...
268
- const taggedOperations = operations;
269
- for (const op of taggedOperations) {
270
- op.confirmed = false;
271
- }
272
- let allDocumentOids = [];
273
- // in a reset scenario, it only makes things confusing if we
274
- // optimistically apply incoming operations, since the local
275
- // history is out of sync
276
- if (reset) {
277
- this.log('Resetting local store to replicate remote synced data', baselines.length, 'baselines, and', operations.length, 'operations');
278
- await this.meta.reset();
279
- await this.resetStoredDocuments();
280
- allDocumentOids = Array.from(new Set(baselines
281
- .map((b) => (0, common_1.getOidRoot)(b.oid))
282
- .concat(operations.map((o) => (0, common_1.getOidRoot)(o.oid)))));
283
- }
284
- else {
285
- // first, synchronously add data to any open caches for immediate change propagation
286
- allDocumentOids = this.addDataToOpenCaches({
287
- operations: taggedOperations,
288
- baselines,
289
- reset,
290
- });
291
- }
292
- // then, asynchronously add data to storage
293
- await this.meta.insertRemoteBaselines(baselines);
294
- await this.meta.insertRemoteOperations(operations);
295
- if (reset) {
296
- await this.refreshAllCaches(true, true);
297
- }
298
- // recompute all affected documents for querying
299
- for (const oid of allDocumentOids) {
300
- await this.writeDocumentToStorage(oid);
259
+ this.getCollectionSchema = (collectionName) => {
260
+ const schema = this.ctx.schema.collections[collectionName];
261
+ if (!schema) {
262
+ this.ctx.log('warn', `Missing schema for collection: ${collectionName}`);
263
+ return {
264
+ schema: null,
265
+ readonlyKeys: [],
266
+ };
301
267
  }
302
- // notify active queries
303
- const affectedCollections = Array.from(new Set(allDocumentOids.map((oid) => (0, common_1.decomposeOid)(oid).collection)));
304
- this.context.log('changes to collections', affectedCollections);
305
- this.context.entityEvents.emit('collectionsChanged', affectedCollections);
306
- };
307
- this.addLocalOperations = async (operations) => {
308
- this.log('Adding local operations', operations.length);
309
- this.addOperationsToOpenCaches(operations, {
310
- isLocal: true,
311
- confirmed: false,
312
- });
313
- this.operationBatcher.add({
314
- key: this.currentBatchKey,
315
- items: operations,
316
- });
317
- };
318
- this.batch = ({ undoable = true, batchName = (0, common_1.generateId)(), max = null, timeout = this.defaultBatchTimeout, } = {}) => {
319
- const internalBatch = this.operationBatcher.add({
320
- key: batchName,
321
- max,
322
- timeout,
323
- items: [],
324
- userData: { undoable },
325
- });
326
- const externalApi = {
327
- run: (fn) => {
328
- // while the provided function runs, operations are forwarded
329
- // to the new batch instead of default. this relies on the function
330
- // being synchronous.
331
- this.currentBatchKey = batchName;
332
- fn();
333
- this.currentBatchKey = DEFAULT_BATCH_KEY;
334
- return externalApi;
335
- },
336
- flush: async () => {
337
- // before running a batch, the default operations must be flushed
338
- // this better preserves undo history behavior...
339
- // if we left the default batch open while flushing a named batch,
340
- // then the default batch would be flushed after the named batch,
341
- // and the default batch could contain operations both prior and
342
- // after the named batch. this would result in a confusing undo
343
- // history where the first undo might reverse changes before and
344
- // after a set of other changes.
345
- await this.operationBatcher.flush(DEFAULT_BATCH_KEY);
346
- return internalBatch.flush();
347
- },
348
- discard: () => {
349
- this.operationBatcher.discard(batchName);
268
+ return {
269
+ // convert to object schema for compatibility
270
+ schema: {
271
+ type: 'object',
272
+ nullable: false,
273
+ properties: schema.fields,
350
274
  },
275
+ readonlyKeys: [schema.primaryKey],
351
276
  };
352
- return externalApi;
353
277
  };
354
278
  /**
355
- * @deprecated use `batch` instead
279
+ * Constructs an entity from an OID, but does not load it.
356
280
  */
357
- this.flushPatches = async () => {
358
- await this.operationBatcher.flush(this.currentBatchKey);
359
- };
360
- this.flushAllBatches = async () => {
361
- await Promise.all(this.operationBatcher.flushAll());
362
- };
363
- this.flushOperations = async (operations, batchKey, meta) => {
364
- if (!operations.length)
365
- return;
366
- this.log('Flushing operations', operations.length, 'to storage / sync');
367
- // rewrite timestamps of all operations to now - this preserves
368
- // the linear history of operations which are sent to the server.
369
- // even if multiple batches are spun up in parallel and flushed
370
- // after delay, the final operations in each one should reflect
371
- // when the batch flushed, not when the changes were made.
372
- // This also corresponds to user-observed behavior, since unconfirmed
373
- // operations are applied universally after confirmed operations locally,
374
- // so even operations which were made before a remote operation but
375
- // have not been confirmed yet will appear to come after the remote one
376
- // despite the provisional timestamp being earlier (see DocumentFamilyCache#computeView)
377
- for (const op of operations) {
378
- op.timestamp = this.meta.now;
379
- }
380
- await this.submitOperations(operations, meta);
381
- };
382
- this.submitOperations = async (operations, { undoable = true } = {}) => {
383
- if (undoable) {
384
- // FIXME: this is too slow and needs to be optimized.
385
- this.undoHistory.addUndo(await this.createUndo(operations));
386
- }
387
- await this.meta.insertLocalOperation(operations);
388
- // confirm the operations
389
- this.addDataToOpenCaches({ operations, baselines: [] });
390
- // recompute all affected documents for querying
391
- const allDocumentOids = Array.from(new Set(operations.map((op) => (0, common_1.getOidRoot)(op.oid))));
392
- for (const oid of allDocumentOids) {
393
- await this.writeDocumentToStorage(oid);
394
- }
395
- // TODO: find a more efficient and straightforward way to update affected
396
- // queries. Move to Metadata?
397
- const affectedCollections = new Set(operations.map(({ oid }) => (0, common_1.decomposeOid)(oid).collection));
398
- this.context.log('changes to collections', affectedCollections);
399
- this.context.entityEvents.emit('collectionsChanged', Array.from(affectedCollections));
400
- };
401
- this.getInverseOperations = async (ops) => {
402
- const grouped = (0, common_1.groupPatchesByIdentifier)(ops);
403
- const inverseOps = [];
404
- const getNow = () => this.meta.now;
405
- for (const [oid, patches] of Object.entries(grouped)) {
406
- const familyCache = await this.openFamilyCache(oid);
407
- let { view, deleted } = familyCache.computeConfirmedView(oid);
408
- const inverse = (0, common_1.getUndoOperations)(oid, view, patches, getNow);
409
- inverseOps.unshift(...inverse);
410
- }
411
- return inverseOps;
412
- };
413
- this.createUndo = async (ops) => {
414
- const inverseOps = await this.getInverseOperations(ops);
415
- return async () => {
416
- const redo = await this.createUndo(inverseOps);
417
- await this.submitOperations(inverseOps.map((op) => {
418
- op.timestamp = this.meta.now;
419
- return op;
420
- }),
421
- // undos should not generate their own undo operations
422
- // since they already calculate redo as the inverse.
423
- { undoable: false });
424
- return redo;
425
- };
426
- };
427
- this.delete = async (oid, options) => {
428
- (0, common_1.assert)(oid === (0, common_1.getOidRoot)(oid), 'Only root documents may be deleted via client methods');
429
- // we need to get all sub-object oids to delete alongside the root
430
- const allOids = await this.meta.getAllDocumentRelatedOids(oid);
431
- const patches = this.meta.patchCreator.createDeleteAll(allOids);
432
- // don't enqueue these, submit as distinct operation
433
- await this.submitOperations(patches, options);
434
- };
435
- this.deleteAll = async (oids, options) => {
436
- const allOids = await Promise.all(oids.map((oid) => this.meta.getAllDocumentRelatedOids(oid)));
437
- const patches = this.meta.patchCreator.createDeleteAll(allOids.flat());
438
- // don't enqueue these, submit as distinct operation
439
- await this.submitOperations(patches, options);
440
- };
441
- this.reset = async () => {
442
- this.context.log('warn', 'Resetting local database');
443
- await this.resetStoredDocuments();
444
- await this.refreshAllCaches(true);
445
- // this.context.entityEvents.emit(
446
- // 'collectionsChanged',
447
- // Object.keys(this.schema.collections),
448
- // );
449
- };
450
- this.destroy = async () => {
451
- this._disposed = true;
452
- for (const unsubscribe of this.unsubscribes) {
453
- unsubscribe();
281
+ this.constructEntity = (oid) => {
282
+ const { collection } = (0, common_1.decomposeOid)(oid);
283
+ const { schema, readonlyKeys } = this.getCollectionSchema(collection);
284
+ if (!schema) {
285
+ return null;
454
286
  }
455
- for (const cache of this.documentFamilyCaches.values()) {
456
- cache.dispose();
287
+ if (this.disposed) {
288
+ throw new Error('Cannot hydrate entity after store has been disposed');
457
289
  }
458
- this.documentFamilyCaches.clear();
459
- await this.flushAllBatches();
460
- };
461
- this.handleRebase = (baselines) => {
462
- this.log('debug', 'Reacting to rebases', baselines.length);
463
- // update any open caches with new baseline. this will automatically
464
- // drop operations before the baseline.
465
- this.addBaselinesToOpenCaches(baselines, { isLocal: true });
290
+ const metadataFamily = new EntityMetadata_js_1.EntityFamilyMetadata({
291
+ ctx: this.ctx,
292
+ onPendingOperations: this.onPendingOperations,
293
+ rootOid: oid,
294
+ });
295
+ // this is created synchronously so it's immediately available
296
+ // to begin capturing incoming data.
297
+ return new Entity_js_1.Entity({
298
+ ctx: this.ctx,
299
+ oid,
300
+ schema,
301
+ readonlyKeys,
302
+ files: this.files,
303
+ metadataFamily: metadataFamily,
304
+ patchCreator: this.meta.patchCreator,
305
+ events: this.events,
306
+ });
466
307
  };
467
- this.resetStoredDocuments = async () => {
468
- const tx = this.db.transaction(Object.keys(this.schema.collections), 'readwrite');
469
- for (const collection of Object.keys(this.schema.collections)) {
470
- const store = tx.objectStore(collection);
471
- await (0, idb_js_1.storeRequestPromise)(store.clear());
472
- }
308
+ this.onPendingOperations = (operations) => {
309
+ this.batcher.addOperations(operations);
473
310
  };
474
- this.refreshAllCaches = async (dropUnconfirmed = false, dropAll = false) => {
475
- for (const [_, cache] of this.documentFamilyCaches) {
476
- await this.refreshFamilyCache(cache, dropUnconfirmed, dropAll);
311
+ /**
312
+ * Loads initial Entity data from storage
313
+ */
314
+ this.loadEntity = async (entity, opts) => {
315
+ const { operations, baselines } = await this.meta.getDocumentData(entity.oid, opts);
316
+ if (!baselines.length && !Object.keys(operations).length) {
317
+ this.ctx.log('debug', 'No data found for entity', entity.oid);
318
+ return null;
477
319
  }
320
+ this.ctx.log('debug', 'Loaded entity from storage', entity.oid);
321
+ this.events.replace.invoke(this, {
322
+ oid: entity.oid,
323
+ baselines,
324
+ operations,
325
+ isLocal: false,
326
+ });
327
+ // only set the cache after loading.
328
+ // TODO: is this cache/promise stuff redundant?
329
+ this.cache.set(entity.oid, this.ctx.weakRef(entity));
330
+ this.entityFinalizationRegistry.register(entity, entity.oid);
331
+ return entity;
478
332
  };
479
- this.context = context;
480
- this.defaultBatchTimeout = batchTimeout;
333
+ this.ctx = ctx;
481
334
  this.meta = meta;
482
335
  this.files = files;
483
- this.operationBatcher = new common_1.Batcher(this.flushOperations);
484
- // initialize default batch
485
- this.operationBatcher.add({
486
- key: DEFAULT_BATCH_KEY,
487
- items: [],
488
- max: 100,
489
- timeout: batchTimeout,
490
- userData: { undoable: true },
336
+ this.queryableStorage = new QueryableStorage_js_1.QueryableStorage({ ctx });
337
+ this.batcher = new OperationBatcher_js_1.OperationBatcher({
338
+ ctx,
339
+ meta,
340
+ entities: this,
491
341
  });
492
- this.unsubscribes.push(this.meta.subscribe('rebase', this.handleRebase));
342
+ }
343
+ // expose batch APIs
344
+ get batch() {
345
+ return this.batcher.batch;
346
+ }
347
+ get flushAllBatches() {
348
+ return this.batcher.flushAll;
493
349
  }
494
350
  }
495
351
  exports.EntityStore = EntityStore;