@rocicorp/zero 1.11.0-canary.0 → 1.11.0-canary.10

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 (577) hide show
  1. package/out/analyze-query/src/bin-analyze.js +1 -1
  2. package/out/analyze-query/src/bin-analyze.js.map +1 -1
  3. package/out/replicache/src/btree/read.d.ts +12 -1
  4. package/out/replicache/src/btree/read.d.ts.map +1 -1
  5. package/out/replicache/src/btree/read.js +24 -12
  6. package/out/replicache/src/btree/read.js.map +1 -1
  7. package/out/replicache/src/dag/gc.d.ts.map +1 -1
  8. package/out/replicache/src/dag/gc.js +2 -1
  9. package/out/replicache/src/dag/gc.js.map +1 -1
  10. package/out/replicache/src/dag/invalid-ref-count-error.d.ts +12 -0
  11. package/out/replicache/src/dag/invalid-ref-count-error.d.ts.map +1 -0
  12. package/out/replicache/src/dag/invalid-ref-count-error.js +19 -0
  13. package/out/replicache/src/dag/invalid-ref-count-error.js.map +1 -0
  14. package/out/replicache/src/dag/store-impl.d.ts +12 -2
  15. package/out/replicache/src/dag/store-impl.d.ts.map +1 -1
  16. package/out/replicache/src/dag/store-impl.js +22 -8
  17. package/out/replicache/src/dag/store-impl.js.map +1 -1
  18. package/out/replicache/src/db/rebase.d.ts +14 -2
  19. package/out/replicache/src/db/rebase.d.ts.map +1 -1
  20. package/out/replicache/src/db/rebase.js +27 -5
  21. package/out/replicache/src/db/rebase.js.map +1 -1
  22. package/out/replicache/src/db/write.js.map +1 -1
  23. package/out/replicache/src/kv/expo-sqlite/store.d.ts.map +1 -1
  24. package/out/replicache/src/kv/expo-sqlite/store.js +28 -5
  25. package/out/replicache/src/kv/expo-sqlite/store.js.map +1 -1
  26. package/out/replicache/src/kv/idb-store-with-mem-fallback.d.ts +15 -5
  27. package/out/replicache/src/kv/idb-store-with-mem-fallback.d.ts.map +1 -1
  28. package/out/replicache/src/kv/idb-store-with-mem-fallback.js +25 -25
  29. package/out/replicache/src/kv/idb-store-with-mem-fallback.js.map +1 -1
  30. package/out/replicache/src/kv/idb-store.d.ts +9 -0
  31. package/out/replicache/src/kv/idb-store.d.ts.map +1 -1
  32. package/out/replicache/src/kv/idb-store.js +14 -2
  33. package/out/replicache/src/kv/idb-store.js.map +1 -1
  34. package/out/replicache/src/kv/mem-store.d.ts +1 -0
  35. package/out/replicache/src/kv/mem-store.d.ts.map +1 -1
  36. package/out/replicache/src/kv/mem-store.js +3 -0
  37. package/out/replicache/src/kv/mem-store.js.map +1 -1
  38. package/out/replicache/src/kv/op-sqlite/store.d.ts.map +1 -1
  39. package/out/replicache/src/kv/op-sqlite/store.js +1 -1
  40. package/out/replicache/src/kv/op-sqlite/store.js.map +1 -1
  41. package/out/replicache/src/kv/sqlite-store.d.ts +13 -1
  42. package/out/replicache/src/kv/sqlite-store.d.ts.map +1 -1
  43. package/out/replicache/src/kv/sqlite-store.js +49 -4
  44. package/out/replicache/src/kv/sqlite-store.js.map +1 -1
  45. package/out/replicache/src/kv/store.d.ts +6 -0
  46. package/out/replicache/src/kv/store.d.ts.map +1 -1
  47. package/out/replicache/src/persist/collect-idb-databases.d.ts +10 -5
  48. package/out/replicache/src/persist/collect-idb-databases.d.ts.map +1 -1
  49. package/out/replicache/src/persist/collect-idb-databases.js +16 -9
  50. package/out/replicache/src/persist/collect-idb-databases.js.map +1 -1
  51. package/out/replicache/src/persist/persist.d.ts.map +1 -1
  52. package/out/replicache/src/persist/persist.js +12 -5
  53. package/out/replicache/src/persist/persist.js.map +1 -1
  54. package/out/replicache/src/persist/refresh.js +6 -2
  55. package/out/replicache/src/persist/refresh.js.map +1 -1
  56. package/out/replicache/src/replicache-impl.d.ts +14 -3
  57. package/out/replicache/src/replicache-impl.d.ts.map +1 -1
  58. package/out/replicache/src/replicache-impl.js +118 -11
  59. package/out/replicache/src/replicache-impl.js.map +1 -1
  60. package/out/replicache/src/replicache-options.d.ts +9 -0
  61. package/out/replicache/src/replicache-options.d.ts.map +1 -1
  62. package/out/replicache/src/transactions.d.ts.map +1 -1
  63. package/out/replicache/src/transactions.js +1 -1
  64. package/out/replicache/src/transactions.js.map +1 -1
  65. package/out/shared/src/error.d.ts +11 -0
  66. package/out/shared/src/error.d.ts.map +1 -1
  67. package/out/shared/src/error.js +35 -1
  68. package/out/shared/src/error.js.map +1 -1
  69. package/out/shared/src/iterables.d.ts +2 -0
  70. package/out/shared/src/iterables.d.ts.map +1 -1
  71. package/out/shared/src/iterables.js +18 -1
  72. package/out/shared/src/iterables.js.map +1 -1
  73. package/out/shared/src/map.d.ts +7 -2
  74. package/out/shared/src/map.d.ts.map +1 -1
  75. package/out/shared/src/map.js.map +1 -1
  76. package/out/shared/src/ring-buffer.d.ts +8 -0
  77. package/out/shared/src/ring-buffer.d.ts.map +1 -1
  78. package/out/shared/src/ring-buffer.js +22 -1
  79. package/out/shared/src/ring-buffer.js.map +1 -1
  80. package/out/zero/package.js +3 -4
  81. package/out/zero/package.js.map +1 -1
  82. package/out/zero-cache/src/config/normalize.d.ts.map +1 -1
  83. package/out/zero-cache/src/config/normalize.js +25 -1
  84. package/out/zero-cache/src/config/normalize.js.map +1 -1
  85. package/out/zero-cache/src/config/zero-config.d.ts +48 -2
  86. package/out/zero-cache/src/config/zero-config.d.ts.map +1 -1
  87. package/out/zero-cache/src/config/zero-config.js +171 -29
  88. package/out/zero-cache/src/config/zero-config.js.map +1 -1
  89. package/out/zero-cache/src/server/change-streamer.d.ts.map +1 -1
  90. package/out/zero-cache/src/server/change-streamer.js +25 -9
  91. package/out/zero-cache/src/server/change-streamer.js.map +1 -1
  92. package/out/zero-cache/src/server/inspector-delegate.d.ts +18 -2
  93. package/out/zero-cache/src/server/inspector-delegate.d.ts.map +1 -1
  94. package/out/zero-cache/src/server/inspector-delegate.js +20 -7
  95. package/out/zero-cache/src/server/inspector-delegate.js.map +1 -1
  96. package/out/zero-cache/src/server/main.d.ts.map +1 -1
  97. package/out/zero-cache/src/server/main.js +8 -1
  98. package/out/zero-cache/src/server/main.js.map +1 -1
  99. package/out/zero-cache/src/server/mutator.js +2 -0
  100. package/out/zero-cache/src/server/mutator.js.map +1 -1
  101. package/out/zero-cache/src/server/reaper.d.ts.map +1 -1
  102. package/out/zero-cache/src/server/reaper.js +2 -0
  103. package/out/zero-cache/src/server/reaper.js.map +1 -1
  104. package/out/zero-cache/src/server/replicator.d.ts.map +1 -1
  105. package/out/zero-cache/src/server/replicator.js +8 -2
  106. package/out/zero-cache/src/server/replicator.js.map +1 -1
  107. package/out/zero-cache/src/server/runner/run-worker.js +1 -1
  108. package/out/zero-cache/src/server/runner/zero-dispatcher.d.ts.map +1 -1
  109. package/out/zero-cache/src/server/runner/zero-dispatcher.js +3 -0
  110. package/out/zero-cache/src/server/runner/zero-dispatcher.js.map +1 -1
  111. package/out/zero-cache/src/server/syncer.d.ts.map +1 -1
  112. package/out/zero-cache/src/server/syncer.js +2 -0
  113. package/out/zero-cache/src/server/syncer.js.map +1 -1
  114. package/out/zero-cache/src/services/analyze.d.ts.map +1 -1
  115. package/out/zero-cache/src/services/analyze.js +3 -2
  116. package/out/zero-cache/src/services/analyze.js.map +1 -1
  117. package/out/zero-cache/src/services/change-source/common/backfill-manager.d.ts.map +1 -1
  118. package/out/zero-cache/src/services/change-source/common/backfill-manager.js +14 -1
  119. package/out/zero-cache/src/services/change-source/common/backfill-manager.js.map +1 -1
  120. package/out/zero-cache/src/services/change-source/common/change-stream-multiplexer.d.ts.map +1 -1
  121. package/out/zero-cache/src/services/change-source/common/change-stream-multiplexer.js +15 -3
  122. package/out/zero-cache/src/services/change-source/common/change-stream-multiplexer.js.map +1 -1
  123. package/out/zero-cache/src/services/change-source/common/replica-schema.d.ts.map +1 -1
  124. package/out/zero-cache/src/services/change-source/common/replica-schema.js +2 -39
  125. package/out/zero-cache/src/services/change-source/common/replica-schema.js.map +1 -1
  126. package/out/zero-cache/src/services/change-source/pg/backfill-resume.d.ts +156 -0
  127. package/out/zero-cache/src/services/change-source/pg/backfill-resume.d.ts.map +1 -0
  128. package/out/zero-cache/src/services/change-source/pg/backfill-resume.js +15 -0
  129. package/out/zero-cache/src/services/change-source/pg/backfill-resume.js.map +1 -0
  130. package/out/zero-cache/src/services/change-source/pg/change-source-init.d.ts +25 -0
  131. package/out/zero-cache/src/services/change-source/pg/change-source-init.d.ts.map +1 -0
  132. package/out/zero-cache/src/services/change-source/pg/change-source-init.js +202 -0
  133. package/out/zero-cache/src/services/change-source/pg/change-source-init.js.map +1 -0
  134. package/out/zero-cache/src/services/change-source/pg/change-source.d.ts +14 -16
  135. package/out/zero-cache/src/services/change-source/pg/change-source.d.ts.map +1 -1
  136. package/out/zero-cache/src/services/change-source/pg/change-source.js +64 -143
  137. package/out/zero-cache/src/services/change-source/pg/change-source.js.map +1 -1
  138. package/out/zero-cache/src/services/change-source/pg/initial-sync.d.ts +28 -2
  139. package/out/zero-cache/src/services/change-source/pg/initial-sync.d.ts.map +1 -1
  140. package/out/zero-cache/src/services/change-source/pg/initial-sync.js +44 -21
  141. package/out/zero-cache/src/services/change-source/pg/initial-sync.js.map +1 -1
  142. package/out/zero-cache/src/services/change-source/pg/replication-slot-cleanup-monitor.d.ts +16 -0
  143. package/out/zero-cache/src/services/change-source/pg/replication-slot-cleanup-monitor.d.ts.map +1 -0
  144. package/out/zero-cache/src/services/change-source/pg/replication-slot-cleanup-monitor.js +113 -0
  145. package/out/zero-cache/src/services/change-source/pg/replication-slot-cleanup-monitor.js.map +1 -0
  146. package/out/zero-cache/src/services/change-source/pg/replication-slots.d.ts +21 -15
  147. package/out/zero-cache/src/services/change-source/pg/replication-slots.d.ts.map +1 -1
  148. package/out/zero-cache/src/services/change-source/pg/replication-slots.js +86 -23
  149. package/out/zero-cache/src/services/change-source/pg/replication-slots.js.map +1 -1
  150. package/out/zero-cache/src/services/change-source/pg/schema/index-predicate.d.ts.map +1 -1
  151. package/out/zero-cache/src/services/change-source/pg/schema/index-predicate.js +68 -8
  152. package/out/zero-cache/src/services/change-source/pg/schema/index-predicate.js.map +1 -1
  153. package/out/zero-cache/src/services/change-source/pg/schema/init.d.ts.map +1 -1
  154. package/out/zero-cache/src/services/change-source/pg/schema/init.js +32 -1
  155. package/out/zero-cache/src/services/change-source/pg/schema/init.js.map +1 -1
  156. package/out/zero-cache/src/services/change-source/pg/schema/replica-stage-enum.d.ts +10 -0
  157. package/out/zero-cache/src/services/change-source/pg/schema/replica-stage-enum.d.ts.map +1 -0
  158. package/out/zero-cache/src/services/change-source/pg/schema/replica-stage-enum.js +6 -0
  159. package/out/zero-cache/src/services/change-source/pg/schema/replica-stage-enum.js.map +1 -0
  160. package/out/zero-cache/src/services/change-source/pg/schema/shard.d.ts +50 -27
  161. package/out/zero-cache/src/services/change-source/pg/schema/shard.d.ts.map +1 -1
  162. package/out/zero-cache/src/services/change-source/pg/schema/shard.js +100 -34
  163. package/out/zero-cache/src/services/change-source/pg/schema/shard.js.map +1 -1
  164. package/out/zero-cache/src/services/change-streamer/backup-monitor.d.ts.map +1 -1
  165. package/out/zero-cache/src/services/change-streamer/backup-monitor.js +58 -7
  166. package/out/zero-cache/src/services/change-streamer/backup-monitor.js.map +1 -1
  167. package/out/zero-cache/src/services/change-streamer/broadcast.d.ts +8 -2
  168. package/out/zero-cache/src/services/change-streamer/broadcast.d.ts.map +1 -1
  169. package/out/zero-cache/src/services/change-streamer/broadcast.js +24 -4
  170. package/out/zero-cache/src/services/change-streamer/broadcast.js.map +1 -1
  171. package/out/zero-cache/src/services/change-streamer/change-log-codec.d.ts +19 -4
  172. package/out/zero-cache/src/services/change-streamer/change-log-codec.d.ts.map +1 -1
  173. package/out/zero-cache/src/services/change-streamer/change-log-codec.js +28 -8
  174. package/out/zero-cache/src/services/change-streamer/change-log-codec.js.map +1 -1
  175. package/out/zero-cache/src/services/change-streamer/change-streamer-http.d.ts +5 -2
  176. package/out/zero-cache/src/services/change-streamer/change-streamer-http.d.ts.map +1 -1
  177. package/out/zero-cache/src/services/change-streamer/change-streamer-http.js +21 -14
  178. package/out/zero-cache/src/services/change-streamer/change-streamer-http.js.map +1 -1
  179. package/out/zero-cache/src/services/change-streamer/change-streamer-service.d.ts +6 -0
  180. package/out/zero-cache/src/services/change-streamer/change-streamer-service.d.ts.map +1 -1
  181. package/out/zero-cache/src/services/change-streamer/change-streamer-service.js +97 -63
  182. package/out/zero-cache/src/services/change-streamer/change-streamer-service.js.map +1 -1
  183. package/out/zero-cache/src/services/change-streamer/change-streamer.d.ts +12 -8
  184. package/out/zero-cache/src/services/change-streamer/change-streamer.d.ts.map +1 -1
  185. package/out/zero-cache/src/services/change-streamer/change-streamer.js.map +1 -1
  186. package/out/zero-cache/src/services/change-streamer/forwarder.d.ts +3 -3
  187. package/out/zero-cache/src/services/change-streamer/forwarder.d.ts.map +1 -1
  188. package/out/zero-cache/src/services/change-streamer/forwarder.js +36 -5
  189. package/out/zero-cache/src/services/change-streamer/forwarder.js.map +1 -1
  190. package/out/zero-cache/src/services/change-streamer/schema/tables.d.ts +1 -1
  191. package/out/zero-cache/src/services/change-streamer/schema/tables.d.ts.map +1 -1
  192. package/out/zero-cache/src/services/change-streamer/schema/tables.js +4 -4
  193. package/out/zero-cache/src/services/change-streamer/schema/tables.js.map +1 -1
  194. package/out/zero-cache/src/services/change-streamer/snapshot.d.ts +3 -1
  195. package/out/zero-cache/src/services/change-streamer/snapshot.d.ts.map +1 -1
  196. package/out/zero-cache/src/services/change-streamer/snapshot.js +54 -20
  197. package/out/zero-cache/src/services/change-streamer/snapshot.js.map +1 -1
  198. package/out/zero-cache/src/services/change-streamer/sqlite-change-log-writer.d.ts +6 -1
  199. package/out/zero-cache/src/services/change-streamer/sqlite-change-log-writer.d.ts.map +1 -1
  200. package/out/zero-cache/src/services/change-streamer/sqlite-change-log-writer.js +27 -9
  201. package/out/zero-cache/src/services/change-streamer/sqlite-change-log-writer.js.map +1 -1
  202. package/out/zero-cache/src/services/change-streamer/storer.d.ts +2 -2
  203. package/out/zero-cache/src/services/change-streamer/storer.d.ts.map +1 -1
  204. package/out/zero-cache/src/services/change-streamer/storer.js +8 -5
  205. package/out/zero-cache/src/services/change-streamer/storer.js.map +1 -1
  206. package/out/zero-cache/src/services/change-streamer/subscriber.d.ts +4 -1
  207. package/out/zero-cache/src/services/change-streamer/subscriber.d.ts.map +1 -1
  208. package/out/zero-cache/src/services/change-streamer/subscriber.js +42 -0
  209. package/out/zero-cache/src/services/change-streamer/subscriber.js.map +1 -1
  210. package/out/zero-cache/src/services/change-streamer/upstream-acker.d.ts +17 -1
  211. package/out/zero-cache/src/services/change-streamer/upstream-acker.d.ts.map +1 -1
  212. package/out/zero-cache/src/services/change-streamer/upstream-acker.js +19 -3
  213. package/out/zero-cache/src/services/change-streamer/upstream-acker.js.map +1 -1
  214. package/out/zero-cache/src/services/change-streamer/vfs-watermark-poller.d.ts.map +1 -1
  215. package/out/zero-cache/src/services/change-streamer/vfs-watermark-poller.js +1 -0
  216. package/out/zero-cache/src/services/change-streamer/vfs-watermark-poller.js.map +1 -1
  217. package/out/zero-cache/src/services/life-cycle.d.ts.map +1 -1
  218. package/out/zero-cache/src/services/life-cycle.js +3 -1
  219. package/out/zero-cache/src/services/life-cycle.js.map +1 -1
  220. package/out/zero-cache/src/services/litestream/commands.d.ts.map +1 -1
  221. package/out/zero-cache/src/services/litestream/commands.js +8 -3
  222. package/out/zero-cache/src/services/litestream/commands.js.map +1 -1
  223. package/out/zero-cache/src/services/litestream/config-v5.yml +15 -1
  224. package/out/zero-cache/src/services/litestream/litestream-checkpointer.js +131 -0
  225. package/out/zero-cache/src/services/litestream/litestream-checkpointer.js.map +1 -0
  226. package/out/zero-cache/src/services/litestream/litestream-controller.d.ts +70 -0
  227. package/out/zero-cache/src/services/litestream/litestream-controller.d.ts.map +1 -0
  228. package/out/zero-cache/src/services/litestream/litestream-controller.js +101 -0
  229. package/out/zero-cache/src/services/litestream/litestream-controller.js.map +1 -0
  230. package/out/zero-cache/src/services/mutagen/mutagen.d.ts +5 -0
  231. package/out/zero-cache/src/services/mutagen/mutagen.d.ts.map +1 -1
  232. package/out/zero-cache/src/services/mutagen/mutagen.js +7 -1
  233. package/out/zero-cache/src/services/mutagen/mutagen.js.map +1 -1
  234. package/out/zero-cache/src/services/mutagen/pusher.js +2 -1
  235. package/out/zero-cache/src/services/mutagen/pusher.js.map +1 -1
  236. package/out/zero-cache/src/services/profz.d.ts +7 -0
  237. package/out/zero-cache/src/services/profz.d.ts.map +1 -0
  238. package/out/zero-cache/src/services/profz.js +105 -0
  239. package/out/zero-cache/src/services/profz.js.map +1 -0
  240. package/out/zero-cache/src/services/replicator/change-log-db.js.map +1 -1
  241. package/out/zero-cache/src/services/replicator/change-log-stream-writer.d.ts +5 -4
  242. package/out/zero-cache/src/services/replicator/change-log-stream-writer.d.ts.map +1 -1
  243. package/out/zero-cache/src/services/replicator/change-log-stream-writer.js +20 -22
  244. package/out/zero-cache/src/services/replicator/change-log-stream-writer.js.map +1 -1
  245. package/out/zero-cache/src/services/replicator/change-processor.js +1 -0
  246. package/out/zero-cache/src/services/replicator/change-processor.js.map +1 -1
  247. package/out/zero-cache/src/services/replicator/incremental-sync.d.ts +1 -1
  248. package/out/zero-cache/src/services/replicator/incremental-sync.d.ts.map +1 -1
  249. package/out/zero-cache/src/services/replicator/incremental-sync.js +58 -17
  250. package/out/zero-cache/src/services/replicator/incremental-sync.js.map +1 -1
  251. package/out/zero-cache/src/services/replicator/replication-status.d.ts +35 -1
  252. package/out/zero-cache/src/services/replicator/replication-status.d.ts.map +1 -1
  253. package/out/zero-cache/src/services/replicator/replication-status.js +96 -6
  254. package/out/zero-cache/src/services/replicator/replication-status.js.map +1 -1
  255. package/out/zero-cache/src/services/replicator/replicator.js.map +1 -1
  256. package/out/zero-cache/src/services/replicator/sqlite-change-log-observability.d.ts +6 -6
  257. package/out/zero-cache/src/services/replicator/sqlite-change-log-observability.d.ts.map +1 -1
  258. package/out/zero-cache/src/services/replicator/sqlite-change-log-observability.js +19 -14
  259. package/out/zero-cache/src/services/replicator/sqlite-change-log-observability.js.map +1 -1
  260. package/out/zero-cache/src/services/replicator/write-worker-client.d.ts +31 -6
  261. package/out/zero-cache/src/services/replicator/write-worker-client.d.ts.map +1 -1
  262. package/out/zero-cache/src/services/replicator/write-worker-client.js +5 -4
  263. package/out/zero-cache/src/services/replicator/write-worker-client.js.map +1 -1
  264. package/out/zero-cache/src/services/replicator/write-worker.js +30 -5
  265. package/out/zero-cache/src/services/replicator/write-worker.js.map +1 -1
  266. package/out/zero-cache/src/services/run-ast.d.ts +12 -0
  267. package/out/zero-cache/src/services/run-ast.d.ts.map +1 -1
  268. package/out/zero-cache/src/services/run-ast.js +26 -14
  269. package/out/zero-cache/src/services/run-ast.js.map +1 -1
  270. package/out/zero-cache/src/services/runner.d.ts.map +1 -1
  271. package/out/zero-cache/src/services/runner.js +1 -1
  272. package/out/zero-cache/src/services/runner.js.map +1 -1
  273. package/out/zero-cache/src/services/view-syncer/client-handler.d.ts +1 -1
  274. package/out/zero-cache/src/services/view-syncer/client-handler.d.ts.map +1 -1
  275. package/out/zero-cache/src/services/view-syncer/client-handler.js +36 -31
  276. package/out/zero-cache/src/services/view-syncer/client-handler.js.map +1 -1
  277. package/out/zero-cache/src/services/view-syncer/cvr.d.ts +45 -0
  278. package/out/zero-cache/src/services/view-syncer/cvr.d.ts.map +1 -1
  279. package/out/zero-cache/src/services/view-syncer/cvr.js +133 -1
  280. package/out/zero-cache/src/services/view-syncer/cvr.js.map +1 -1
  281. package/out/zero-cache/src/services/view-syncer/drain-coordinator.d.ts +14 -1
  282. package/out/zero-cache/src/services/view-syncer/drain-coordinator.d.ts.map +1 -1
  283. package/out/zero-cache/src/services/view-syncer/drain-coordinator.js +30 -4
  284. package/out/zero-cache/src/services/view-syncer/drain-coordinator.js.map +1 -1
  285. package/out/zero-cache/src/services/view-syncer/hydration-budget.d.ts +33 -0
  286. package/out/zero-cache/src/services/view-syncer/hydration-budget.d.ts.map +1 -0
  287. package/out/zero-cache/src/services/view-syncer/hydration-budget.js +62 -0
  288. package/out/zero-cache/src/services/view-syncer/hydration-budget.js.map +1 -0
  289. package/out/zero-cache/src/services/view-syncer/hydration-circuit-breaker.d.ts +51 -0
  290. package/out/zero-cache/src/services/view-syncer/hydration-circuit-breaker.d.ts.map +1 -0
  291. package/out/zero-cache/src/services/view-syncer/hydration-circuit-breaker.js +78 -0
  292. package/out/zero-cache/src/services/view-syncer/hydration-circuit-breaker.js.map +1 -0
  293. package/out/zero-cache/src/services/view-syncer/inspect-handler.d.ts +1 -1
  294. package/out/zero-cache/src/services/view-syncer/inspect-handler.d.ts.map +1 -1
  295. package/out/zero-cache/src/services/view-syncer/inspect-handler.js +2 -2
  296. package/out/zero-cache/src/services/view-syncer/inspect-handler.js.map +1 -1
  297. package/out/zero-cache/src/services/view-syncer/pipeline-driver.d.ts +2 -1
  298. package/out/zero-cache/src/services/view-syncer/pipeline-driver.d.ts.map +1 -1
  299. package/out/zero-cache/src/services/view-syncer/pipeline-driver.js +33 -18
  300. package/out/zero-cache/src/services/view-syncer/pipeline-driver.js.map +1 -1
  301. package/out/zero-cache/src/services/view-syncer/snapshotter.d.ts +4 -1
  302. package/out/zero-cache/src/services/view-syncer/snapshotter.d.ts.map +1 -1
  303. package/out/zero-cache/src/services/view-syncer/snapshotter.js +9 -3
  304. package/out/zero-cache/src/services/view-syncer/snapshotter.js.map +1 -1
  305. package/out/zero-cache/src/services/view-syncer/view-syncer.d.ts +11 -1
  306. package/out/zero-cache/src/services/view-syncer/view-syncer.d.ts.map +1 -1
  307. package/out/zero-cache/src/services/view-syncer/view-syncer.js +507 -93
  308. package/out/zero-cache/src/services/view-syncer/view-syncer.js.map +1 -1
  309. package/out/zero-cache/src/types/processes.d.ts +32 -0
  310. package/out/zero-cache/src/types/processes.d.ts.map +1 -1
  311. package/out/zero-cache/src/types/processes.js +43 -4
  312. package/out/zero-cache/src/types/processes.js.map +1 -1
  313. package/out/zero-cache/src/types/profiler.d.ts +12 -0
  314. package/out/zero-cache/src/types/profiler.d.ts.map +1 -1
  315. package/out/zero-cache/src/types/profiler.js +46 -1
  316. package/out/zero-cache/src/types/profiler.js.map +1 -1
  317. package/out/zero-cache/src/types/streams.d.ts +25 -9
  318. package/out/zero-cache/src/types/streams.d.ts.map +1 -1
  319. package/out/zero-cache/src/types/streams.js +118 -34
  320. package/out/zero-cache/src/types/streams.js.map +1 -1
  321. package/out/zero-cache/src/types/subscription.d.ts +4 -0
  322. package/out/zero-cache/src/types/subscription.d.ts.map +1 -1
  323. package/out/zero-cache/src/types/subscription.js +71 -0
  324. package/out/zero-cache/src/types/subscription.js.map +1 -1
  325. package/out/zero-cache/src/types/ws.d.ts.map +1 -1
  326. package/out/zero-cache/src/types/ws.js +23 -8
  327. package/out/zero-cache/src/types/ws.js.map +1 -1
  328. package/out/zero-cache/src/workers/connection.js +2 -2
  329. package/out/zero-cache/src/workers/replicator.d.ts +1 -0
  330. package/out/zero-cache/src/workers/replicator.d.ts.map +1 -1
  331. package/out/zero-cache/src/workers/replicator.js +4 -2
  332. package/out/zero-cache/src/workers/replicator.js.map +1 -1
  333. package/out/zero-cache/src/workers/syncer-ws-message-handler.d.ts.map +1 -1
  334. package/out/zero-cache/src/workers/syncer-ws-message-handler.js +21 -16
  335. package/out/zero-cache/src/workers/syncer-ws-message-handler.js.map +1 -1
  336. package/out/zero-cache/src/workers/syncer.d.ts.map +1 -1
  337. package/out/zero-cache/src/workers/syncer.js +10 -1
  338. package/out/zero-cache/src/workers/syncer.js.map +1 -1
  339. package/out/zero-client/src/client/connection-manager.d.ts +20 -0
  340. package/out/zero-client/src/client/connection-manager.d.ts.map +1 -1
  341. package/out/zero-client/src/client/connection-manager.js +77 -13
  342. package/out/zero-client/src/client/connection-manager.js.map +1 -1
  343. package/out/zero-client/src/client/connection-status-enum.d.ts +2 -0
  344. package/out/zero-client/src/client/connection-status-enum.d.ts.map +1 -1
  345. package/out/zero-client/src/client/connection-status-enum.js +3 -1
  346. package/out/zero-client/src/client/connection-status-enum.js.map +1 -1
  347. package/out/zero-client/src/client/connection.d.ts +6 -0
  348. package/out/zero-client/src/client/connection.d.ts.map +1 -1
  349. package/out/zero-client/src/client/connection.js +2 -1
  350. package/out/zero-client/src/client/connection.js.map +1 -1
  351. package/out/zero-client/src/client/context.d.ts +27 -2
  352. package/out/zero-client/src/client/context.d.ts.map +1 -1
  353. package/out/zero-client/src/client/context.js +112 -1
  354. package/out/zero-client/src/client/context.js.map +1 -1
  355. package/out/zero-client/src/client/custom.js +2 -2
  356. package/out/zero-client/src/client/custom.js.map +1 -1
  357. package/out/zero-client/src/client/error.js +1 -1
  358. package/out/zero-client/src/client/inspector/lazy-inspector.js +2 -2
  359. package/out/zero-client/src/client/inspector/lazy-inspector.js.map +1 -1
  360. package/out/zero-client/src/client/ivm-branch.d.ts.map +1 -1
  361. package/out/zero-client/src/client/ivm-branch.js +20 -3
  362. package/out/zero-client/src/client/ivm-branch.js.map +1 -1
  363. package/out/zero-client/src/client/mutator-proxy.d.ts.map +1 -1
  364. package/out/zero-client/src/client/mutator-proxy.js +2 -1
  365. package/out/zero-client/src/client/mutator-proxy.js.map +1 -1
  366. package/out/zero-client/src/client/options.d.ts +3 -0
  367. package/out/zero-client/src/client/options.d.ts.map +1 -1
  368. package/out/zero-client/src/client/options.js.map +1 -1
  369. package/out/zero-client/src/client/query-manager.d.ts.map +1 -1
  370. package/out/zero-client/src/client/query-manager.js +42 -6
  371. package/out/zero-client/src/client/query-manager.js.map +1 -1
  372. package/out/zero-client/src/client/version.js +1 -1
  373. package/out/zero-client/src/client/zero-rep.d.ts +1 -0
  374. package/out/zero-client/src/client/zero-rep.d.ts.map +1 -1
  375. package/out/zero-client/src/client/zero-rep.js +9 -6
  376. package/out/zero-client/src/client/zero-rep.js.map +1 -1
  377. package/out/zero-client/src/client/zero.d.ts +18 -3
  378. package/out/zero-client/src/client/zero.d.ts.map +1 -1
  379. package/out/zero-client/src/client/zero.js +122 -34
  380. package/out/zero-client/src/client/zero.js.map +1 -1
  381. package/out/zero-client/src/types/query-result.d.ts +2 -0
  382. package/out/zero-client/src/types/query-result.d.ts.map +1 -1
  383. package/out/zero-events/src/status.d.ts +26 -0
  384. package/out/zero-events/src/status.d.ts.map +1 -1
  385. package/out/zero-protocol/src/analyze-query-result.d.ts +5 -5
  386. package/out/zero-protocol/src/analyze-query-result.d.ts.map +1 -1
  387. package/out/zero-protocol/src/analyze-query-result.js +1 -1
  388. package/out/zero-protocol/src/analyze-query-result.js.map +1 -1
  389. package/out/zero-protocol/src/down.d.ts +4 -4
  390. package/out/zero-protocol/src/inspect-down.d.ts +6 -6
  391. package/out/zero-protocol/src/protocol-version.d.ts +1 -1
  392. package/out/zero-protocol/src/protocol-version.d.ts.map +1 -1
  393. package/out/zero-protocol/src/protocol-version.js.map +1 -1
  394. package/out/zero-react/src/use-query.d.ts +5 -4
  395. package/out/zero-react/src/use-query.d.ts.map +1 -1
  396. package/out/zero-react/src/use-query.js +86 -9
  397. package/out/zero-react/src/use-query.js.map +1 -1
  398. package/out/zero-server/src/adapters/drizzle.d.ts +2 -1
  399. package/out/zero-server/src/adapters/drizzle.d.ts.map +1 -1
  400. package/out/zero-server/src/adapters/drizzle.js +12 -7
  401. package/out/zero-server/src/adapters/drizzle.js.map +1 -1
  402. package/out/zero-server/src/adapters/kysely.d.ts +2 -1
  403. package/out/zero-server/src/adapters/kysely.d.ts.map +1 -1
  404. package/out/zero-server/src/adapters/kysely.js +8 -2
  405. package/out/zero-server/src/adapters/kysely.js.map +1 -1
  406. package/out/zero-server/src/adapters/pg.d.ts +1 -0
  407. package/out/zero-server/src/adapters/pg.d.ts.map +1 -1
  408. package/out/zero-server/src/adapters/pg.js +8 -2
  409. package/out/zero-server/src/adapters/pg.js.map +1 -1
  410. package/out/zero-server/src/adapters/postgresjs.d.ts +1 -0
  411. package/out/zero-server/src/adapters/postgresjs.d.ts.map +1 -1
  412. package/out/zero-server/src/adapters/postgresjs.js +7 -1
  413. package/out/zero-server/src/adapters/postgresjs.js.map +1 -1
  414. package/out/zero-server/src/adapters/prisma.d.ts +3 -2
  415. package/out/zero-server/src/adapters/prisma.d.ts.map +1 -1
  416. package/out/zero-server/src/adapters/prisma.js +8 -2
  417. package/out/zero-server/src/adapters/prisma.js.map +1 -1
  418. package/out/zero-server/src/custom.d.ts +5 -1
  419. package/out/zero-server/src/custom.d.ts.map +1 -1
  420. package/out/zero-server/src/custom.js +6 -3
  421. package/out/zero-server/src/custom.js.map +1 -1
  422. package/out/zero-server/src/mod.d.ts +1 -1
  423. package/out/zero-server/src/mod.d.ts.map +1 -1
  424. package/out/zero-server/src/pg-query-executor.d.ts +2 -2
  425. package/out/zero-server/src/pg-query-executor.d.ts.map +1 -1
  426. package/out/zero-server/src/pg-query-executor.js +2 -2
  427. package/out/zero-server/src/pg-query-executor.js.map +1 -1
  428. package/out/zero-server/src/schema.d.ts +2 -2
  429. package/out/zero-server/src/schema.d.ts.map +1 -1
  430. package/out/zero-server/src/schema.js +2 -2
  431. package/out/zero-server/src/schema.js.map +1 -1
  432. package/out/zero-server/src/zql-database.d.ts +21 -1
  433. package/out/zero-server/src/zql-database.d.ts.map +1 -1
  434. package/out/zero-server/src/zql-database.js +33 -2
  435. package/out/zero-server/src/zql-database.js.map +1 -1
  436. package/out/zero-solid/src/solid-view.d.ts +17 -0
  437. package/out/zero-solid/src/solid-view.d.ts.map +1 -1
  438. package/out/zero-solid/src/solid-view.js +39 -1
  439. package/out/zero-solid/src/solid-view.js.map +1 -1
  440. package/out/zero-solid/src/use-query.js +5 -3
  441. package/out/zero-solid/src/use-query.js.map +1 -1
  442. package/out/zql/src/builder/builder.d.ts.map +1 -1
  443. package/out/zql/src/builder/builder.js +16 -10
  444. package/out/zql/src/builder/builder.js.map +1 -1
  445. package/out/zql/src/builder/debug-delegate.d.ts +6 -1
  446. package/out/zql/src/builder/debug-delegate.d.ts.map +1 -1
  447. package/out/zql/src/builder/debug-delegate.js +14 -2
  448. package/out/zql/src/builder/debug-delegate.js.map +1 -1
  449. package/out/zql/src/ivm/array-view.d.ts +29 -0
  450. package/out/zql/src/ivm/array-view.d.ts.map +1 -1
  451. package/out/zql/src/ivm/array-view.js +63 -12
  452. package/out/zql/src/ivm/array-view.js.map +1 -1
  453. package/out/zql/src/ivm/cap.d.ts.map +1 -1
  454. package/out/zql/src/ivm/cap.js +5 -3
  455. package/out/zql/src/ivm/cap.js.map +1 -1
  456. package/out/zql/src/ivm/data.d.ts.map +1 -1
  457. package/out/zql/src/ivm/data.js +8 -3
  458. package/out/zql/src/ivm/data.js.map +1 -1
  459. package/out/zql/src/ivm/deferred-input.d.ts +44 -0
  460. package/out/zql/src/ivm/deferred-input.d.ts.map +1 -0
  461. package/out/zql/src/ivm/deferred-input.js +89 -0
  462. package/out/zql/src/ivm/deferred-input.js.map +1 -0
  463. package/out/zql/src/ivm/exists.d.ts +1 -1
  464. package/out/zql/src/ivm/exists.d.ts.map +1 -1
  465. package/out/zql/src/ivm/exists.js.map +1 -1
  466. package/out/zql/src/ivm/fan-in.d.ts +2 -2
  467. package/out/zql/src/ivm/fan-in.d.ts.map +1 -1
  468. package/out/zql/src/ivm/fan-in.js +6 -5
  469. package/out/zql/src/ivm/fan-in.js.map +1 -1
  470. package/out/zql/src/ivm/fan-out.d.ts +2 -2
  471. package/out/zql/src/ivm/fan-out.d.ts.map +1 -1
  472. package/out/zql/src/ivm/fan-out.js.map +1 -1
  473. package/out/zql/src/ivm/filter-operators.d.ts +7 -6
  474. package/out/zql/src/ivm/filter-operators.d.ts.map +1 -1
  475. package/out/zql/src/ivm/filter-operators.js +29 -12
  476. package/out/zql/src/ivm/filter-operators.js.map +1 -1
  477. package/out/zql/src/ivm/filter.d.ts +2 -2
  478. package/out/zql/src/ivm/filter.d.ts.map +1 -1
  479. package/out/zql/src/ivm/filter.js +7 -4
  480. package/out/zql/src/ivm/filter.js.map +1 -1
  481. package/out/zql/src/ivm/flipped-join.js +2 -2
  482. package/out/zql/src/ivm/flipped-join.js.map +1 -1
  483. package/out/zql/src/ivm/maybe-split-and-push-edit-change.d.ts +1 -1
  484. package/out/zql/src/ivm/maybe-split-and-push-edit-change.d.ts.map +1 -1
  485. package/out/zql/src/ivm/maybe-split-and-push-edit-change.js +6 -4
  486. package/out/zql/src/ivm/maybe-split-and-push-edit-change.js.map +1 -1
  487. package/out/zql/src/ivm/memory-source.d.ts +15 -3
  488. package/out/zql/src/ivm/memory-source.d.ts.map +1 -1
  489. package/out/zql/src/ivm/memory-source.js +57 -23
  490. package/out/zql/src/ivm/memory-source.js.map +1 -1
  491. package/out/zql/src/ivm/operator.d.ts +40 -0
  492. package/out/zql/src/ivm/operator.d.ts.map +1 -1
  493. package/out/zql/src/ivm/operator.js.map +1 -1
  494. package/out/zql/src/ivm/take.d.ts.map +1 -1
  495. package/out/zql/src/ivm/take.js +3 -4
  496. package/out/zql/src/ivm/take.js.map +1 -1
  497. package/out/zql/src/ivm/union-fan-in.d.ts.map +1 -1
  498. package/out/zql/src/ivm/union-fan-in.js +9 -7
  499. package/out/zql/src/ivm/union-fan-in.js.map +1 -1
  500. package/out/zql/src/ivm/view-apply-change.d.ts.map +1 -1
  501. package/out/zql/src/ivm/view-apply-change.js +9 -4
  502. package/out/zql/src/ivm/view-apply-change.js.map +1 -1
  503. package/out/zql/src/ivm/view.d.ts +15 -0
  504. package/out/zql/src/ivm/view.d.ts.map +1 -1
  505. package/out/zql/src/mutate/custom.d.ts +24 -1
  506. package/out/zql/src/mutate/custom.d.ts.map +1 -1
  507. package/out/zql/src/mutate/custom.js.map +1 -1
  508. package/out/zql/src/mutate/mutator-registry.d.ts.map +1 -1
  509. package/out/zql/src/mutate/mutator-registry.js +2 -2
  510. package/out/zql/src/mutate/mutator-registry.js.map +1 -1
  511. package/out/zql/src/planner/planner-builder.d.ts.map +1 -1
  512. package/out/zql/src/planner/planner-builder.js +14 -4
  513. package/out/zql/src/planner/planner-builder.js.map +1 -1
  514. package/out/zql/src/planner/planner-connection.d.ts +13 -0
  515. package/out/zql/src/planner/planner-connection.d.ts.map +1 -1
  516. package/out/zql/src/planner/planner-connection.js +42 -7
  517. package/out/zql/src/planner/planner-connection.js.map +1 -1
  518. package/out/zql/src/planner/planner-debug.d.ts +1 -1
  519. package/out/zql/src/planner/planner-debug.d.ts.map +1 -1
  520. package/out/zql/src/planner/planner-debug.js.map +1 -1
  521. package/out/zql/src/planner/planner-fan-out.d.ts +1 -0
  522. package/out/zql/src/planner/planner-fan-out.d.ts.map +1 -1
  523. package/out/zql/src/planner/planner-fan-out.js +3 -0
  524. package/out/zql/src/planner/planner-fan-out.js.map +1 -1
  525. package/out/zql/src/planner/planner-filter.d.ts +59 -0
  526. package/out/zql/src/planner/planner-filter.d.ts.map +1 -0
  527. package/out/zql/src/planner/planner-filter.js +116 -0
  528. package/out/zql/src/planner/planner-filter.js.map +1 -0
  529. package/out/zql/src/planner/planner-graph.d.ts +2 -0
  530. package/out/zql/src/planner/planner-graph.d.ts.map +1 -1
  531. package/out/zql/src/planner/planner-graph.js +2 -0
  532. package/out/zql/src/planner/planner-graph.js.map +1 -1
  533. package/out/zql/src/planner/planner-join.d.ts +2 -0
  534. package/out/zql/src/planner/planner-join.d.ts.map +1 -1
  535. package/out/zql/src/planner/planner-join.js +7 -0
  536. package/out/zql/src/planner/planner-join.js.map +1 -1
  537. package/out/zql/src/planner/planner-node.d.ts +2 -1
  538. package/out/zql/src/planner/planner-node.d.ts.map +1 -1
  539. package/out/zql/src/planner/planner-node.js.map +1 -1
  540. package/out/zql/src/query/query-delegate-base.d.ts +9 -1
  541. package/out/zql/src/query/query-delegate-base.d.ts.map +1 -1
  542. package/out/zql/src/query/query-delegate-base.js +129 -33
  543. package/out/zql/src/query/query-delegate-base.js.map +1 -1
  544. package/out/zql/src/query/query-delegate.d.ts +50 -1
  545. package/out/zql/src/query/query-delegate.d.ts.map +1 -1
  546. package/out/zql/src/query/query-impl.d.ts +13 -11
  547. package/out/zql/src/query/query-impl.d.ts.map +1 -1
  548. package/out/zql/src/query/query-impl.js +383 -60
  549. package/out/zql/src/query/query-impl.js.map +1 -1
  550. package/out/zql/src/query/query-transitions.d.ts +171 -0
  551. package/out/zql/src/query/query-transitions.d.ts.map +1 -0
  552. package/out/zql/src/query/query-transitions.js +207 -0
  553. package/out/zql/src/query/query-transitions.js.map +1 -0
  554. package/out/zql/src/query/query.d.ts +11 -2
  555. package/out/zql/src/query/query.d.ts.map +1 -1
  556. package/out/zql/src/query/runnable-query-impl.d.ts +1 -0
  557. package/out/zql/src/query/runnable-query-impl.d.ts.map +1 -1
  558. package/out/zql/src/query/runnable-query-impl.js +21 -1
  559. package/out/zql/src/query/runnable-query-impl.js.map +1 -1
  560. package/out/zql/src/query/typed-view.d.ts +1 -1
  561. package/out/zql/src/query/typed-view.d.ts.map +1 -1
  562. package/out/zqlite/src/db.d.ts +9 -1
  563. package/out/zqlite/src/db.d.ts.map +1 -1
  564. package/out/zqlite/src/db.js +8 -2
  565. package/out/zqlite/src/db.js.map +1 -1
  566. package/out/zqlite/src/query-builder.d.ts +1 -1
  567. package/out/zqlite/src/query-builder.d.ts.map +1 -1
  568. package/out/zqlite/src/query-builder.js +2 -1
  569. package/out/zqlite/src/query-builder.js.map +1 -1
  570. package/out/zqlite/src/sqlite-cost-model.d.ts.map +1 -1
  571. package/out/zqlite/src/sqlite-cost-model.js +2 -30
  572. package/out/zqlite/src/sqlite-cost-model.js.map +1 -1
  573. package/out/zqlite/src/table-source.d.ts +1 -0
  574. package/out/zqlite/src/table-source.d.ts.map +1 -1
  575. package/out/zqlite/src/table-source.js +20 -8
  576. package/out/zqlite/src/table-source.js.map +1 -1
  577. package/package.json +3 -4
