@agentshouse/mdruntime 0.1.0-c7fd3d0e03135 → 0.1.0-c809974611fbc

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 (436) hide show
  1. package/README.md +24 -19
  2. package/dist/acceptance.d.ts +3 -0
  3. package/dist/acceptance.d.ts.map +1 -0
  4. package/dist/acceptance.js +328 -0
  5. package/dist/act-lanes.d.ts +30 -0
  6. package/dist/act-lanes.d.ts.map +1 -0
  7. package/dist/act-lanes.js +225 -0
  8. package/dist/actions.d.ts +26 -0
  9. package/dist/actions.d.ts.map +1 -0
  10. package/dist/actions.js +341 -0
  11. package/dist/adapter.d.ts +306 -130
  12. package/dist/adapter.d.ts.map +1 -1
  13. package/dist/attached-model.d.ts +6 -10
  14. package/dist/attached-model.d.ts.map +1 -1
  15. package/dist/attached-model.js +35 -41
  16. package/dist/{binary.d.ts → attachment.d.ts} +8 -9
  17. package/dist/attachment.d.ts.map +1 -0
  18. package/dist/attachment.js +15 -0
  19. package/dist/authored.d.ts +88 -0
  20. package/dist/authored.d.ts.map +1 -0
  21. package/dist/authored.js +347 -0
  22. package/dist/bootstrap.d.ts +30 -11
  23. package/dist/bootstrap.d.ts.map +1 -1
  24. package/dist/bootstrap.js +309 -113
  25. package/dist/cell.d.ts +6 -0
  26. package/dist/cell.d.ts.map +1 -0
  27. package/dist/cell.js +9 -0
  28. package/dist/chain.d.ts +12 -0
  29. package/dist/chain.d.ts.map +1 -0
  30. package/dist/chain.js +67 -0
  31. package/dist/change-set.d.ts +13 -16
  32. package/dist/change-set.d.ts.map +1 -1
  33. package/dist/change-set.js +17 -89
  34. package/dist/change.d.ts +36 -1
  35. package/dist/change.d.ts.map +1 -1
  36. package/dist/change.js +17 -3
  37. package/dist/chat-types.d.ts +3 -0
  38. package/dist/chat-types.d.ts.map +1 -0
  39. package/dist/chat-types.js +11 -0
  40. package/dist/client.d.ts +56 -28
  41. package/dist/client.d.ts.map +1 -1
  42. package/dist/client.js +91 -26
  43. package/dist/collection.d.ts +34 -21
  44. package/dist/collection.d.ts.map +1 -1
  45. package/dist/collection.js +58 -286
  46. package/dist/column.d.ts +5 -0
  47. package/dist/column.d.ts.map +1 -0
  48. package/dist/column.js +9 -0
  49. package/dist/compile.d.ts +19 -0
  50. package/dist/compile.d.ts.map +1 -0
  51. package/dist/compile.js +755 -0
  52. package/dist/connection.d.ts +7 -134
  53. package/dist/connection.d.ts.map +1 -1
  54. package/dist/connection.js +113 -3612
  55. package/dist/context.d.ts +2 -0
  56. package/dist/context.d.ts.map +1 -0
  57. package/dist/{chat/context.js → context.js} +2 -5
  58. package/dist/conversation-live.d.ts +149 -0
  59. package/dist/conversation-live.d.ts.map +1 -0
  60. package/dist/conversation-live.js +259 -0
  61. package/dist/conversation-row.d.ts +40 -0
  62. package/dist/conversation-row.d.ts.map +1 -0
  63. package/dist/conversation-row.js +66 -0
  64. package/dist/conversation.d.ts +87 -51
  65. package/dist/conversation.d.ts.map +1 -1
  66. package/dist/conversation.js +1051 -775
  67. package/dist/declaration.d.ts +10 -0
  68. package/dist/declaration.d.ts.map +1 -0
  69. package/dist/declaration.js +24 -0
  70. package/dist/digest.d.ts +7 -0
  71. package/dist/digest.d.ts.map +1 -0
  72. package/dist/digest.js +113 -0
  73. package/dist/discovery.d.ts +12 -0
  74. package/dist/discovery.d.ts.map +1 -0
  75. package/dist/discovery.js +175 -0
  76. package/dist/download.d.ts +6 -0
  77. package/dist/download.d.ts.map +1 -0
  78. package/dist/download.js +42 -0
  79. package/dist/durability.d.ts +21 -4
  80. package/dist/durability.d.ts.map +1 -1
  81. package/dist/durability.js +12 -0
  82. package/dist/entry.d.ts +29 -2
  83. package/dist/entry.d.ts.map +1 -1
  84. package/dist/entry.js +121 -39
  85. package/dist/expectation.d.ts +26 -13
  86. package/dist/expectation.d.ts.map +1 -1
  87. package/dist/expectation.js +72 -19
  88. package/dist/failure.d.ts +3 -0
  89. package/dist/failure.d.ts.map +1 -1
  90. package/dist/failure.js +8 -0
  91. package/dist/figures.d.ts +49 -0
  92. package/dist/figures.d.ts.map +1 -0
  93. package/dist/figures.js +199 -0
  94. package/dist/file.d.ts +2 -19
  95. package/dist/file.d.ts.map +1 -1
  96. package/dist/file.js +8 -122
  97. package/dist/holdings.d.ts +29 -0
  98. package/dist/holdings.d.ts.map +1 -0
  99. package/dist/holdings.js +95 -0
  100. package/dist/host.d.ts +25 -11
  101. package/dist/host.d.ts.map +1 -1
  102. package/dist/host.js +163 -58
  103. package/dist/house-query.d.ts +24 -0
  104. package/dist/house-query.d.ts.map +1 -0
  105. package/dist/house-query.js +102 -0
  106. package/dist/identity.d.ts +1 -3
  107. package/dist/identity.d.ts.map +1 -1
  108. package/dist/identity.js +1 -7
  109. package/dist/index.d.ts +50 -17
  110. package/dist/index.d.ts.map +1 -1
  111. package/dist/index.js +25 -5
  112. package/dist/install.d.ts +60 -0
  113. package/dist/install.d.ts.map +1 -0
  114. package/dist/install.js +537 -0
  115. package/dist/lifecycle.d.ts +7 -0
  116. package/dist/lifecycle.d.ts.map +1 -0
  117. package/dist/lifecycle.js +292 -0
  118. package/dist/listing.d.ts +60 -0
  119. package/dist/listing.d.ts.map +1 -0
  120. package/dist/listing.js +168 -0
  121. package/dist/live.d.ts +39 -0
  122. package/dist/live.d.ts.map +1 -0
  123. package/dist/live.js +223 -0
  124. package/dist/loaded.d.ts +7 -0
  125. package/dist/loaded.d.ts.map +1 -0
  126. package/dist/loaded.js +46 -0
  127. package/dist/locks.d.ts +7 -0
  128. package/dist/locks.d.ts.map +1 -0
  129. package/dist/locks.js +34 -0
  130. package/dist/lost.d.ts +5 -0
  131. package/dist/lost.d.ts.map +1 -0
  132. package/dist/lost.js +49 -0
  133. package/dist/media.d.ts +24 -0
  134. package/dist/media.d.ts.map +1 -0
  135. package/dist/media.js +365 -0
  136. package/dist/memory.d.ts +22 -9
  137. package/dist/memory.d.ts.map +1 -1
  138. package/dist/memory.js +318 -126
  139. package/dist/messages.d.ts +63 -0
  140. package/dist/messages.d.ts.map +1 -0
  141. package/dist/messages.js +169 -0
  142. package/dist/modal.d.ts +2 -0
  143. package/dist/modal.d.ts.map +1 -0
  144. package/dist/modal.js +11 -0
  145. package/dist/model.d.ts +15 -15
  146. package/dist/model.d.ts.map +1 -1
  147. package/dist/model.js +11 -22
  148. package/dist/models.d.ts +10 -0
  149. package/dist/models.d.ts.map +1 -0
  150. package/dist/models.js +153 -0
  151. package/dist/name.d.ts +3 -0
  152. package/dist/name.d.ts.map +1 -0
  153. package/dist/name.js +118 -0
  154. package/dist/observation.d.ts +40 -10
  155. package/dist/observation.d.ts.map +1 -1
  156. package/dist/observation.js +265 -44
  157. package/dist/online.d.ts +7 -0
  158. package/dist/online.d.ts.map +1 -0
  159. package/dist/online.js +29 -0
  160. package/dist/opened.d.ts +22 -0
  161. package/dist/opened.d.ts.map +1 -0
  162. package/dist/opened.js +107 -0
  163. package/dist/opening.d.ts +4 -0
  164. package/dist/opening.d.ts.map +1 -0
  165. package/dist/opening.js +71 -0
  166. package/dist/outcome.d.ts +68 -13
  167. package/dist/outcome.d.ts.map +1 -1
  168. package/dist/outcome.js +74 -5
  169. package/dist/overlay.d.ts +24 -11
  170. package/dist/overlay.d.ts.map +1 -1
  171. package/dist/overlay.js +190 -180
  172. package/dist/page-metadata.d.ts +8 -0
  173. package/dist/page-metadata.d.ts.map +1 -0
  174. package/dist/page-metadata.js +19 -0
  175. package/dist/parts.d.ts +6 -0
  176. package/dist/parts.d.ts.map +1 -0
  177. package/dist/parts.js +36 -0
  178. package/dist/paths.d.ts +2 -0
  179. package/dist/paths.d.ts.map +1 -1
  180. package/dist/paths.js +26 -0
  181. package/dist/preparation.d.ts +46 -0
  182. package/dist/preparation.d.ts.map +1 -0
  183. package/dist/preparation.js +344 -0
  184. package/dist/presence.d.ts +58 -0
  185. package/dist/presence.d.ts.map +1 -0
  186. package/dist/presence.js +231 -0
  187. package/dist/queries.d.ts +16 -0
  188. package/dist/queries.d.ts.map +1 -0
  189. package/dist/queries.js +330 -0
  190. package/dist/query.d.ts +23 -209
  191. package/dist/query.d.ts.map +1 -1
  192. package/dist/query.js +3 -493
  193. package/dist/{chat/questions.d.ts → questions.d.ts} +6 -7
  194. package/dist/questions.d.ts.map +1 -0
  195. package/dist/{chat/questions.js → questions.js} +14 -8
  196. package/dist/queue.d.ts +40 -0
  197. package/dist/queue.d.ts.map +1 -0
  198. package/dist/queue.js +480 -0
  199. package/dist/reach.d.ts +31 -11
  200. package/dist/reach.d.ts.map +1 -1
  201. package/dist/reach.js +460 -115
  202. package/dist/react.d.ts +28 -5
  203. package/dist/react.d.ts.map +1 -1
  204. package/dist/react.js +35 -15
  205. package/dist/{chat/read.d.ts → read.d.ts} +2 -0
  206. package/dist/read.d.ts.map +1 -0
  207. package/dist/{chat/read.js → read.js} +4 -0
  208. package/dist/readiness.d.ts +36 -21
  209. package/dist/readiness.d.ts.map +1 -1
  210. package/dist/readiness.js +20 -18
  211. package/dist/reads.d.ts +38 -10
  212. package/dist/reads.d.ts.map +1 -1
  213. package/dist/reads.js +174 -83
  214. package/dist/recovery.d.ts +6 -2
  215. package/dist/recovery.d.ts.map +1 -1
  216. package/dist/recovery.js +67 -9
  217. package/dist/reference-forms.d.ts +4 -0
  218. package/dist/reference-forms.d.ts.map +1 -0
  219. package/dist/reference-forms.js +74 -0
  220. package/dist/refresh.d.ts +18 -15
  221. package/dist/refresh.d.ts.map +1 -1
  222. package/dist/refresh.js +145 -60
  223. package/dist/refusal.d.ts +5 -0
  224. package/dist/refusal.d.ts.map +1 -0
  225. package/dist/refusal.js +55 -0
  226. package/dist/replica.d.ts +44 -36
  227. package/dist/replica.d.ts.map +1 -1
  228. package/dist/replica.js +322 -198
  229. package/dist/retried.d.ts +30 -0
  230. package/dist/retried.d.ts.map +1 -0
  231. package/dist/retried.js +72 -0
  232. package/dist/room-draft.d.ts +56 -14
  233. package/dist/room-draft.d.ts.map +1 -1
  234. package/dist/room-draft.js +424 -127
  235. package/dist/room-drafts.d.ts +24 -0
  236. package/dist/room-drafts.d.ts.map +1 -0
  237. package/dist/room-drafts.js +222 -0
  238. package/dist/room-list.d.ts +10 -0
  239. package/dist/room-list.d.ts.map +1 -0
  240. package/dist/room-list.js +47 -0
  241. package/dist/rows.d.ts +40 -31
  242. package/dist/rows.d.ts.map +1 -1
  243. package/dist/rows.js +72 -123
  244. package/dist/run-tables.d.ts +25 -0
  245. package/dist/run-tables.d.ts.map +1 -0
  246. package/dist/run-tables.js +94 -0
  247. package/dist/running.d.ts +5 -0
  248. package/dist/running.d.ts.map +1 -0
  249. package/dist/running.js +10 -0
  250. package/dist/runs.d.ts +27 -0
  251. package/dist/runs.d.ts.map +1 -0
  252. package/dist/runs.js +90 -0
  253. package/dist/runtime.d.ts +37 -0
  254. package/dist/runtime.d.ts.map +1 -0
  255. package/dist/runtime.js +1 -0
  256. package/dist/save.d.ts +44 -49
  257. package/dist/save.d.ts.map +1 -1
  258. package/dist/save.js +736 -304
  259. package/dist/saves.d.ts +34 -0
  260. package/dist/saves.d.ts.map +1 -0
  261. package/dist/saves.js +463 -0
  262. package/dist/search.d.ts +12 -95
  263. package/dist/search.d.ts.map +1 -1
  264. package/dist/search.js +10 -266
  265. package/dist/seed.d.ts +11 -2
  266. package/dist/seed.d.ts.map +1 -1
  267. package/dist/seed.js +71 -1
  268. package/dist/selector.d.ts +3 -0
  269. package/dist/selector.d.ts.map +1 -0
  270. package/dist/selector.js +22 -0
  271. package/dist/shared.d.ts +6 -7
  272. package/dist/shared.d.ts.map +1 -1
  273. package/dist/shared.js +75 -104
  274. package/dist/shown.d.ts +35 -0
  275. package/dist/shown.d.ts.map +1 -0
  276. package/dist/shown.js +326 -0
  277. package/dist/socket/admission.d.ts +7 -0
  278. package/dist/socket/admission.d.ts.map +1 -0
  279. package/dist/socket/admission.js +39 -0
  280. package/dist/socket/answers.d.ts +6 -0
  281. package/dist/socket/answers.d.ts.map +1 -0
  282. package/dist/socket/answers.js +20 -0
  283. package/dist/socket/lanes.d.ts +28 -0
  284. package/dist/socket/lanes.d.ts.map +1 -0
  285. package/dist/socket/lanes.js +103 -0
  286. package/dist/socket/reads.d.ts +10 -0
  287. package/dist/socket/reads.d.ts.map +1 -0
  288. package/dist/socket/reads.js +31 -0
  289. package/dist/socket/session.d.ts +41 -0
  290. package/dist/socket/session.d.ts.map +1 -0
  291. package/dist/socket/session.js +26 -0
  292. package/dist/socket.d.ts +70 -0
  293. package/dist/socket.d.ts.map +1 -0
  294. package/dist/socket.js +172 -0
  295. package/dist/source.d.ts +61 -0
  296. package/dist/source.d.ts.map +1 -0
  297. package/dist/source.js +95 -0
  298. package/dist/status.d.ts +4 -5
  299. package/dist/status.d.ts.map +1 -1
  300. package/dist/status.js +3 -7
  301. package/dist/store/installs.d.ts +30 -0
  302. package/dist/store/installs.d.ts.map +1 -0
  303. package/dist/store/installs.js +40 -0
  304. package/dist/store/readiness.d.ts +11 -0
  305. package/dist/store/readiness.d.ts.map +1 -0
  306. package/dist/store/readiness.js +68 -0
  307. package/dist/store/records.d.ts +234 -0
  308. package/dist/store/records.d.ts.map +1 -0
  309. package/dist/store/records.js +474 -0
  310. package/dist/store/room-held.d.ts +8 -0
  311. package/dist/store/room-held.d.ts.map +1 -0
  312. package/dist/store/room-held.js +17 -0
  313. package/dist/store/rooms-shown.d.ts +8 -0
  314. package/dist/store/rooms-shown.d.ts.map +1 -0
  315. package/dist/store/rooms-shown.js +13 -0
  316. package/dist/store/space.d.ts +11 -0
  317. package/dist/store/space.d.ts.map +1 -0
  318. package/dist/store/space.js +41 -0
  319. package/dist/store/watchers.d.ts +17 -0
  320. package/dist/store/watchers.d.ts.map +1 -0
  321. package/dist/store/watchers.js +56 -0
  322. package/dist/store/writes.d.ts +24 -0
  323. package/dist/store/writes.d.ts.map +1 -0
  324. package/dist/store/writes.js +125 -0
  325. package/dist/store.d.ts +115 -163
  326. package/dist/store.d.ts.map +1 -1
  327. package/dist/store.js +166 -750
  328. package/dist/stored.d.ts +181 -0
  329. package/dist/stored.d.ts.map +1 -0
  330. package/dist/stored.js +807 -0
  331. package/dist/surface.d.ts +100 -0
  332. package/dist/surface.d.ts.map +1 -0
  333. package/dist/surface.js +258 -0
  334. package/dist/sync.d.ts +21 -0
  335. package/dist/sync.d.ts.map +1 -0
  336. package/dist/sync.js +557 -0
  337. package/dist/tables.d.ts +401 -0
  338. package/dist/tables.d.ts.map +1 -0
  339. package/dist/tables.js +521 -0
  340. package/dist/tallies.d.ts +18 -0
  341. package/dist/tallies.d.ts.map +1 -0
  342. package/dist/tallies.js +128 -0
  343. package/dist/transfer.d.ts +0 -5
  344. package/dist/transfer.d.ts.map +1 -1
  345. package/dist/turn.d.ts +5 -3
  346. package/dist/turn.d.ts.map +1 -1
  347. package/dist/turn.js +51 -21
  348. package/dist/validation.d.ts +31 -0
  349. package/dist/validation.d.ts.map +1 -0
  350. package/dist/validation.js +144 -0
  351. package/dist/windows.d.ts +1 -16
  352. package/dist/windows.d.ts.map +1 -1
  353. package/dist/windows.js +2 -79
  354. package/dist/wire.d.ts +107 -11
  355. package/dist/wire.d.ts.map +1 -1
  356. package/dist/wire.js +42 -13
  357. package/package.json +20 -20
  358. package/AGENTS.md +0 -299
  359. package/dist/acquisition.d.ts +0 -14
  360. package/dist/acquisition.d.ts.map +0 -1
  361. package/dist/acquisition.js +0 -24
  362. package/dist/attempt.d.ts +0 -16
  363. package/dist/attempt.d.ts.map +0 -1
  364. package/dist/attempt.js +0 -70
  365. package/dist/binary.d.ts.map +0 -1
  366. package/dist/binary.js +0 -8
  367. package/dist/chat/acts.d.ts +0 -33
  368. package/dist/chat/acts.d.ts.map +0 -1
  369. package/dist/chat/acts.js +0 -36
  370. package/dist/chat/context.d.ts +0 -3
  371. package/dist/chat/context.d.ts.map +0 -1
  372. package/dist/chat/earlier.d.ts +0 -10
  373. package/dist/chat/earlier.d.ts.map +0 -1
  374. package/dist/chat/earlier.js +0 -54
  375. package/dist/chat/input.d.ts +0 -9
  376. package/dist/chat/input.d.ts.map +0 -1
  377. package/dist/chat/input.js +0 -99
  378. package/dist/chat/kit.d.ts +0 -61
  379. package/dist/chat/kit.d.ts.map +0 -1
  380. package/dist/chat/kit.js +0 -123
  381. package/dist/chat/lasting.d.ts +0 -6
  382. package/dist/chat/lasting.d.ts.map +0 -1
  383. package/dist/chat/lasting.js +0 -19
  384. package/dist/chat/list.d.ts +0 -27
  385. package/dist/chat/list.d.ts.map +0 -1
  386. package/dist/chat/list.js +0 -99
  387. package/dist/chat/markdown.d.ts +0 -20
  388. package/dist/chat/markdown.d.ts.map +0 -1
  389. package/dist/chat/markdown.js +0 -188
  390. package/dist/chat/message.d.ts +0 -6
  391. package/dist/chat/message.d.ts.map +0 -1
  392. package/dist/chat/message.js +0 -192
  393. package/dist/chat/messages.d.ts +0 -22
  394. package/dist/chat/messages.d.ts.map +0 -1
  395. package/dist/chat/messages.js +0 -155
  396. package/dist/chat/name.d.ts +0 -6
  397. package/dist/chat/name.d.ts.map +0 -1
  398. package/dist/chat/name.js +0 -37
  399. package/dist/chat/opened.d.ts +0 -11
  400. package/dist/chat/opened.d.ts.map +0 -1
  401. package/dist/chat/opened.js +0 -17
  402. package/dist/chat/overlay.d.ts +0 -31
  403. package/dist/chat/overlay.d.ts.map +0 -1
  404. package/dist/chat/overlay.js +0 -97
  405. package/dist/chat/parts.d.ts +0 -57
  406. package/dist/chat/parts.d.ts.map +0 -1
  407. package/dist/chat/parts.js +0 -119
  408. package/dist/chat/process.d.ts +0 -20
  409. package/dist/chat/process.d.ts.map +0 -1
  410. package/dist/chat/process.js +0 -32
  411. package/dist/chat/provider.d.ts +0 -63
  412. package/dist/chat/provider.d.ts.map +0 -1
  413. package/dist/chat/provider.js +0 -635
  414. package/dist/chat/question.d.ts +0 -9
  415. package/dist/chat/question.d.ts.map +0 -1
  416. package/dist/chat/question.js +0 -69
  417. package/dist/chat/questions.d.ts.map +0 -1
  418. package/dist/chat/read.d.ts.map +0 -1
  419. package/dist/chat/ready.d.ts +0 -34
  420. package/dist/chat/ready.d.ts.map +0 -1
  421. package/dist/chat/ready.js +0 -91
  422. package/dist/chat/style.d.ts +0 -4
  423. package/dist/chat/style.d.ts.map +0 -1
  424. package/dist/chat/style.js +0 -302
  425. package/dist/chat.d.ts +0 -14
  426. package/dist/chat.d.ts.map +0 -1
  427. package/dist/chat.js +0 -8
  428. package/dist/fence.d.ts +0 -6
  429. package/dist/fence.d.ts.map +0 -1
  430. package/dist/fence.js +0 -10
  431. package/dist/log.d.ts +0 -5
  432. package/dist/log.d.ts.map +0 -1
  433. package/dist/log.js +0 -14
  434. package/dist/read-side.d.ts +0 -25
  435. package/dist/read-side.d.ts.map +0 -1
  436. package/dist/read-side.js +0 -767
