@agentshouse/mdruntime 0.1.0-cf28918c2bf91 → 0.1.0-cf53a3b60b194

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 (186) hide show
  1. package/README.md +24 -11
  2. package/dist/adapter.d.ts +188 -48
  3. package/dist/adapter.d.ts.map +1 -1
  4. package/dist/adapter.js +1 -9
  5. package/dist/attached-model.d.ts +15 -0
  6. package/dist/attached-model.d.ts.map +1 -0
  7. package/dist/attached-model.js +64 -0
  8. package/dist/attempt.d.ts +16 -0
  9. package/dist/attempt.d.ts.map +1 -0
  10. package/dist/attempt.js +70 -0
  11. package/dist/binary.d.ts +18 -0
  12. package/dist/binary.d.ts.map +1 -0
  13. package/dist/binary.js +8 -0
  14. package/dist/bootstrap.d.ts +44 -7
  15. package/dist/bootstrap.d.ts.map +1 -1
  16. package/dist/bootstrap.js +224 -45
  17. package/dist/chain.d.ts +11 -0
  18. package/dist/chain.d.ts.map +1 -0
  19. package/dist/chain.js +64 -0
  20. package/dist/change-set.d.ts +9 -14
  21. package/dist/change-set.d.ts.map +1 -1
  22. package/dist/change-set.js +11 -56
  23. package/dist/change.d.ts +45 -0
  24. package/dist/change.d.ts.map +1 -1
  25. package/dist/change.js +34 -0
  26. package/dist/client.d.ts +37 -15
  27. package/dist/client.d.ts.map +1 -1
  28. package/dist/client.js +159 -29
  29. package/dist/collection.d.ts +14 -13
  30. package/dist/collection.d.ts.map +1 -1
  31. package/dist/collection.js +66 -54
  32. package/dist/compile.d.ts +16 -0
  33. package/dist/compile.d.ts.map +1 -0
  34. package/dist/compile.js +765 -0
  35. package/dist/connection.d.ts +68 -28
  36. package/dist/connection.d.ts.map +1 -1
  37. package/dist/connection.js +4091 -978
  38. package/dist/conversation.d.ts +57 -16
  39. package/dist/conversation.d.ts.map +1 -1
  40. package/dist/conversation.js +789 -243
  41. package/dist/digest.d.ts +7 -0
  42. package/dist/digest.d.ts.map +1 -0
  43. package/dist/digest.js +113 -0
  44. package/dist/durability.d.ts +5 -1
  45. package/dist/durability.d.ts.map +1 -1
  46. package/dist/durability.js +5 -0
  47. package/dist/entry.d.ts +19 -0
  48. package/dist/entry.d.ts.map +1 -0
  49. package/dist/entry.js +151 -0
  50. package/dist/expectation.d.ts +21 -0
  51. package/dist/expectation.d.ts.map +1 -0
  52. package/dist/expectation.js +70 -0
  53. package/dist/failure.d.ts +13 -0
  54. package/dist/failure.d.ts.map +1 -0
  55. package/dist/failure.js +19 -0
  56. package/dist/fence.d.ts +6 -0
  57. package/dist/fence.d.ts.map +1 -0
  58. package/dist/fence.js +10 -0
  59. package/dist/file.d.ts +2 -19
  60. package/dist/file.d.ts.map +1 -1
  61. package/dist/file.js +7 -122
  62. package/dist/fulltext.d.ts +27 -0
  63. package/dist/fulltext.d.ts.map +1 -0
  64. package/dist/fulltext.js +368 -0
  65. package/dist/host.d.ts +21 -2
  66. package/dist/host.d.ts.map +1 -1
  67. package/dist/host.js +163 -47
  68. package/dist/identity.d.ts +4 -0
  69. package/dist/identity.d.ts.map +1 -1
  70. package/dist/identity.js +16 -1
  71. package/dist/index.d.ts +19 -9
  72. package/dist/index.d.ts.map +1 -1
  73. package/dist/index.js +8 -0
  74. package/dist/live.d.ts +22 -0
  75. package/dist/live.d.ts.map +1 -0
  76. package/dist/live.js +124 -0
  77. package/dist/media.d.ts +8 -0
  78. package/dist/media.d.ts.map +1 -0
  79. package/dist/media.js +208 -0
  80. package/dist/memory.d.ts +22 -8
  81. package/dist/memory.d.ts.map +1 -1
  82. package/dist/memory.js +207 -47
  83. package/dist/messages.d.ts +28 -0
  84. package/dist/messages.d.ts.map +1 -0
  85. package/dist/messages.js +90 -0
  86. package/dist/model.d.ts +23 -14
  87. package/dist/model.d.ts.map +1 -1
  88. package/dist/model.js +22 -4
  89. package/dist/name.d.ts +4 -0
  90. package/dist/name.d.ts.map +1 -0
  91. package/dist/name.js +122 -0
  92. package/dist/observation.d.ts +21 -1
  93. package/dist/observation.d.ts.map +1 -1
  94. package/dist/observation.js +128 -17
  95. package/dist/open.d.ts +2 -0
  96. package/dist/open.d.ts.map +1 -0
  97. package/dist/open.js +1 -0
  98. package/dist/opened.d.ts +22 -0
  99. package/dist/opened.d.ts.map +1 -0
  100. package/dist/opened.js +107 -0
  101. package/dist/outcome.d.ts +73 -3
  102. package/dist/outcome.d.ts.map +1 -1
  103. package/dist/outcome.js +81 -1
  104. package/dist/overlay.d.ts +15 -5
  105. package/dist/overlay.d.ts.map +1 -1
  106. package/dist/overlay.js +136 -180
  107. package/dist/paths.d.ts +1 -1
  108. package/dist/paths.d.ts.map +1 -1
  109. package/dist/paths.js +4 -16
  110. package/dist/query.d.ts +78 -7
  111. package/dist/query.d.ts.map +1 -1
  112. package/dist/query.js +126 -43
  113. package/dist/reach.d.ts +54 -0
  114. package/dist/reach.d.ts.map +1 -0
  115. package/dist/reach.js +448 -0
  116. package/dist/react.d.ts +43 -0
  117. package/dist/react.d.ts.map +1 -0
  118. package/dist/react.js +54 -0
  119. package/dist/readiness.d.ts +64 -0
  120. package/dist/readiness.d.ts.map +1 -0
  121. package/dist/readiness.js +33 -0
  122. package/dist/reads.d.ts +11 -0
  123. package/dist/reads.d.ts.map +1 -0
  124. package/dist/reads.js +100 -0
  125. package/dist/recovery.d.ts +25 -0
  126. package/dist/recovery.d.ts.map +1 -0
  127. package/dist/recovery.js +198 -0
  128. package/dist/refresh.d.ts +19 -7
  129. package/dist/refresh.d.ts.map +1 -1
  130. package/dist/refresh.js +125 -53
  131. package/dist/replica.d.ts +59 -33
  132. package/dist/replica.d.ts.map +1 -1
  133. package/dist/replica.js +322 -156
  134. package/dist/room-draft.d.ts +44 -9
  135. package/dist/room-draft.d.ts.map +1 -1
  136. package/dist/room-draft.js +191 -40
  137. package/dist/rows.d.ts +39 -34
  138. package/dist/rows.d.ts.map +1 -1
  139. package/dist/rows.js +195 -81
  140. package/dist/save.d.ts +74 -6
  141. package/dist/save.d.ts.map +1 -1
  142. package/dist/save.js +898 -181
  143. package/dist/search.d.ts +41 -29
  144. package/dist/search.d.ts.map +1 -1
  145. package/dist/search.js +37 -105
  146. package/dist/seed.d.ts +16 -0
  147. package/dist/seed.d.ts.map +1 -0
  148. package/dist/seed.js +75 -0
  149. package/dist/shared.d.ts +16 -0
  150. package/dist/shared.d.ts.map +1 -0
  151. package/dist/shared.js +431 -0
  152. package/dist/status.d.ts +11 -2
  153. package/dist/status.d.ts.map +1 -1
  154. package/dist/status.js +3 -9
  155. package/dist/store.d.ts +132 -59
  156. package/dist/store.d.ts.map +1 -1
  157. package/dist/store.js +379 -344
  158. package/dist/stored.d.ts +154 -0
  159. package/dist/stored.d.ts.map +1 -0
  160. package/dist/stored.js +578 -0
  161. package/dist/tables.d.ts +362 -0
  162. package/dist/tables.d.ts.map +1 -0
  163. package/dist/tables.js +627 -0
  164. package/dist/turn.d.ts +9 -0
  165. package/dist/turn.d.ts.map +1 -0
  166. package/dist/turn.js +75 -0
  167. package/dist/validation.d.ts +30 -0
  168. package/dist/validation.d.ts.map +1 -0
  169. package/dist/validation.js +162 -0
  170. package/dist/windows.d.ts +22 -0
  171. package/dist/windows.d.ts.map +1 -0
  172. package/dist/windows.js +89 -0
  173. package/dist/wire.d.ts +194 -19
  174. package/dist/wire.d.ts.map +1 -1
  175. package/dist/wire.js +90 -61
  176. package/dist/words.d.ts +19 -0
  177. package/dist/words.d.ts.map +1 -0
  178. package/dist/words.js +156 -0
  179. package/package.json +33 -10
  180. package/AGENTS.md +0 -207
  181. package/dist/attachment.d.ts +0 -3
  182. package/dist/attachment.d.ts.map +0 -1
  183. package/dist/attachment.js +0 -8
  184. package/dist/read-side.d.ts +0 -14
  185. package/dist/read-side.d.ts.map +0 -1
  186. package/dist/read-side.js +0 -371