@@ -47,13 +47,16 @@ var ArrayView = class {
47
47
  #root;
48
48
  onDestroy;
49
49
  #dirty = false;
50
+ #committedRoot;
51
+ #held = false;
52
+ #heldResultType = "unknown";
53
+ #heldError;
50
54
  #resultType = "unknown";
51
55
  #error;
52
56
  #updateTTL;
53
57
  #txnDirty = /* @__PURE__ */ new WeakSet();
54
58
  constructor(input, format, queryComplete, updateTTL) {
55
59
  this.#input = input;
56
- this.#schema = input.getSchema();
57
60
  this.#format = format;
58
61
  this.#updateTTL = updateTTL;
59
62
  this.#root = { "": format.singular ? void 0 : [] };
@@ -62,18 +65,14 @@ var ArrayView = class {
62
65
  else if ("error" in queryComplete) {
63
66
  this.#resultType = "error";
64
67
  this.#error = queryComplete;
65
- } else queryComplete.then(() => {
66
- this.#resultType = "complete";
67
- this.#fireListeners();
68
- }).catch((e) => {
69
- this.#resultType = "error";
70
- this.#error = e;
71
- this.#fireListeners();
72
- });
68
+ } else queryComplete.then(() => this.#setResultType("complete")).catch((e) => this.#setResultType("error", e));
73
69
  this.#hydrate();
74
70
  }
75
71
  get data() {
76
- return this.#root[""];
72
+ return (this.#committedRoot ?? this.#root)[""];
73
+ }
74
+ #getSchema() {
75
+ return this.#schema ??= this.#input.getSchema();
77
76
  }
78
77
  addListener(listener) {
79
78
  assert(!this.#listeners.has(listener), "Listener already registered");
@@ -87,6 +86,10 @@ var ArrayView = class {
87
86
  for (const listener of this.#listeners) this.#fireListener(listener);
88
87
  }
89
88
  #fireListener(listener) {
89
+ if (this.#committedRoot !== void 0) {
90
+ listener(this.data, this.#heldResultType, this.#heldError);
91
+ return;
92
+ }
90
93
  listener(this.data, this.#resultType, this.#error);
91
94
  }
92
95
  destroy() {
@@ -97,15 +100,43 @@ var ArrayView = class {
97
100
  for (const node of skipYields(this.#input.fetch({}))) this.#root = applyChange(this.#root, {
98
101
  type: "add",
99
102
  node
100
- }, this.#schema, "", this.#format, false, true);
103
+ }, this.#getSchema(), "", this.#format, false, true);
101
104
  this.flush();
102
105
  }
103
106
  push(change) {
104
107
  this.#dirty = true;
105
- this.#root = applyChange(this.#root, changeToViewChange(change), this.#schema, "", this.#format, false, this.#txnDirty);
108
+ this.#root = applyChange(this.#root, changeToViewChange(change), this.#getSchema(), "", this.#format, false, this.#txnDirty);
106
109
  return emptyArray;
107
110
  }
111
+ /**
112
+ * Keep exposing the current snapshot, whatever is pushed in the meantime,
113
+ * until {@link releaseData} or the next {@link flush}. Result type
114
+ * notifications are held back until that flush.
115
+ *
116
+ * A transaction is normally one synchronous task, so nobody can look at a
117
+ * view between its pushes and its flush. Deferred pipelines are different:
118
+ * they are hydrated over several tasks and flushed together (see
119
+ * `QueryDelegate.onPipelinesReady`), and a view that already has its rows
120
+ * must not show them early.
121
+ */
122
+ holdData() {
123
+ this.#committedRoot = this.#root;
124
+ this.#held = true;
125
+ this.#heldResultType = this.#resultType;
126
+ this.#heldError = this.#error;
127
+ }
128
+ /**
129
+ * Make `data` current again without notifying anyone yet; listeners are
130
+ * still only called by the next {@link flush}. Views released together get
131
+ * this called on all of them before any is flushed, so a listener of one
132
+ * that reads another sees its rows too.
133
+ */
134
+ releaseData() {
135
+ this.#committedRoot = void 0;
136
+ }
108
137
  flush() {
138
+ this.#committedRoot = void 0;
139
+ this.#held = false;
109
140
  if (!this.#dirty) return;
110
141
  this.#dirty = false;
111
142
  this.#fireListeners();
@@ -114,6 +145,26 @@ var ArrayView = class {
114
145
  updateTTL(ttl) {
115
146
  this.#updateTTL(ttl);
116
147
  }
148
+ /**
149
+ * The store holds the server-confirmed complete result of this query from a
150
+ * previous sync (the persisted got-queries key exists, pre-authoritative).
151
+ * Never downgrades 'complete'/'error', and never resolves anything that
152
+ * waits on 'complete' — freshness stays a promise only the connection can
153
+ * keep.
154
+ */
155
+ markCached() {
156
+ if (this.#resultType === "unknown") this.#setResultType("cached");
157
+ }
158
+ /** The got key was deleted (eviction) while still pre-authoritative. */
159
+ unmarkCached() {
160
+ if (this.#resultType === "cached") this.#setResultType("unknown");
161
+ }
162
+ #setResultType(resultType, error) {
163
+ this.#resultType = resultType;
164
+ this.#error = error;
165
+ if (this.#held) this.#dirty = true;
166
+ else if (!this.#dirty) this.#fireListeners();
167
+ }
117
168
  };
118
169
  //#endregion
119
170
  export { ArrayView };
@@ -1 +1 @@
1
- {"version":3,"file":"array-view.js","names":["#input","#listeners","#schema","#format","#updateTTL","#root","#resultType","#error","#fireListeners","#hydrate","#fireListener","#dirty","#txnDirty"],"sources":["../../../../../zql/src/ivm/array-view.ts"],"sourcesContent":["import {assert} from '../../../shared/src/asserts.ts';\nimport type {Immutable} from '../../../shared/src/immutable.ts';\nimport {emptyArray} from '../../../shared/src/sentinels.ts';\nimport type {ErroredQuery} from '../../../zero-protocol/src/custom-queries.ts';\nimport type {TTL} from '../query/ttl.ts';\nimport type {Listener, ResultType, TypedView} from '../query/typed-view.ts';\nimport {ChangeIndex} from './change-index.ts';\nimport {ChangeType} from './change-type.ts';\nimport type {Change} from './change.ts';\nimport {skipYields, type Input, type Output} from './operator.ts';\nimport type {SourceSchema} from './schema.ts';\nimport {applyChange, type ViewChange} from './view-apply-change.ts';\nimport type {Entry, Format, View} from './view.ts';\n\nfunction changeToViewChange(change: Change): ViewChange {\n switch (change[ChangeIndex.TYPE]) {\n case ChangeType.ADD:\n return {type: 'add', node: change[ChangeIndex.NODE]};\n case ChangeType.REMOVE:\n return {type: 'remove', node: change[ChangeIndex.NODE]};\n case ChangeType.CHILD:\n return {\n type: 'child',\n node: change[ChangeIndex.NODE],\n child: {\n relationshipName: change[ChangeIndex.CHILD_DATA].relationshipName,\n change: changeToViewChange(change[ChangeIndex.CHILD_DATA].change),\n },\n };\n case ChangeType.EDIT:\n return {\n type: 'edit',\n node: change[ChangeIndex.NODE],\n oldNode: change[ChangeIndex.OLD_NODE],\n };\n }\n}\n\n/**\n * Implements a materialized view of the output of an operator.\n *\n * It might seem more efficient to use an immutable b-tree for the\n * materialization, but it's not so clear. Inserts in the middle are\n * asymptotically slower in an array, but can often be done with zero\n * allocations, where changes to the b-tree will often require several allocs.\n *\n * Also the plain array view is more convenient for consumers since you can dump\n * it into console to see what it is, rather than having to iterate it.\n */\nexport class ArrayView<V extends View> implements Output, TypedView<V> {\n readonly #input: Input;\n readonly #listeners = new Set<Listener<V>>();\n readonly #schema: SourceSchema;\n readonly #format: Format;\n\n // Synthetic \"root\" entry that has a single \"\" relationship, so that we can\n // treat all changes, including the root change, generically.\n //\n // applyChange is immutable: it returns a new root, preserving the references\n // of unchanged subtrees so consumers (React.memo / Solid) can skip them. We\n // therefore reassign #root on every change rather than mutating in place.\n #root: Entry;\n\n onDestroy: (() => void) | undefined;\n\n #dirty = false;\n #resultType: ResultType = 'unknown';\n #error: ErroredQuery | undefined;\n readonly #updateTTL: (ttl: TTL) => void;\n\n // Objects/arrays created or cloned during the current (un-flushed)\n // transaction. applyChange may mutate these in place rather than copying\n // again, since they are not yet observed by any listener. Replaced at\n // flush(), after which the committed snapshot must be copied-on-write again.\n // A WeakSet (not a Set) so it never extends the lifetime of transient clones.\n #txnDirty: WeakSet<object> = new WeakSet();\n\n constructor(\n input: Input,\n format: Format,\n queryComplete: true | ErroredQuery | Promise<true>,\n updateTTL: (ttl: TTL) => void,\n ) {\n this.#input = input;\n this.#schema = input.getSchema();\n this.#format = format;\n this.#updateTTL = updateTTL;\n this.#root = {'': format.singular ? undefined : []};\n input.setOutput(this);\n\n if (queryComplete === true) {\n this.#resultType = 'complete';\n } else if ('error' in queryComplete) {\n this.#resultType = 'error';\n this.#error = queryComplete;\n } else {\n void queryComplete\n .then(() => {\n this.#resultType = 'complete';\n this.#fireListeners();\n })\n .catch(e => {\n this.#resultType = 'error';\n this.#error = e;\n this.#fireListeners();\n });\n }\n this.#hydrate();\n }\n\n get data() {\n return this.#root[''] as V;\n }\n\n addListener(listener: Listener<V>) {\n assert(!this.#listeners.has(listener), 'Listener already registered');\n this.#listeners.add(listener);\n\n this.#fireListener(listener);\n\n return () => {\n this.#listeners.delete(listener);\n };\n }\n\n #fireListeners() {\n for (const listener of this.#listeners) {\n this.#fireListener(listener);\n }\n }\n\n #fireListener(listener: Listener<V>) {\n listener(this.data as Immutable<V>, this.#resultType, this.#error);\n }\n\n destroy() {\n this.onDestroy?.();\n }\n\n #hydrate() {\n this.#dirty = true;\n for (const node of skipYields(this.#input.fetch({}))) {\n this.#root = applyChange(\n this.#root,\n {type: 'add', node},\n this.#schema,\n '',\n this.#format,\n false /* withIDs */,\n true /* mutate: #root is freshly created and not yet observed by any\n consumer, so build it in place to avoid O(N^2) array copies.\n Every later push() is immutable, preserving reference\n stability for unchanged subtrees. */,\n );\n }\n this.flush();\n }\n\n push(change: Change) {\n this.#dirty = true;\n this.#root = applyChange(\n this.#root,\n changeToViewChange(change),\n this.#schema,\n '',\n this.#format,\n false /* withIDs */,\n this.#txnDirty /* mutate: copy-on-write within this transaction */,\n );\n return emptyArray;\n }\n\n flush() {\n if (!this.#dirty) {\n return;\n }\n this.#dirty = false;\n this.#fireListeners();\n // The snapshot just handed to listeners is now observed; the next\n // transaction must copy-on-write rather than mutate these objects. A fresh\n // WeakSet drops all \"owned\" marks (WeakSet has no clear()).\n this.#txnDirty = new WeakSet();\n }\n\n updateTTL(ttl: TTL) {\n this.#updateTTL(ttl);\n }\n}\n"],"mappings":";;;;;AAcA,SAAS,mBAAmB,QAA4B;CACtD,QAAQ,OAAO,IAAf;EACE,KAAK,GACH,OAAO;GAAC,MAAM;GAAO,MAAM,OAAO;EAAiB;EACrD,KAAK,GACH,OAAO;GAAC,MAAM;GAAU,MAAM,OAAO;EAAiB;EACxD,KAAK,GACH,OAAO;GACL,MAAM;GACN,MAAM,OAAO;GACb,OAAO;IACL,kBAAkB,OAAO,GAAwB;IACjD,QAAQ,mBAAmB,OAAO,GAAwB,MAAM;GAClE;EACF;EACF,KAAK,GACH,OAAO;GACL,MAAM;GACN,MAAM,OAAO;GACb,SAAS,OAAO;EAClB;CACJ;AACF;;;;;;;;;;;;AAaA,IAAa,YAAb,MAAuE;CACrE;CACA,6BAAsB,IAAI,IAAiB;CAC3C;CACA;CAQA;CAEA;CAEA,SAAS;CACT,cAA0B;CAC1B;CACA;CAOA,4BAA6B,IAAI,QAAQ;CAEzC,YACE,OACA,QACA,eACA,WACA;EACA,KAAKA,SAAS;EACd,KAAKE,UAAU,MAAM,UAAU;EAC/B,KAAKC,UAAU;EACf,KAAKC,aAAa;EAClB,KAAKC,QAAQ,EAAC,IAAI,OAAO,WAAW,KAAA,IAAY,CAAC,EAAC;EAClD,MAAM,UA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/B;CAEA,UAAU,KAAU;EAClB,KAAKR,WAAW,GAAG;CACrB;AACF"}
1
+ {"version":3,"file":"array-view.js","names":["#input","#listeners","#format","#updateTTL","#root","#resultType","#error","#setResultType","#hydrate","#committedRoot","#schema","#fireListener","#heldResultType","#heldError","#dirty","#getSchema","#txnDirty","#held","#fireListeners"],"sources":["../../../../../zql/src/ivm/array-view.ts"],"sourcesContent":["import {assert} from '../../../shared/src/asserts.ts';\nimport type {Immutable} from '../../../shared/src/immutable.ts';\nimport {emptyArray} from '../../../shared/src/sentinels.ts';\nimport type {ErroredQuery} from '../../../zero-protocol/src/custom-queries.ts';\nimport type {TTL} from '../query/ttl.ts';\nimport type {Listener, ResultType, TypedView} from '../query/typed-view.ts';\nimport {ChangeIndex} from './change-index.ts';\nimport {ChangeType} from './change-type.ts';\nimport type {Change} from './change.ts';\nimport {skipYields, type Input, type Output} from './operator.ts';\nimport type {SourceSchema} from './schema.ts';\nimport {applyChange, type ViewChange} from './view-apply-change.ts';\nimport type {Entry, Format, View} from './view.ts';\n\nfunction changeToViewChange(change: Change): ViewChange {\n switch (change[ChangeIndex.TYPE]) {\n case ChangeType.ADD:\n return {type: 'add', node: change[ChangeIndex.NODE]};\n case ChangeType.REMOVE:\n return {type: 'remove', node: change[ChangeIndex.NODE]};\n case ChangeType.CHILD:\n return {\n type: 'child',\n node: change[ChangeIndex.NODE],\n child: {\n relationshipName: change[ChangeIndex.CHILD_DATA].relationshipName,\n change: changeToViewChange(change[ChangeIndex.CHILD_DATA].change),\n },\n };\n case ChangeType.EDIT:\n return {\n type: 'edit',\n node: change[ChangeIndex.NODE],\n oldNode: change[ChangeIndex.OLD_NODE],\n };\n }\n}\n\n/**\n * Implements a materialized view of the output of an operator.\n *\n * It might seem more efficient to use an immutable b-tree for the\n * materialization, but it's not so clear. Inserts in the middle are\n * asymptotically slower in an array, but can often be done with zero\n * allocations, where changes to the b-tree will often require several allocs.\n *\n * Also the plain array view is more convenient for consumers since you can dump\n * it into console to see what it is, rather than having to iterate it.\n */\nexport class ArrayView<V extends View> implements Output, TypedView<V> {\n readonly #input: Input;\n readonly #listeners = new Set<Listener<V>>();\n // Read lazily: a DeferredInput has no schema until its pipeline is attached,\n // and the schema is only needed once there is a change to apply.\n #schema: SourceSchema | undefined;\n readonly #format: Format;\n\n // Synthetic \"root\" entry that has a single \"\" relationship, so that we can\n // treat all changes, including the root change, generically.\n //\n // applyChange is immutable: it returns a new root, preserving the references\n // of unchanged subtrees so consumers (React.memo / Solid) can skip them. We\n // therefore reassign #root on every change rather than mutating in place.\n #root: Entry;\n\n onDestroy: (() => void) | undefined;\n\n #dirty = false;\n // Set by holdData(): the root that `data` and newly added listeners keep\n // seeing until releaseData() or the next flush, while pushes build up #root\n // behind it.\n #committedRoot: Entry | undefined;\n // From holdData() until the next flush. Outlasts #committedRoot, which\n // releaseData() drops first.\n #held = false;\n // The result type and error as of holdData(): what a listener added while\n // held is called with, so it never sees old rows paired with a new type.\n #heldResultType: ResultType = 'unknown';\n #heldError: ErroredQuery | undefined;\n #resultType: ResultType = 'unknown';\n #error: ErroredQuery | undefined;\n readonly #updateTTL: (ttl: TTL) => void;\n\n // Objects/arrays created or cloned during the current (un-flushed)\n // transaction. applyChange may mutate these in place rather than copying\n // again, since they are not yet observed by any listener. Replaced at\n // flush(), after which the committed snapshot must be copied-on-write again.\n // A WeakSet (not a Set) so it never extends the lifetime of transient clones.\n #txnDirty: WeakSet<object> = new WeakSet();\n\n constructor(\n input: Input,\n format: Format,\n queryComplete: true | ErroredQuery | Promise<true>,\n updateTTL: (ttl: TTL) => void,\n ) {\n this.#input = input;\n this.#format = format;\n this.#updateTTL = updateTTL;\n this.#root = {'': format.singular ? undefined : []};\n input.setOutput(this);\n\n if (queryComplete === true) {\n this.#resultType = 'complete';\n } else if ('error' in queryComplete) {\n this.#resultType = 'error';\n this.#error = queryComplete;\n } else {\n void queryComplete\n .then(() => this.#setResultType('complete'))\n .catch(e => this.#setResultType('error', e));\n }\n this.#hydrate();\n }\n\n get data() {\n return (this.#committedRoot ?? this.#root)[''] as V;\n }\n\n #getSchema(): SourceSchema {\n return (this.#schema ??= this.#input.getSchema());\n }\n\n addListener(listener: Listener<V>) {\n assert(!this.#listeners.has(listener), 'Listener already registered');\n this.#listeners.add(listener);\n\n this.#fireListener(listener);\n\n return () => {\n this.#listeners.delete(listener);\n };\n }\n\n #fireListeners() {\n for (const listener of this.#listeners) {\n this.#fireListener(listener);\n }\n }\n\n #fireListener(listener: Listener<V>) {\n if (this.#committedRoot !== undefined) {\n // Held rows go with the held type; after releaseData() both are current.\n listener(\n this.data as Immutable<V>,\n this.#heldResultType,\n this.#heldError,\n );\n return;\n }\n listener(this.data as Immutable<V>, this.#resultType, this.#error);\n }\n\n destroy() {\n this.onDestroy?.();\n }\n\n #hydrate() {\n this.#dirty = true;\n for (const node of skipYields(this.#input.fetch({}))) {\n this.#root = applyChange(\n this.#root,\n {type: 'add', node},\n this.#getSchema(),\n '',\n this.#format,\n false /* withIDs */,\n true /* mutate: #root is freshly created and not yet observed by any\n consumer, so build it in place to avoid O(N^2) array copies.\n Every later push() is immutable, preserving reference\n stability for unchanged subtrees. */,\n );\n }\n this.flush();\n }\n\n push(change: Change) {\n this.#dirty = true;\n this.#root = applyChange(\n this.#root,\n changeToViewChange(change),\n this.#getSchema(),\n '',\n this.#format,\n false /* withIDs */,\n this.#txnDirty /* mutate: copy-on-write within this transaction */,\n );\n return emptyArray;\n }\n\n /**\n * Keep exposing the current snapshot, whatever is pushed in the meantime,\n * until {@link releaseData} or the next {@link flush}. Result type\n * notifications are held back until that flush.\n *\n * A transaction is normally one synchronous task, so nobody can look at a\n * view between its pushes and its flush. Deferred pipelines are different:\n * they are hydrated over several tasks and flushed together (see\n * `QueryDelegate.onPipelinesReady`), and a view that already has its rows\n * must not show them early.\n */\n holdData(): void {\n this.#committedRoot = this.#root;\n this.#held = true;\n this.#heldResultType = this.#resultType;\n this.#heldError = this.#error;\n }\n\n /**\n * Make `data` current again without notifying anyone yet; listeners are\n * still only called by the next {@link flush}. Views released together get\n * this called on all of them before any is flushed, so a listener of one\n * that reads another sees its rows too.\n */\n releaseData(): void {\n this.#committedRoot = undefined;\n }\n\n flush() {\n this.#committedRoot = undefined;\n this.#held = false;\n if (!this.#dirty) {\n return;\n }\n this.#dirty = false;\n this.#fireListeners();\n // The snapshot just handed to listeners is now observed; the next\n // transaction must copy-on-write rather than mutate these objects. A fresh\n // WeakSet drops all \"owned\" marks (WeakSet has no clear()).\n this.#txnDirty = new WeakSet();\n }\n\n updateTTL(ttl: TTL) {\n this.#updateTTL(ttl);\n }\n\n /**\n * The store holds the server-confirmed complete result of this query from a\n * previous sync (the persisted got-queries key exists, pre-authoritative).\n * Never downgrades 'complete'/'error', and never resolves anything that\n * waits on 'complete' — freshness stays a promise only the connection can\n * keep.\n */\n markCached(): void {\n if (this.#resultType === 'unknown') {\n this.#setResultType('cached');\n }\n }\n\n /** The got key was deleted (eviction) while still pre-authoritative. */\n unmarkCached(): void {\n if (this.#resultType === 'cached') {\n this.#setResultType('unknown');\n }\n }\n\n #setResultType(resultType: ResultType, error?: ErroredQuery) {\n this.#resultType = resultType;\n this.#error = error;\n // A dirty view is mid-transaction: rows were pushed and not yet flushed,\n // and the objects in #txnDirty are still mutable. Firing now would hand\n // those to listeners and then fire again at flush() with the same data, so\n // let the pending flush deliver the new result type instead.\n // A held view is waiting to be exposed together with others at the next\n // flush; that goes for its result type as much as for its rows.\n if (this.#held) {\n // Dirty so that the flush notifies even when no rows were pushed.\n this.#dirty = true;\n } else if (!this.#dirty) {\n this.#fireListeners();\n }\n }\n}\n"],"mappings":";;;;;AAcA,SAAS,mBAAmB,QAA4B;CACtD,QAAQ,OAAO,IAAf;EACE,KAAK,GACH,OAAO;GAAC,MAAM;GAAO,MAAM,OAAO;EAAiB;EACrD,KAAK,GACH,OAAO;GAAC,MAAM;GAAU,MAAM,OAAO;EAAiB;EACxD,KAAK,GACH,OAAO;GACL,MAAM;GACN,MAAM,OAAO;GACb,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,CAAC,KAAKA,QACf,KAAKI,eAAe;CAExB;AACF"}
@@ -1 +1 @@
1
- {"version":3,"file":"cap.d.ts","sourceRoot":"","sources":["../../../../../zql/src/ivm/cap.ts"],"names":[],"mappings":"AAKA,OAAO,EAAgB,KAAK,MAAM,EAAkB,MAAM,aAAa,CAAC;AAExE,OAAO,KAAK,EAAa,IAAI,EAAC,MAAM,WAAW,CAAC;AAChD,OAAO,EAEL,KAAK,YAAY,EACjB,KAAK,KAAK,EACV,KAAK,QAAQ,EACb,KAAK,MAAM,EACX,KAAK,OAAO,EACb,MAAM,eAAe,CAAC;AACvB,OAAO,KAAK,EAAC,YAAY,EAAC,MAAM,aAAa,CAAC;AAC9C,OAAO,EAAC,KAAK,MAAM,EAAC,MAAM,aAAa,CAAC;AACxC,OAAO,EAGL,KAAK,YAAY,EAClB,MAAM,WAAW,CAAC;AAanB;;;;;;;;;;;;;;;GAeG;AACH,qBAAa,GAAI,YAAW,QAAQ;;IAUlC,YACE,KAAK,EAAE,KAAK,EACZ,OAAO,EAAE,OAAO,EAChB,KAAK,EAAE,MAAM,EACb,YAAY,CAAC,EAAE,YAAY,EAW5B;IAED,SAAS,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,CAE9B;IAED,SAAS,IAAI,YAAY,CAExB;IAEA,KAAK,CAAC,GAAG,EAAE,YAAY,GAAG,MAAM,CAAC,IAAI,GAAG,OAAO,CAAC,CAoChD;IAsDA,IAAI,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAAC,OAAO,CAAC,CAiFrC;IAqCD,OAAO,IAAI,IAAI,CAEd;CACF"}
1
+ {"version":3,"file":"cap.d.ts","sourceRoot":"","sources":["../../../../../zql/src/ivm/cap.ts"],"names":[],"mappings":"AAMA,OAAO,EAAgB,KAAK,MAAM,EAAkB,MAAM,aAAa,CAAC;AAExE,OAAO,KAAK,EAAa,IAAI,EAAC,MAAM,WAAW,CAAC;AAChD,OAAO,EAEL,KAAK,YAAY,EACjB,KAAK,KAAK,EACV,KAAK,QAAQ,EACb,KAAK,MAAM,EACX,KAAK,OAAO,EACb,MAAM,eAAe,CAAC;AACvB,OAAO,KAAK,EAAC,YAAY,EAAC,MAAM,aAAa,CAAC;AAC9C,OAAO,EAAC,KAAK,MAAM,EAAC,MAAM,aAAa,CAAC;AACxC,OAAO,EAGL,KAAK,YAAY,EAClB,MAAM,WAAW,CAAC;AAanB;;;;;;;;;;;;;;;GAeG;AACH,qBAAa,GAAI,YAAW,QAAQ;;IAUlC,YACE,KAAK,EAAE,KAAK,EACZ,OAAO,EAAE,OAAO,EAChB,KAAK,EAAE,MAAM,EACb,YAAY,CAAC,EAAE,YAAY,EAW5B;IAED,SAAS,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,CAE9B;IAED,SAAS,IAAI,YAAY,CAExB;IAEA,KAAK,CAAC,GAAG,EAAE,YAAY,GAAG,MAAM,CAAC,IAAI,GAAG,OAAO,CAAC,CAoChD;IAsDA,IAAI,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAAC,OAAO,CAAC,CAiFrC;IAsCD,OAAO,IAAI,IAAI,CAEd;CACF"}
@@ -1,4 +1,5 @@
1
1
  import { assert } from "../../../shared/src/asserts.js";
2
+ import { emptyArray } from "../../../shared/src/sentinels.js";
2
3
  import { throwOutput } from "./operator.js";
3
4
  import { makeAddChange } from "./change.js";
4
5
  import { constraintMatchesPartitionKey, makePartitionKeyComparator } from "./take.js";
@@ -163,11 +164,11 @@ var Cap = class {
163
164
  if (new Set(capState.pks).has(pk)) yield* this.#output.push(change, this);
164
165
  }
165
166
  }
166
- *#pushEditChange(change) {
167
+ #pushEditChange(change) {
167
168
  assert(!this.#partitionKeyComparator || this.#partitionKeyComparator(change[2].row, change[1].row) === 0, "Unexpected change of partition key");
168
169
  const capStateKey = getCapStateKey(this.#partitionKey, change[2].row);
169
170
  const capState = this.#storage.get(capStateKey);
170
- if (!capState) return;
171
+ if (!capState) return emptyArray;
171
172
  const oldPK = serializePK(change[2].row, this.#primaryKey);
172
173
  if (new Set(capState.pks).has(oldPK)) {
173
174
  const newPK = serializePK(change[1].row, this.#primaryKey);
@@ -178,8 +179,9 @@ var Cap = class {
178
179
  pks
179
180
  });
180
181
  }
181
- yield* this.#output.push(change, this);
182
+ return this.#output.push(change, this);
182
183
  }
184
+ return emptyArray;
183
185
  }
184
186
  destroy() {
185
187
  this.#input.destroy();
@@ -1 +1 @@
1
- {"version":3,"file":"cap.js","names":["#input","#storage","#limit","#partitionKey","#partitionKeyComparator","#primaryKey","#output","#initialFetch","#pushEditChange"],"sources":["../../../../../zql/src/ivm/cap.ts"],"sourcesContent":["import {assert} from '../../../shared/src/asserts.ts';\nimport type {Row, Value} from '../../../zero-protocol/src/data.ts';\nimport type {PrimaryKey} from '../../../zero-protocol/src/primary-key.ts';\nimport {ChangeIndex} from './change-index.ts';\nimport {ChangeType} from './change-type.ts';\nimport {makeAddChange, type Change, type EditChange} from './change.ts';\nimport type {Constraint} from './constraint.ts';\nimport type {Comparator, Node} from './data.ts';\nimport {\n throwOutput,\n type FetchRequest,\n type Input,\n type Operator,\n type Output,\n type Storage,\n} from './operator.ts';\nimport type {SourceSchema} from './schema.ts';\nimport {type Stream} from './stream.ts';\nimport {\n constraintMatchesPartitionKey,\n makePartitionKeyComparator,\n type PartitionKey,\n} from './take.ts';\n\ntype CapState = {\n size: number;\n pks: string[];\n};\n\ninterface CapStorage {\n get(key: string): CapState | undefined;\n set(key: string, value: CapState): void;\n del(key: string): void;\n}\n\n/**\n * The Cap operator is a count-based limiter for EXISTS subqueries that\n * does not require ordering. Unlike Take, it tracks membership by primary\n * key set rather than by a sorted bound. This means:\n *\n * - No comparator needed (no ordering requirement)\n * - No `start` or `reverse` fetch support\n * - No `#rowHiddenFromFetch` complexity (we can defer when adding to the pk set)\n *\n * Cap is used in EXISTS child pipelines where only the count of matching\n * rows matters, not their order. This allows SQLite to skip ORDER BY\n * entirely, enabling much faster query plans.\n *\n * Cap can count rows globally or by unique value of some partition key\n * (same as Take).\n */\nexport class Cap implements Operator {\n readonly #input: Input;\n readonly #storage: CapStorage;\n readonly #limit: number;\n readonly #partitionKey: PartitionKey | undefined;\n readonly #partitionKeyComparator: Comparator | undefined;\n readonly #primaryKey: PrimaryKey;\n\n #output: Output = throwOutput;\n\n constructor(\n input: Input,\n storage: Storage,\n limit: number,\n partitionKey?: PartitionKey,\n ) {\n assert(limit >= 0, 'Limit must be non-negative');\n input.setOutput(this);\n this.#input = input;\n this.#storage = storage as CapStorage;\n this.#limit = limit;\n this.#partitionKey = partitionKey;\n this.#partitionKeyComparator =\n partitionKey && makePartitionKeyComparator(partitionKey);\n this.#primaryKey = input.getSchema().primaryKey;\n }\n\n setOutput(output: Output): void {\n this.#output = output;\n }\n\n getSchema(): SourceSchema {\n return this.#input.getSchema();\n }\n\n *fetch(req: FetchRequest): Stream<Node | 'yield'> {\n assert(!req.start, 'Cap does not support start');\n assert(!req.reverse, 'Cap does not support reverse');\n\n // Cap is only built for non-flipped EXISTS subqueries, whose only\n // downstream consumer is a Join that always fetches with a constraint\n // built from the correlation's childField — which is Cap's partition\n // key. So either partitionKey is undefined, or constraint matches.\n assert(\n !this.#partitionKey ||\n (req.constraint !== undefined &&\n constraintMatchesPartitionKey(req.constraint, this.#partitionKey)),\n 'Cap fetch: constraint must match partition key when partitioned',\n );\n\n const capStateKey = getCapStateKey(this.#partitionKey, req.constraint);\n const capState = this.#storage.get(capStateKey);\n if (!capState) {\n yield* this.#initialFetch(req);\n return;\n }\n if (capState.size === 0) {\n return;\n }\n // PK-based point lookups: fetch each tracked row by its PK directly,\n // rather than scanning the partition and filtering.\n for (const pk of capState.pks) {\n const constraint = deserializePKToConstraint(pk, this.#primaryKey);\n for (const inputNode of this.#input.fetch({constraint})) {\n if (inputNode === 'yield') {\n yield inputNode;\n continue;\n }\n yield inputNode;\n }\n }\n }\n\n *#initialFetch(req: FetchRequest): Stream<Node | 'yield'> {\n if (this.#limit === 0) {\n return;\n }\n\n assert(\n constraintMatchesPartitionKey(req.constraint, this.#partitionKey),\n 'Constraint should match partition key',\n );\n\n const capStateKey = getCapStateKey(this.#partitionKey, req.constraint);\n assert(\n this.#storage.get(capStateKey) === undefined,\n 'Cap state should be undefined',\n );\n\n let size = 0;\n const pks: string[] = [];\n let downstreamEarlyReturn = true;\n let exceptionThrown = false;\n try {\n for (const inputNode of this.#input.fetch(req)) {\n if (inputNode === 'yield') {\n yield 'yield';\n continue;\n }\n yield inputNode;\n pks.push(serializePK(inputNode.row, this.#primaryKey));\n size++;\n if (size === this.#limit) {\n break;\n }\n }\n downstreamEarlyReturn = false;\n } catch (e) {\n exceptionThrown = true;\n throw e;\n } finally {\n if (!exceptionThrown) {\n this.#storage.set(capStateKey, {size, pks});\n // If it becomes necessary to support downstream early return, this\n // assert should be removed, and replaced with code that consumes\n // the input stream until limit is reached or the input stream is\n // exhausted so that capState is properly hydrated.\n assert(\n !downstreamEarlyReturn,\n 'Unexpected early return prevented full hydration',\n );\n }\n }\n }\n\n *push(change: Change): Stream<'yield'> {\n if (change[ChangeIndex.TYPE] === ChangeType.EDIT) {\n yield* this.#pushEditChange(change);\n return;\n }\n\n const capStateKey = getCapStateKey(\n this.#partitionKey,\n change[ChangeIndex.NODE].row,\n );\n const capState = this.#storage.get(capStateKey);\n if (!capState) {\n return;\n }\n\n const pk = serializePK(change[ChangeIndex.NODE].row, this.#primaryKey);\n\n if (change[ChangeIndex.TYPE] === ChangeType.ADD) {\n if (capState.size < this.#limit) {\n const pks = [...capState.pks, pk];\n this.#storage.set(capStateKey, {size: capState.size + 1, pks});\n yield* this.#output.push(change, this);\n return;\n }\n // Full — drop\n return;\n } else if (change[ChangeIndex.TYPE] === ChangeType.REMOVE) {\n const pkIndex = capState.pks.indexOf(pk);\n if (pkIndex === -1) {\n // Not in our set — drop\n return;\n }\n // Remove from set\n const pks = [...capState.pks];\n pks.splice(pkIndex, 1);\n const newSize = capState.size - 1;\n\n // Try to refill: fetch from input with partition constraint,\n // find first row NOT in PK set\n const pkSet = new Set(pks);\n const constraint = this.#partitionKey\n ? (Object.fromEntries(\n this.#partitionKey.map(\n key => [key, change[ChangeIndex.NODE].row[key]] as const,\n ),\n ) as Constraint)\n : undefined;\n\n let replacement: Node | undefined;\n for (const node of this.#input.fetch({constraint})) {\n if (node === 'yield') {\n yield node;\n continue;\n }\n const nodePK = serializePK(node.row, this.#primaryKey);\n if (!pkSet.has(nodePK)) {\n replacement = node;\n break;\n }\n }\n\n if (replacement) {\n // Store state WITHOUT replacement during remove forward,\n // matching Take's pattern of hiding in-flight changes from re-fetches.\n this.#storage.set(capStateKey, {size: newSize, pks});\n yield* this.#output.push(change, this);\n // Now add replacement to set and forward the add.\n const replacementPK = serializePK(replacement.row, this.#primaryKey);\n pks.push(replacementPK);\n this.#storage.set(capStateKey, {size: newSize + 1, pks});\n yield* this.#output.push(makeAddChange(replacement), this);\n } else {\n this.#storage.set(capStateKey, {size: newSize, pks});\n yield* this.#output.push(change, this);\n }\n } else if (change[ChangeIndex.TYPE] === ChangeType.CHILD) {\n const pkSet = new Set(capState.pks);\n if (pkSet.has(pk)) {\n yield* this.#output.push(change, this);\n }\n }\n }\n\n *#pushEditChange(change: EditChange): Stream<'yield'> {\n assert(\n !this.#partitionKeyComparator ||\n this.#partitionKeyComparator(\n change[ChangeIndex.OLD_NODE].row,\n change[ChangeIndex.NODE].row,\n ) === 0,\n 'Unexpected change of partition key',\n );\n const capStateKey = getCapStateKey(\n this.#partitionKey,\n change[ChangeIndex.OLD_NODE].row,\n );\n const capState = this.#storage.get(capStateKey);\n if (!capState) {\n return;\n }\n\n const oldPK = serializePK(\n change[ChangeIndex.OLD_NODE].row,\n this.#primaryKey,\n );\n const pkSet = new Set(capState.pks);\n if (pkSet.has(oldPK)) {\n // Update the PK in our set if it changed\n const newPK = serializePK(change[ChangeIndex.NODE].row, this.#primaryKey);\n if (oldPK !== newPK) {\n const pks = capState.pks.map(p => (p === oldPK ? newPK : p));\n this.#storage.set(capStateKey, {size: capState.size, pks});\n }\n yield* this.#output.push(change, this);\n }\n // If not in our set, drop\n }\n\n destroy(): void {\n this.#input.destroy();\n }\n}\n\nfunction getCapStateKey(\n partitionKey: PartitionKey | undefined,\n rowOrConstraint: Row | Constraint | undefined,\n): string {\n const partitionValues: Value[] = [];\n\n if (partitionKey && rowOrConstraint) {\n for (const key of partitionKey) {\n partitionValues.push(rowOrConstraint[key]);\n }\n }\n\n return JSON.stringify(['cap', ...partitionValues]);\n}\n\nfunction serializePK(row: Row, primaryKey: PrimaryKey): string {\n return JSON.stringify(primaryKey.map(k => row[k]));\n}\n\nfunction deserializePKToConstraint(\n pk: string,\n primaryKey: PrimaryKey,\n): Constraint {\n const values = JSON.parse(pk) as Value[];\n const constraint: Record<string, Value> = {};\n for (let i = 0; i < primaryKey.length; i++) {\n constraint[primaryKey[i]] = values[i];\n }\n return constraint;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAmDA,IAAa,MAAb,MAAqC;CACnC;CACA;CACA;CACA;CACA;CACA;CAEA,UAAkB;CAElB,YACE,OACA,SACA,OACA,cACA;EACA,OAAO,SAAS,GAAG,4BAA4B;EAC/C,MAAM,UAAU,IAAI;EACpB,KAAKA,SAAS;EACd,KAAKC,WAAW;EAChB,KAAKC,SAAS;EACd,KAAKC,gBAAgB;EACrB,KAAKC,0BACH,gBAAgB,2BAA2B,YAAY;EACzD,KAAKC,cAAc,MAAM,UAAU,EAAE;CACvC;CAEA,UAAU,QAAsB;EAC9B,KAAKC,UAAU;CACjB;CAEA,YAA0B;EACxB,OAAO,KAAKN,OAAO,UAAU;CAC/B;CAEA,CAAC,MAAM,KAA2C;EAChD,OAAO,CAAC,IAAI,OAAO,4BAA4B;EAC/C,OAAO,CAAC,IAAI,SAAS,8BAA8B;EAMnD,OACE,CAAC,KAAKG,iBACH,IAAI,eAAe,KAAA,KAClB,8BAA8B,IAAI,YAAY,KAAKA,aAAa,GACpE,iEACF;EAEA,MAAM,cAAc,eAAe,KAAKA,eAAe,IAAI,UAAU;EAC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C,OAAO,GAAG,eAAe,CAAC;AACnD;AAEA,SAAS,YAAY,KAAU,YAAgC;CAC7D,OAAO,KAAK,UAAU,WAAW,KAAI,MAAK,IAAI,EAAE,CAAC;AACnD;AAEA,SAAS,0BACP,IACA,YACY;CACZ,MAAM,SAAS,KAAK,MAAM,EAAE;CAC5B,MAAM,aAAoC,CAAC;CAC3C,KAAK,IAAI,IAAI,GAAG,IAAI,WAAW,QAAQ,KACrC,WAAW,WAAW,MAAM,OAAO;CAErC,OAAO;AACT"}
1
+ {"version":3,"file":"cap.js","names":["#input","#storage","#limit","#partitionKey","#partitionKeyComparator","#primaryKey","#output","#initialFetch","#pushEditChange"],"sources":["../../../../../zql/src/ivm/cap.ts"],"sourcesContent":["import {assert} from '../../../shared/src/asserts.ts';\nimport {emptyArray} from '../../../shared/src/sentinels.ts';\nimport type {Row, Value} from '../../../zero-protocol/src/data.ts';\nimport type {PrimaryKey} from '../../../zero-protocol/src/primary-key.ts';\nimport {ChangeIndex} from './change-index.ts';\nimport {ChangeType} from './change-type.ts';\nimport {makeAddChange, type Change, type EditChange} from './change.ts';\nimport type {Constraint} from './constraint.ts';\nimport type {Comparator, Node} from './data.ts';\nimport {\n throwOutput,\n type FetchRequest,\n type Input,\n type Operator,\n type Output,\n type Storage,\n} from './operator.ts';\nimport type {SourceSchema} from './schema.ts';\nimport {type Stream} from './stream.ts';\nimport {\n constraintMatchesPartitionKey,\n makePartitionKeyComparator,\n type PartitionKey,\n} from './take.ts';\n\ntype CapState = {\n size: number;\n pks: string[];\n};\n\ninterface CapStorage {\n get(key: string): CapState | undefined;\n set(key: string, value: CapState): void;\n del(key: string): void;\n}\n\n/**\n * The Cap operator is a count-based limiter for EXISTS subqueries that\n * does not require ordering. Unlike Take, it tracks membership by primary\n * key set rather than by a sorted bound. This means:\n *\n * - No comparator needed (no ordering requirement)\n * - No `start` or `reverse` fetch support\n * - No `#rowHiddenFromFetch` complexity (we can defer when adding to the pk set)\n *\n * Cap is used in EXISTS child pipelines where only the count of matching\n * rows matters, not their order. This allows SQLite to skip ORDER BY\n * entirely, enabling much faster query plans.\n *\n * Cap can count rows globally or by unique value of some partition key\n * (same as Take).\n */\nexport class Cap implements Operator {\n readonly #input: Input;\n readonly #storage: CapStorage;\n readonly #limit: number;\n readonly #partitionKey: PartitionKey | undefined;\n readonly #partitionKeyComparator: Comparator | undefined;\n readonly #primaryKey: PrimaryKey;\n\n #output: Output = throwOutput;\n\n constructor(\n input: Input,\n storage: Storage,\n limit: number,\n partitionKey?: PartitionKey,\n ) {\n assert(limit >= 0, 'Limit must be non-negative');\n input.setOutput(this);\n this.#input = input;\n this.#storage = storage as CapStorage;\n this.#limit = limit;\n this.#partitionKey = partitionKey;\n this.#partitionKeyComparator =\n partitionKey && makePartitionKeyComparator(partitionKey);\n this.#primaryKey = input.getSchema().primaryKey;\n }\n\n setOutput(output: Output): void {\n this.#output = output;\n }\n\n getSchema(): SourceSchema {\n return this.#input.getSchema();\n }\n\n *fetch(req: FetchRequest): Stream<Node | 'yield'> {\n assert(!req.start, 'Cap does not support start');\n assert(!req.reverse, 'Cap does not support reverse');\n\n // Cap is only built for non-flipped EXISTS subqueries, whose only\n // downstream consumer is a Join that always fetches with a constraint\n // built from the correlation's childField — which is Cap's partition\n // key. So either partitionKey is undefined, or constraint matches.\n assert(\n !this.#partitionKey ||\n (req.constraint !== undefined &&\n constraintMatchesPartitionKey(req.constraint, this.#partitionKey)),\n 'Cap fetch: constraint must match partition key when partitioned',\n );\n\n const capStateKey = getCapStateKey(this.#partitionKey, req.constraint);\n const capState = this.#storage.get(capStateKey);\n if (!capState) {\n yield* this.#initialFetch(req);\n return;\n }\n if (capState.size === 0) {\n return;\n }\n // PK-based point lookups: fetch each tracked row by its PK directly,\n // rather than scanning the partition and filtering.\n for (const pk of capState.pks) {\n const constraint = deserializePKToConstraint(pk, this.#primaryKey);\n for (const inputNode of this.#input.fetch({constraint})) {\n if (inputNode === 'yield') {\n yield inputNode;\n continue;\n }\n yield inputNode;\n }\n }\n }\n\n *#initialFetch(req: FetchRequest): Stream<Node | 'yield'> {\n if (this.#limit === 0) {\n return;\n }\n\n assert(\n constraintMatchesPartitionKey(req.constraint, this.#partitionKey),\n 'Constraint should match partition key',\n );\n\n const capStateKey = getCapStateKey(this.#partitionKey, req.constraint);\n assert(\n this.#storage.get(capStateKey) === undefined,\n 'Cap state should be undefined',\n );\n\n let size = 0;\n const pks: string[] = [];\n let downstreamEarlyReturn = true;\n let exceptionThrown = false;\n try {\n for (const inputNode of this.#input.fetch(req)) {\n if (inputNode === 'yield') {\n yield 'yield';\n continue;\n }\n yield inputNode;\n pks.push(serializePK(inputNode.row, this.#primaryKey));\n size++;\n if (size === this.#limit) {\n break;\n }\n }\n downstreamEarlyReturn = false;\n } catch (e) {\n exceptionThrown = true;\n throw e;\n } finally {\n if (!exceptionThrown) {\n this.#storage.set(capStateKey, {size, pks});\n // If it becomes necessary to support downstream early return, this\n // assert should be removed, and replaced with code that consumes\n // the input stream until limit is reached or the input stream is\n // exhausted so that capState is properly hydrated.\n assert(\n !downstreamEarlyReturn,\n 'Unexpected early return prevented full hydration',\n );\n }\n }\n }\n\n *push(change: Change): Stream<'yield'> {\n if (change[ChangeIndex.TYPE] === ChangeType.EDIT) {\n yield* this.#pushEditChange(change);\n return;\n }\n\n const capStateKey = getCapStateKey(\n this.#partitionKey,\n change[ChangeIndex.NODE].row,\n );\n const capState = this.#storage.get(capStateKey);\n if (!capState) {\n return;\n }\n\n const pk = serializePK(change[ChangeIndex.NODE].row, this.#primaryKey);\n\n if (change[ChangeIndex.TYPE] === ChangeType.ADD) {\n if (capState.size < this.#limit) {\n const pks = [...capState.pks, pk];\n this.#storage.set(capStateKey, {size: capState.size + 1, pks});\n yield* this.#output.push(change, this);\n return;\n }\n // Full — drop\n return;\n } else if (change[ChangeIndex.TYPE] === ChangeType.REMOVE) {\n const pkIndex = capState.pks.indexOf(pk);\n if (pkIndex === -1) {\n // Not in our set — drop\n return;\n }\n // Remove from set\n const pks = [...capState.pks];\n pks.splice(pkIndex, 1);\n const newSize = capState.size - 1;\n\n // Try to refill: fetch from input with partition constraint,\n // find first row NOT in PK set\n const pkSet = new Set(pks);\n const constraint = this.#partitionKey\n ? (Object.fromEntries(\n this.#partitionKey.map(\n key => [key, change[ChangeIndex.NODE].row[key]] as const,\n ),\n ) as Constraint)\n : undefined;\n\n let replacement: Node | undefined;\n for (const node of this.#input.fetch({constraint})) {\n if (node === 'yield') {\n yield node;\n continue;\n }\n const nodePK = serializePK(node.row, this.#primaryKey);\n if (!pkSet.has(nodePK)) {\n replacement = node;\n break;\n }\n }\n\n if (replacement) {\n // Store state WITHOUT replacement during remove forward,\n // matching Take's pattern of hiding in-flight changes from re-fetches.\n this.#storage.set(capStateKey, {size: newSize, pks});\n yield* this.#output.push(change, this);\n // Now add replacement to set and forward the add.\n const replacementPK = serializePK(replacement.row, this.#primaryKey);\n pks.push(replacementPK);\n this.#storage.set(capStateKey, {size: newSize + 1, pks});\n yield* this.#output.push(makeAddChange(replacement), this);\n } else {\n this.#storage.set(capStateKey, {size: newSize, pks});\n yield* this.#output.push(change, this);\n }\n } else if (change[ChangeIndex.TYPE] === ChangeType.CHILD) {\n const pkSet = new Set(capState.pks);\n if (pkSet.has(pk)) {\n yield* this.#output.push(change, this);\n }\n }\n }\n\n #pushEditChange(change: EditChange): Stream<'yield'> {\n assert(\n !this.#partitionKeyComparator ||\n this.#partitionKeyComparator(\n change[ChangeIndex.OLD_NODE].row,\n change[ChangeIndex.NODE].row,\n ) === 0,\n 'Unexpected change of partition key',\n );\n const capStateKey = getCapStateKey(\n this.#partitionKey,\n change[ChangeIndex.OLD_NODE].row,\n );\n const capState = this.#storage.get(capStateKey);\n if (!capState) {\n return emptyArray;\n }\n\n const oldPK = serializePK(\n change[ChangeIndex.OLD_NODE].row,\n this.#primaryKey,\n );\n const pkSet = new Set(capState.pks);\n if (pkSet.has(oldPK)) {\n // Update the PK in our set if it changed\n const newPK = serializePK(change[ChangeIndex.NODE].row, this.#primaryKey);\n if (oldPK !== newPK) {\n const pks = capState.pks.map(p => (p === oldPK ? newPK : p));\n this.#storage.set(capStateKey, {size: capState.size, pks});\n }\n return this.#output.push(change, this);\n }\n // If not in our set, drop\n return emptyArray;\n }\n\n destroy(): void {\n this.#input.destroy();\n }\n}\n\nfunction getCapStateKey(\n partitionKey: PartitionKey | undefined,\n rowOrConstraint: Row | Constraint | undefined,\n): string {\n const partitionValues: Value[] = [];\n\n if (partitionKey && rowOrConstraint) {\n for (const key of partitionKey) {\n partitionValues.push(rowOrConstraint[key]);\n }\n }\n\n return JSON.stringify(['cap', ...partitionValues]);\n}\n\nfunction serializePK(row: Row, primaryKey: PrimaryKey): string {\n return JSON.stringify(primaryKey.map(k => row[k]));\n}\n\nfunction deserializePKToConstraint(\n pk: string,\n primaryKey: PrimaryKey,\n): Constraint {\n const values = JSON.parse(pk) as Value[];\n const constraint: Record<string, Value> = {};\n for (let i = 0; i < primaryKey.length; i++) {\n constraint[primaryKey[i]] = values[i];\n }\n return constraint;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAoDA,IAAa,MAAb,MAAqC;CACnC;CACA;CACA;CACA;CACA;CACA;CAEA,UAAkB;CAElB,YACE,OACA,SACA,OACA,cACA;EACA,OAAO,SAAS,GAAG,4BAA4B;EAC/C,MAAM,UAAU,IAAI;EACpB,KAAKA,SAAS;EACd,KAAKC,WAAW;EAChB,KAAKC,SAAS;EACd,KAAKC,gBAAgB;EACrB,KAAKC,0BACH,gBAAgB,2BAA2B,YAAY;EACzD,KAAKC,cAAc,MAAM,UAAU,EAAE;CACvC;CAEA,UAAU,QAAsB;EAC9B,KAAKC,UAAU;CACjB;CAEA,YAA0B;EACxB,OAAO,KAAKN,OAAO,UAAU;CAC/B;CAEA,CAAC,MAAM,KAA2C;EAChD,OAAO,CAAC,IAAI,OAAO,4BAA4B;EAC/C,OAAO,CAAC,IAAI,SAAS,8BAA8B;EAMnD,OACE,CAAC,KAAKG,iBACH,IAAI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,wBAAgB,WAAW,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,KAAK,GAAG,OAAO,CAMvD;AAED,wBAAgB,YAAY,CAAC,IAAI,EAAE,IAAI,GAAG,OAAO,QAShD"}
@@ -33,10 +33,15 @@ function normalizeUndefined(v) {
33
33
  return v ?? null;
34
34
  }
35
35
  function makeComparator(order, reverse) {
36
+ if (order.length === 1 && order[0][1] === "asc" && !reverse) {
37
+ const field = order[0][0];
38
+ return (a, b) => compareValues(a[field], b[field]);
39
+ }
40
+ const length = order.length;
36
41
  return (a, b) => {
37
- for (const ord of order) {
38
- const field = ord[0];
39
- const comp = compareValues(a[field], b[field]);
42
+ for (let i = 0; i < length; i++) {
43
+ const ord = order[i];
44
+ const comp = compareValues(a[ord[0]], b[ord[0]]);
40
45
  if (comp !== 0) {
41
46
  const result = ord[1] === "asc" ? comp : -comp;
42
47
  return reverse ? -result : result;
@@ -1 +1 @@
1
- {"version":3,"file":"data.js","names":[],"sources":["../../../../../zql/src/ivm/data.ts"],"sourcesContent":["import {compareUTF8} from 'compare-utf8';\nimport type {Ordering} from '../../../zero-protocol/src/ast.ts';\nimport type {Row, Value} from '../../../zero-protocol/src/data.ts';\nimport type {Stream} from './stream.ts';\n\n/**\n * A row flowing through the pipeline, plus its relationships.\n * Relationships are generated lazily as read.\n */\nexport type Node = {\n row: Row;\n /**\n * Relationships are generated lazily as read.\n * The stream may contain 'yield' to indicate the operator has yielded control.\n * See {@linkcode Operator.fetch} for more details about yields.\n */\n relationships: Record<string, () => Stream<Node | 'yield'>>;\n};\n\n/**\n * Compare two values. The values must be of the same type. This function\n * throws at runtime if the types differ.\n *\n * Note, this function considers `null === null` and\n * `undefined === undefined`. This is different than SQL. In join code,\n * null must be treated separately.\n *\n * See: https://github.com/rocicorp/mono/pull/2116/files#r1704811479\n *\n * @returns < 0 if a < b, 0 if a === b, > 0 if a > b\n */\nexport function compareValues(a: Value, b: Value): number {\n a = a ?? null;\n b = b ?? null;\n\n if (a === b) {\n return 0;\n }\n\n if (typeof a === 'string' && typeof b === 'string') {\n // We compare all strings in Zero as UTF-8. This is the default on SQLite\n // and we need to match it. See:\n // https://blog.replicache.dev/blog/replicache-11-adventures-in-text-encoding.\n //\n // TODO: We could change this since SQLite supports UTF-16. Microbenchmark\n // to see if there's a big win.\n //\n // https://www.sqlite.org/c3ref/create_collation.html\n return compareUTF8(a, b);\n }\n\n if (typeof a === 'number' && typeof b === 'number') {\n return a - b;\n }\n\n if (typeof a === 'boolean' && typeof b === 'boolean') {\n return a ? 1 : -1;\n }\n\n // check with null after since it is less likely to be the common case and we\n // want to avoid the extra checks in that case\n if (a === null) {\n return -1;\n }\n if (b === null) {\n return 1;\n }\n\n if (typeof a !== typeof b) {\n throw new Error(\n `Cannot compare values of different types: ${typeof a} and ${typeof b}`,\n );\n }\n\n throw new Error(`Unsupported type: ${typeof a}`);\n}\n\nexport type NormalizedValue = Exclude<Value, undefined>;\n\n/**\n * We allow undefined to be passed for the convenience of developers, but we\n * treat it equivalently to null. It's better for perf to not create an copy\n * of input values, so we just normalize at use when necessary.\n */\nexport function normalizeUndefined(v: Value): NormalizedValue {\n return v ?? null;\n}\n\nexport type Comparator = (r1: Row, r2: Row) => number;\n\nexport function makeComparator(order: Ordering, reverse?: boolean): Comparator {\n return (a, b) => {\n // Skip destructuring here since it is hot code.\n for (const ord of order) {\n const field = ord[0];\n const comp = compareValues(a[field], b[field]);\n if (comp !== 0) {\n const result = ord[1] === 'asc' ? comp : -comp;\n return reverse ? -result : result;\n }\n }\n return 0;\n };\n}\n\n/**\n * Determine if two values are equal. Note that unlike compareValues() above,\n * this function treats `null` as unequal to itself (and same for `undefined`).\n * This is required to make joins work correctly, but may not be the right\n * semantic for your application.\n */\nexport function valuesEqual(a: Value, b: Value): boolean {\n // oxlint-disable-next-line eqeqeq\n if (a == null || b == null) {\n return false;\n }\n return a === b;\n}\n\nexport function drainStreams(node: Node | 'yield') {\n if (node === 'yield') {\n return;\n }\n for (const stream of Object.values(node.relationships)) {\n for (const node of stream()) {\n drainStreams(node);\n }\n }\n}\n"],"mappings":";;;;;;;;;;;;;;AA+BA,SAAgB,cAAc,GAAU,GAAkB;CACxD,IAAI,KAAK;CACT,IAAI,KAAK;CAET,IAAI,MAAM,GACR,OAAO;CAGT,IAAI,OAAO,MAAM,YAAY,OAAO,MAAM,UASxC,OAAO,YAAY,GAAG,CAAC;CAGzB,IAAI,OAAO,MAAM,YAAY,OAAO,MAAM,UACxC,OAAO,IAAI;CAGb,IAAI,OAAO,MAAM,aAAa,OAAO,MAAM,WACzC,OAAO,IAAI,IAAI;CAKjB,IAAI,MAAM,MACR,OAAO;CAET,IAAI,MAAM,MACR,OAAO;CAGT,IAAI,OAAO,MAAM,OAAO,GACtB,MAAM,IAAI,MACR,6CAA6C,OAAO,EAAE,OAAO,OAAO,GACtE;CAGF,MAAM,IAAI,MAAM,qBAAqB,OAAO,GAAG;AACjD;;;;;;AASA,SAAgB,mBAAmB,GAA2B;CAC5D,OAAO,KAAK;AACd;AAIA,SAAgB,eAAe,OAAiB,SAA+B;CAC7E,QAAQ,GAAG,MAAM;EAEf,KAAK,MAAM,OAAO,OAAO;GACvB,MAAM,QAAQ,IAAI;GAClB,MAAM,OAAO,cAAc,EAAE,QAAQ,EAAE,MAAM;GAC7C,IAAI,SAAS,GAAG;IACd,MAAM,SAAS,IAAI,OAAO,QAAQ,OAAO,CAAC;IAC1C,OAAO,UAAU,CAAC,SAAS;GAC7B;EACF;EACA,OAAO;CACT;AACF;;;;;;;AAQA,SAAgB,YAAY,GAAU,GAAmB;CAEvD,IAAI,KAAK,QAAQ,KAAK,MACpB,OAAO;CAET,OAAO,MAAM;AACf"}
1
+ {"version":3,"file":"data.js","names":[],"sources":["../../../../../zql/src/ivm/data.ts"],"sourcesContent":["import {compareUTF8} from 'compare-utf8';\nimport type {Ordering} from '../../../zero-protocol/src/ast.ts';\nimport type {Row, Value} from '../../../zero-protocol/src/data.ts';\nimport type {Stream} from './stream.ts';\n\n/**\n * A row flowing through the pipeline, plus its relationships.\n * Relationships are generated lazily as read.\n */\nexport type Node = {\n row: Row;\n /**\n * Relationships are generated lazily as read.\n * The stream may contain 'yield' to indicate the operator has yielded control.\n * See {@linkcode Operator.fetch} for more details about yields.\n */\n relationships: Record<string, () => Stream<Node | 'yield'>>;\n};\n\n/**\n * Compare two values. The values must be of the same type. This function\n * throws at runtime if the types differ.\n *\n * Note, this function considers `null === null` and\n * `undefined === undefined`. This is different than SQL. In join code,\n * null must be treated separately.\n *\n * See: https://github.com/rocicorp/mono/pull/2116/files#r1704811479\n *\n * @returns < 0 if a < b, 0 if a === b, > 0 if a > b\n */\nexport function compareValues(a: Value, b: Value): number {\n a = a ?? null;\n b = b ?? null;\n\n if (a === b) {\n return 0;\n }\n\n if (typeof a === 'string' && typeof b === 'string') {\n // We compare all strings in Zero as UTF-8. This is the default on SQLite\n // and we need to match it. See:\n // https://blog.replicache.dev/blog/replicache-11-adventures-in-text-encoding.\n //\n // TODO: We could change this since SQLite supports UTF-16. Microbenchmark\n // to see if there's a big win.\n //\n // https://www.sqlite.org/c3ref/create_collation.html\n return compareUTF8(a, b);\n }\n\n if (typeof a === 'number' && typeof b === 'number') {\n return a - b;\n }\n\n if (typeof a === 'boolean' && typeof b === 'boolean') {\n return a ? 1 : -1;\n }\n\n // check with null after since it is less likely to be the common case and we\n // want to avoid the extra checks in that case\n if (a === null) {\n return -1;\n }\n if (b === null) {\n return 1;\n }\n\n if (typeof a !== typeof b) {\n throw new Error(\n `Cannot compare values of different types: ${typeof a} and ${typeof b}`,\n );\n }\n\n throw new Error(`Unsupported type: ${typeof a}`);\n}\n\nexport type NormalizedValue = Exclude<Value, undefined>;\n\n/**\n * We allow undefined to be passed for the convenience of developers, but we\n * treat it equivalently to null. It's better for perf to not create an copy\n * of input values, so we just normalize at use when necessary.\n */\nexport function normalizeUndefined(v: Value): NormalizedValue {\n return v ?? null;\n}\n\nexport type Comparator = (r1: Row, r2: Row) => number;\n\nexport function makeComparator(order: Ordering, reverse?: boolean): Comparator {\n // A single ascending field is the common shape -- it is what every source\n // ordered by a single-column primary key gets -- and specializing it drops\n // the loop, the direction test and the reverse test from the hottest\n // function in hydration.\n if (order.length === 1 && order[0][1] === 'asc' && !reverse) {\n const field = order[0][0];\n return (a, b) => compareValues(a[field], b[field]);\n }\n\n const length = order.length;\n return (a, b) => {\n // Skip destructuring here since it is hot code. An indexed loop rather\n // than `for...of` for the same reason: Hermes allocates an iterator and\n // calls `next()` per element, which costs more than the comparison for an\n // ordering this short.\n for (let i = 0; i < length; i++) {\n const ord = order[i];\n const comp = compareValues(a[ord[0]], b[ord[0]]);\n if (comp !== 0) {\n const result = ord[1] === 'asc' ? comp : -comp;\n return reverse ? -result : result;\n }\n }\n return 0;\n };\n}\n\n/**\n * Determine if two values are equal. Note that unlike compareValues() above,\n * this function treats `null` as unequal to itself (and same for `undefined`).\n * This is required to make joins work correctly, but may not be the right\n * semantic for your application.\n */\nexport function valuesEqual(a: Value, b: Value): boolean {\n // oxlint-disable-next-line eqeqeq\n if (a == null || b == null) {\n return false;\n }\n return a === b;\n}\n\nexport function drainStreams(node: Node | 'yield') {\n if (node === 'yield') {\n return;\n }\n for (const stream of Object.values(node.relationships)) {\n for (const node of stream()) {\n drainStreams(node);\n }\n }\n}\n"],"mappings":";;;;;;;;;;;;;;AA+BA,SAAgB,cAAc,GAAU,GAAkB;CACxD,IAAI,KAAK;CACT,IAAI,KAAK;CAET,IAAI,MAAM,GACR,OAAO;CAGT,IAAI,OAAO,MAAM,YAAY,OAAO,MAAM,UASxC,OAAO,YAAY,GAAG,CAAC;CAGzB,IAAI,OAAO,MAAM,YAAY,OAAO,MAAM,UACxC,OAAO,IAAI;CAGb,IAAI,OAAO,MAAM,aAAa,OAAO,MAAM,WACzC,OAAO,IAAI,IAAI;CAKjB,IAAI,MAAM,MACR,OAAO;CAET,IAAI,MAAM,MACR,OAAO;CAGT,IAAI,OAAO,MAAM,OAAO,GACtB,MAAM,IAAI,MACR,6CAA6C,OAAO,EAAE,OAAO,OAAO,GACtE;CAGF,MAAM,IAAI,MAAM,qBAAqB,OAAO,GAAG;AACjD;;;;;;AASA,SAAgB,mBAAmB,GAA2B;CAC5D,OAAO,KAAK;AACd;AAIA,SAAgB,eAAe,OAAiB,SAA+B;CAK7E,IAAI,MAAM,WAAW,KAAK,MAAM,GAAG,OAAO,SAAS,CAAC,SAAS;EAC3D,MAAM,QAAQ,MAAM,GAAG;EACvB,QAAQ,GAAG,MAAM,cAAc,EAAE,QAAQ,EAAE,MAAM;CACnD;CAEA,MAAM,SAAS,MAAM;CACrB,QAAQ,GAAG,MAAM;EAKf,KAAK,IAAI,IAAI,GAAG,IAAI,QAAQ,KAAK;GAC/B,MAAM,MAAM,MAAM;GAClB,MAAM,OAAO,cAAc,EAAE,IAAI,KAAK,EAAE,IAAI,GAAG;GAC/C,IAAI,SAAS,GAAG;IACd,MAAM,SAAS,IAAI,OAAO,QAAQ,OAAO,CAAC;IAC1C,OAAO,UAAU,CAAC,SAAS;GAC7B;EACF;EACA,OAAO;CACT;AACF;;;;;;;AAQA,SAAgB,YAAY,GAAU,GAAmB;CAEvD,IAAI,KAAK,QAAQ,KAAK,MACpB,OAAO;CAET,OAAO,MAAM;AACf"}
@@ -0,0 +1,44 @@
1
+ import type { Node } from './data.ts';
2
+ import type { FetchRequest, Input, Output } from './operator.ts';
3
+ import type { SourceSchema } from './schema.ts';
4
+ import { type Stream } from './stream.ts';
5
+ /**
6
+ * A placeholder `Input` for a view whose pipeline has not been built yet.
7
+ *
8
+ * Until {@link attach} is called it behaves like an empty pipeline: `fetch`
9
+ * yields nothing and pushes never happen. When the real pipeline is attached
10
+ * the view is hydrated with a single `fetch`, whose rows are delivered to the
11
+ * view as add changes, so the view sees the data exactly as it would see rows
12
+ * arriving incrementally.
13
+ *
14
+ * This lets `materialize()` stay synchronous and return a view immediately
15
+ * while the expensive pipeline construction and hydration are postponed, for
16
+ * example until the client's replica has been loaded into the IVM sources.
17
+ */
18
+ export declare class DeferredInput implements Input {
19
+ #private;
20
+ /**
21
+ * @param schema The schema the built pipeline reports. Passed in so this
22
+ * input can answer `getSchema()` unconditionally, as the `Input` contract
23
+ * requires, before its pipeline exists.
24
+ * @param build Builds the real pipeline. Called once, from {@link attach}.
25
+ */
26
+ constructor(schema: SourceSchema, build: () => Input);
27
+ get attached(): boolean;
28
+ get destroyed(): boolean;
29
+ setOutput(output: Output): void;
30
+ getSchema(): SourceSchema;
31
+ fetch(req: FetchRequest): Stream<Node | 'yield'>;
32
+ destroy(): void;
33
+ /**
34
+ * Build the real pipeline and hydrate the output with its current contents.
35
+ *
36
+ * Must be called inside the delegate's `batchViewUpdates`; the delegate is
37
+ * responsible for notifying commit listeners afterwards so views flush.
38
+ *
39
+ * If building or hydrating throws, the pipeline is torn down again and this
40
+ * input stays unattached so the caller can report the failure.
41
+ */
42
+ attach(): void;
43
+ }
44
+ //# sourceMappingURL=deferred-input.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"deferred-input.d.ts","sourceRoot":"","sources":["../../../../../zql/src/ivm/deferred-input.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAC,IAAI,EAAC,MAAM,WAAW,CAAC;AACpC,OAAO,KAAK,EAAC,YAAY,EAAE,KAAK,EAAE,MAAM,EAAC,MAAM,eAAe,CAAC;AAC/D,OAAO,KAAK,EAAC,YAAY,EAAC,MAAM,aAAa,CAAC;AAC9C,OAAO,EAAU,KAAK,MAAM,EAAC,MAAM,aAAa,CAAC;AAEjD;;;;;;;;;;;;GAYG;AACH,qBAAa,aAAc,YAAW,KAAK;;IAOzC;;;;;OAKG;IACH,YAAY,MAAM,EAAE,YAAY,EAAE,KAAK,EAAE,MAAM,KAAK,EAGnD;IAED,IAAI,QAAQ,IAAI,OAAO,CAEtB;IAED,IAAI,SAAS,IAAI,OAAO,CAEvB;IAED,SAAS,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,CAG9B;IAED,SAAS,IAAI,YAAY,CAExB;IAED,KAAK,CAAC,GAAG,EAAE,YAAY,GAAG,MAAM,CAAC,IAAI,GAAG,OAAO,CAAC,CAE/C;IAED,OAAO,IAAI,IAAI,CAGd;IAED;;;;;;;;OAQG;IACH,MAAM,IAAI,IAAI,CAuBb;CACF"}
@@ -0,0 +1,89 @@
1
+ import { assert } from "../../../shared/src/asserts.js";
2
+ import { emptyArray } from "../../../shared/src/sentinels.js";
3
+ import { makeAddChange } from "./change.js";
4
+ import { consume } from "./stream.js";
5
+ //#region ../zql/src/ivm/deferred-input.ts
6
+ /**
7
+ * A placeholder `Input` for a view whose pipeline has not been built yet.
8
+ *
9
+ * Until {@link attach} is called it behaves like an empty pipeline: `fetch`
10
+ * yields nothing and pushes never happen. When the real pipeline is attached
11
+ * the view is hydrated with a single `fetch`, whose rows are delivered to the
12
+ * view as add changes, so the view sees the data exactly as it would see rows
13
+ * arriving incrementally.
14
+ *
15
+ * This lets `materialize()` stay synchronous and return a view immediately
16
+ * while the expensive pipeline construction and hydration are postponed, for
17
+ * example until the client's replica has been loaded into the IVM sources.
18
+ */
19
+ var DeferredInput = class {
20
+ #schema;
21
+ #build;
22
+ #output;
23
+ #input;
24
+ #destroyed = false;
25
+ /**
26
+ * @param schema The schema the built pipeline reports. Passed in so this
27
+ * input can answer `getSchema()` unconditionally, as the `Input` contract
28
+ * requires, before its pipeline exists.
29
+ * @param build Builds the real pipeline. Called once, from {@link attach}.
30
+ */
31
+ constructor(schema, build) {
32
+ this.#schema = schema;
33
+ this.#build = build;
34
+ }
35
+ get attached() {
36
+ return this.#input !== void 0;
37
+ }
38
+ get destroyed() {
39
+ return this.#destroyed;
40
+ }
41
+ setOutput(output) {
42
+ this.#output = output;
43
+ this.#input?.setOutput(output);
44
+ }
45
+ getSchema() {
46
+ return this.#schema;
47
+ }
48
+ fetch(req) {
49
+ return this.#input ? this.#input.fetch(req) : emptyArray;
50
+ }
51
+ destroy() {
52
+ this.#destroyed = true;
53
+ this.#input?.destroy();
54
+ }
55
+ /**
56
+ * Build the real pipeline and hydrate the output with its current contents.
57
+ *
58
+ * Must be called inside the delegate's `batchViewUpdates`; the delegate is
59
+ * responsible for notifying commit listeners afterwards so views flush.
60
+ *
61
+ * If building or hydrating throws, the pipeline is torn down again and this
62
+ * input stays unattached so the caller can report the failure.
63
+ */
64
+ attach() {
65
+ assert(!this.#input, "DeferredInput: pipeline already attached");
66
+ assert(!this.#destroyed, "DeferredInput: cannot attach after destroy");
67
+ const input = this.#build();
68
+ const output = this.#output;
69
+ if (!output) {
70
+ this.#input = input;
71
+ return;
72
+ }
73
+ try {
74
+ input.setOutput(output);
75
+ for (const node of input.fetch({})) {
76
+ if (node === "yield") continue;
77
+ consume(output.push(makeAddChange(node), input));
78
+ }
79
+ } catch (e) {
80
+ input.destroy();
81
+ throw e;
82
+ }
83
+ this.#input = input;
84
+ }
85
+ };
86
+ //#endregion
87
+ export { DeferredInput };
88
+
89
+ //# sourceMappingURL=deferred-input.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"deferred-input.js","names":["#schema","#build","#input","#destroyed","#output"],"sources":["../../../../../zql/src/ivm/deferred-input.ts"],"sourcesContent":["import {assert} from '../../../shared/src/asserts.ts';\nimport {emptyArray} from '../../../shared/src/sentinels.ts';\nimport {makeAddChange} from './change.ts';\nimport type {Node} from './data.ts';\nimport type {FetchRequest, Input, Output} from './operator.ts';\nimport type {SourceSchema} from './schema.ts';\nimport {consume, type Stream} from './stream.ts';\n\n/**\n * A placeholder `Input` for a view whose pipeline has not been built yet.\n *\n * Until {@link attach} is called it behaves like an empty pipeline: `fetch`\n * yields nothing and pushes never happen. When the real pipeline is attached\n * the view is hydrated with a single `fetch`, whose rows are delivered to the\n * view as add changes, so the view sees the data exactly as it would see rows\n * arriving incrementally.\n *\n * This lets `materialize()` stay synchronous and return a view immediately\n * while the expensive pipeline construction and hydration are postponed, for\n * example until the client's replica has been loaded into the IVM sources.\n */\nexport class DeferredInput implements Input {\n readonly #schema: SourceSchema;\n readonly #build: () => Input;\n #output: Output | undefined;\n #input: Input | undefined;\n #destroyed = false;\n\n /**\n * @param schema The schema the built pipeline reports. Passed in so this\n * input can answer `getSchema()` unconditionally, as the `Input` contract\n * requires, before its pipeline exists.\n * @param build Builds the real pipeline. Called once, from {@link attach}.\n */\n constructor(schema: SourceSchema, build: () => Input) {\n this.#schema = schema;\n this.#build = build;\n }\n\n get attached(): boolean {\n return this.#input !== undefined;\n }\n\n get destroyed(): boolean {\n return this.#destroyed;\n }\n\n setOutput(output: Output): void {\n this.#output = output;\n this.#input?.setOutput(output);\n }\n\n getSchema(): SourceSchema {\n return this.#schema;\n }\n\n fetch(req: FetchRequest): Stream<Node | 'yield'> {\n return this.#input ? this.#input.fetch(req) : emptyArray;\n }\n\n destroy(): void {\n this.#destroyed = true;\n this.#input?.destroy();\n }\n\n /**\n * Build the real pipeline and hydrate the output with its current contents.\n *\n * Must be called inside the delegate's `batchViewUpdates`; the delegate is\n * responsible for notifying commit listeners afterwards so views flush.\n *\n * If building or hydrating throws, the pipeline is torn down again and this\n * input stays unattached so the caller can report the failure.\n */\n attach(): void {\n assert(!this.#input, 'DeferredInput: pipeline already attached');\n assert(!this.#destroyed, 'DeferredInput: cannot attach after destroy');\n const input = this.#build();\n const output = this.#output;\n if (!output) {\n // The view never asked for output; there is nothing to hydrate.\n this.#input = input;\n return;\n }\n try {\n input.setOutput(output);\n for (const node of input.fetch({})) {\n if (node === 'yield') {\n continue;\n }\n consume(output.push(makeAddChange(node), input));\n }\n } catch (e) {\n input.destroy();\n throw e;\n }\n this.#input = input;\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAqBA,IAAa,gBAAb,MAA4C;CAC1C;CACA;CACA;CACA;CACA,aAAa;;;;;;;CAQb,YAAY,QAAsB,OAAoB;EACpD,KAAKA,UAAU;EACf,KAAKC,SAAS;CAChB;CAEA,IAAI,WAAoB;EACtB,OAAO,KAAKC,WAAW,KAAA;CACzB;CAEA,IAAI,YAAqB;EACvB,OAAO,KAAKC;CACd;CAEA,UAAU,QAAsB;EAC9B,KAAKC,UAAU;EACf,KAAKF,QAAQ,UAAU,MAAM;CAC/B;CAEA,YAA0B;EACxB,OAAO,KAAKF;CACd;CAEA,MAAM,KAA2C;EAC/C,OAAO,KAAKE,SAAS,KAAKA,OAAO,MAAM,GAAG,IAAI;CAChD;CAEA,UAAgB;EACd,KAAKC,aAAa;EAClB,KAAKD,QAAQ,QAAQ;CACvB;;;;;;;;;;CAWA,SAAe;EACb,OAAO,CAAC,KAAKA,QAAQ,0CAA0C;EAC/D,OAAO,CAAC,KAAKC,YAAY,4CAA4C;EACrE,MAAM,QAAQ,KAAKF,OAAO;EAC1B,MAAM,SAAS,KAAKG;EACpB,IAAI,CAAC,QAAQ;GAEX,KAAKF,SAAS;GACd;EACF;EACA,IAAI;GACF,MAAM,UAAU,MAAM;GACtB,KAAK,MAAM,QAAQ,MAAM,MAAM,CAAC,CAAC,GAAG;IAClC,IAAI,SAAS,SACX;IAEF,QAAQ,OAAO,KAAK,cAAc,IAAI,GAAG,KAAK,CAAC;GACjD;EACF,SAAS,GAAG;GACV,MAAM,QAAQ;GACd,MAAM;EACR;EACA,KAAKA,SAAS;CAChB;AACF"}
@@ -14,7 +14,7 @@ export declare class Exists implements FilterOperator {
14
14
  setFilterOutput(output: FilterOutput): void;
15
15
  beginFilter(): void;
16
16
  endFilter(): void;
17
- filter(node: Node): Generator<'yield', boolean>;
17
+ filter(node: Node): IterableIterator<'yield', boolean>;
18
18
  destroy(): void;
19
19
  getSchema(): SourceSchema;
20
20
  push(change: Change): Stream<'yield'>;
@@ -1 +1 @@
1
- {"version":3,"file":"exists.d.ts","sourceRoot":"","sources":["../../../../../zql/src/ivm/exists.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAC,WAAW,EAAC,MAAM,mCAAmC,CAAC;AAGnE,OAAO,EAAkC,KAAK,MAAM,EAAC,MAAM,aAAa,CAAC;AACzE,OAAO,EAAqB,KAAK,IAAI,EAAuB,MAAM,WAAW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GG;AACH,qBAAa,MAAO,YAAW,cAAc;;IAoB3C,YACE,KAAK,EAAE,WAAW,EAClB,gBAAgB,EAAE,MAAM,EACxB,aAAa,EAAE,WAAW,EAC1B,IAAI,EAAE,QAAQ,GAAG,YAAY,EAC7B,wBAAwB,CAAC,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,EAoB/C;IAED,eAAe,CAAC,MAAM,EAAE,YAAY,GAAG,IAAI,CAE1C;IAED,WAAW,SAEV;IAED,SAAS,SAGR;IAEA,MAAM,CAAC,IAAI,EAAE,IAAI,GAAG,gBAAgB,CAAC,OAAO,EAAE,OAAO,CAAC,CAmBtD;IAED,OAAO,IAAI,IAAI,CAEd;IAED,SAAS,IAAI,YAAY,CAExB;IAEA,IAAI,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAAC,OAAO,CAAC,CAmGrC;CAyDF"}
@@ -1 +1 @@
1
- {"version":3,"file":"exists.js","names":["#input","#relationshipName","#not","#parentJoinKey","#noSizeReuse","#cache","#cacheHitCountsForTesting","#output","#inPush","#getCacheKey","#fetchExists","#filter","#pushWithFilter","#fetchSize"],"sources":["../../../../../zql/src/ivm/exists.ts"],"sourcesContent":["import {areEqual} from '../../../shared/src/arrays.ts';\nimport {assert, unreachable} from '../../../shared/src/asserts.ts';\nimport type {CompoundKey} from '../../../zero-protocol/src/ast.ts';\nimport {ChangeIndex} from './change-index.ts';\nimport {ChangeType} from './change-type.ts';\nimport {makeAddChange, makeRemoveChange, type Change} from './change.ts';\nimport {normalizeUndefined, type Node, type NormalizedValue} from './data.ts';\nimport {\n throwFilterOutput,\n type FilterInput,\n type FilterOperator,\n type FilterOutput,\n} from './filter-operators.ts';\nimport type {SourceSchema} from './schema.ts';\nimport {type Stream} from './stream.ts';\n\n/**\n * The Exists operator filters data based on whether or not a relationship is\n * non-empty.\n */\nexport class Exists implements FilterOperator {\n readonly #input: FilterInput;\n readonly #relationshipName: string;\n readonly #not: boolean;\n readonly #parentJoinKey: CompoundKey;\n readonly #noSizeReuse: boolean;\n #cache: Map<string, boolean>;\n #cacheHitCountsForTesting: Map<string, number> | undefined;\n #output: FilterOutput = throwFilterOutput;\n\n /**\n * This instance variable is `true` when this operator is processing a `push`,\n * and is used to disable reuse of cached sizes across rows with the\n * same parent join key value.\n * This is necessary because during a push relationships can be inconsistent\n * due to push communicating changes (which may change multiple Nodes) one\n * Node at a time.\n */\n #inPush = false;\n\n constructor(\n input: FilterInput,\n relationshipName: string,\n parentJoinKey: CompoundKey,\n type: 'EXISTS' | 'NOT EXISTS',\n cacheHitCountsForTesting?: Map<string, number>,\n ) {\n this.#input = input;\n this.#relationshipName = relationshipName;\n this.#input.setFilterOutput(this);\n this.#cache = new Map();\n this.#cacheHitCountsForTesting = cacheHitCountsForTesting;\n assert(\n this.#input.getSchema().relationships[relationshipName],\n // log-leak-ignore -- relationship name is schema, allowed in errors\n `Input schema missing ${relationshipName}`,\n );\n this.#not = type === 'NOT EXISTS';\n this.#parentJoinKey = parentJoinKey;\n\n // If the parentJoinKey is the primary key, no sense in trying to reuse.\n this.#noSizeReuse = areEqual(\n parentJoinKey,\n this.#input.getSchema().primaryKey,\n );\n }\n\n setFilterOutput(output: FilterOutput): void {\n this.#output = output;\n }\n\n beginFilter() {\n this.#output.beginFilter();\n }\n\n endFilter() {\n this.#cache = new Map();\n this.#output.endFilter();\n }\n\n *filter(node: Node): Generator<'yield', boolean> {\n let exists: boolean | undefined;\n if (!this.#noSizeReuse && !this.#inPush) {\n const key = this.#getCacheKey(node, this.#parentJoinKey);\n exists = this.#cache.get(key);\n if (exists === undefined) {\n exists = yield* this.#fetchExists(node);\n this.#cache.set(key, exists);\n } else if (this.#cacheHitCountsForTesting) {\n this.#cacheHitCountsForTesting.set(\n key,\n (this.#cacheHitCountsForTesting.get(key) ?? 0) + 1,\n );\n }\n }\n\n const result =\n (yield* this.#filter(node, exists)) && (yield* this.#output.filter(node));\n return result;\n }\n\n destroy(): void {\n this.#input.destroy();\n }\n\n getSchema(): SourceSchema {\n return this.#input.getSchema();\n }\n\n *push(change: Change): Stream<'yield'> {\n assert(!this.#inPush, 'Unexpected re-entrancy');\n this.#inPush = true;\n try {\n switch (change[ChangeIndex.TYPE]) {\n // add, remove and edit cannot change the size of the\n // this.#relationshipName relationship, so simply #pushWithFilter\n case ChangeType.ADD:\n case ChangeType.EDIT:\n case ChangeType.REMOVE: {\n yield* this.#pushWithFilter(change);\n return;\n }\n case ChangeType.CHILD:\n // Only add and remove child changes for the\n // this.#relationshipName relationship, can change the size\n // of the this.#relationshipName relationship, for other\n // child changes simply #pushWithFilter\n if (\n change[ChangeIndex.CHILD_DATA].relationshipName !==\n this.#relationshipName ||\n change[ChangeIndex.CHILD_DATA].change[ChangeIndex.TYPE] ===\n ChangeType.EDIT ||\n change[ChangeIndex.CHILD_DATA].change[ChangeIndex.TYPE] ===\n ChangeType.CHILD\n ) {\n yield* this.#pushWithFilter(change);\n return;\n }\n switch (change[ChangeIndex.CHILD_DATA].change[ChangeIndex.TYPE]) {\n case ChangeType.ADD: {\n const size = yield* this.#fetchSize(change[ChangeIndex.NODE]);\n if (size === 1) {\n if (this.#not) {\n // Since the add child change currently being processed is not\n // pushed to output, the added child needs to be excluded from\n // the remove being pushed to output (since the child has\n // never been added to the output).\n yield* this.#output.push(\n makeRemoveChange({\n row: change[ChangeIndex.NODE].row,\n relationships: {\n ...change[ChangeIndex.NODE].relationships,\n [this.#relationshipName]: () => [],\n },\n }),\n this,\n );\n } else {\n yield* this.#output.push(\n makeAddChange(change[ChangeIndex.NODE]),\n this,\n );\n }\n } else {\n yield* this.#pushWithFilter(change, size > 0);\n }\n return;\n }\n case ChangeType.REMOVE: {\n const size = yield* this.#fetchSize(change[ChangeIndex.NODE]);\n if (size === 0) {\n if (this.#not) {\n yield* this.#output.push(\n makeAddChange(change[ChangeIndex.NODE]),\n this,\n );\n } else {\n // Since the remove child change currently being processed is\n // not pushed to output, the removed child needs to be added to\n // the remove being pushed to output.\n yield* this.#output.push(\n makeRemoveChange({\n row: change[ChangeIndex.NODE].row,\n relationships: {\n ...change[ChangeIndex.NODE].relationships,\n [this.#relationshipName]: () => [\n change[ChangeIndex.CHILD_DATA].change[\n ChangeIndex.NODE\n ],\n ],\n },\n }),\n this,\n );\n }\n } else {\n yield* this.#pushWithFilter(change, size > 0);\n }\n return;\n }\n }\n return;\n default:\n unreachable(change);\n }\n } finally {\n this.#inPush = false;\n }\n }\n\n /**\n * Returns whether or not the node's this.#relationshipName\n * relationship passes the exist/not exists filter condition.\n * If the optional `size` is passed it is used.\n * Otherwise, if there is a stored size for the row it is used.\n * Otherwise the size is computed by streaming the node's\n * relationship with this.#relationshipName (this computed size is also\n * stored).\n */\n *#filter(node: Node, exists?: boolean): Generator<'yield', boolean> {\n exists = exists ?? (yield* this.#fetchExists(node));\n return this.#not ? !exists : exists;\n }\n\n #getCacheKey(node: Node, def: CompoundKey): string {\n const values: NormalizedValue[] = [];\n for (const key of def) {\n values.push(normalizeUndefined(node.row[key]));\n }\n return JSON.stringify(values);\n }\n\n /**\n * Pushes a change if this.#filter is true for its row.\n */\n *#pushWithFilter(change: Change, exists?: boolean): Stream<'yield'> {\n if (yield* this.#filter(change[ChangeIndex.NODE], exists)) {\n yield* this.#output.push(change, this);\n }\n }\n\n *#fetchExists(node: Node): Generator<'yield', boolean> {\n // While it seems like this should be able to fetch just 1 node\n // to check for exists, we can't because Take does not support\n // early return during initial fetch.\n return (yield* this.#fetchSize(node)) > 0;\n }\n\n *#fetchSize(node: Node): Generator<'yield', number> {\n const relationship = node.relationships[this.#relationshipName];\n assert(\n relationship,\n () =>\n `Exists: relationship \"${this.#relationshipName}\" not found on node`,\n );\n let size = 0;\n for (const n of relationship()) {\n if (n === 'yield') {\n yield 'yield';\n } else {\n size++;\n }\n }\n return size;\n }\n}\n"],"mappings":";;;;;;;;;;AAoBA,IAAa,SAAb,MAA8C;CAC5C;CACA;CACA;CACA;CACA;CACA;CACA;CACA,UAAwB;;;;;;;;;CAUxB,UAAU;CAEV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AAKR,OAAO,CAAC,SAAS;CAC/B;CAEA,aAAa,MAAY,KAA0B;EACjD,MAAM,SAA4B,CAAC;EACnC,KAAK,MAAM,OAAO,KAChB,OAAO,KAAK,mBAAmB,KAAK,IAAI,IAAI,CAAC;EAE/C,OAAO,KAAK,UAAU,MAAM;CAC9B;;;;CAKA,CAACU,gBAAgB,QAAgB,QAAmC;EAClE,IAAI,OAAO,KAAKD,QAAQ,OAAO,IAAmB,MAAM,GACtD,OAAO,KAAKJ,QAAQ,KAAK,QAAQ,IAAI;CAEzC;CAEA,CAACG,aAAa,MAAyC;EAIrD,QAAQ,OAAO,KAAKG,WAAW,IAAI,KAAK;CAC1C;CAEA,CAACA,WAAW,MAAwC;EAClD,MAAM,eAAe,KAAK,cAAc,KAAKZ;EAC7C,OACE,oBAEE,yBAAyB,KAAKA,kBAAkB,oBACpD;EACA,IAAI,OAAO;EACX,KAAK,MAAM,KAAK,aAAa,GAC3B,IAAI,MAAM,SACR,MAAM;OAEN;EAGJ,OAAO;CACT;AACF"}
1
+ {"version":3,"file":"exists.js","names":["#input","#relationshipName","#not","#parentJoinKey","#noSizeReuse","#cache","#cacheHitCountsForTesting","#output","#inPush","#getCacheKey","#fetchExists","#filter","#pushWithFilter","#fetchSize"],"sources":["../../../../../zql/src/ivm/exists.ts"],"sourcesContent":["import {areEqual} from '../../../shared/src/arrays.ts';\nimport {assert, unreachable} from '../../../shared/src/asserts.ts';\nimport type {CompoundKey} from '../../../zero-protocol/src/ast.ts';\nimport {ChangeIndex} from './change-index.ts';\nimport {ChangeType} from './change-type.ts';\nimport {makeAddChange, makeRemoveChange, type Change} from './change.ts';\nimport {normalizeUndefined, type Node, type NormalizedValue} from './data.ts';\nimport {\n throwFilterOutput,\n type FilterInput,\n type FilterOperator,\n type FilterOutput,\n} from './filter-operators.ts';\nimport type {SourceSchema} from './schema.ts';\nimport {type Stream} from './stream.ts';\n\n/**\n * The Exists operator filters data based on whether or not a relationship is\n * non-empty.\n */\nexport class Exists implements FilterOperator {\n readonly #input: FilterInput;\n readonly #relationshipName: string;\n readonly #not: boolean;\n readonly #parentJoinKey: CompoundKey;\n readonly #noSizeReuse: boolean;\n #cache: Map<string, boolean>;\n #cacheHitCountsForTesting: Map<string, number> | undefined;\n #output: FilterOutput = throwFilterOutput;\n\n /**\n * This instance variable is `true` when this operator is processing a `push`,\n * and is used to disable reuse of cached sizes across rows with the\n * same parent join key value.\n * This is necessary because during a push relationships can be inconsistent\n * due to push communicating changes (which may change multiple Nodes) one\n * Node at a time.\n */\n #inPush = false;\n\n constructor(\n input: FilterInput,\n relationshipName: string,\n parentJoinKey: CompoundKey,\n type: 'EXISTS' | 'NOT EXISTS',\n cacheHitCountsForTesting?: Map<string, number>,\n ) {\n this.#input = input;\n this.#relationshipName = relationshipName;\n this.#input.setFilterOutput(this);\n this.#cache = new Map();\n this.#cacheHitCountsForTesting = cacheHitCountsForTesting;\n assert(\n this.#input.getSchema().relationships[relationshipName],\n // log-leak-ignore -- relationship name is schema, allowed in errors\n `Input schema missing ${relationshipName}`,\n );\n this.#not = type === 'NOT EXISTS';\n this.#parentJoinKey = parentJoinKey;\n\n // If the parentJoinKey is the primary key, no sense in trying to reuse.\n this.#noSizeReuse = areEqual(\n parentJoinKey,\n this.#input.getSchema().primaryKey,\n );\n }\n\n setFilterOutput(output: FilterOutput): void {\n this.#output = output;\n }\n\n beginFilter() {\n this.#output.beginFilter();\n }\n\n endFilter() {\n this.#cache = new Map();\n this.#output.endFilter();\n }\n\n *filter(node: Node): IterableIterator<'yield', boolean> {\n let exists: boolean | undefined;\n if (!this.#noSizeReuse && !this.#inPush) {\n const key = this.#getCacheKey(node, this.#parentJoinKey);\n exists = this.#cache.get(key);\n if (exists === undefined) {\n exists = yield* this.#fetchExists(node);\n this.#cache.set(key, exists);\n } else if (this.#cacheHitCountsForTesting) {\n this.#cacheHitCountsForTesting.set(\n key,\n (this.#cacheHitCountsForTesting.get(key) ?? 0) + 1,\n );\n }\n }\n\n const result =\n (yield* this.#filter(node, exists)) && (yield* this.#output.filter(node));\n return result;\n }\n\n destroy(): void {\n this.#input.destroy();\n }\n\n getSchema(): SourceSchema {\n return this.#input.getSchema();\n }\n\n *push(change: Change): Stream<'yield'> {\n assert(!this.#inPush, 'Unexpected re-entrancy');\n this.#inPush = true;\n try {\n switch (change[ChangeIndex.TYPE]) {\n // add, remove and edit cannot change the size of the\n // this.#relationshipName relationship, so simply #pushWithFilter\n case ChangeType.ADD:\n case ChangeType.EDIT:\n case ChangeType.REMOVE: {\n yield* this.#pushWithFilter(change);\n return;\n }\n case ChangeType.CHILD:\n // Only add and remove child changes for the\n // this.#relationshipName relationship, can change the size\n // of the this.#relationshipName relationship, for other\n // child changes simply #pushWithFilter\n if (\n change[ChangeIndex.CHILD_DATA].relationshipName !==\n this.#relationshipName ||\n change[ChangeIndex.CHILD_DATA].change[ChangeIndex.TYPE] ===\n ChangeType.EDIT ||\n change[ChangeIndex.CHILD_DATA].change[ChangeIndex.TYPE] ===\n ChangeType.CHILD\n ) {\n yield* this.#pushWithFilter(change);\n return;\n }\n switch (change[ChangeIndex.CHILD_DATA].change[ChangeIndex.TYPE]) {\n case ChangeType.ADD: {\n const size = yield* this.#fetchSize(change[ChangeIndex.NODE]);\n if (size === 1) {\n if (this.#not) {\n // Since the add child change currently being processed is not\n // pushed to output, the added child needs to be excluded from\n // the remove being pushed to output (since the child has\n // never been added to the output).\n yield* this.#output.push(\n makeRemoveChange({\n row: change[ChangeIndex.NODE].row,\n relationships: {\n ...change[ChangeIndex.NODE].relationships,\n [this.#relationshipName]: () => [],\n },\n }),\n this,\n );\n } else {\n yield* this.#output.push(\n makeAddChange(change[ChangeIndex.NODE]),\n this,\n );\n }\n } else {\n yield* this.#pushWithFilter(change, size > 0);\n }\n return;\n }\n case ChangeType.REMOVE: {\n const size = yield* this.#fetchSize(change[ChangeIndex.NODE]);\n if (size === 0) {\n if (this.#not) {\n yield* this.#output.push(\n makeAddChange(change[ChangeIndex.NODE]),\n this,\n );\n } else {\n // Since the remove child change currently being processed is\n // not pushed to output, the removed child needs to be added to\n // the remove being pushed to output.\n yield* this.#output.push(\n makeRemoveChange({\n row: change[ChangeIndex.NODE].row,\n relationships: {\n ...change[ChangeIndex.NODE].relationships,\n [this.#relationshipName]: () => [\n change[ChangeIndex.CHILD_DATA].change[\n ChangeIndex.NODE\n ],\n ],\n },\n }),\n this,\n );\n }\n } else {\n yield* this.#pushWithFilter(change, size > 0);\n }\n return;\n }\n }\n return;\n default:\n unreachable(change);\n }\n } finally {\n this.#inPush = false;\n }\n }\n\n /**\n * Returns whether or not the node's this.#relationshipName\n * relationship passes the exist/not exists filter condition.\n * If the optional `size` is passed it is used.\n * Otherwise, if there is a stored size for the row it is used.\n * Otherwise the size is computed by streaming the node's\n * relationship with this.#relationshipName (this computed size is also\n * stored).\n */\n *#filter(node: Node, exists?: boolean): IterableIterator<'yield', boolean> {\n exists = exists ?? (yield* this.#fetchExists(node));\n return this.#not ? !exists : exists;\n }\n\n #getCacheKey(node: Node, def: CompoundKey): string {\n const values: NormalizedValue[] = [];\n for (const key of def) {\n values.push(normalizeUndefined(node.row[key]));\n }\n return JSON.stringify(values);\n }\n\n /**\n * Pushes a change if this.#filter is true for its row.\n */\n *#pushWithFilter(change: Change, exists?: boolean): Stream<'yield'> {\n if (yield* this.#filter(change[ChangeIndex.NODE], exists)) {\n yield* this.#output.push(change, this);\n }\n }\n\n *#fetchExists(node: Node): IterableIterator<'yield', boolean> {\n // While it seems like this should be able to fetch just 1 node\n // to check for exists, we can't because Take does not support\n // early return during initial fetch.\n return (yield* this.#fetchSize(node)) > 0;\n }\n\n *#fetchSize(node: Node): IterableIterator<'yield', number> {\n const relationship = node.relationships[this.#relationshipName];\n assert(\n relationship,\n () =>\n `Exists: relationship \"${this.#relationshipName}\" not found on node`,\n );\n let size = 0;\n for (const n of relationship()) {\n if (n === 'yield') {\n yield 'yield';\n } else {\n size++;\n }\n }\n return size;\n }\n}\n"],"mappings":";;;;;;;;;;AAoBA,I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;;;CAWA,CAACG,QAAQ,MAAY,QAAsD;EACzE,SAAS,WAAW,OAAO,KAAKD,aAAa,IAAI;EACjD,OAAO,KAAKR,OAAO,CAAC,SAAS;CAC/B;CAEA,aAAa,MAAY,KAA0B;EACjD,MAAM,SAA4B,CAAC;EACnC,KAAK,MAAM,OAAO,KAChB,OAAO,KAAK,mBAAmB,KAAK,IAAI,IAAI,CAAC;EAE/C,OAAO,KAAK,UAAU,MAAM;CAC9B;;;;CAKA,CAACU,gBAAgB,QAAgB,QAAmC;EAClE,IAAI,OAAO,KAAKD,QAAQ,OAAO,IAAmB,MAAM,GACtD,OAAO,KAAKJ,QAAQ,KAAK,QAAQ,IAAI;CAEzC;CAEA,CAACG,aAAa,MAAgD;EAI5D,QAAQ,OAAO,KAAKG,WAAW,IAAI,KAAK;CAC1C;CAEA,CAACA,WAAW,MAA+C;EACzD,MAAM,eAAe,KAAK,cAAc,KAAKZ;EAC7C,OACE,oBAEE,yBAAyB,KAAKA,kBAAkB,oBACpD;EACA,IAAI,OAAO;EACX,KAAK,MAAM,KAAK,aAAa,GAC3B,IAAI,MAAM,SACR,MAAM;OAEN;EAGJ,OAAO;CACT;AACF"}
@@ -26,8 +26,8 @@ export declare class FanIn implements FilterOperator {
26
26
  getSchema(): SourceSchema;
27
27
  beginFilter(): void;
28
28
  endFilter(): void;
29
- filter(node: Node): Generator<'yield', boolean>;
29
+ filter(node: Node): IterableIterator<'yield', boolean>;
30
30
  push(change: Change): readonly never[];
31
- fanOutDonePushingToAllBranches(fanOutChangeType: ChangeType): Generator<"yield", void, any>;
31
+ fanOutDonePushingToAllBranches(fanOutChangeType: ChangeType): IterableIterator<any, undefined> | import("./stream.ts").Stream<"yield">;
32
32
  }
33
33
  //# sourceMappingURL=fan-in.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"fan-in.d.ts","sourceRoot":"","sources":["../../../../../zql/src/ivm/fan-in.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAC,UAAU,EAA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