package/dist/read-side.js DELETED
@@ -1,767 +0,0 @@
1
- import { and, BasicIndex, configureIndexDevMode, count, createCollection, createLiveQueryCollection, deepEquals, eq, inArray, or, toArray, } from '@tanstack/db';
2
- import { ARRAY_INDEX, parse, pointerSegments } from '@agentshouse/mdmodel';
3
- import { documentKey } from './rows.js';
4
- import { after, inUnits, ROW_WEIGHT } from './turn.js';
5
- import { beyond, cursorOf, MAXIMUM_WINDOW, windowed } from './windows.js';
6
- function every(expressions) {
7
- const [first, second, ...rest] = expressions;
8
- return second === undefined ? first : and(first, second, ...rest);
9
- }
10
- function same(held, value) {
11
- return Array.isArray(held) ? held.includes(value) : held === value;
12
- }
13
- function ordered(held, [operator, bound]) {
14
- if (typeof held !== typeof bound || held === null || typeof held === 'object')
15
- return false;
16
- const left = held;
17
- const right = bound;
18
- if (operator === 'gt')
19
- return left > right;
20
- if (operator === 'gte')
21
- return left >= right;
22
- if (operator === 'lt')
23
- return left < right;
24
- return left <= right;
25
- }
26
- function satisfied(test, held) {
27
- if (test.op === 'eq')
28
- return same(held, test.value);
29
- if (test.op === 'ne')
30
- return !same(held, test.value);
31
- if (test.op === 'in')
32
- return test.values.some((value) => same(held, value));
33
- if (test.op === 'nin')
34
- return !test.values.some((value) => same(held, value));
35
- if (test.op === 'range')
36
- return test.bounds.every((bound) => ordered(held, bound));
37
- if (test.op === 'all')
38
- return Array.isArray(held) && test.values.every((value) => same(held, value));
39
- return Array.isArray(held) && held.length === test.size;
40
- }
41
- function tested(test, reached) {
42
- if (test.op === 'exists')
43
- return test.exists === (reached !== undefined);
44
- if (reached === undefined)
45
- return test.op === 'ne' || test.op === 'nin';
46
- return reached.some((held) => satisfied(test, held));
47
- }
48
- function conditioned(condition, row) {
49
- if ('every' in condition)
50
- return condition.every.every((item) => conditioned(item, row));
51
- if ('some' in condition)
52
- return condition.some.some((item) => conditioned(item, row));
53
- return tested(condition.test, row.reached[condition.field]);
54
- }
55
- function narrowed(query, where, alias) {
56
- return where === null ? query : query.fn.where((row) => conditioned(where, row[alias]));
57
- }
58
- function reached(reach, room, document) {
59
- return reach === null
60
- ? []
61
- : [or(inArray(room, [...reach.rooms]), inArray(document, [...reach.documents]))];
62
- }
63
- function scoped(scope, row, rooms, reach, paths = null, folder = null) {
64
- return every([
65
- eq(row.kind, 'document'),
66
- ...(rooms === null ? [] : [inArray(row.room, [...rooms])]),
67
- ...(paths === null ? [] : [inArray(row.path, [...paths])]),
68
- ...(folder === null ? [] : [eq(row.folder, folder)]),
69
- ...reached(reach, row.room, row.key),
70
- ...(scope.types === null ? [] : [inArray(row.type, [...scope.types])]),
71
- ...(scope.pathPrefix === null ? [] : [inArray(scope.pathPrefix, row.directories)]),
72
- ]);
73
- }
74
- function keyed(key, row) {
75
- return key.of === 'field' ? row.values[key.field] : row[key.of];
76
- }
77
- function live(query) {
78
- return createLiveQueryCollection({
79
- query: query,
80
- startSync: true,
81
- });
82
- }
83
- function related(q, rows, scope, field, kept, reach) {
84
- const matched = kept === 'source' ? 'target' : 'source';
85
- const from = q.from({ r: rows });
86
- const joined = from
87
- .where(({ r }) => every([eq(r.kind, 'reference'), ...(field === null ? [] : [inArray(field, r.forms)])]))
88
- .innerJoin({ m: rows }, ({ r, m }) => eq(r[matched], m.key))
89
- .where(({ m }) => scoped(scope, m, null, reach));
90
- return narrowed(joined, scope.where, 'm')
91
- .groupBy(({ r }) => r[kept])
92
- .select(({ r }) => ({ key: r[kept] }));
93
- }
94
- function filtered(q, rows, plan) {
95
- const from = q.from({ d: rows });
96
- let query = narrowed(from.where(({ d }) => scoped(plan.root, d, plan.rooms, plan.reach, plan.paths, plan.folder)), plan.root.where, 'd');
97
- if (plan.hop !== null) {
98
- const hop = related(q, rows, plan.hop, plan.hop.field, 'source', plan.reach);
99
- query = query.innerJoin({ h: hop }, ({ d, h }) => eq(d.key, h.key));
100
- }
101
- if (plan.backlinks !== null) {
102
- const backlinks = related(q, rows, plan.backlinks, plan.backlinks.field, 'target', plan.reach);
103
- query = query.innerJoin({ k: backlinks }, ({ d, k }) => eq(d.key, k.key));
104
- }
105
- return query;
106
- }
107
- function bodied(shape) {
108
- return shape.sections !== null || shape.preamble || shape.markdown;
109
- }
110
- function orderingOf(options) {
111
- const collated = options.stringSort === 'locale';
112
- return {
113
- direction: options.direction,
114
- nulls: options.nulls ?? 'first',
115
- stringSort: options.stringSort ?? 'locale',
116
- locale: collated ? options.locale : undefined,
117
- localeOptions: collated
118
- ? options.localeOptions
119
- : undefined,
120
- };
121
- }
122
- function storedValue(key, row) {
123
- return key.of === 'field' ? row.values[key.field] : row[key.of];
124
- }
125
- function documentOrder(shape) {
126
- return shape.orderBy.map(({ options }) => orderingOf(options));
127
- }
128
- function documentCursor(shape, row) {
129
- return cursorOf([
130
- ...shape.orderBy.map((_, index) => row[`order${index}`]),
131
- documentKey(row.room, row.path),
132
- ]);
133
- }
134
- function documents(q, rows, plan, shape) {
135
- let query = filtered(q, rows, plan);
136
- const cursor = plan.after?.values ?? null;
137
- if (cursor !== null) {
138
- const orderings = documentOrder(shape);
139
- query = query.fn.where(({ d }) => beyond([...shape.orderBy.map(({ key }) => storedValue(key, d)), d.key], cursor, orderings));
140
- }
141
- for (const { key, options } of shape.orderBy) {
142
- query = query.orderBy(({ d }) => keyed(key, d), options);
143
- }
144
- if (shape.orderBy.length > 0 || plan.limit !== null || plan.after !== null) {
145
- query = query.orderBy(({ d }) => d.key, { stringSort: 'lexical' });
146
- }
147
- if (plan.limit !== null)
148
- query = query.limit(plan.limit + 1);
149
- return query.select(({ d }) => ({
150
- room: d.room,
151
- path: d.path,
152
- type: d.type,
153
- binary: d.binary,
154
- ...Object.fromEntries(shape.orderBy.map(({ key }, index) => [`order${index}`, keyed(key, d)])),
155
- ...(shape.fields === null ? {} : { fields: d.fields }),
156
- ...(bodied(shape) ? { text: d.text } : {}),
157
- ...(shape.backlinks ? { backlinks: toArray(backlinked(q, rows, d, plan.reach)) } : {}),
158
- ...(shape.change ? { change: d.change } : {}),
159
- ...(shape.fileId ? { fileId: d.fileId } : {}),
160
- }));
161
- }
162
- function backlinked(q, rows, document, reach) {
163
- const from = q.from({ r: rows });
164
- return from
165
- .where(({ r }) => every([eq(r.target, document.key), ...reached(reach, r.room, r.source)]))
166
- .orderBy(({ r }) => r.path, { stringSort: 'lexical' })
167
- .orderBy(({ r }) => r.field, { stringSort: 'lexical' })
168
- .select(({ r }) => ({ path: r.path, type: r.type, field: r.field }));
169
- }
170
- function groups(q, stage, shape) {
171
- const keys = shape.groupBy.map((_, index) => `key${index}`);
172
- const from = q.from({ m: stage });
173
- let query = from.groupBy(({ m }) => keys.map((key) => m[key]));
174
- for (const { by, direction } of shape.orderBy) {
175
- query = query.orderBy(({ m }) => (by === 'count' ? count(m.key) : m[keys[by]]), direction);
176
- }
177
- return query.select(({ m }) => ({
178
- ...Object.fromEntries(keys.map((key) => [key, m[key]])),
179
- count: count(m.key),
180
- }));
181
- }
182
- function built(rows, plan) {
183
- const shape = plan.shape;
184
- if (shape.kind === 'documents') {
185
- const collection = live((q) => documents(q, rows, plan, shape));
186
- const count = plan.total
187
- ? live((q) => filtered(q, rows, plan).select(({ d }) => ({ key: d.key })))
188
- : null;
189
- return {
190
- live: collection,
191
- count,
192
- dispose: () => {
193
- void collection.cleanup();
194
- if (count !== null)
195
- void count.cleanup();
196
- },
197
- };
198
- }
199
- const stage = live((q) => filtered(q, rows, plan).select(({ d }) => ({
200
- key: d.key,
201
- ...Object.fromEntries(shape.groupBy.map((key, index) => [`key${index}`, keyed(key, d)])),
202
- })));
203
- const collection = live((q) => groups(q, stage, shape));
204
- return {
205
- live: collection,
206
- count: null,
207
- dispose: () => {
208
- void collection.cleanup();
209
- void stage.cleanup();
210
- },
211
- };
212
- }
213
- function sectionsOf(sections, names) {
214
- if (names === 'all')
215
- return sections;
216
- return Object.fromEntries(names.flatMap((name) => (Object.hasOwn(sections, name) ? [[name, sections[name]]] : [])));
217
- }
218
- function bodyOf(row, key, bodies) {
219
- const text = row.text;
220
- if (text === null)
221
- return null;
222
- const held = bodies?.get(key);
223
- if (held !== undefined && held.text === text)
224
- return held.document;
225
- const read = parse(text, row.path);
226
- const document = read.ok ? read.document : null;
227
- bodies?.set(key, { text, document });
228
- return document;
229
- }
230
- function masked(pointers) {
231
- const root = { whole: false, members: new Map() };
232
- for (const pointer of pointers) {
233
- let mask = root;
234
- for (const token of pointerSegments(pointer)) {
235
- const next = mask.members.get(token) ?? { whole: false, members: new Map() };
236
- mask.members.set(token, next);
237
- mask = next;
238
- }
239
- mask.whole = true;
240
- }
241
- return root;
242
- }
243
- function merged(left, right) {
244
- if (left === undefined)
245
- return right;
246
- const members = new Map(left.members);
247
- for (const [token, mask] of right.members)
248
- members.set(token, merged(members.get(token), mask));
249
- return { whole: left.whole || right.whole, members };
250
- }
251
- function cut(value, mask) {
252
- if (mask.whole)
253
- return value;
254
- if (Array.isArray(value)) {
255
- const each = {
256
- whole: false,
257
- members: new Map([...mask.members].filter(([token]) => !ARRAY_INDEX.test(token))),
258
- };
259
- const kept = [];
260
- value.forEach((item, index) => {
261
- const part = cut(item, merged(mask.members.get(String(index)), each));
262
- if (part !== undefined)
263
- kept[index] = part;
264
- });
265
- return kept.length === 0 ? undefined : kept;
266
- }
267
- if (typeof value !== 'object' || value === null)
268
- return undefined;
269
- const kept = Object.entries(value).flatMap(([key, held]) => {
270
- const member = mask.members.get(key);
271
- const part = member === undefined ? undefined : cut(held, member);
272
- return part === undefined ? [] : [[key, part]];
273
- });
274
- return kept.length === 0 ? undefined : Object.fromEntries(kept);
275
- }
276
- function selected(fields, pointers) {
277
- return (cut(fields, masked(pointers)) ?? {});
278
- }
279
- function documentOf(key, row, shape, bodies) {
280
- if (row.type === null) {
281
- const binary = row.binary;
282
- return {
283
- room: row.room,
284
- path: row.path,
285
- ...(binary === null ? {} : { binaryFile: binary }),
286
- ...(shape.fileId ? { fileId: row.fileId } : {}),
287
- };
288
- }
289
- const body = bodied(shape) ? bodyOf(row, key, bodies) : null;
290
- return {
291
- room: row.room,
292
- path: row.path,
293
- type: row.type,
294
- ...(shape.fields === null
295
- ? {}
296
- : {
297
- fields: shape.fields === 'all'
298
- ? row.fields
299
- : selected(row.fields, shape.fields),
300
- }),
301
- ...(shape.sections === null || body === null
302
- ? {}
303
- : { sections: sectionsOf(body.sections, shape.sections) }),
304
- ...(shape.preamble && body !== null ? { preamble: body.preamble } : {}),
305
- ...(shape.markdown && body !== null ? { markdown: body.content } : {}),
306
- ...(shape.backlinks
307
- ? {
308
- backlinks: row.backlinks
309
- .slice(0, MAXIMUM_WINDOW)
310
- .map(({ path, type, field }) => ({ path, type, field })),
311
- }
312
- : {}),
313
- ...(shape.change ? { change: row.change } : {}),
314
- ...(shape.fileId ? { fileId: row.fileId } : {}),
315
- };
316
- }
317
- function weight(row) {
318
- return ROW_WEIGHT + (typeof row?.text === 'string' ? row.text.length : 0);
319
- }
320
- function groupValues(shape, [key, row]) {
321
- return [...shape.orderBy.map(({ by }) => (by === 'count' ? row.count : row[`key${by}`])), key];
322
- }
323
- function groupOrder(shape) {
324
- return shape.orderBy.map(({ direction }) => ({
325
- direction,
326
- nulls: 'first',
327
- stringSort: 'locale',
328
- locale: undefined,
329
- localeOptions: undefined,
330
- }));
331
- }
332
- function windowOf(plan, entries, total) {
333
- const shape = plan.shape;
334
- if (shape.kind === 'documents') {
335
- return {
336
- ...windowed(entries, plan.limit, plan.after?.text, ([, row]) => documentCursor(shape, row)),
337
- total,
338
- };
339
- }
340
- const cursor = plan.after?.values ?? null;
341
- const orderings = groupOrder(shape);
342
- const following = cursor === null
343
- ? entries
344
- : entries.filter((entry) => beyond(groupValues(shape, entry), cursor, orderings));
345
- return {
346
- ...windowed(following, plan.limit, plan.after?.text, (entry) => cursorOf(groupValues(shape, entry))),
347
- total: plan.total ? entries.length : undefined,
348
- };
349
- }
350
- function framed(rows, { next, total }) {
351
- return {
352
- ...rows,
353
- ...(next === undefined ? {} : { next }),
354
- ...(total === undefined ? {} : { total }),
355
- };
356
- }
357
- function rowsOfPlan(plan, entries, total, bodies, each = (row) => row) {
358
- const shape = plan.shape;
359
- const { kept, ...window } = windowOf(plan, entries, total);
360
- const keys = kept.map(([key]) => key);
361
- if (shape.kind === 'documents') {
362
- const documents = [];
363
- return after(inUnits(kept, ([key, row]) => {
364
- documents.push(each(documentOf(key, row, shape, bodies)));
365
- return weight(row);
366
- }), () => ({ rows: framed({ documents }, window), keys, ...window }));
367
- }
368
- const groups = [];
369
- return after(inUnits(kept, ([, row]) => {
370
- groups.push(each({
371
- key: shape.groupBy.map((_, index) => row[`key${index}`]),
372
- count: row.count,
373
- }));
374
- return ROW_WEIGHT;
375
- }), () => ({ rows: framed({ groups }, window), keys, ...window }));
376
- }
377
- export function reconciled(previous, next) {
378
- const before = previous.result;
379
- const after = next.result;
380
- if ('pending' in before || 'pending' in after) {
381
- if (deepEquals(before, after))
382
- return null;
383
- return { keyed: next, message: next };
384
- }
385
- const earlier = 'groups' in before ? before.groups : before.documents;
386
- const held = new Map(previous.keys.map((key, index) => [key, earlier[index]]));
387
- const changed = [];
388
- const rows = ('groups' in after ? after.groups : after.documents).map((row, index) => {
389
- const key = next.keys[index];
390
- const kept = held.get(key);
391
- if (kept !== undefined && deepEquals(kept, row))
392
- return kept;
393
- changed.push([key, row]);
394
- return row;
395
- });
396
- const present = new Set(next.keys);
397
- const removed = previous.keys.filter((key) => !present.has(key));
398
- const moved = reordered(previous.keys, next.keys, present);
399
- const covered = !deepEquals(before.coverage, after.coverage);
400
- const framing = reframed(before, after);
401
- if (changed.length === 0 && removed.length === 0 && !moved && !covered && framing === null) {
402
- return null;
403
- }
404
- const window = { next: after.next, total: after.total };
405
- return {
406
- keyed: {
407
- result: 'groups' in after
408
- ? framed({ groups: rows, coverage: after.coverage }, window)
409
- : framed({ documents: rows, coverage: after.coverage }, window),
410
- keys: next.keys,
411
- },
412
- message: {
413
- changed,
414
- removed,
415
- order: moved ? next.keys : null,
416
- ...(covered ? { coverage: after.coverage } : {}),
417
- ...framing,
418
- },
419
- };
420
- }
421
- function reframed(before, after) {
422
- const next = before.next === after.next ? {} : { next: after.next ?? null };
423
- const total = before.total === after.total || after.total === undefined ? {} : { total: after.total };
424
- return Object.keys(next).length === 0 && Object.keys(total).length === 0
425
- ? null
426
- : { ...next, ...total };
427
- }
428
- function reordered(previous, next, present) {
429
- let at = 0;
430
- for (const key of previous) {
431
- if (!present.has(key))
432
- continue;
433
- if (next[at] !== key)
434
- return true;
435
- at += 1;
436
- }
437
- return at !== next.length;
438
- }
439
- export function createReadSide() {
440
- configureIndexDevMode({ enabled: false });
441
- let sync;
442
- const rows = createCollection({
443
- getKey: (row) => row.key,
444
- gcTime: 0,
445
- startSync: true,
446
- autoIndex: 'eager',
447
- defaultIndexType: BasicIndex,
448
- sync: {
449
- rowUpdateMode: 'full',
450
- sync: (params) => {
451
- sync = params;
452
- params.markReady();
453
- },
454
- },
455
- });
456
- const shared = new Map();
457
- function searched(row) {
458
- const read = row.text === null ? null : parse(row.text, row.path);
459
- if (read === null || !read.ok)
460
- return null;
461
- return {
462
- room: row.room,
463
- path: row.path,
464
- type: row.type,
465
- fields: row.fields,
466
- preamble: read.document.preamble,
467
- sections: read.document.sections,
468
- };
469
- }
470
- function kept(entry, key, row) {
471
- const held = entry.rows.get(key);
472
- if (held !== undefined && deepEquals(held, row))
473
- return;
474
- entry.rows.set(key, row);
475
- }
476
- function counted(entry) {
477
- return entry.built.count === null ? undefined : entry.built.count.size;
478
- }
479
- function rebuilt(entry) {
480
- return after(rowsOfPlan(entry.plan, [...entry.built.live.entries()], counted(entry), entry.bodies), ({ rows: found, keys, next, total }) => {
481
- const present = new Set(keys);
482
- for (const key of [...entry.rows.keys()])
483
- if (!present.has(key))
484
- entry.rows.delete(key);
485
- for (const key of [...entry.bodies.keys()]) {
486
- if (!present.has(key))
487
- entry.bodies.delete(key);
488
- }
489
- keys.forEach((key, index) => kept(entry, key, rowAt(found, index)));
490
- return { keys, next, total };
491
- });
492
- }
493
- function refreshed(entry, dirty) {
494
- const shape = entry.plan.shape;
495
- const { kept: inWindow, next, total, } = windowOf(entry.plan, [...entry.built.live.entries()], counted(entry));
496
- const keys = inWindow.map(([key]) => key);
497
- const present = new Set(keys);
498
- for (const key of [...entry.rows.keys()]) {
499
- if (present.has(key))
500
- continue;
501
- entry.rows.delete(key);
502
- entry.bodies.delete(key);
503
- }
504
- const values = inWindow.filter(([key]) => dirty.has(key) || !entry.rows.has(key));
505
- const done = inUnits(values, ([key, value]) => {
506
- kept(entry, key, documentOf(key, value, shape, entry.bodies));
507
- return weight(value);
508
- });
509
- return after(done, () => ({ keys, next, total }));
510
- }
511
- function current(entry, found, coverage) {
512
- const rows = found.keys.map((key) => entry.rows.get(key));
513
- return {
514
- result: entry.plan.shape.kind === 'groups'
515
- ? framed({ groups: rows, coverage }, found)
516
- : framed({ documents: rows, coverage }, found),
517
- keys: found.keys,
518
- };
519
- }
520
- function delivered(entry, next) {
521
- const previous = entry.result;
522
- entry.result = next;
523
- const message = previous === undefined ? null : changedFrom(previous, next);
524
- if (message !== null)
525
- for (const listener of entry.ready)
526
- listener(message);
527
- for (const listener of entry.fresh) {
528
- entry.ready.add(listener);
529
- listener(next);
530
- }
531
- entry.fresh.clear();
532
- }
533
- function run(entry) {
534
- while (entry.listeners.size > 0 &&
535
- (entry.whole || entry.dirty.size > 0 || entry.stale || entry.fresh.size > 0)) {
536
- entry.stale = false;
537
- const cover = entry.cover();
538
- if ('pending' in cover) {
539
- entry.whole = true;
540
- entry.dirty.clear();
541
- entry.rows.clear();
542
- entry.bodies.clear();
543
- delivered(entry, { result: cover, keys: [] });
544
- break;
545
- }
546
- const whole = entry.whole ||
547
- entry.plan.shape.kind === 'groups' ||
548
- entry.result === undefined ||
549
- 'pending' in entry.result.result;
550
- const dirty = entry.dirty;
551
- entry.dirty = new Set();
552
- entry.whole = false;
553
- const found = whole ? rebuilt(entry) : refreshed(entry, dirty);
554
- if (found instanceof Promise) {
555
- void found
556
- .then((window) => {
557
- if (entry.listeners.size > 0)
558
- delivered(entry, current(entry, window, cover.coverage));
559
- run(entry);
560
- })
561
- .catch(() => {
562
- entry.running = false;
563
- });
564
- return;
565
- }
566
- delivered(entry, current(entry, found, cover.coverage));
567
- }
568
- entry.running = false;
569
- }
570
- function wake(entry) {
571
- if (entry.subscription === null || entry.running)
572
- return;
573
- entry.running = true;
574
- try {
575
- run(entry);
576
- }
577
- catch (error) {
578
- entry.running = false;
579
- throw error;
580
- }
581
- }
582
- function recover() {
583
- for (const entry of shared.values()) {
584
- if (entry.result === undefined || entry.subscription === null)
585
- continue;
586
- const cover = entry.cover();
587
- const before = entry.result.result;
588
- const held = 'pending' in cover
589
- ? deepEquals(before, cover)
590
- : 'coverage' in before && deepEquals(before.coverage, cover.coverage);
591
- if (held)
592
- continue;
593
- entry.stale = true;
594
- wake(entry);
595
- }
596
- }
597
- return {
598
- apply(set) {
599
- const next = new Map();
600
- for (const change of set.documents) {
601
- for (const row of change.before)
602
- next.set(row.key, null);
603
- for (const row of change.after)
604
- next.set(row.key, row);
605
- }
606
- if (next.size > 0) {
607
- sync.begin();
608
- for (const [key, row] of next) {
609
- if (row === null) {
610
- if (rows.has(key))
611
- sync.write({ type: 'delete', key });
612
- continue;
613
- }
614
- sync.write({ type: rows.has(key) ? 'update' : 'insert', value: row });
615
- }
616
- sync.commit();
617
- }
618
- recover();
619
- },
620
- recover,
621
- async query(plan, at) {
622
- const { live: collection, count, dispose } = built(rows, plan);
623
- try {
624
- await collection.toArrayWhenReady();
625
- if (count !== null)
626
- await count.toArrayWhenReady();
627
- const entries = [...collection.entries()];
628
- const held = at();
629
- const found = await rowsOfPlan(plan, entries, count?.size, null, structuredClone);
630
- return { rows: found.rows, at: held };
631
- }
632
- finally {
633
- dispose();
634
- }
635
- },
636
- watch(plan, identity, cover, deliverTo) {
637
- let entry = shared.get(identity);
638
- if (entry === undefined) {
639
- const created = {
640
- plan,
641
- built: built(rows, plan),
642
- cover,
643
- result: undefined,
644
- rows: new Map(),
645
- bodies: new Map(),
646
- listeners: new Set(),
647
- fresh: new Set(),
648
- ready: new Set(),
649
- subscription: null,
650
- counting: null,
651
- whole: true,
652
- dirty: new Set(),
653
- stale: false,
654
- running: false,
655
- };
656
- created.built.live.on('status:change', ({ status }) => {
657
- if (status !== 'cleaned-up' || created.listeners.size > 0)
658
- return;
659
- if (shared.get(identity) === created)
660
- shared.delete(identity);
661
- });
662
- shared.set(identity, created);
663
- entry = created;
664
- }
665
- const observed = entry;
666
- observed.listeners.add(deliverTo);
667
- const ready = (then) => observed.built.live.onFirstReady(() => observed.built.count === null ? then() : observed.built.count.onFirstReady(then));
668
- const settled = () => {
669
- if (observed.built.count === null)
670
- wake(observed);
671
- else
672
- queueMicrotask(() => wake(observed));
673
- };
674
- ready(() => {
675
- if (!observed.listeners.has(deliverTo))
676
- return;
677
- observed.fresh.add(deliverTo);
678
- if (observed.subscription === null) {
679
- observed.whole = true;
680
- observed.subscription = observed.built.live.subscribeChanges((changes) => {
681
- if (observed.plan.shape.kind === 'groups')
682
- observed.whole = true;
683
- else
684
- for (const { key } of changes)
685
- observed.dirty.add(key);
686
- settled();
687
- }, { includeInitialState: false });
688
- observed.counting =
689
- observed.built.count?.subscribeChanges(() => {
690
- observed.stale = true;
691
- settled();
692
- }, { includeInitialState: false }) ?? null;
693
- }
694
- wake(observed);
695
- });
696
- return () => {
697
- observed.fresh.delete(deliverTo);
698
- observed.ready.delete(deliverTo);
699
- if (!observed.listeners.delete(deliverTo) || observed.listeners.size > 0)
700
- return;
701
- observed.subscription?.unsubscribe();
702
- observed.subscription = null;
703
- observed.counting?.unsubscribe();
704
- observed.counting = null;
705
- observed.result = undefined;
706
- observed.rows.clear();
707
- observed.bodies.clear();
708
- observed.dirty.clear();
709
- observed.whole = true;
710
- if (shared.get(identity) === observed)
711
- shared.delete(identity);
712
- observed.built.dispose();
713
- };
714
- },
715
- room(room) {
716
- return rows.toArray.flatMap((row) => row.kind !== 'document' || row.type === null || row.room !== room ? [] : [row.path]);
717
- },
718
- document(room, path) {
719
- const row = rows.get(documentKey(room, path));
720
- return row === undefined || row.type === null ? null : searched(row);
721
- },
722
- close() {
723
- for (const entry of shared.values()) {
724
- entry.listeners.clear();
725
- entry.fresh.clear();
726
- entry.ready.clear();
727
- entry.subscription?.unsubscribe();
728
- entry.counting?.unsubscribe();
729
- entry.built.dispose();
730
- }
731
- shared.clear();
732
- void rows.cleanup();
733
- },
734
- };
735
- }
736
- function changedFrom(previous, next) {
737
- const before = previous.result;
738
- const after = next.result;
739
- if ('pending' in before || 'pending' in after)
740
- return deepEquals(before, after) ? null : next;
741
- const earlier = 'groups' in before ? before.groups : before.documents;
742
- const held = new Map(previous.keys.map((key, index) => [key, earlier[index]]));
743
- const later = 'groups' in after ? after.groups : after.documents;
744
- const changed = [];
745
- next.keys.forEach((key, index) => {
746
- if (held.get(key) !== later[index])
747
- changed.push([key, later[index]]);
748
- });
749
- const present = new Set(next.keys);
750
- const removed = previous.keys.filter((key) => !present.has(key));
751
- const moved = reordered(previous.keys, next.keys, present);
752
- const covered = !deepEquals(before.coverage, after.coverage);
753
- const framing = reframed(before, after);
754
- if (changed.length === 0 && removed.length === 0 && !moved && !covered && framing === null) {
755
- return null;
756
- }
757
- return {
758
- changed,
759
- removed,
760
- order: moved ? next.keys : null,
761
- ...(covered ? { coverage: after.coverage } : {}),
762
- ...framing,
763
- };
764
- }
765
- function rowAt(rows, index) {
766
- return 'groups' in rows ? rows.groups[index] : rows.documents[index];
767
- }