@verdant-web/store 2.0.1 → 2.0.3

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 (569) hide show
  1. package/dist/cjs/BackoffScheduler.d.ts +19 -19
  2. package/dist/cjs/BackoffScheduler.js +44 -44
  3. package/dist/cjs/DocumentManager.d.ts +28 -28
  4. package/dist/cjs/DocumentManager.js +44 -44
  5. package/dist/cjs/IDBService.d.ts +9 -9
  6. package/dist/cjs/IDBService.js +46 -46
  7. package/dist/cjs/UndoHistory.d.ts +16 -16
  8. package/dist/cjs/UndoHistory.js +50 -50
  9. package/dist/cjs/__tests__/batching.test.d.ts +1 -1
  10. package/dist/cjs/__tests__/batching.test.js +44 -44
  11. package/dist/cjs/__tests__/documents.test.d.ts +1 -1
  12. package/dist/cjs/__tests__/documents.test.js +398 -398
  13. package/dist/cjs/__tests__/fixtures/testStorage.d.ts +118 -118
  14. package/dist/cjs/__tests__/fixtures/testStorage.js +115 -115
  15. package/dist/cjs/__tests__/queries.test.d.ts +1 -1
  16. package/dist/cjs/__tests__/queries.test.js +262 -262
  17. package/dist/cjs/__tests__/setup/indexedDB.d.ts +1 -1
  18. package/dist/cjs/__tests__/setup/indexedDB.js +3 -3
  19. package/dist/cjs/__tests__/undo.test.d.ts +1 -1
  20. package/dist/cjs/__tests__/undo.test.js +86 -86
  21. package/dist/cjs/client/Client.d.ts +86 -86
  22. package/dist/cjs/client/Client.js +226 -226
  23. package/dist/cjs/client/ClientDescriptor.d.ts +62 -62
  24. package/dist/cjs/client/ClientDescriptor.js +108 -108
  25. package/dist/cjs/constants.d.ts +1 -1
  26. package/dist/cjs/constants.js +4 -4
  27. package/dist/cjs/context.d.ts +17 -17
  28. package/dist/cjs/context.js +2 -2
  29. package/dist/cjs/files/EntityFile.d.ts +27 -27
  30. package/dist/cjs/files/EntityFile.js +71 -71
  31. package/dist/cjs/files/FileManager.d.ts +42 -42
  32. package/dist/cjs/files/FileManager.js +145 -145
  33. package/dist/cjs/files/FileStorage.d.ts +27 -27
  34. package/dist/cjs/files/FileStorage.js +93 -93
  35. package/dist/cjs/files/utils.d.ts +8 -8
  36. package/dist/cjs/files/utils.js +55 -55
  37. package/dist/cjs/files/utils.test.d.ts +1 -1
  38. package/dist/cjs/files/utils.test.js +80 -80
  39. package/dist/cjs/idb.d.ts +8 -8
  40. package/dist/cjs/idb.js +87 -87
  41. package/dist/cjs/index.d.ts +19 -19
  42. package/dist/cjs/index.js +20 -20
  43. package/dist/cjs/indexes.d.ts +3 -3
  44. package/dist/cjs/indexes.js +19 -19
  45. package/dist/cjs/metadata/AckInfoStore.d.ts +10 -10
  46. package/dist/cjs/metadata/AckInfoStore.js +25 -25
  47. package/dist/cjs/metadata/BaselinesStore.d.ts +33 -33
  48. package/dist/cjs/metadata/BaselinesStore.js +59 -59
  49. package/dist/cjs/metadata/LocalReplicaStore.d.ts +17 -17
  50. package/dist/cjs/metadata/LocalReplicaStore.js +47 -47
  51. package/dist/cjs/metadata/MessageCreator.d.ts +19 -19
  52. package/dist/cjs/metadata/MessageCreator.js +127 -127
  53. package/dist/cjs/metadata/Metadata.d.ts +106 -106
  54. package/dist/cjs/metadata/Metadata.js +336 -336
  55. package/dist/cjs/metadata/OperationsStore.d.ts +50 -50
  56. package/dist/cjs/metadata/OperationsStore.js +145 -145
  57. package/dist/cjs/metadata/SchemaStore.d.ts +9 -9
  58. package/dist/cjs/metadata/SchemaStore.js +38 -38
  59. package/dist/cjs/metadata/openMetadataDatabase.d.ts +8 -8
  60. package/dist/cjs/metadata/openMetadataDatabase.js +105 -105
  61. package/dist/cjs/openDocumentDatabase.d.ts +12 -12
  62. package/dist/cjs/openDocumentDatabase.js +424 -424
  63. package/dist/cjs/queries2/BaseQuery.d.ts +39 -39
  64. package/dist/cjs/queries2/BaseQuery.js +98 -98
  65. package/dist/cjs/queries2/CollectionQueries.d.ts +55 -55
  66. package/dist/cjs/queries2/CollectionQueries.js +82 -82
  67. package/dist/cjs/queries2/FindAllQuery.d.ts +11 -11
  68. package/dist/cjs/queries2/FindAllQuery.js +33 -33
  69. package/dist/cjs/queries2/FindInfiniteQuery.d.ts +18 -18
  70. package/dist/cjs/queries2/FindInfiniteQuery.js +60 -60
  71. package/dist/cjs/queries2/FindOneQuery.d.ts +11 -11
  72. package/dist/cjs/queries2/FindOneQuery.js +33 -33
  73. package/dist/cjs/queries2/FindPageQuery.d.ts +23 -23
  74. package/dist/cjs/queries2/FindPageQuery.js +67 -67
  75. package/dist/cjs/queries2/GetQuery.d.ts +10 -10
  76. package/dist/cjs/queries2/GetQuery.js +29 -29
  77. package/dist/cjs/queries2/QueryCache.d.ts +16 -16
  78. package/dist/cjs/queries2/QueryCache.js +39 -39
  79. package/dist/cjs/queries2/dbQueries.d.ts +22 -22
  80. package/dist/cjs/queries2/dbQueries.js +125 -125
  81. package/dist/cjs/queries2/keys.d.ts +10 -10
  82. package/dist/cjs/queries2/keys.js +33 -33
  83. package/dist/cjs/queries2/ranges.d.ts +2 -2
  84. package/dist/cjs/queries2/ranges.js +69 -69
  85. package/dist/cjs/queries2/types.d.ts +6 -6
  86. package/dist/cjs/queries2/types.js +2 -2
  87. package/dist/cjs/queries2/utils.d.ts +1 -1
  88. package/dist/cjs/queries2/utils.js +19 -19
  89. package/dist/cjs/reactives/DocumentFamiliyCache.d.ts +51 -47
  90. package/dist/cjs/reactives/DocumentFamiliyCache.js +214 -210
  91. package/dist/cjs/reactives/DocumentFamiliyCache.js.map +1 -1
  92. package/dist/cjs/reactives/Entity.d.ts +192 -191
  93. package/dist/cjs/reactives/Entity.js +466 -462
  94. package/dist/cjs/reactives/Entity.js.map +1 -1
  95. package/dist/cjs/reactives/EntityStore.d.ts +84 -84
  96. package/dist/cjs/reactives/EntityStore.js +425 -421
  97. package/dist/cjs/reactives/EntityStore.js.map +1 -1
  98. package/dist/cjs/reactives/FakeWeakRef.d.ts +11 -11
  99. package/dist/cjs/reactives/FakeWeakRef.js +18 -18
  100. package/dist/cjs/sync/FileSync.d.ts +23 -23
  101. package/dist/cjs/sync/FileSync.js +84 -84
  102. package/dist/cjs/sync/Heartbeat.d.ts +24 -24
  103. package/dist/cjs/sync/Heartbeat.js +55 -55
  104. package/dist/cjs/sync/PresenceManager.d.ts +41 -41
  105. package/dist/cjs/sync/PresenceManager.js +107 -107
  106. package/dist/cjs/sync/PushPullSync.d.ts +36 -36
  107. package/dist/cjs/sync/PushPullSync.js +141 -141
  108. package/dist/cjs/sync/ServerSyncEndpointProvider.d.ts +27 -27
  109. package/dist/cjs/sync/ServerSyncEndpointProvider.js +64 -64
  110. package/dist/cjs/sync/Sync.d.ts +127 -127
  111. package/dist/cjs/sync/Sync.js +244 -244
  112. package/dist/cjs/sync/WebSocketSync.d.ts +41 -41
  113. package/dist/cjs/sync/WebSocketSync.js +183 -183
  114. package/dist/cjs/types.d.ts +5 -5
  115. package/dist/cjs/types.js +2 -2
  116. package/dist/cjs/utils/Disposable.d.ts +6 -6
  117. package/dist/cjs/utils/Disposable.js +18 -18
  118. package/dist/cjs/utils/Resolvable.d.ts +8 -8
  119. package/dist/cjs/utils/Resolvable.js +22 -22
  120. package/dist/esm/BackoffScheduler.d.ts +19 -19
  121. package/dist/esm/BackoffScheduler.js +39 -39
  122. package/dist/esm/DocumentManager.d.ts +28 -28
  123. package/dist/esm/DocumentManager.js +40 -40
  124. package/dist/esm/IDBService.d.ts +9 -9
  125. package/dist/esm/IDBService.js +42 -42
  126. package/dist/esm/UndoHistory.d.ts +16 -16
  127. package/dist/esm/UndoHistory.js +46 -46
  128. package/dist/esm/__tests__/batching.test.d.ts +1 -1
  129. package/dist/esm/__tests__/batching.test.js +42 -42
  130. package/dist/esm/__tests__/documents.test.d.ts +1 -1
  131. package/dist/esm/__tests__/documents.test.js +396 -396
  132. package/dist/esm/__tests__/fixtures/testStorage.d.ts +118 -118
  133. package/dist/esm/__tests__/fixtures/testStorage.js +111 -111
  134. package/dist/esm/__tests__/queries.test.d.ts +1 -1
  135. package/dist/esm/__tests__/queries.test.js +260 -260
  136. package/dist/esm/__tests__/setup/indexedDB.d.ts +1 -1
  137. package/dist/esm/__tests__/setup/indexedDB.js +1 -1
  138. package/dist/esm/__tests__/undo.test.d.ts +1 -1
  139. package/dist/esm/__tests__/undo.test.js +84 -84
  140. package/dist/esm/client/Client.d.ts +86 -86
  141. package/dist/esm/client/Client.js +222 -222
  142. package/dist/esm/client/ClientDescriptor.d.ts +62 -62
  143. package/dist/esm/client/ClientDescriptor.js +104 -104
  144. package/dist/esm/constants.d.ts +1 -1
  145. package/dist/esm/constants.js +1 -1
  146. package/dist/esm/context.d.ts +17 -17
  147. package/dist/esm/context.js +1 -1
  148. package/dist/esm/files/EntityFile.d.ts +27 -27
  149. package/dist/esm/files/EntityFile.js +67 -67
  150. package/dist/esm/files/FileManager.d.ts +42 -42
  151. package/dist/esm/files/FileManager.js +141 -141
  152. package/dist/esm/files/FileStorage.d.ts +27 -27
  153. package/dist/esm/files/FileStorage.js +88 -88
  154. package/dist/esm/files/utils.d.ts +8 -8
  155. package/dist/esm/files/utils.js +46 -46
  156. package/dist/esm/files/utils.test.d.ts +1 -1
  157. package/dist/esm/files/utils.test.js +78 -78
  158. package/dist/esm/idb.d.ts +8 -8
  159. package/dist/esm/idb.js +79 -79
  160. package/dist/esm/index.d.ts +19 -19
  161. package/dist/esm/index.js +11 -11
  162. package/dist/esm/indexes.d.ts +3 -3
  163. package/dist/esm/indexes.js +14 -14
  164. package/dist/esm/metadata/AckInfoStore.d.ts +10 -10
  165. package/dist/esm/metadata/AckInfoStore.js +21 -21
  166. package/dist/esm/metadata/BaselinesStore.d.ts +33 -33
  167. package/dist/esm/metadata/BaselinesStore.js +55 -55
  168. package/dist/esm/metadata/LocalReplicaStore.d.ts +17 -17
  169. package/dist/esm/metadata/LocalReplicaStore.js +40 -40
  170. package/dist/esm/metadata/MessageCreator.d.ts +19 -19
  171. package/dist/esm/metadata/MessageCreator.js +123 -123
  172. package/dist/esm/metadata/Metadata.d.ts +106 -106
  173. package/dist/esm/metadata/Metadata.js +332 -332
  174. package/dist/esm/metadata/OperationsStore.d.ts +50 -50
  175. package/dist/esm/metadata/OperationsStore.js +141 -141
  176. package/dist/esm/metadata/SchemaStore.d.ts +9 -9
  177. package/dist/esm/metadata/SchemaStore.js +34 -34
  178. package/dist/esm/metadata/openMetadataDatabase.d.ts +8 -8
  179. package/dist/esm/metadata/openMetadataDatabase.js +101 -101
  180. package/dist/esm/openDocumentDatabase.d.ts +12 -12
  181. package/dist/esm/openDocumentDatabase.js +420 -420
  182. package/dist/esm/queries2/BaseQuery.d.ts +39 -39
  183. package/dist/esm/queries2/BaseQuery.js +94 -94
  184. package/dist/esm/queries2/CollectionQueries.d.ts +55 -55
  185. package/dist/esm/queries2/CollectionQueries.js +78 -78
  186. package/dist/esm/queries2/FindAllQuery.d.ts +11 -11
  187. package/dist/esm/queries2/FindAllQuery.js +29 -29
  188. package/dist/esm/queries2/FindInfiniteQuery.d.ts +18 -18
  189. package/dist/esm/queries2/FindInfiniteQuery.js +56 -56
  190. package/dist/esm/queries2/FindOneQuery.d.ts +11 -11
  191. package/dist/esm/queries2/FindOneQuery.js +29 -29
  192. package/dist/esm/queries2/FindPageQuery.d.ts +23 -23
  193. package/dist/esm/queries2/FindPageQuery.js +63 -63
  194. package/dist/esm/queries2/GetQuery.d.ts +10 -10
  195. package/dist/esm/queries2/GetQuery.js +25 -25
  196. package/dist/esm/queries2/QueryCache.d.ts +16 -16
  197. package/dist/esm/queries2/QueryCache.js +35 -35
  198. package/dist/esm/queries2/dbQueries.d.ts +22 -22
  199. package/dist/esm/queries2/dbQueries.js +119 -119
  200. package/dist/esm/queries2/keys.d.ts +10 -10
  201. package/dist/esm/queries2/keys.js +29 -29
  202. package/dist/esm/queries2/ranges.d.ts +2 -2
  203. package/dist/esm/queries2/ranges.js +65 -65
  204. package/dist/esm/queries2/types.d.ts +6 -6
  205. package/dist/esm/queries2/types.js +1 -1
  206. package/dist/esm/queries2/utils.d.ts +1 -1
  207. package/dist/esm/queries2/utils.js +15 -15
  208. package/dist/esm/reactives/DocumentFamiliyCache.d.ts +51 -47
  209. package/dist/esm/reactives/DocumentFamiliyCache.js +210 -206
  210. package/dist/esm/reactives/DocumentFamiliyCache.js.map +1 -1
  211. package/dist/esm/reactives/Entity.d.ts +192 -191
  212. package/dist/esm/reactives/Entity.js +461 -457
  213. package/dist/esm/reactives/Entity.js.map +1 -1
  214. package/dist/esm/reactives/EntityStore.d.ts +84 -84
  215. package/dist/esm/reactives/EntityStore.js +421 -417
  216. package/dist/esm/reactives/EntityStore.js.map +1 -1
  217. package/dist/esm/reactives/FakeWeakRef.d.ts +11 -11
  218. package/dist/esm/reactives/FakeWeakRef.js +14 -14
  219. package/dist/esm/sync/FileSync.d.ts +23 -23
  220. package/dist/esm/sync/FileSync.js +80 -80
  221. package/dist/esm/sync/Heartbeat.d.ts +24 -24
  222. package/dist/esm/sync/Heartbeat.js +51 -51
  223. package/dist/esm/sync/PresenceManager.d.ts +41 -41
  224. package/dist/esm/sync/PresenceManager.js +103 -103
  225. package/dist/esm/sync/PushPullSync.d.ts +36 -36
  226. package/dist/esm/sync/PushPullSync.js +137 -137
  227. package/dist/esm/sync/ServerSyncEndpointProvider.d.ts +27 -27
  228. package/dist/esm/sync/ServerSyncEndpointProvider.js +57 -57
  229. package/dist/esm/sync/Sync.d.ts +127 -127
  230. package/dist/esm/sync/Sync.js +239 -239
  231. package/dist/esm/sync/{WebsocketSync.d.ts → WebSocketSync.d.ts} +41 -41
  232. package/dist/esm/sync/{WebsocketSync.js → WebSocketSync.js} +179 -179
  233. package/dist/esm/types.d.ts +5 -5
  234. package/dist/esm/types.js +1 -1
  235. package/dist/esm/utils/Disposable.d.ts +6 -6
  236. package/dist/esm/utils/Disposable.js +14 -14
  237. package/dist/esm/utils/Resolvable.d.ts +8 -8
  238. package/dist/esm/utils/Resolvable.js +18 -18
  239. package/dist/tsconfig-cjs.tsbuildinfo +1 -1
  240. package/dist/tsconfig.tsbuildinfo +1 -1
  241. package/package.json +2 -2
  242. package/src/reactives/DocumentFamiliyCache.ts +4 -0
  243. package/src/reactives/Entity.ts +8 -1
  244. package/src/reactives/EntityStore.ts +18 -13
  245. package/dist/cjs/AckInfoStore.d.ts +0 -10
  246. package/dist/cjs/AckInfoStore.js +0 -26
  247. package/dist/cjs/AckInfoStore.js.map +0 -1
  248. package/dist/cjs/BaselinesStore.d.ts +0 -10
  249. package/dist/cjs/BaselinesStore.js +0 -36
  250. package/dist/cjs/BaselinesStore.js.map +0 -1
  251. package/dist/cjs/Entity.d.ts +0 -96
  252. package/dist/cjs/Entity.js +0 -345
  253. package/dist/cjs/Entity.js.map +0 -1
  254. package/dist/cjs/EntityStore.d.ts +0 -36
  255. package/dist/cjs/EntityStore.js +0 -172
  256. package/dist/cjs/EntityStore.js.map +0 -1
  257. package/dist/cjs/LocalHistoryStore.d.ts +0 -15
  258. package/dist/cjs/LocalHistoryStore.js +0 -53
  259. package/dist/cjs/LocalHistoryStore.js.map +0 -1
  260. package/dist/cjs/LocalReplicaStore.d.ts +0 -13
  261. package/dist/cjs/LocalReplicaStore.js +0 -49
  262. package/dist/cjs/LocalReplicaStore.js.map +0 -1
  263. package/dist/cjs/MessageCreator.d.ts +0 -19
  264. package/dist/cjs/MessageCreator.js +0 -104
  265. package/dist/cjs/MessageCreator.js.map +0 -1
  266. package/dist/cjs/Metadata.d.ts +0 -91
  267. package/dist/cjs/Metadata.js +0 -250
  268. package/dist/cjs/Metadata.js.map +0 -1
  269. package/dist/cjs/OperationsStore.d.ts +0 -38
  270. package/dist/cjs/OperationsStore.js +0 -146
  271. package/dist/cjs/OperationsStore.js.map +0 -1
  272. package/dist/cjs/PresenceManager.d.ts +0 -30
  273. package/dist/cjs/PresenceManager.js +0 -89
  274. package/dist/cjs/PresenceManager.js.map +0 -1
  275. package/dist/cjs/Query.d.ts +0 -26
  276. package/dist/cjs/Query.js +0 -86
  277. package/dist/cjs/Query.js.map +0 -1
  278. package/dist/cjs/QueryMaker.d.ts +0 -18
  279. package/dist/cjs/QueryMaker.js +0 -98
  280. package/dist/cjs/QueryMaker.js.map +0 -1
  281. package/dist/cjs/QueryStore.d.ts +0 -38
  282. package/dist/cjs/QueryStore.js +0 -146
  283. package/dist/cjs/QueryStore.js.map +0 -1
  284. package/dist/cjs/SchemaStore.d.ts +0 -9
  285. package/dist/cjs/SchemaStore.js +0 -35
  286. package/dist/cjs/SchemaStore.js.map +0 -1
  287. package/dist/cjs/Storage.d.ts +0 -147
  288. package/dist/cjs/Storage.js +0 -346
  289. package/dist/cjs/Storage.js.map +0 -1
  290. package/dist/cjs/Sync.d.ts +0 -117
  291. package/dist/cjs/Sync.js +0 -561
  292. package/dist/cjs/Sync.js.map +0 -1
  293. package/dist/cjs/SyncHarness.d.ts +0 -45
  294. package/dist/cjs/SyncHarness.js +0 -110
  295. package/dist/cjs/SyncHarness.js.map +0 -1
  296. package/dist/cjs/metadata/LocalHistoryStore.d.ts +0 -15
  297. package/dist/cjs/metadata/LocalHistoryStore.js +0 -53
  298. package/dist/cjs/metadata/LocalHistoryStore.js.map +0 -1
  299. package/dist/cjs/queries/LiveQuery.d.ts +0 -28
  300. package/dist/cjs/queries/LiveQuery.js +0 -96
  301. package/dist/cjs/queries/LiveQuery.js.map +0 -1
  302. package/dist/cjs/queries/LiveQueryMaker.d.ts +0 -15
  303. package/dist/cjs/queries/LiveQueryMaker.js +0 -42
  304. package/dist/cjs/queries/LiveQueryMaker.js.map +0 -1
  305. package/dist/cjs/queries/LiveQueryStore.d.ts +0 -33
  306. package/dist/cjs/queries/LiveQueryStore.js +0 -85
  307. package/dist/cjs/queries/LiveQueryStore.js.map +0 -1
  308. package/dist/cjs/queries/Query.d.ts +0 -18
  309. package/dist/cjs/queries/Query.js +0 -36
  310. package/dist/cjs/queries/Query.js.map +0 -1
  311. package/dist/cjs/queries/QueryMaker.d.ts +0 -14
  312. package/dist/cjs/queries/QueryMaker.js +0 -42
  313. package/dist/cjs/queries/QueryMaker.js.map +0 -1
  314. package/dist/cjs/queries/QueryStore.d.ts +0 -35
  315. package/dist/cjs/queries/QueryStore.js +0 -130
  316. package/dist/cjs/queries/QueryStore.js.map +0 -1
  317. package/dist/cjs/queries/ranges.d.ts +0 -2
  318. package/dist/cjs/queries/ranges.js +0 -70
  319. package/dist/cjs/queries/ranges.js.map +0 -1
  320. package/dist/cjs/v2/AckInfoStore.d.ts +0 -10
  321. package/dist/cjs/v2/AckInfoStore.js +0 -26
  322. package/dist/cjs/v2/AckInfoStore.js.map +0 -1
  323. package/dist/cjs/v2/BaselinesStore.d.ts +0 -10
  324. package/dist/cjs/v2/BaselinesStore.js +0 -36
  325. package/dist/cjs/v2/BaselinesStore.js.map +0 -1
  326. package/dist/cjs/v2/DocumentManager.d.ts +0 -19
  327. package/dist/cjs/v2/DocumentManager.js +0 -47
  328. package/dist/cjs/v2/DocumentManager.js.map +0 -1
  329. package/dist/cjs/v2/Entity.d.ts +0 -87
  330. package/dist/cjs/v2/Entity.js +0 -305
  331. package/dist/cjs/v2/Entity.js.map +0 -1
  332. package/dist/cjs/v2/EntityStore.d.ts +0 -37
  333. package/dist/cjs/v2/EntityStore.js +0 -165
  334. package/dist/cjs/v2/EntityStore.js.map +0 -1
  335. package/dist/cjs/v2/IDBService.d.ts +0 -7
  336. package/dist/cjs/v2/IDBService.js +0 -26
  337. package/dist/cjs/v2/IDBService.js.map +0 -1
  338. package/dist/cjs/v2/LocalHistoryStore.d.ts +0 -15
  339. package/dist/cjs/v2/LocalHistoryStore.js +0 -53
  340. package/dist/cjs/v2/LocalHistoryStore.js.map +0 -1
  341. package/dist/cjs/v2/LocalReplicaStore.d.ts +0 -14
  342. package/dist/cjs/v2/LocalReplicaStore.js +0 -49
  343. package/dist/cjs/v2/LocalReplicaStore.js.map +0 -1
  344. package/dist/cjs/v2/MessageCreator.d.ts +0 -19
  345. package/dist/cjs/v2/MessageCreator.js +0 -99
  346. package/dist/cjs/v2/MessageCreator.js.map +0 -1
  347. package/dist/cjs/v2/Metadata.d.ts +0 -90
  348. package/dist/cjs/v2/Metadata.js +0 -251
  349. package/dist/cjs/v2/Metadata.js.map +0 -1
  350. package/dist/cjs/v2/OperationsStore.d.ts +0 -38
  351. package/dist/cjs/v2/OperationsStore.js +0 -146
  352. package/dist/cjs/v2/OperationsStore.js.map +0 -1
  353. package/dist/cjs/v2/PresenceManager.d.ts +0 -24
  354. package/dist/cjs/v2/PresenceManager.js +0 -73
  355. package/dist/cjs/v2/PresenceManager.js.map +0 -1
  356. package/dist/cjs/v2/Query.d.ts +0 -24
  357. package/dist/cjs/v2/Query.js +0 -79
  358. package/dist/cjs/v2/Query.js.map +0 -1
  359. package/dist/cjs/v2/QueryMaker.d.ts +0 -16
  360. package/dist/cjs/v2/QueryMaker.js +0 -84
  361. package/dist/cjs/v2/QueryMaker.js.map +0 -1
  362. package/dist/cjs/v2/QueryStore.d.ts +0 -26
  363. package/dist/cjs/v2/QueryStore.js +0 -136
  364. package/dist/cjs/v2/QueryStore.js.map +0 -1
  365. package/dist/cjs/v2/SchemaStore.d.ts +0 -9
  366. package/dist/cjs/v2/SchemaStore.js +0 -35
  367. package/dist/cjs/v2/SchemaStore.js.map +0 -1
  368. package/dist/cjs/v2/Storage.d.ts +0 -80
  369. package/dist/cjs/v2/Storage.js +0 -154
  370. package/dist/cjs/v2/Storage.js.map +0 -1
  371. package/dist/cjs/v2/Sync.d.ts +0 -42
  372. package/dist/cjs/v2/Sync.js +0 -98
  373. package/dist/cjs/v2/Sync.js.map +0 -1
  374. package/dist/cjs/v2/SyncHarness.d.ts +0 -45
  375. package/dist/cjs/v2/SyncHarness.js +0 -110
  376. package/dist/cjs/v2/SyncHarness.js.map +0 -1
  377. package/dist/cjs/v2/__tests__/documents.test.d.ts +0 -1
  378. package/dist/cjs/v2/__tests__/documents.test.js +0 -185
  379. package/dist/cjs/v2/__tests__/documents.test.js.map +0 -1
  380. package/dist/cjs/v2/__tests__/fixtures/testStorage.d.ts +0 -189
  381. package/dist/cjs/v2/__tests__/fixtures/testStorage.js +0 -93
  382. package/dist/cjs/v2/__tests__/fixtures/testStorage.js.map +0 -1
  383. package/dist/cjs/v2/__tests__/queries.test.d.ts +0 -1
  384. package/dist/cjs/v2/__tests__/queries.test.js +0 -92
  385. package/dist/cjs/v2/__tests__/queries.test.js.map +0 -1
  386. package/dist/cjs/v2/__tests__/setup/indexedDB.d.ts +0 -1
  387. package/dist/cjs/v2/__tests__/setup/indexedDB.js +0 -4
  388. package/dist/cjs/v2/__tests__/setup/indexedDB.js.map +0 -1
  389. package/dist/cjs/v2/constants.d.ts +0 -1
  390. package/dist/cjs/v2/constants.js +0 -5
  391. package/dist/cjs/v2/constants.js.map +0 -1
  392. package/dist/cjs/v2/idb.d.ts +0 -6
  393. package/dist/cjs/v2/idb.js +0 -71
  394. package/dist/cjs/v2/idb.js.map +0 -1
  395. package/dist/cjs/v2/index.d.ts +0 -6
  396. package/dist/cjs/v2/index.js +0 -14
  397. package/dist/cjs/v2/index.js.map +0 -1
  398. package/dist/cjs/v2/indexes.d.ts +0 -3
  399. package/dist/cjs/v2/indexes.js +0 -20
  400. package/dist/cjs/v2/indexes.js.map +0 -1
  401. package/dist/cjs/v2/openDocumentDatabase.d.ts +0 -9
  402. package/dist/cjs/v2/openDocumentDatabase.js +0 -100
  403. package/dist/cjs/v2/openDocumentDatabase.js.map +0 -1
  404. package/dist/cjs/v2/types.d.ts +0 -3
  405. package/dist/cjs/v2/types.js +0 -3
  406. package/dist/cjs/v2/types.js.map +0 -1
  407. package/dist/esm/AckInfoStore.d.ts +0 -10
  408. package/dist/esm/AckInfoStore.js +0 -22
  409. package/dist/esm/AckInfoStore.js.map +0 -1
  410. package/dist/esm/BaselinesStore.d.ts +0 -10
  411. package/dist/esm/BaselinesStore.js +0 -32
  412. package/dist/esm/BaselinesStore.js.map +0 -1
  413. package/dist/esm/Entity.d.ts +0 -96
  414. package/dist/esm/Entity.js +0 -337
  415. package/dist/esm/Entity.js.map +0 -1
  416. package/dist/esm/EntityStore.d.ts +0 -36
  417. package/dist/esm/EntityStore.js +0 -168
  418. package/dist/esm/EntityStore.js.map +0 -1
  419. package/dist/esm/LocalHistoryStore.d.ts +0 -15
  420. package/dist/esm/LocalHistoryStore.js +0 -49
  421. package/dist/esm/LocalHistoryStore.js.map +0 -1
  422. package/dist/esm/LocalReplicaStore.d.ts +0 -13
  423. package/dist/esm/LocalReplicaStore.js +0 -42
  424. package/dist/esm/LocalReplicaStore.js.map +0 -1
  425. package/dist/esm/MessageCreator.d.ts +0 -19
  426. package/dist/esm/MessageCreator.js +0 -97
  427. package/dist/esm/MessageCreator.js.map +0 -1
  428. package/dist/esm/Metadata.d.ts +0 -91
  429. package/dist/esm/Metadata.js +0 -245
  430. package/dist/esm/Metadata.js.map +0 -1
  431. package/dist/esm/OperationsStore.d.ts +0 -38
  432. package/dist/esm/OperationsStore.js +0 -142
  433. package/dist/esm/OperationsStore.js.map +0 -1
  434. package/dist/esm/PresenceManager.d.ts +0 -30
  435. package/dist/esm/PresenceManager.js +0 -85
  436. package/dist/esm/PresenceManager.js.map +0 -1
  437. package/dist/esm/Query.d.ts +0 -26
  438. package/dist/esm/Query.js +0 -82
  439. package/dist/esm/Query.js.map +0 -1
  440. package/dist/esm/QueryMaker.d.ts +0 -18
  441. package/dist/esm/QueryMaker.js +0 -94
  442. package/dist/esm/QueryMaker.js.map +0 -1
  443. package/dist/esm/QueryStore.d.ts +0 -38
  444. package/dist/esm/QueryStore.js +0 -142
  445. package/dist/esm/QueryStore.js.map +0 -1
  446. package/dist/esm/SchemaStore.d.ts +0 -9
  447. package/dist/esm/SchemaStore.js +0 -31
  448. package/dist/esm/SchemaStore.js.map +0 -1
  449. package/dist/esm/Storage.d.ts +0 -147
  450. package/dist/esm/Storage.js +0 -341
  451. package/dist/esm/Storage.js.map +0 -1
  452. package/dist/esm/Sync.d.ts +0 -117
  453. package/dist/esm/Sync.js +0 -553
  454. package/dist/esm/Sync.js.map +0 -1
  455. package/dist/esm/SyncHarness.d.ts +0 -45
  456. package/dist/esm/SyncHarness.js +0 -105
  457. package/dist/esm/SyncHarness.js.map +0 -1
  458. package/dist/esm/metadata/LocalHistoryStore.d.ts +0 -15
  459. package/dist/esm/metadata/LocalHistoryStore.js +0 -49
  460. package/dist/esm/metadata/LocalHistoryStore.js.map +0 -1
  461. package/dist/esm/queries/LiveQuery.d.ts +0 -28
  462. package/dist/esm/queries/LiveQuery.js +0 -92
  463. package/dist/esm/queries/LiveQuery.js.map +0 -1
  464. package/dist/esm/queries/LiveQueryMaker.d.ts +0 -15
  465. package/dist/esm/queries/LiveQueryMaker.js +0 -38
  466. package/dist/esm/queries/LiveQueryMaker.js.map +0 -1
  467. package/dist/esm/queries/LiveQueryStore.d.ts +0 -33
  468. package/dist/esm/queries/LiveQueryStore.js +0 -81
  469. package/dist/esm/queries/LiveQueryStore.js.map +0 -1
  470. package/dist/esm/queries/Query.d.ts +0 -18
  471. package/dist/esm/queries/Query.js +0 -32
  472. package/dist/esm/queries/Query.js.map +0 -1
  473. package/dist/esm/queries/QueryMaker.d.ts +0 -14
  474. package/dist/esm/queries/QueryMaker.js +0 -38
  475. package/dist/esm/queries/QueryMaker.js.map +0 -1
  476. package/dist/esm/queries/QueryStore.d.ts +0 -35
  477. package/dist/esm/queries/QueryStore.js +0 -126
  478. package/dist/esm/queries/QueryStore.js.map +0 -1
  479. package/dist/esm/queries/ranges.d.ts +0 -2
  480. package/dist/esm/queries/ranges.js +0 -66
  481. package/dist/esm/queries/ranges.js.map +0 -1
  482. package/dist/esm/v2/AckInfoStore.d.ts +0 -10
  483. package/dist/esm/v2/AckInfoStore.js +0 -22
  484. package/dist/esm/v2/AckInfoStore.js.map +0 -1
  485. package/dist/esm/v2/BaselinesStore.d.ts +0 -10
  486. package/dist/esm/v2/BaselinesStore.js +0 -32
  487. package/dist/esm/v2/BaselinesStore.js.map +0 -1
  488. package/dist/esm/v2/DocumentManager.d.ts +0 -19
  489. package/dist/esm/v2/DocumentManager.js +0 -43
  490. package/dist/esm/v2/DocumentManager.js.map +0 -1
  491. package/dist/esm/v2/Entity.d.ts +0 -91
  492. package/dist/esm/v2/Entity.js +0 -305
  493. package/dist/esm/v2/Entity.js.map +0 -1
  494. package/dist/esm/v2/EntityStore.d.ts +0 -37
  495. package/dist/esm/v2/EntityStore.js +0 -166
  496. package/dist/esm/v2/EntityStore.js.map +0 -1
  497. package/dist/esm/v2/IDBService.d.ts +0 -7
  498. package/dist/esm/v2/IDBService.js +0 -22
  499. package/dist/esm/v2/IDBService.js.map +0 -1
  500. package/dist/esm/v2/LocalHistoryStore.d.ts +0 -15
  501. package/dist/esm/v2/LocalHistoryStore.js +0 -49
  502. package/dist/esm/v2/LocalHistoryStore.js.map +0 -1
  503. package/dist/esm/v2/LocalReplicaStore.d.ts +0 -14
  504. package/dist/esm/v2/LocalReplicaStore.js +0 -42
  505. package/dist/esm/v2/LocalReplicaStore.js.map +0 -1
  506. package/dist/esm/v2/MessageCreator.d.ts +0 -19
  507. package/dist/esm/v2/MessageCreator.js +0 -92
  508. package/dist/esm/v2/MessageCreator.js.map +0 -1
  509. package/dist/esm/v2/Metadata.d.ts +0 -90
  510. package/dist/esm/v2/Metadata.js +0 -246
  511. package/dist/esm/v2/Metadata.js.map +0 -1
  512. package/dist/esm/v2/OperationsStore.d.ts +0 -38
  513. package/dist/esm/v2/OperationsStore.js +0 -142
  514. package/dist/esm/v2/OperationsStore.js.map +0 -1
  515. package/dist/esm/v2/PresenceManager.d.ts +0 -24
  516. package/dist/esm/v2/PresenceManager.js +0 -69
  517. package/dist/esm/v2/PresenceManager.js.map +0 -1
  518. package/dist/esm/v2/Query.d.ts +0 -24
  519. package/dist/esm/v2/Query.js +0 -75
  520. package/dist/esm/v2/Query.js.map +0 -1
  521. package/dist/esm/v2/QueryMaker.d.ts +0 -16
  522. package/dist/esm/v2/QueryMaker.js +0 -80
  523. package/dist/esm/v2/QueryMaker.js.map +0 -1
  524. package/dist/esm/v2/QueryStore.d.ts +0 -26
  525. package/dist/esm/v2/QueryStore.js +0 -132
  526. package/dist/esm/v2/QueryStore.js.map +0 -1
  527. package/dist/esm/v2/SchemaStore.d.ts +0 -9
  528. package/dist/esm/v2/SchemaStore.js +0 -31
  529. package/dist/esm/v2/SchemaStore.js.map +0 -1
  530. package/dist/esm/v2/Storage.d.ts +0 -80
  531. package/dist/esm/v2/Storage.js +0 -149
  532. package/dist/esm/v2/Storage.js.map +0 -1
  533. package/dist/esm/v2/Sync.d.ts +0 -42
  534. package/dist/esm/v2/Sync.js +0 -94
  535. package/dist/esm/v2/Sync.js.map +0 -1
  536. package/dist/esm/v2/SyncHarness.d.ts +0 -45
  537. package/dist/esm/v2/SyncHarness.js +0 -105
  538. package/dist/esm/v2/SyncHarness.js.map +0 -1
  539. package/dist/esm/v2/__tests__/documents.test.d.ts +0 -1
  540. package/dist/esm/v2/__tests__/documents.test.js +0 -208
  541. package/dist/esm/v2/__tests__/documents.test.js.map +0 -1
  542. package/dist/esm/v2/__tests__/fixtures/testStorage.d.ts +0 -223
  543. package/dist/esm/v2/__tests__/fixtures/testStorage.js +0 -106
  544. package/dist/esm/v2/__tests__/fixtures/testStorage.js.map +0 -1
  545. package/dist/esm/v2/__tests__/queries.test.d.ts +0 -1
  546. package/dist/esm/v2/__tests__/queries.test.js +0 -90
  547. package/dist/esm/v2/__tests__/queries.test.js.map +0 -1
  548. package/dist/esm/v2/__tests__/setup/indexedDB.d.ts +0 -1
  549. package/dist/esm/v2/__tests__/setup/indexedDB.js +0 -2
  550. package/dist/esm/v2/__tests__/setup/indexedDB.js.map +0 -1
  551. package/dist/esm/v2/constants.d.ts +0 -1
  552. package/dist/esm/v2/constants.js +0 -2
  553. package/dist/esm/v2/constants.js.map +0 -1
  554. package/dist/esm/v2/idb.d.ts +0 -6
  555. package/dist/esm/v2/idb.js +0 -65
  556. package/dist/esm/v2/idb.js.map +0 -1
  557. package/dist/esm/v2/index.d.ts +0 -6
  558. package/dist/esm/v2/index.js +0 -5
  559. package/dist/esm/v2/index.js.map +0 -1
  560. package/dist/esm/v2/indexes.d.ts +0 -3
  561. package/dist/esm/v2/indexes.js +0 -15
  562. package/dist/esm/v2/indexes.js.map +0 -1
  563. package/dist/esm/v2/openDocumentDatabase.d.ts +0 -9
  564. package/dist/esm/v2/openDocumentDatabase.js +0 -96
  565. package/dist/esm/v2/openDocumentDatabase.js.map +0 -1
  566. package/dist/esm/v2/types.d.ts +0 -3
  567. package/dist/esm/v2/types.js +0 -2
  568. package/dist/esm/v2/types.js.map +0 -1
  569. /package/dist/esm/sync/{WebsocketSync.js.map → WebSocketSync.js.map} +0 -0
