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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (307) 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 +6 -5
  4. package/dist/cjs/DocumentManager.js +2 -2
  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 +54 -6
  15. package/dist/cjs/__tests__/documents.test.js.map +1 -1
  16. package/dist/cjs/__tests__/fixtures/testStorage.d.ts +8 -2
  17. package/dist/cjs/__tests__/fixtures/testStorage.js +8 -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 +13 -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 +2 -3
  31. package/dist/cjs/client/Client.js +8 -4
  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/2/Entity.d.ts +148 -0
  37. package/dist/cjs/entities/2/Entity.js +711 -0
  38. package/dist/cjs/entities/2/Entity.js.map +1 -0
  39. package/dist/cjs/entities/2/Entity.test.d.ts +1 -0
  40. package/dist/cjs/entities/2/Entity.test.js +194 -0
  41. package/dist/cjs/entities/2/Entity.test.js.map +1 -0
  42. package/dist/cjs/entities/2/EntityCache.d.ts +15 -0
  43. package/dist/cjs/entities/2/EntityCache.js +39 -0
  44. package/dist/cjs/entities/2/EntityCache.js.map +1 -0
  45. package/dist/cjs/entities/2/EntityMetadata.d.ts +68 -0
  46. package/dist/cjs/entities/2/EntityMetadata.js +261 -0
  47. package/dist/cjs/entities/2/EntityMetadata.js.map +1 -0
  48. package/dist/cjs/entities/2/EntityStore.d.ts +78 -0
  49. package/dist/cjs/entities/2/EntityStore.js +352 -0
  50. package/dist/cjs/entities/2/EntityStore.js.map +1 -0
  51. package/dist/cjs/entities/2/OperationBatcher.d.ts +52 -0
  52. package/dist/cjs/entities/2/OperationBatcher.js +165 -0
  53. package/dist/cjs/entities/2/OperationBatcher.js.map +1 -0
  54. package/dist/cjs/entities/2/types.d.ts +84 -0
  55. package/dist/cjs/entities/2/types.js +3 -0
  56. package/dist/cjs/entities/2/types.js.map +1 -0
  57. package/dist/cjs/entities/Entity.d.ts +0 -7
  58. package/dist/cjs/entities/Entity.js +7 -0
  59. package/dist/cjs/entities/Entity.js.map +1 -1
  60. package/dist/cjs/entities/EntityStore.js +4 -20
  61. package/dist/cjs/entities/EntityStore.js.map +1 -1
  62. package/dist/cjs/entities/FakeWeakRef.d.ts +11 -0
  63. package/dist/cjs/entities/FakeWeakRef.js +19 -0
  64. package/dist/cjs/entities/FakeWeakRef.js.map +1 -0
  65. package/dist/cjs/files/EntityFile.d.ts +5 -2
  66. package/dist/cjs/files/EntityFile.js +8 -4
  67. package/dist/cjs/files/EntityFile.js.map +1 -1
  68. package/dist/cjs/files/FileManager.d.ts +3 -1
  69. package/dist/cjs/files/FileManager.js +5 -3
  70. package/dist/cjs/files/FileManager.js.map +1 -1
  71. package/dist/cjs/files/FileStorage.js +7 -7
  72. package/dist/cjs/files/FileStorage.js.map +1 -1
  73. package/dist/cjs/files/utils.d.ts +2 -0
  74. package/dist/cjs/files/utils.js +8 -2
  75. package/dist/cjs/files/utils.js.map +1 -1
  76. package/dist/cjs/idb.d.ts +2 -0
  77. package/dist/cjs/idb.js +50 -4
  78. package/dist/cjs/idb.js.map +1 -1
  79. package/dist/cjs/index.d.ts +2 -2
  80. package/dist/cjs/index.js +1 -1
  81. package/dist/cjs/index.js.map +1 -1
  82. package/dist/cjs/indexes.d.ts +3 -0
  83. package/dist/cjs/indexes.js +20 -0
  84. package/dist/cjs/indexes.js.map +1 -0
  85. package/dist/cjs/metadata/AckInfoStore.js +1 -1
  86. package/dist/cjs/metadata/AckInfoStore.js.map +1 -1
  87. package/dist/cjs/metadata/BaselinesStore.d.ts +4 -1
  88. package/dist/cjs/metadata/BaselinesStore.js +19 -10
  89. package/dist/cjs/metadata/BaselinesStore.js.map +1 -1
  90. package/dist/cjs/metadata/LocalReplicaStore.d.ts +1 -1
  91. package/dist/cjs/metadata/LocalReplicaStore.js +11 -5
  92. package/dist/cjs/metadata/LocalReplicaStore.js.map +1 -1
  93. package/dist/cjs/metadata/Metadata.d.ts +26 -5
  94. package/dist/cjs/metadata/Metadata.js +55 -18
  95. package/dist/cjs/metadata/Metadata.js.map +1 -1
  96. package/dist/cjs/metadata/OperationsStore.d.ts +3 -0
  97. package/dist/cjs/metadata/OperationsStore.js +35 -15
  98. package/dist/cjs/metadata/OperationsStore.js.map +1 -1
  99. package/dist/cjs/migration/openDatabase.js +31 -10
  100. package/dist/cjs/migration/openDatabase.js.map +1 -1
  101. package/dist/cjs/queries/BaseQuery.js +14 -2
  102. package/dist/cjs/queries/BaseQuery.js.map +1 -1
  103. package/dist/cjs/queries/CollectionQueries.d.ts +2 -4
  104. package/dist/cjs/queries/CollectionQueries.js +1 -1
  105. package/dist/cjs/queries/CollectionQueries.js.map +1 -1
  106. package/dist/cjs/queries/FindAllQuery.js +1 -0
  107. package/dist/cjs/queries/FindAllQuery.js.map +1 -1
  108. package/dist/cjs/queries/QueryCache.d.ts +1 -0
  109. package/dist/cjs/queries/QueryCache.js +4 -0
  110. package/dist/cjs/queries/QueryCache.js.map +1 -1
  111. package/dist/cjs/queries/QueryableStorage.d.ts +20 -0
  112. package/dist/cjs/queries/QueryableStorage.js +84 -0
  113. package/dist/cjs/queries/QueryableStorage.js.map +1 -0
  114. package/dist/cjs/queries/dbQueries.js +13 -3
  115. package/dist/cjs/queries/dbQueries.js.map +1 -1
  116. package/dist/cjs/queries/utils.js +1 -1
  117. package/dist/cjs/queries/utils.js.map +1 -1
  118. package/dist/cjs/sync/FileSync.d.ts +1 -0
  119. package/dist/cjs/sync/FileSync.js +1 -0
  120. package/dist/cjs/sync/FileSync.js.map +1 -1
  121. package/dist/cjs/sync/PushPullSync.d.ts +2 -1
  122. package/dist/cjs/sync/PushPullSync.js +7 -1
  123. package/dist/cjs/sync/PushPullSync.js.map +1 -1
  124. package/dist/cjs/sync/Sync.d.ts +6 -3
  125. package/dist/cjs/sync/Sync.js +9 -4
  126. package/dist/cjs/sync/Sync.js.map +1 -1
  127. package/dist/cjs/sync/WebSocketSync.d.ts +4 -1
  128. package/dist/cjs/sync/WebSocketSync.js +41 -11
  129. package/dist/cjs/sync/WebSocketSync.js.map +1 -1
  130. package/dist/esm/DocumentManager.d.ts +6 -5
  131. package/dist/esm/DocumentManager.js +2 -2
  132. package/dist/esm/DocumentManager.js.map +1 -1
  133. package/dist/esm/IDBService.d.ts +28 -7
  134. package/dist/esm/IDBService.js +51 -14
  135. package/dist/esm/IDBService.js.map +1 -1
  136. package/dist/esm/UndoHistory.d.ts +1 -1
  137. package/dist/esm/UndoHistory.js +6 -2
  138. package/dist/esm/UndoHistory.js.map +1 -1
  139. package/dist/esm/__tests__/batching.test.js +3 -1
  140. package/dist/esm/__tests__/batching.test.js.map +1 -1
  141. package/dist/esm/__tests__/documents.test.js +54 -6
  142. package/dist/esm/__tests__/documents.test.js.map +1 -1
  143. package/dist/esm/__tests__/fixtures/testStorage.d.ts +8 -2
  144. package/dist/esm/__tests__/fixtures/testStorage.js +8 -1
  145. package/dist/esm/__tests__/fixtures/testStorage.js.map +1 -1
  146. package/dist/esm/__tests__/legacyOids.test.js +50 -17
  147. package/dist/esm/__tests__/legacyOids.test.js.map +1 -1
  148. package/dist/esm/__tests__/mutations.test.js +9 -3
  149. package/dist/esm/__tests__/mutations.test.js.map +1 -1
  150. package/dist/esm/__tests__/queries.test.js +6 -2
  151. package/dist/esm/__tests__/queries.test.js.map +1 -1
  152. package/dist/esm/__tests__/setup/indexedDB.d.ts +1 -1
  153. package/dist/esm/__tests__/setup/indexedDB.js +13 -1
  154. package/dist/esm/__tests__/setup/indexedDB.js.map +1 -1
  155. package/dist/esm/__tests__/undo.test.js +16 -9
  156. package/dist/esm/__tests__/undo.test.js.map +1 -1
  157. package/dist/esm/client/Client.d.ts +2 -3
  158. package/dist/esm/client/Client.js +8 -4
  159. package/dist/esm/client/Client.js.map +1 -1
  160. package/dist/esm/client/ClientDescriptor.js +21 -6
  161. package/dist/esm/client/ClientDescriptor.js.map +1 -1
  162. package/dist/esm/context.d.ts +10 -1
  163. package/dist/esm/entities/2/Entity.d.ts +148 -0
  164. package/dist/esm/entities/2/Entity.js +707 -0
  165. package/dist/esm/entities/2/Entity.js.map +1 -0
  166. package/dist/esm/entities/2/Entity.test.d.ts +1 -0
  167. package/dist/esm/entities/2/Entity.test.js +192 -0
  168. package/dist/esm/entities/2/Entity.test.js.map +1 -0
  169. package/dist/esm/entities/2/EntityCache.d.ts +15 -0
  170. package/dist/esm/entities/2/EntityCache.js +35 -0
  171. package/dist/esm/entities/2/EntityCache.js.map +1 -0
  172. package/dist/esm/entities/2/EntityMetadata.d.ts +68 -0
  173. package/dist/esm/entities/2/EntityMetadata.js +256 -0
  174. package/dist/esm/entities/2/EntityMetadata.js.map +1 -0
  175. package/dist/esm/entities/2/EntityStore.d.ts +78 -0
  176. package/dist/esm/entities/2/EntityStore.js +348 -0
  177. package/dist/esm/entities/2/EntityStore.js.map +1 -0
  178. package/dist/esm/entities/2/OperationBatcher.d.ts +52 -0
  179. package/dist/esm/entities/2/OperationBatcher.js +161 -0
  180. package/dist/esm/entities/2/OperationBatcher.js.map +1 -0
  181. package/dist/esm/entities/2/types.d.ts +84 -0
  182. package/dist/esm/entities/2/types.js +2 -0
  183. package/dist/esm/entities/2/types.js.map +1 -0
  184. package/dist/esm/entities/Entity.d.ts +0 -7
  185. package/dist/esm/entities/Entity.js +7 -0
  186. package/dist/esm/entities/Entity.js.map +1 -1
  187. package/dist/esm/entities/EntityStore.js +4 -20
  188. package/dist/esm/entities/EntityStore.js.map +1 -1
  189. package/dist/esm/entities/FakeWeakRef.d.ts +11 -0
  190. package/dist/esm/entities/FakeWeakRef.js +15 -0
  191. package/dist/esm/entities/FakeWeakRef.js.map +1 -0
  192. package/dist/esm/files/EntityFile.d.ts +5 -2
  193. package/dist/esm/files/EntityFile.js +8 -4
  194. package/dist/esm/files/EntityFile.js.map +1 -1
  195. package/dist/esm/files/FileManager.d.ts +3 -1
  196. package/dist/esm/files/FileManager.js +5 -3
  197. package/dist/esm/files/FileManager.js.map +1 -1
  198. package/dist/esm/files/FileStorage.js +7 -7
  199. package/dist/esm/files/FileStorage.js.map +1 -1
  200. package/dist/esm/files/utils.d.ts +2 -0
  201. package/dist/esm/files/utils.js +6 -1
  202. package/dist/esm/files/utils.js.map +1 -1
  203. package/dist/esm/idb.d.ts +2 -0
  204. package/dist/esm/idb.js +47 -3
  205. package/dist/esm/idb.js.map +1 -1
  206. package/dist/esm/index.d.ts +2 -2
  207. package/dist/esm/index.js +1 -1
  208. package/dist/esm/index.js.map +1 -1
  209. package/dist/esm/indexes.d.ts +3 -0
  210. package/dist/esm/indexes.js +15 -0
  211. package/dist/esm/indexes.js.map +1 -0
  212. package/dist/esm/metadata/AckInfoStore.js +1 -1
  213. package/dist/esm/metadata/AckInfoStore.js.map +1 -1
  214. package/dist/esm/metadata/BaselinesStore.d.ts +4 -1
  215. package/dist/esm/metadata/BaselinesStore.js +19 -10
  216. package/dist/esm/metadata/BaselinesStore.js.map +1 -1
  217. package/dist/esm/metadata/LocalReplicaStore.d.ts +1 -1
  218. package/dist/esm/metadata/LocalReplicaStore.js +11 -5
  219. package/dist/esm/metadata/LocalReplicaStore.js.map +1 -1
  220. package/dist/esm/metadata/Metadata.d.ts +26 -5
  221. package/dist/esm/metadata/Metadata.js +56 -19
  222. package/dist/esm/metadata/Metadata.js.map +1 -1
  223. package/dist/esm/metadata/OperationsStore.d.ts +3 -0
  224. package/dist/esm/metadata/OperationsStore.js +35 -15
  225. package/dist/esm/metadata/OperationsStore.js.map +1 -1
  226. package/dist/esm/migration/openDatabase.js +32 -11
  227. package/dist/esm/migration/openDatabase.js.map +1 -1
  228. package/dist/esm/queries/BaseQuery.js +14 -2
  229. package/dist/esm/queries/BaseQuery.js.map +1 -1
  230. package/dist/esm/queries/CollectionQueries.d.ts +2 -4
  231. package/dist/esm/queries/CollectionQueries.js +1 -1
  232. package/dist/esm/queries/CollectionQueries.js.map +1 -1
  233. package/dist/esm/queries/FindAllQuery.js +1 -0
  234. package/dist/esm/queries/FindAllQuery.js.map +1 -1
  235. package/dist/esm/queries/QueryCache.d.ts +1 -0
  236. package/dist/esm/queries/QueryCache.js +4 -0
  237. package/dist/esm/queries/QueryCache.js.map +1 -1
  238. package/dist/esm/queries/QueryableStorage.d.ts +20 -0
  239. package/dist/esm/queries/QueryableStorage.js +80 -0
  240. package/dist/esm/queries/QueryableStorage.js.map +1 -0
  241. package/dist/esm/queries/dbQueries.js +13 -3
  242. package/dist/esm/queries/dbQueries.js.map +1 -1
  243. package/dist/esm/queries/utils.js +1 -1
  244. package/dist/esm/queries/utils.js.map +1 -1
  245. package/dist/esm/sync/FileSync.d.ts +1 -0
  246. package/dist/esm/sync/FileSync.js +1 -0
  247. package/dist/esm/sync/FileSync.js.map +1 -1
  248. package/dist/esm/sync/PushPullSync.d.ts +2 -1
  249. package/dist/esm/sync/PushPullSync.js +7 -1
  250. package/dist/esm/sync/PushPullSync.js.map +1 -1
  251. package/dist/esm/sync/Sync.d.ts +6 -3
  252. package/dist/esm/sync/Sync.js +9 -4
  253. package/dist/esm/sync/Sync.js.map +1 -1
  254. package/dist/esm/sync/WebSocketSync.d.ts +4 -1
  255. package/dist/esm/sync/WebSocketSync.js +41 -11
  256. package/dist/esm/sync/WebSocketSync.js.map +1 -1
  257. package/dist/tsconfig-cjs.tsbuildinfo +1 -1
  258. package/dist/tsconfig.tsbuildinfo +1 -1
  259. package/package.json +8 -7
  260. package/src/DocumentManager.ts +3 -7
  261. package/src/IDBService.ts +78 -17
  262. package/src/UndoHistory.ts +5 -3
  263. package/src/__tests__/batching.test.ts +5 -2
  264. package/src/__tests__/documents.test.ts +66 -6
  265. package/src/__tests__/fixtures/testStorage.ts +9 -0
  266. package/src/__tests__/legacyOids.test.ts +53 -17
  267. package/src/__tests__/mutations.test.ts +9 -3
  268. package/src/__tests__/queries.test.ts +6 -2
  269. package/src/__tests__/setup/indexedDB.ts +14 -1
  270. package/src/__tests__/undo.test.ts +17 -9
  271. package/src/client/Client.ts +8 -4
  272. package/src/client/ClientDescriptor.ts +24 -8
  273. package/src/context.ts +16 -1
  274. package/src/entities/2/Entity.test.ts +218 -0
  275. package/src/entities/2/Entity.ts +954 -0
  276. package/src/entities/2/EntityCache.ts +41 -0
  277. package/src/entities/2/EntityMetadata.ts +364 -0
  278. package/src/entities/2/EntityStore.ts +490 -0
  279. package/src/entities/2/NOTES.md +22 -0
  280. package/src/entities/2/OperationBatcher.ts +251 -0
  281. package/src/entities/2/types.ts +154 -0
  282. package/src/files/EntityFile.ts +9 -4
  283. package/src/files/FileManager.ts +5 -3
  284. package/src/files/FileStorage.ts +7 -13
  285. package/src/files/utils.ts +9 -1
  286. package/src/idb.ts +51 -3
  287. package/src/index.ts +2 -2
  288. package/src/metadata/AckInfoStore.ts +1 -1
  289. package/src/metadata/BaselinesStore.ts +16 -24
  290. package/src/metadata/LocalReplicaStore.ts +13 -6
  291. package/src/metadata/Metadata.ts +109 -24
  292. package/src/metadata/OperationsStore.ts +37 -16
  293. package/src/migration/openDatabase.ts +32 -10
  294. package/src/queries/BaseQuery.ts +15 -2
  295. package/src/queries/CollectionQueries.ts +3 -3
  296. package/src/queries/FindAllQuery.ts +4 -0
  297. package/src/queries/QueryCache.ts +5 -0
  298. package/src/queries/QueryableStorage.ts +107 -0
  299. package/src/queries/dbQueries.ts +10 -3
  300. package/src/queries/utils.ts +1 -1
  301. package/src/sync/FileSync.ts +2 -0
  302. package/src/sync/PushPullSync.ts +8 -1
  303. package/src/sync/Sync.ts +14 -6
  304. package/src/sync/WebSocketSync.ts +47 -10
  305. package/src/entities/DocumentFamiliyCache.ts +0 -426
  306. package/src/entities/Entity.ts +0 -874
  307. package/src/entities/EntityStore.ts +0 -731
