@verdant-web/store 3.0.0-next.0 → 3.0.1

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