@@ -1,335 +1,335 @@
1
- import { applyPatch, assert, assignOid, EventSubscriber, isFileRef, getOidRoot, HybridLogicalClockTimestampProvider, PatchCreator, substituteRefsWithObjects, } from '@verdant-web/common';
2
- import { AckInfoStore } from './AckInfoStore.js';
3
- import { BaselinesStore } from './BaselinesStore.js';
4
- import { getAllFromObjectStores, getSizeOfObjectStore, storeRequestPromise, } from '../idb.js';
5
- import { LocalReplicaStore } from './LocalReplicaStore.js';
6
- import { MessageCreator } from './MessageCreator.js';
7
- import { OperationsStore } from './OperationsStore.js';
8
- import { SchemaStore } from './SchemaStore.js';
9
- export class Metadata extends EventSubscriber {
10
- constructor({ disableRebasing, context, }) {
11
- super();
12
- this.time = new HybridLogicalClockTimestampProvider();
13
- this.disableRebasing = false;
14
- this._closing = false;
15
- this.setContext = (context) => {
16
- this.context = context;
17
- };
18
- this.close = () => {
19
- this._closing = true;
20
- };
21
- /**
22
- * Methods for accessing data
23
- */
24
- this.createTransaction = (stores) => {
25
- return this.db.transaction(stores, 'readwrite');
26
- };
27
- /**
28
- * Gets the OID and every sub-object OID for a given document.
29
- * Includes any sub-objects that are not referenced by the root object
30
- * but still happen to be in storage.
31
- */
32
- this.getAllDocumentRelatedOids = async (oid) => {
33
- const oids = new Set();
34
- const documentOid = getOidRoot(oid);
35
- assert(documentOid === oid, 'Must be root document OID');
36
- oids.add(documentOid);
37
- // readwrite mode to block on other write transactions
38
- const transaction = this.db.transaction(['baselines', 'operations'], 'readwrite');
39
- await Promise.all([
40
- this.baselines.iterateOverAllForDocument(documentOid, (baseline) => {
41
- oids.add(baseline.oid);
42
- }, { transaction }),
43
- this.operations.iterateOverAllOperationsForDocument(documentOid, (patch) => {
44
- oids.add(patch.oid);
45
- }, { transaction }),
46
- ]);
47
- return Array.from(oids);
48
- };
49
- this.getDocumentSnapshot = async (oid) => {
50
- const documentOid = getOidRoot(oid);
51
- assert(documentOid === oid, 'Must be root document OID');
52
- const transaction = this.db.transaction(['baselines', 'operations'], 'readwrite');
53
- const baselines = await this.baselines.getAllForDocument(documentOid, {
54
- transaction,
55
- });
56
- const objectMap = new Map();
57
- for (const baseline of baselines) {
58
- if (baseline.snapshot) {
59
- assignOid(baseline.snapshot, baseline.oid);
60
- }
61
- objectMap.set(baseline.oid, baseline.snapshot);
62
- }
63
- await this.operations.iterateOverAllOperationsForDocument(documentOid, (op) => {
64
- const obj = objectMap.get(op.oid) || undefined;
65
- const newObj = applyPatch(obj, op.data);
66
- if (newObj) {
67
- assignOid(newObj, op.oid);
68
- }
69
- objectMap.set(op.oid, newObj);
70
- }, { transaction });
71
- const root = objectMap.get(documentOid);
72
- if (root) {
73
- substituteRefsWithObjects(root, objectMap);
74
- }
75
- return root;
76
- };
77
- /**
78
- * Methods for writing data
79
- */
80
- /**
81
- * Acks that we have seen a timestamp to the server
82
- * and stores it as our local ackedLogicalTime if it's
83
- * greater than our current ackedLogicalTime.
84
- */
85
- this.ack = async (timestamp) => {
86
- const localReplicaInfo = await this.localReplica.get();
87
- this.emit('message', {
88
- type: 'ack',
89
- replicaId: localReplicaInfo.id,
90
- timestamp,
91
- });
92
- if (!localReplicaInfo.ackedLogicalTime ||
93
- timestamp > localReplicaInfo.ackedLogicalTime) {
94
- this.localReplica.update({ ackedLogicalTime: timestamp });
95
- }
96
- };
97
- /**
98
- * Applies a patch to the document and stores it in the database.
99
- * @returns the oldest local history timestamp
100
- */
101
- this.insertLocalOperation = async (operations) => {
102
- if (operations.length === 0)
103
- return;
104
- // await this.rebaseLock;
105
- this.log(`Inserting ${operations.length} local operations`);
106
- // add local flag, in place.
107
- for (const operation of operations) {
108
- operation.isLocal = true;
109
- }
110
- await this.operations.addOperations(operations);
111
- const message = await this.messageCreator.createOperation({ operations });
112
- this.emit('message', message);
113
- // we can now enqueue and check for rebase opportunities
114
- this.tryAutonomousRebase();
115
- };
116
- /**
117
- * Inserts remote operations. This does not affect local history.
118
- * @returns a list of affected document OIDs
119
- */
120
- this.insertRemoteOperations = async (operations) => {
121
- if (operations.length === 0)
122
- return [];
123
- // await this.rebaseLock;
124
- this.log(`Inserting ${operations.length} remote operations`);
125
- const affectedDocumentOids = await this.operations.addOperations(operations.map((patch) => (Object.assign(Object.assign({}, patch), { isLocal: false }))));
126
- this.ack(operations[operations.length - 1].timestamp);
127
- return affectedDocumentOids;
128
- };
129
- this.insertRemoteBaselines = async (baselines) => {
130
- if (baselines.length === 0)
131
- return [];
132
- this.log(`Inserting ${baselines.length} remote baselines`);
133
- await this.baselines.setAll(baselines);
134
- // this.ack(baselines[baselines.length - 1].timestamp);
135
- const affectedOidSet = new Set();
136
- baselines.forEach((baseline) => {
137
- affectedOidSet.add(getOidRoot(baseline.oid));
138
- });
139
- return Array.from(affectedOidSet);
140
- };
141
- this.updateLastSynced = async (timestamp) => {
142
- return this.localReplica.update({
143
- lastSyncedLogicalTime: timestamp,
144
- });
145
- };
146
- this.lastSyncedTimestamp = async () => {
147
- const localReplicaInfo = await this.localReplica.get();
148
- return localReplicaInfo.lastSyncedLogicalTime;
149
- };
150
- this.tryAutonomousRebase = async () => {
151
- if (this.disableRebasing)
152
- return;
153
- const localReplicaInfo = await this.localReplica.get();
154
- if (localReplicaInfo.lastSyncedLogicalTime)
155
- return; // cannot autonomously rebase if we've synced
156
- // but if we have never synced... we can rebase everything!
157
- await this.runRebase(this.now);
158
- };
159
- /**
160
- * Attempt to autonomously rebase local documents without server intervention.
161
- * This can currently only happen for a client who has never synced before.
162
- * The goal is to allow local-only clients to compress their history to exactly
163
- * their undo stack.
164
- */
165
- this.runRebase = async (globalAckTimestamp) => {
166
- if (this._closing)
167
- return;
168
- // find all operations before the global ack
169
- let lastTimestamp;
170
- const toRebase = new Set();
171
- const transaction = this.db.transaction(['baselines', 'operations'], 'readwrite');
172
- let operationCount = 0;
173
- await this.operations.iterateOverAllOperations((patch) => {
174
- toRebase.add(patch.oid);
175
- lastTimestamp = patch.timestamp;
176
- operationCount++;
177
- }, {
178
- before: globalAckTimestamp,
179
- transaction,
180
- });
181
- if (!toRebase.size) {
182
- this.log('Cannot rebase, no operations prior to', globalAckTimestamp);
183
- return;
184
- }
185
- if (this._closing) {
186
- return;
187
- }
188
- // rebase each affected document
189
- let newBaselines = [];
190
- for (const oid of toRebase) {
191
- newBaselines.push(await this.rebase(oid, lastTimestamp || globalAckTimestamp, transaction));
192
- }
193
- this.emit('rebase', newBaselines);
194
- };
195
- this.rebase = async (oid, upTo, providedTx) => {
196
- // including replica Id for testing I guess
197
- const replicaId = (await this.localReplica.get()).id;
198
- this.log('[', replicaId, ']', 'Rebasing', oid, 'up to', upTo);
199
- const transaction = providedTx ||
200
- this.db.transaction(['operations', 'baselines'], 'readwrite');
201
- const baseline = await this.baselines.get(oid, { transaction });
202
- let current = (baseline === null || baseline === void 0 ? void 0 : baseline.snapshot) || undefined;
203
- let operationsApplied = 0;
204
- const deletedRefs = [];
205
- await this.operations.iterateOverAllOperationsForEntity(oid, (patch, store) => {
206
- // FIXME: this seems like the wrong place to do this
207
- // but it's here as a safety measure...
208
- if (!baseline || patch.timestamp > baseline.timestamp) {
209
- current = applyPatch(current, patch.data, deletedRefs);
210
- }
211
- // delete all prior operations to the baseline
212
- operationsApplied++;
213
- store.delete(patch.oid_timestamp);
214
- }, {
215
- to: upTo,
216
- transaction,
217
- });
218
- if (current) {
219
- assignOid(current, oid);
220
- }
221
- const newBaseline = {
222
- oid,
223
- snapshot: current,
224
- timestamp: upTo,
225
- };
226
- if (newBaseline.snapshot) {
227
- await this.baselines.set(newBaseline, { transaction });
228
- }
229
- else {
230
- await this.baselines.delete(oid, { transaction });
231
- }
232
- this.log('successfully rebased', oid, 'up to', upTo, ':', current, 'and deleted', operationsApplied, 'operations');
233
- // cleanup deleted refs
234
- if (deletedRefs.length) {
235
- const fileRefs = deletedRefs.filter(isFileRef);
236
- if (fileRefs.length) {
237
- this.emit('filesDeleted', fileRefs);
238
- }
239
- }
240
- return newBaseline;
241
- };
242
- this.reset = async () => {
243
- await this.operations.reset();
244
- await this.baselines.reset();
245
- await this.localReplica.reset();
246
- };
247
- this.updateSchema = async (schema, overrideConflict) => {
248
- const storedSchema = await this.schema.get();
249
- if (storedSchema) {
250
- // version changes will be handled by migration routines in
251
- // the actual idb database loading code (see: initializeDatabases)
252
- // but this check determines if the schema has been changed without
253
- // a version change. if so, it will error.
254
- if (overrideConflict === storedSchema.version &&
255
- storedSchema.version === schema.version &&
256
- JSON.stringify(storedSchema) !== JSON.stringify(schema)) {
1
+ import { applyPatch, assert, assignOid, EventSubscriber, isFileRef, getOidRoot, HybridLogicalClockTimestampProvider, PatchCreator, substituteRefsWithObjects, } from '@verdant-web/common';
2
+ import { AckInfoStore } from './AckInfoStore.js';
3
+ import { BaselinesStore } from './BaselinesStore.js';
4
+ import { getAllFromObjectStores, getSizeOfObjectStore, storeRequestPromise, } from '../idb.js';
5
+ import { LocalReplicaStore } from './LocalReplicaStore.js';
6
+ import { MessageCreator } from './MessageCreator.js';
7
+ import { OperationsStore } from './OperationsStore.js';
8
+ import { SchemaStore } from './SchemaStore.js';
9
+ export class Metadata extends EventSubscriber {
10
+ constructor({ disableRebasing, context, }) {
11
+ super();
12
+ this.time = new HybridLogicalClockTimestampProvider();
13
+ this.disableRebasing = false;
14
+ this._closing = false;
15
+ this.setContext = (context) => {
16
+ this.context = context;
17
+ };
18
+ this.close = () => {
19
+ this._closing = true;
20
+ };
21
+ /**
22
+ * Methods for accessing data
23
+ */
24
+ this.createTransaction = (stores) => {
25
+ return this.db.transaction(stores, 'readwrite');
26
+ };
27
+ /**
28
+ * Gets the OID and every sub-object OID for a given document.
29
+ * Includes any sub-objects that are not referenced by the root object
30
+ * but still happen to be in storage.
31
+ */
32
+ this.getAllDocumentRelatedOids = async (oid) => {
33
+ const oids = new Set();
34
+ const documentOid = getOidRoot(oid);
35
+ assert(documentOid === oid, 'Must be root document OID');
36
+ oids.add(documentOid);
37
+ // readwrite mode to block on other write transactions
38
+ const transaction = this.db.transaction(['baselines', 'operations'], 'readwrite');
39
+ await Promise.all([
40
+ this.baselines.iterateOverAllForDocument(documentOid, (baseline) => {
41
+ oids.add(baseline.oid);
42
+ }, { transaction }),
43
+ this.operations.iterateOverAllOperationsForDocument(documentOid, (patch) => {
44
+ oids.add(patch.oid);
45
+ }, { transaction }),
46
+ ]);
47
+ return Array.from(oids);
48
+ };
49
+ this.getDocumentSnapshot = async (oid) => {
50
+ const documentOid = getOidRoot(oid);
51
+ assert(documentOid === oid, 'Must be root document OID');
52
+ const transaction = this.db.transaction(['baselines', 'operations'], 'readwrite');
53
+ const baselines = await this.baselines.getAllForDocument(documentOid, {
54
+ transaction,
55
+ });
56
+ const objectMap = new Map();
57
+ for (const baseline of baselines) {
58
+ if (baseline.snapshot) {
59
+ assignOid(baseline.snapshot, baseline.oid);
60
+ }
61
+ objectMap.set(baseline.oid, baseline.snapshot);
62
+ }
63
+ await this.operations.iterateOverAllOperationsForDocument(documentOid, (op) => {
64
+ const obj = objectMap.get(op.oid) || undefined;
65
+ const newObj = applyPatch(obj, op.data);
66
+ if (newObj) {
67
+ assignOid(newObj, op.oid);
68
+ }
69
+ objectMap.set(op.oid, newObj);
70
+ }, { transaction });
71
+ const root = objectMap.get(documentOid);
72
+ if (root) {
73
+ substituteRefsWithObjects(root, objectMap);
74
+ }
75
+ return root;
76
+ };
77
+ /**
78
+ * Methods for writing data
79
+ */
80
+ /**
81
+ * Acks that we have seen a timestamp to the server
82
+ * and stores it as our local ackedLogicalTime if it's
83
+ * greater than our current ackedLogicalTime.
84
+ */
85
+ this.ack = async (timestamp) => {
86
+ const localReplicaInfo = await this.localReplica.get();
87
+ this.emit('message', {
88
+ type: 'ack',
89
+ replicaId: localReplicaInfo.id,
90
+ timestamp,
91
+ });
92
+ if (!localReplicaInfo.ackedLogicalTime ||
93
+ timestamp > localReplicaInfo.ackedLogicalTime) {
94
+ this.localReplica.update({ ackedLogicalTime: timestamp });
95
+ }
96
+ };
97
+ /**
98
+ * Applies a patch to the document and stores it in the database.
99
+ * @returns the oldest local history timestamp
100
+ */
101
+ this.insertLocalOperation = async (operations) => {
102
+ if (operations.length === 0)
103
+ return;
104
+ // await this.rebaseLock;
105
+ this.log(`Inserting ${operations.length} local operations`);
106
+ // add local flag, in place.
107
+ for (const operation of operations) {
108
+ operation.isLocal = true;
109
+ }
110
+ await this.operations.addOperations(operations);
111
+ const message = await this.messageCreator.createOperation({ operations });
112
+ this.emit('message', message);
113
+ // we can now enqueue and check for rebase opportunities
114
+ this.tryAutonomousRebase();
115
+ };
116
+ /**
117
+ * Inserts remote operations. This does not affect local history.
118
+ * @returns a list of affected document OIDs
119
+ */
120
+ this.insertRemoteOperations = async (operations) => {
121
+ if (operations.length === 0)
122
+ return [];
123
+ // await this.rebaseLock;
124
+ this.log(`Inserting ${operations.length} remote operations`);
125
+ const affectedDocumentOids = await this.operations.addOperations(operations.map((patch) => (Object.assign(Object.assign({}, patch), { isLocal: false }))));
126
+ this.ack(operations[operations.length - 1].timestamp);
127
+ return affectedDocumentOids;
128
+ };
129
+ this.insertRemoteBaselines = async (baselines) => {
130
+ if (baselines.length === 0)
131
+ return [];
132
+ this.log(`Inserting ${baselines.length} remote baselines`);
133
+ await this.baselines.setAll(baselines);
134
+ // this.ack(baselines[baselines.length - 1].timestamp);
135
+ const affectedOidSet = new Set();
136
+ baselines.forEach((baseline) => {
137
+ affectedOidSet.add(getOidRoot(baseline.oid));
138
+ });
139
+ return Array.from(affectedOidSet);
140
+ };
141
+ this.updateLastSynced = async (timestamp) => {
142
+ return this.localReplica.update({
143
+ lastSyncedLogicalTime: timestamp,
144
+ });
145
+ };
146
+ this.lastSyncedTimestamp = async () => {
147
+ const localReplicaInfo = await this.localReplica.get();
148
+ return localReplicaInfo.lastSyncedLogicalTime;
149
+ };
150
+ this.tryAutonomousRebase = async () => {
151
+ if (this.disableRebasing)
152
+ return;
153
+ const localReplicaInfo = await this.localReplica.get();
154
+ if (localReplicaInfo.lastSyncedLogicalTime)
155
+ return; // cannot autonomously rebase if we've synced
156
+ // but if we have never synced... we can rebase everything!
157
+ await this.runRebase(this.now);
158
+ };
159
+ /**
160
+ * Attempt to autonomously rebase local documents without server intervention.
161
+ * This can currently only happen for a client who has never synced before.
162
+ * The goal is to allow local-only clients to compress their history to exactly
163
+ * their undo stack.
164
+ */
165
+ this.runRebase = async (globalAckTimestamp) => {
166
+ if (this._closing)
167
+ return;
168
+ // find all operations before the global ack
169
+ let lastTimestamp;
170
+ const toRebase = new Set();
171
+ const transaction = this.db.transaction(['baselines', 'operations'], 'readwrite');
172
+ let operationCount = 0;
173
+ await this.operations.iterateOverAllOperations((patch) => {
174
+ toRebase.add(patch.oid);
175
+ lastTimestamp = patch.timestamp;
176
+ operationCount++;
177
+ }, {
178
+ before: globalAckTimestamp,
179
+ transaction,
180
+ });
181
+ if (!toRebase.size) {
182
+ this.log('Cannot rebase, no operations prior to', globalAckTimestamp);
183
+ return;
184
+ }
185
+ if (this._closing) {
186
+ return;
187
+ }
188
+ // rebase each affected document
189
+ let newBaselines = [];
190
+ for (const oid of toRebase) {
191
+ newBaselines.push(await this.rebase(oid, lastTimestamp || globalAckTimestamp, transaction));
192
+ }
193
+ this.emit('rebase', newBaselines);
194
+ };
195
+ this.rebase = async (oid, upTo, providedTx) => {
196
+ // including replica Id for testing I guess
197
+ const replicaId = (await this.localReplica.get()).id;
198
+ this.log('[', replicaId, ']', 'Rebasing', oid, 'up to', upTo);
199
+ const transaction = providedTx ||
200
+ this.db.transaction(['operations', 'baselines'], 'readwrite');
201
+ const baseline = await this.baselines.get(oid, { transaction });
202
+ let current = (baseline === null || baseline === void 0 ? void 0 : baseline.snapshot) || undefined;
203
+ let operationsApplied = 0;
204
+ const deletedRefs = [];
205
+ await this.operations.iterateOverAllOperationsForEntity(oid, (patch, store) => {
206
+ // FIXME: this seems like the wrong place to do this
207
+ // but it's here as a safety measure...
208
+ if (!baseline || patch.timestamp > baseline.timestamp) {
209
+ current = applyPatch(current, patch.data, deletedRefs);
210
+ }
211
+ // delete all prior operations to the baseline
212
+ operationsApplied++;
213
+ store.delete(patch.oid_timestamp);
214
+ }, {
215
+ to: upTo,
216
+ transaction,
217
+ });
218
+ if (current) {
219
+ assignOid(current, oid);
220
+ }
221
+ const newBaseline = {
222
+ oid,
223
+ snapshot: current,
224
+ timestamp: upTo,
225
+ };
226
+ if (newBaseline.snapshot) {
227
+ await this.baselines.set(newBaseline, { transaction });
228
+ }
229
+ else {
230
+ await this.baselines.delete(oid, { transaction });
231
+ }
232
+ this.log('successfully rebased', oid, 'up to', upTo, ':', current, 'and deleted', operationsApplied, 'operations');
233
+ // cleanup deleted refs
234
+ if (deletedRefs.length) {
235
+ const fileRefs = deletedRefs.filter(isFileRef);
236
+ if (fileRefs.length) {
237
+ this.emit('filesDeleted', fileRefs);
238
+ }
239
+ }
240
+ return newBaseline;
241
+ };
242
+ this.reset = async () => {
243
+ await this.operations.reset();
244
+ await this.baselines.reset();
245
+ await this.localReplica.reset();
246
+ };
247
+ this.updateSchema = async (schema, overrideConflict) => {
248
+ const storedSchema = await this.schema.get();
249
+ if (storedSchema) {
250
+ // version changes will be handled by migration routines in
251
+ // the actual idb database loading code (see: initializeDatabases)
252
+ // but this check determines if the schema has been changed without
253
+ // a version change. if so, it will error.
254
+ if (overrideConflict === storedSchema.version &&
255
+ storedSchema.version === schema.version &&
256
+ JSON.stringify(storedSchema) !== JSON.stringify(schema)) {
257
257
  console.error(`Schema mismatch for version ${schema.version}
258
258
  ${JSON.stringify(storedSchema)}
259
- ${JSON.stringify(schema)}`);
260
- throw new Error('Schema has changed without a version change! Any changes to your schema must be accompanied by a change in schema version and a migration routine.');
261
- }
262
- }
263
- await this.schema.set(schema);
264
- };
265
- this.setGlobalAck = async (timestamp) => {
266
- await this.ackInfo.setGlobalAck(timestamp);
267
- if (!this.disableRebasing) {
268
- await this.runRebase(timestamp);
269
- }
270
- };
271
- this.export = async () => {
272
- const db = this.db;
273
- const [baselines, operations] = await getAllFromObjectStores(db, [
274
- 'baselines',
275
- 'operations',
276
- ]);
277
- const localReplica = await this.localReplica.get();
278
- const schema = await this.schema.get();
279
- if (!schema) {
280
- throw new Error('Cannot export Client data before initializing');
281
- }
282
- return {
283
- operations,
284
- baselines,
285
- localReplica,
286
- schema,
287
- };
288
- };
289
- /**
290
- * Resets local metadata and clears operation/baseline stores.
291
- * DOES NOT add operations/baselines - this should be done
292
- * through the normal higher level systems.
293
- */
294
- this.resetFrom = async (data) => {
295
- const db = this.db;
296
- const transaction = db.transaction(['baselines', 'operations', 'info'], 'readwrite');
297
- await storeRequestPromise(transaction.objectStore('baselines').clear());
298
- await storeRequestPromise(transaction.objectStore('operations').clear());
299
- await storeRequestPromise(transaction.objectStore('info').clear());
300
- await this.localReplica.update({
301
- ackedLogicalTime: data.localReplica.ackedLogicalTime,
302
- lastSyncedLogicalTime: data.localReplica.lastSyncedLogicalTime,
303
- }, { transaction });
304
- };
305
- this.stats = async () => {
306
- const db = this.db;
307
- const operationsSize = await getSizeOfObjectStore(db, 'operations');
308
- const baselinesSize = await getSizeOfObjectStore(db, 'baselines');
309
- return {
310
- operationsSize,
311
- baselinesSize,
312
- };
313
- };
314
- this.context = context;
315
- this.schema = new SchemaStore(context.metaDb, context.schema.version);
316
- this.operations = new OperationsStore(this.db);
317
- this.baselines = new BaselinesStore(this.db);
318
- this.localReplica = new LocalReplicaStore(this.db);
319
- this.ackInfo = new AckInfoStore(this.db);
320
- this.messageCreator = new MessageCreator(this);
321
- this.patchCreator = new PatchCreator(() => this.now);
322
- if (disableRebasing)
323
- this.disableRebasing = disableRebasing;
324
- }
325
- get db() {
326
- return this.context.metaDb;
327
- }
328
- get log() {
329
- return this.context.log;
330
- }
331
- get now() {
332
- return this.time.now(this.schema.currentVersion);
333
- }
334
- }
259
+ ${JSON.stringify(schema)}`);
260
+ throw new Error('Schema has changed without a version change! Any changes to your schema must be accompanied by a change in schema version and a migration routine.');
261
+ }
262
+ }
263
+ await this.schema.set(schema);
264
+ };
265
+ this.setGlobalAck = async (timestamp) => {
266
+ await this.ackInfo.setGlobalAck(timestamp);
267
+ if (!this.disableRebasing) {
268
+ await this.runRebase(timestamp);
269
+ }
270
+ };
271
+ this.export = async () => {
272
+ const db = this.db;
273
+ const [baselines, operations] = await getAllFromObjectStores(db, [
274
+ 'baselines',
275
+ 'operations',
276
+ ]);
277
+ const localReplica = await this.localReplica.get();
278
+ const schema = await this.schema.get();
279
+ if (!schema) {
280
+ throw new Error('Cannot export Client data before initializing');
281
+ }
282
+ return {
283
+ operations,
284
+ baselines,
285
+ localReplica,
286
+ schema,
287
+ };
288
+ };
289
+ /**
290
+ * Resets local metadata and clears operation/baseline stores.
291
+ * DOES NOT add operations/baselines - this should be done
292
+ * through the normal higher level systems.
293
+ */
294
+ this.resetFrom = async (data) => {
295
+ const db = this.db;
296
+ const transaction = db.transaction(['baselines', 'operations', 'info'], 'readwrite');
297
+ await storeRequestPromise(transaction.objectStore('baselines').clear());
298
+ await storeRequestPromise(transaction.objectStore('operations').clear());
299
+ await storeRequestPromise(transaction.objectStore('info').clear());
300
+ await this.localReplica.update({
301
+ ackedLogicalTime: data.localReplica.ackedLogicalTime,
302
+ lastSyncedLogicalTime: data.localReplica.lastSyncedLogicalTime,
303
+ }, { transaction });
304
+ };
305
+ this.stats = async () => {
306
+ const db = this.db;
307
+ const operationsSize = await getSizeOfObjectStore(db, 'operations');
308
+ const baselinesSize = await getSizeOfObjectStore(db, 'baselines');
309
+ return {
310
+ operationsSize,
311
+ baselinesSize,
312
+ };
313
+ };
314
+ this.context = context;
315
+ this.schema = new SchemaStore(context.metaDb, context.schema.version);
316
+ this.operations = new OperationsStore(this.db);
317
+ this.baselines = new BaselinesStore(this.db);
318
+ this.localReplica = new LocalReplicaStore(this.db);
319
+ this.ackInfo = new AckInfoStore(this.db);
320
+ this.messageCreator = new MessageCreator(this);
321
+ this.patchCreator = new PatchCreator(() => this.now);
322
+ if (disableRebasing)
323
+ this.disableRebasing = disableRebasing;
324
+ }
325
+ get db() {
326
+ return this.context.metaDb;
327
+ }
328
+ get log() {
329
+ return this.context.log;
330
+ }
331
+ get now() {
332
+ return this.time.now(this.schema.currentVersion);
333
+ }
334
+ }
335
335
  //# sourceMappingURL=Metadata.js.map