@@ -1,14 +0,0 @@
1
- import type { ChangeSet } from './change-set.js';
2
- import { type KeyedResult, type QueryChange } from './observation.js';
3
- import type { Plan, QueryResult } from './query.js';
4
- import type { SearchedDocument } from './search.js';
5
- export type ReadSide = {
6
- apply(set: ChangeSet): void;
7
- query(plan: Plan): Promise<QueryResult>;
8
- watch(plan: Plan, identity: string, deliver: (message: KeyedResult | QueryChange) => void): () => void;
9
- documents(): readonly SearchedDocument[];
10
- document(room: string, path: string): SearchedDocument | null;
11
- close(): void;
12
- };
13
- export declare function createReadSide(): ReadSide;
14
- //# sourceMappingURL=read-side.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"read-side.d.ts","sourceRoot":"","sources":["../src/read-side.ts"],"names":[],"mappings":"AA2BA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AACjD,OAAO,EAAU,KAAK,WAAW,EAAE,KAAK,WAAW,EAAe,MAAM,kBAAkB,CAAC;AAC3F,OAAO,KAAK,EAOV,IAAI,EAGJ,WAAW,EAIZ,MAAM,YAAY,CAAC;AAEpB,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAqDpD,MAAM,MAAM,QAAQ,GAAG;IACrB,KAAK,CAAC,GAAG,EAAE,SAAS,GAAG,IAAI,CAAC;IAC5B,KAAK,CAAC,IAAI,EAAE,IAAI,GAAG,OAAO,CAAC,WAAW,CAAC,CAAC;IACxC,KAAK,CACH,IAAI,EAAE,IAAI,EACV,QAAQ,EAAE,MAAM,EAChB,OAAO,EAAE,CAAC,OAAO,EAAE,WAAW,GAAG,WAAW,KAAK,IAAI,GACpD,MAAM,IAAI,CAAC;IACd,SAAS,IAAI,SAAS,gBAAgB,EAAE,CAAC;IACzC,QAAQ,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,gBAAgB,GAAG,IAAI,CAAC;IAC9D,KAAK,IAAI,IAAI,CAAC;CACf,CAAC;AAySF,wBAAgB,cAAc,IAAI,QAAQ,CA4HzC"}
package/dist/read-side.js DELETED
@@ -1,371 +0,0 @@
1
- import { and, BasicIndex, configureIndexDevMode, count, createCollection, createLiveQueryCollection, deepEquals, eq, gt, gte, inArray, isNull, isUndefined, lt, lte, not, or, toArray, } from '@tanstack/db';
2
- import { rowsOf } from './observation.js';
3
- import { documentKey } from './rows.js';
4
- function every(expressions) {
5
- const [first, second, ...rest] = expressions;
6
- return second === undefined ? first : and(first, second, ...rest);
7
- }
8
- function some(expressions) {
9
- const [first, second, ...rest] = expressions;
10
- return second === undefined ? first : or(first, second, ...rest);
11
- }
12
- function equal(field, value) {
13
- return or(eq(field, value), inArray(value, field));
14
- }
15
- function tested(test, field, size, written) {
16
- if (test.op === 'eq')
17
- return test.value === null ? isNull(field) : equal(field, test.value);
18
- if (test.op === 'ne') {
19
- return test.value === null
20
- ? not(isNull(field))
21
- : or(isUndefined(field), isNull(field), not(equal(field, test.value)));
22
- }
23
- if (test.op === 'in' || test.op === 'nin') {
24
- const matches = test.values.length === 0
25
- ? inArray(field, [])
26
- : some(test.values.map((value) => (value === null ? isNull(field) : equal(field, value))));
27
- if (test.op === 'in')
28
- return matches;
29
- return some([
30
- isUndefined(field),
31
- ...(test.values.includes(null) ? [] : [isNull(field)]),
32
- not(matches),
33
- ]);
34
- }
35
- if (test.op === 'range') {
36
- const compare = { gt, gte, lt, lte };
37
- return every(test.bounds.map(([operator, bound]) => compare[operator](field, bound)));
38
- }
39
- if (test.op === 'all') {
40
- return every([not(isUndefined(size)), ...test.values.map((value) => inArray(value, field))]);
41
- }
42
- if (test.op === 'size')
43
- return eq(size, test.size);
44
- return test.exists ? not(isUndefined(written)) : isUndefined(written);
45
- }
46
- function conditioned(condition, row) {
47
- if ('every' in condition)
48
- return every(condition.every.map((item) => conditioned(item, row)));
49
- if ('some' in condition)
50
- return some(condition.some.map((item) => conditioned(item, row)));
51
- return tested(condition.test, row.values[condition.field], row.sizes[condition.field], row.fields[condition.field]);
52
- }
53
- function scoped(scope, row, rooms) {
54
- return every([
55
- eq(row.kind, 'document'),
56
- ...(rooms === null ? [] : [inArray(row.room, [...rooms])]),
57
- ...(scope.types === null ? [] : [inArray(row.type, [...scope.types])]),
58
- ...(scope.pathPrefix === null ? [] : [inArray(scope.pathPrefix, row.directories)]),
59
- ...(scope.where === null ? [] : [conditioned(scope.where, row)]),
60
- ]);
61
- }
62
- function keyed(key, row) {
63
- return key.of === 'field' ? row.values[key.field] : row[key.of];
64
- }
65
- function live(query) {
66
- return createLiveQueryCollection({
67
- query: query,
68
- startSync: true,
69
- });
70
- }
71
- function related(q, rows, scope, field, kept) {
72
- const matched = kept === 'source' ? 'target' : 'source';
73
- const from = q.from({ r: rows });
74
- return from
75
- .where(({ r }) => every([eq(r.kind, 'reference'), ...(field === null ? [] : [eq(r.field, field)])]))
76
- .innerJoin({ m: rows }, ({ r, m }) => eq(r[matched], m.key))
77
- .where(({ m }) => scoped(scope, m, null))
78
- .groupBy(({ r }) => r[kept])
79
- .select(({ r }) => ({ key: r[kept] }));
80
- }
81
- function filtered(q, rows, plan) {
82
- const from = q.from({ d: rows });
83
- let query = from.where(({ d }) => scoped(plan.root, d, plan.rooms));
84
- if (plan.hop !== null) {
85
- const hop = related(q, rows, plan.hop, plan.hop.field, 'source');
86
- query = query.innerJoin({ h: hop }, ({ d, h }) => eq(d.key, h.key));
87
- }
88
- if (plan.backlinks !== null) {
89
- const backlinks = related(q, rows, plan.backlinks, plan.backlinks.field, 'target');
90
- query = query.innerJoin({ k: backlinks }, ({ d, k }) => eq(d.key, k.key));
91
- }
92
- return query;
93
- }
94
- function named(prefix, names, source) {
95
- return Object.fromEntries(names.map((name, index) => [`${prefix}${index}`, source[name]]));
96
- }
97
- function documents(q, rows, plan, shape) {
98
- let query = filtered(q, rows, plan);
99
- if (shape.sections !== null || shape.preamble || shape.markdown) {
100
- query = query.leftJoin({ b: rows }, ({ d, b }) => eq(d.body, b.key));
101
- }
102
- for (const { key, options } of shape.orderBy) {
103
- query = query.orderBy(({ d }) => keyed(key, d), options);
104
- }
105
- if (shape.orderBy.length === 0 && plan.limit !== null) {
106
- query = query.orderBy(({ d }) => d.key, { stringSort: 'lexical' });
107
- }
108
- if (plan.limit !== null)
109
- query = query.limit(plan.limit);
110
- return query.select(({ d, b }) => ({
111
- room: d.room,
112
- path: d.path,
113
- type: d.type,
114
- ...(shape.fields === 'all' ? { fields: d.fields } : {}),
115
- ...(Array.isArray(shape.fields) ? named('field', shape.fields, d.fields) : {}),
116
- ...(shape.sections === 'all' ? { sections: b.sections } : {}),
117
- ...(Array.isArray(shape.sections) ? named('section', shape.sections, b.sections) : {}),
118
- ...(shape.preamble ? { preamble: b.preamble } : {}),
119
- ...(shape.markdown ? { markdown: b.markdown } : {}),
120
- ...(shape.backlinks ? { backlinks: toArray(backlinked(q, rows, d)) } : {}),
121
- ...(shape.change ? { change: d.change } : {}),
122
- }));
123
- }
124
- function backlinked(q, rows, document) {
125
- const from = q.from({ r: rows });
126
- return from
127
- .where(({ r }) => eq(r.target, document.key))
128
- .orderBy(({ r }) => r.path, { stringSort: 'lexical' })
129
- .orderBy(({ r }) => r.field, { stringSort: 'lexical' })
130
- .select(({ r }) => ({ path: r.path, type: r.type, field: r.field }));
131
- }
132
- function groups(q, stage, shape, limit) {
133
- const keys = shape.groupBy.map((_, index) => `key${index}`);
134
- const from = q.from({ m: stage });
135
- let query = from.groupBy(({ m }) => keys.map((key) => m[key]));
136
- for (const { by, direction } of shape.orderBy) {
137
- query = query.orderBy(({ m }) => (by === 'count' ? count(m.key) : m[keys[by]]), direction);
138
- }
139
- if (limit !== null && shape.orderBy.length > 0)
140
- query = query.limit(limit);
141
- return query.select(({ m }) => ({
142
- ...Object.fromEntries(keys.map((key) => [key, m[key]])),
143
- count: count(m.key),
144
- }));
145
- }
146
- function built(rows, plan) {
147
- const shape = plan.shape;
148
- if (shape.kind === 'documents') {
149
- const collection = live((q) => documents(q, rows, plan, shape));
150
- return { live: collection, dispose: () => void collection.cleanup() };
151
- }
152
- const stage = live((q) => filtered(q, rows, plan).select(({ d }) => ({
153
- key: d.key,
154
- ...Object.fromEntries(shape.groupBy.map((key, index) => [`key${index}`, keyed(key, d)])),
155
- })));
156
- const collection = live((q) => groups(q, stage, shape, plan.limit));
157
- return {
158
- live: collection,
159
- dispose: () => {
160
- void collection.cleanup();
161
- void stage.cleanup();
162
- },
163
- };
164
- }
165
- function present(entries) {
166
- return Object.fromEntries(entries.filter(([, value]) => value !== undefined));
167
- }
168
- function part(row, prefix, names, whole) {
169
- if (names === 'all')
170
- return whole;
171
- return present(names.map((name, index) => [name, row[`${prefix}${index}`]]));
172
- }
173
- function documentOf(row, shape) {
174
- if (row.type === null)
175
- return { room: row.room, path: row.path };
176
- return {
177
- room: row.room,
178
- path: row.path,
179
- type: row.type,
180
- ...(shape.fields === null
181
- ? {}
182
- : { fields: part(row, 'field', shape.fields, row.fields) }),
183
- ...(shape.sections === null
184
- ? {}
185
- : {
186
- sections: part(row, 'section', shape.sections, row.sections),
187
- }),
188
- ...(shape.preamble ? { preamble: row.preamble } : {}),
189
- ...(shape.markdown ? { markdown: row.markdown } : {}),
190
- ...(shape.backlinks
191
- ? {
192
- backlinks: row.backlinks.map(({ path, type, field }) => ({ path, type, field })),
193
- }
194
- : {}),
195
- ...(shape.change ? { change: row.change } : {}),
196
- };
197
- }
198
- function keyedOf(plan, entries) {
199
- const shape = plan.shape;
200
- if (shape.kind === 'documents') {
201
- return {
202
- result: structuredClone({ documents: entries.map(([, row]) => documentOf(row, shape)) }),
203
- keys: entries.map(([key]) => key),
204
- };
205
- }
206
- const bounded = shape.orderBy.length === 0 && plan.limit !== null ? entries.slice(0, plan.limit) : entries;
207
- return {
208
- result: structuredClone({
209
- groups: bounded.map(([, row]) => ({
210
- key: shape.groupBy.map((_, index) => row[`key${index}`]),
211
- count: row.count,
212
- })),
213
- }),
214
- keys: bounded.map(([key]) => key),
215
- };
216
- }
217
- function reconciled(previous, next) {
218
- const before = rowsOf(previous.result);
219
- const held = new Map(previous.keys.map((key, index) => [key, before[index]]));
220
- const changed = [];
221
- const rows = rowsOf(next.result).map((row, index) => {
222
- const key = next.keys[index];
223
- const kept = held.get(key);
224
- if (kept !== undefined && deepEquals(kept, row))
225
- return kept;
226
- changed.push([key, row]);
227
- return row;
228
- });
229
- const present = new Set(next.keys);
230
- const survivors = previous.keys.filter((key) => present.has(key));
231
- const removed = previous.keys.filter((key) => !present.has(key));
232
- const moved = survivors.length !== next.keys.length ||
233
- survivors.some((key, index) => key !== next.keys[index]);
234
- if (changed.length === 0 && removed.length === 0 && !moved)
235
- return null;
236
- return {
237
- keyed: {
238
- result: 'groups' in next.result
239
- ? { groups: rows }
240
- : { documents: rows },
241
- keys: next.keys,
242
- },
243
- change: { changed, removed, order: moved ? next.keys : null },
244
- };
245
- }
246
- export function createReadSide() {
247
- configureIndexDevMode({ enabled: false });
248
- let sync;
249
- const rows = createCollection({
250
- getKey: (row) => row.key,
251
- gcTime: 0,
252
- startSync: true,
253
- autoIndex: 'eager',
254
- defaultIndexType: BasicIndex,
255
- sync: {
256
- rowUpdateMode: 'full',
257
- sync: (params) => {
258
- sync = params;
259
- params.markReady();
260
- },
261
- },
262
- });
263
- const shared = new Map();
264
- function searched(row) {
265
- const body = rows.get(row.body);
266
- return {
267
- room: row.room,
268
- path: row.path,
269
- type: row.type,
270
- fields: row.fields,
271
- preamble: body.preamble,
272
- sections: body.sections,
273
- };
274
- }
275
- function publish(entry) {
276
- const next = reconciled(entry.result, keyedOf(entry.plan, [...entry.built.live.entries()]));
277
- if (next === null)
278
- return;
279
- entry.result = next.keyed;
280
- for (const listener of entry.listeners)
281
- listener(next.change);
282
- }
283
- return {
284
- apply(set) {
285
- const next = new Map();
286
- for (const change of set.documents) {
287
- for (const row of change.before)
288
- next.set(row.key, null);
289
- for (const row of change.after)
290
- next.set(row.key, row);
291
- }
292
- if (next.size === 0)
293
- return;
294
- sync.begin();
295
- for (const [key, row] of next) {
296
- if (row === null) {
297
- if (rows.has(key))
298
- sync.write({ type: 'delete', key });
299
- continue;
300
- }
301
- sync.write({ type: rows.has(key) ? 'update' : 'insert', value: row });
302
- }
303
- sync.commit();
304
- },
305
- async query(plan) {
306
- const { live: collection, dispose } = built(rows, plan);
307
- try {
308
- await collection.toArrayWhenReady();
309
- return keyedOf(plan, [...collection.entries()]).result;
310
- }
311
- finally {
312
- dispose();
313
- }
314
- },
315
- watch(plan, identity, deliver) {
316
- let entry = shared.get(identity);
317
- if (entry === undefined) {
318
- const created = {
319
- plan,
320
- built: built(rows, plan),
321
- result: undefined,
322
- listeners: new Set(),
323
- subscription: null,
324
- };
325
- created.built.live.on('status:change', ({ status }) => {
326
- if (status === 'cleaned-up' && created.listeners.size === 0)
327
- shared.delete(identity);
328
- });
329
- shared.set(identity, created);
330
- entry = created;
331
- }
332
- const observed = entry;
333
- observed.listeners.add(deliver);
334
- observed.built.live.onFirstReady(() => {
335
- if (!observed.listeners.has(deliver))
336
- return;
337
- if (observed.subscription === null) {
338
- observed.result = keyedOf(observed.plan, [...observed.built.live.entries()]);
339
- observed.subscription = observed.built.live.subscribeChanges(() => {
340
- publish(observed);
341
- }, { includeInitialState: false });
342
- }
343
- deliver(observed.result);
344
- });
345
- return () => {
346
- if (!observed.listeners.delete(deliver) || observed.listeners.size > 0)
347
- return;
348
- observed.subscription?.unsubscribe();
349
- observed.subscription = null;
350
- };
351
- },
352
- documents() {
353
- return rows.toArray
354
- .filter((row) => row.kind === 'document' && row.type !== null)
355
- .map(searched);
356
- },
357
- document(room, path) {
358
- const row = rows.get(documentKey(room, path));
359
- return row === undefined || row.type === null ? null : searched(row);
360
- },
361
- close() {
362
- for (const entry of shared.values()) {
363
- entry.listeners.clear();
364
- entry.subscription?.unsubscribe();
365
- entry.built.dispose();
366
- }
367
- shared.clear();
368
- void rows.cleanup();
369
- },
370
- };
371
- }