@@ -1,731 +0,0 @@
1
- import {
2
- assert,
3
- assignIndexValues,
4
- assignOid,
5
- assignOidPropertiesToAllSubObjects,
6
- Batcher,
7
- cloneDeep,
8
- decomposeOid,
9
- DocumentBaseline,
10
- EventSubscriber,
11
- generateId,
12
- getIndexValues,
13
- getOidRoot,
14
- getUndoOperations,
15
- groupBaselinesByRootOid,
16
- groupPatchesByIdentifier,
17
- groupPatchesByRootOid,
18
- ObjectIdentifier,
19
- Operation,
20
- removeOidsFromAllSubObjects,
21
- StorageCollectionSchema,
22
- StorageObjectFieldSchema,
23
- } from '@verdant-web/common';
24
- import { Context } from '../context.js';
25
- import { FileManager } from '../files/FileManager.js';
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';
31
-
32
- const DEFAULT_BATCH_KEY = '@@default';
33
-
34
- export interface OperationBatch {
35
- run: (fn: () => void) => this;
36
- flush: () => Promise<void>;
37
- discard: () => void;
38
- }
39
-
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;
68
-
69
- constructor({
70
- context,
71
- meta,
72
- batchTimeout = 200,
73
- files,
74
- }: {
75
- context: Context;
76
- meta: Metadata;
77
- files: FileManager;
78
- batchTimeout?: number;
79
- }) {
80
- this.context = context;
81
-
82
- this.defaultBatchTimeout = batchTimeout;
83
- this.meta = meta;
84
- 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 },
95
- });
96
- this.unsubscribes.push(this.meta.subscribe('rebase', this.handleRebase));
97
- }
98
-
99
- setContext = (context: Context) => {
100
- this.context = context;
101
- };
102
-
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: [] };
110
- }
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
- };
119
- };
120
-
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`,
131
- );
132
- return;
133
- }
134
-
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,
169
- });
170
- };
171
-
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
- });
183
-
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
204
- }
205
- await familyCache.initializedPromise;
206
-
207
- return familyCache;
208
- };
209
-
210
- private onEntityChange = async (oid: ObjectIdentifier) => {
211
- // queueMicrotask(() => this.writeDocumentToStorage(oid));
212
- };
213
-
214
- private writeDocumentToStorage = async (oid: ObjectIdentifier) => {
215
- if (this._disposed) {
216
- this.log('warn', 'EntityStore is disposed, not writing to storage');
217
- return;
218
- }
219
- const rootOid = getOidRoot(oid);
220
- const { id, collection } = decomposeOid(rootOid);
221
- const entity = await this.get(rootOid);
222
-
223
- if (this._disposed) {
224
- this.log('warn', 'EntityStore is disposed, not writing to storage');
225
- return;
226
- }
227
-
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;
266
- }
267
- }
268
- }
269
- }
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
-
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) {
291
- return null;
292
- }
293
- return cache.getEntity({ oid, fieldSchema: schema, readonlyKeys });
294
- }
295
- return null;
296
- };
297
-
298
- /**
299
- * Creates a new document and returns an Entity for it. The created
300
- * document is submitted to storage and sync.
301
- */
302
- create = async (
303
- initial: any,
304
- oid: ObjectIdentifier,
305
- options: { undoable?: boolean },
306
- ) => {
307
- // remove all OID associations from initial data
308
- removeOidsFromAllSubObjects(initial);
309
- // first grab any file and replace them with refs
310
- const processed = processValueFiles(initial, this.files.add);
311
-
312
- 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) {
323
- 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.`,
327
- );
328
- }
329
- return familyCache.getEntity({ oid, fieldSchema: schema, readonlyKeys });
330
- };
331
-
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
- }
354
- });
355
- };
356
-
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
- };
376
-
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
- }
424
-
425
- return allDocumentOids;
426
- };
427
-
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
- }
491
-
492
- // notify active queries
493
- const affectedCollections = Array.from(
494
- new Set<string>(
495
- allDocumentOids.map((oid) => decomposeOid(oid).collection),
496
- ),
497
- );
498
- this.context.log('changes to collections', affectedCollections);
499
- this.context.entityEvents.emit('collectionsChanged', affectedCollections);
500
- };
501
-
502
- addLocalOperations = async (operations: Operation[]) => {
503
- this.log('Adding local operations', operations.length);
504
- this.addOperationsToOpenCaches(operations, {
505
- isLocal: true,
506
- confirmed: false,
507
- });
508
- this.operationBatcher.add({
509
- key: this.currentBatchKey,
510
- items: operations,
511
- });
512
- };
513
-
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);
556
- },
557
- };
558
- return externalApi;
559
- };
560
-
561
- /**
562
- * @deprecated use `batch` instead
563
- */
564
- flushPatches = async () => {
565
- await this.operationBatcher.flush(this.currentBatchKey);
566
- };
567
-
568
- flushAllBatches = async () => {
569
- await Promise.all(this.operationBatcher.flushAll());
570
- };
571
-
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;
592
- }
593
- await this.submitOperations(operations, meta);
594
- };
595
-
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));
603
- }
604
- await this.meta.insertLocalOperation(operations);
605
-
606
- // confirm the operations
607
- this.addDataToOpenCaches({ operations, baselines: [] });
608
-
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
- };
628
-
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;
640
- };
641
-
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
- };
657
- };
658
-
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)),
677
- );
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
-
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();
700
- }
701
- this.documentFamilyCaches.clear();
702
- await this.flushAllBatches();
703
- };
704
-
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
- };
711
-
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
- };
722
-
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
- }
730
- };
731
- }