@agentshouse/mdruntime 0.1.0-alpha.9 → 0.1.0-c1954630fadb6

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 (133) hide show
  1. package/AGENTS.md +227 -0
  2. package/README.md +44 -0
  3. package/dist/adapter.d.ts +184 -34
  4. package/dist/adapter.d.ts.map +1 -1
  5. package/dist/adapter.js +1 -10
  6. package/dist/attached-model.d.ts +11 -0
  7. package/dist/attached-model.d.ts.map +1 -0
  8. package/dist/attached-model.js +34 -0
  9. package/dist/attachment.d.ts +3 -0
  10. package/dist/attachment.d.ts.map +1 -0
  11. package/dist/attachment.js +8 -0
  12. package/dist/bootstrap.d.ts +7 -9
  13. package/dist/bootstrap.d.ts.map +1 -1
  14. package/dist/bootstrap.js +29 -28
  15. package/dist/change-set.d.ts +33 -0
  16. package/dist/change-set.d.ts.map +1 -0
  17. package/dist/change-set.js +83 -0
  18. package/dist/change.d.ts +9 -0
  19. package/dist/change.d.ts.map +1 -1
  20. package/dist/change.js +6 -5
  21. package/dist/client.d.ts +38 -32
  22. package/dist/client.d.ts.map +1 -1
  23. package/dist/client.js +114 -183
  24. package/dist/collection.d.ts +13 -16
  25. package/dist/collection.d.ts.map +1 -1
  26. package/dist/collection.js +45 -88
  27. package/dist/connection.d.ts +47 -44
  28. package/dist/connection.d.ts.map +1 -1
  29. package/dist/connection.js +1542 -1158
  30. package/dist/conversation.d.ts +44 -22
  31. package/dist/conversation.d.ts.map +1 -1
  32. package/dist/conversation.js +540 -328
  33. package/dist/durability.js +6 -6
  34. package/dist/file.d.ts +2 -2
  35. package/dist/file.d.ts.map +1 -1
  36. package/dist/file.js +48 -71
  37. package/dist/host.d.ts +12 -2
  38. package/dist/host.d.ts.map +1 -1
  39. package/dist/host.js +97 -119
  40. package/dist/identity.d.ts +1 -0
  41. package/dist/identity.d.ts.map +1 -1
  42. package/dist/identity.js +10 -9
  43. package/dist/index.d.ts +12 -11
  44. package/dist/index.d.ts.map +1 -1
  45. package/dist/index.js +1 -3
  46. package/dist/log.js +5 -6
  47. package/dist/memory.d.ts +15 -7
  48. package/dist/memory.d.ts.map +1 -1
  49. package/dist/memory.js +255 -220
  50. package/dist/model.d.ts +10 -29
  51. package/dist/model.d.ts.map +1 -1
  52. package/dist/model.js +18 -21
  53. package/dist/observation.d.ts +27 -0
  54. package/dist/observation.d.ts.map +1 -0
  55. package/dist/observation.js +95 -0
  56. package/dist/outcome.d.ts +10 -29
  57. package/dist/outcome.d.ts.map +1 -1
  58. package/dist/outcome.js +1 -23
  59. package/dist/overlay.d.ts +9 -2
  60. package/dist/overlay.d.ts.map +1 -1
  61. package/dist/overlay.js +187 -43
  62. package/dist/paths.d.ts +0 -1
  63. package/dist/paths.d.ts.map +1 -1
  64. package/dist/paths.js +12 -19
  65. package/dist/query.d.ts +215 -10
  66. package/dist/query.d.ts.map +1 -1
  67. package/dist/query.js +421 -15
  68. package/dist/reach.d.ts +40 -0
  69. package/dist/reach.d.ts.map +1 -0
  70. package/dist/reach.js +277 -0
  71. package/dist/react.d.ts +30 -0
  72. package/dist/react.d.ts.map +1 -0
  73. package/dist/react.js +62 -0
  74. package/dist/read-side.d.ts +18 -0
  75. package/dist/read-side.d.ts.map +1 -0
  76. package/dist/read-side.js +377 -0
  77. package/dist/refresh.d.ts +5 -5
  78. package/dist/refresh.d.ts.map +1 -1
  79. package/dist/refresh.js +49 -30
  80. package/dist/replica.d.ts +25 -5
  81. package/dist/replica.d.ts.map +1 -1
  82. package/dist/replica.js +227 -38
  83. package/dist/room-draft.d.ts +127 -0
  84. package/dist/room-draft.d.ts.map +1 -0
  85. package/dist/room-draft.js +329 -0
  86. package/dist/rows.d.ts +41 -0
  87. package/dist/rows.d.ts.map +1 -0
  88. package/dist/rows.js +100 -0
  89. package/dist/save.d.ts +6 -4
  90. package/dist/save.d.ts.map +1 -1
  91. package/dist/save.js +262 -350
  92. package/dist/search.d.ts +61 -0
  93. package/dist/search.d.ts.map +1 -0
  94. package/dist/search.js +125 -0
  95. package/dist/status.d.ts +4 -6
  96. package/dist/status.d.ts.map +1 -1
  97. package/dist/status.js +9 -13
  98. package/dist/store.d.ts +59 -32
  99. package/dist/store.d.ts.map +1 -1
  100. package/dist/store.js +338 -134
  101. package/dist/transfer.d.ts +5 -0
  102. package/dist/transfer.d.ts.map +1 -1
  103. package/dist/transfer.js +1 -5
  104. package/dist/wire.d.ts +51 -16
  105. package/dist/wire.d.ts.map +1 -1
  106. package/dist/wire.js +77 -108
  107. package/package.json +17 -11
  108. package/dist/adapter.js.map +0 -1
  109. package/dist/bootstrap.js.map +0 -1
  110. package/dist/change.js.map +0 -1
  111. package/dist/client.js.map +0 -1
  112. package/dist/collection.js.map +0 -1
  113. package/dist/connection.js.map +0 -1
  114. package/dist/conversation.js.map +0 -1
  115. package/dist/durability.js.map +0 -1
  116. package/dist/file.js.map +0 -1
  117. package/dist/host.js.map +0 -1
  118. package/dist/identity.js.map +0 -1
  119. package/dist/index.js.map +0 -1
  120. package/dist/log.js.map +0 -1
  121. package/dist/memory.js.map +0 -1
  122. package/dist/model.js.map +0 -1
  123. package/dist/outcome.js.map +0 -1
  124. package/dist/overlay.js.map +0 -1
  125. package/dist/paths.js.map +0 -1
  126. package/dist/query.js.map +0 -1
  127. package/dist/refresh.js.map +0 -1
  128. package/dist/replica.js.map +0 -1
  129. package/dist/save.js.map +0 -1
  130. package/dist/status.js.map +0 -1
  131. package/dist/store.js.map +0 -1
  132. package/dist/transfer.js.map +0 -1
  133. package/dist/wire.js.map +0 -1
@@ -0,0 +1,377 @@
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 reached(reach, room, document) {
54
+ return reach === null
55
+ ? []
56
+ : [or(inArray(room, [...reach.rooms]), inArray(document, [...reach.documents]))];
57
+ }
58
+ function scoped(scope, row, rooms, reach) {
59
+ return every([
60
+ eq(row.kind, 'document'),
61
+ ...(rooms === null ? [] : [inArray(row.room, [...rooms])]),
62
+ ...reached(reach, row.room, row.key),
63
+ ...(scope.types === null ? [] : [inArray(row.type, [...scope.types])]),
64
+ ...(scope.pathPrefix === null ? [] : [inArray(scope.pathPrefix, row.directories)]),
65
+ ...(scope.where === null ? [] : [conditioned(scope.where, row)]),
66
+ ]);
67
+ }
68
+ function keyed(key, row) {
69
+ return key.of === 'field' ? row.values[key.field] : row[key.of];
70
+ }
71
+ function live(query) {
72
+ return createLiveQueryCollection({
73
+ query: query,
74
+ startSync: true,
75
+ });
76
+ }
77
+ function related(q, rows, scope, field, kept, reach) {
78
+ const matched = kept === 'source' ? 'target' : 'source';
79
+ const from = q.from({ r: rows });
80
+ return from
81
+ .where(({ r }) => every([eq(r.kind, 'reference'), ...(field === null ? [] : [eq(r.field, field)])]))
82
+ .innerJoin({ m: rows }, ({ r, m }) => eq(r[matched], m.key))
83
+ .where(({ m }) => scoped(scope, m, null, reach))
84
+ .groupBy(({ r }) => r[kept])
85
+ .select(({ r }) => ({ key: r[kept] }));
86
+ }
87
+ function filtered(q, rows, plan) {
88
+ const from = q.from({ d: rows });
89
+ let query = from.where(({ d }) => scoped(plan.root, d, plan.rooms, plan.reach));
90
+ if (plan.hop !== null) {
91
+ const hop = related(q, rows, plan.hop, plan.hop.field, 'source', plan.reach);
92
+ query = query.innerJoin({ h: hop }, ({ d, h }) => eq(d.key, h.key));
93
+ }
94
+ if (plan.backlinks !== null) {
95
+ const backlinks = related(q, rows, plan.backlinks, plan.backlinks.field, 'target', plan.reach);
96
+ query = query.innerJoin({ k: backlinks }, ({ d, k }) => eq(d.key, k.key));
97
+ }
98
+ return query;
99
+ }
100
+ function named(prefix, names, source) {
101
+ return Object.fromEntries(names.map((name, index) => [`${prefix}${index}`, source[name]]));
102
+ }
103
+ function documents(q, rows, plan, shape) {
104
+ let query = filtered(q, rows, plan);
105
+ if (shape.sections !== null || shape.preamble || shape.markdown) {
106
+ query = query.leftJoin({ b: rows }, ({ d, b }) => eq(d.body, b.key));
107
+ }
108
+ for (const { key, options } of shape.orderBy) {
109
+ query = query.orderBy(({ d }) => keyed(key, d), options);
110
+ }
111
+ if (shape.orderBy.length === 0 && plan.limit !== null) {
112
+ query = query.orderBy(({ d }) => d.key, { stringSort: 'lexical' });
113
+ }
114
+ if (plan.limit !== null)
115
+ query = query.limit(plan.limit);
116
+ return query.select(({ d, b }) => ({
117
+ room: d.room,
118
+ path: d.path,
119
+ type: d.type,
120
+ ...(shape.fields === 'all' ? { fields: d.fields } : {}),
121
+ ...(Array.isArray(shape.fields) ? named('field', shape.fields, d.fields) : {}),
122
+ ...(shape.sections === 'all' ? { sections: b.sections } : {}),
123
+ ...(Array.isArray(shape.sections) ? named('section', shape.sections, b.sections) : {}),
124
+ ...(shape.preamble ? { preamble: b.preamble } : {}),
125
+ ...(shape.markdown ? { markdown: b.markdown } : {}),
126
+ ...(shape.backlinks ? { backlinks: toArray(backlinked(q, rows, d, plan.reach)) } : {}),
127
+ ...(shape.change ? { change: d.change } : {}),
128
+ }));
129
+ }
130
+ function backlinked(q, rows, document, reach) {
131
+ const from = q.from({ r: rows });
132
+ return from
133
+ .where(({ r }) => every([eq(r.target, document.key), ...reached(reach, r.room, r.source)]))
134
+ .orderBy(({ r }) => r.path, { stringSort: 'lexical' })
135
+ .orderBy(({ r }) => r.field, { stringSort: 'lexical' })
136
+ .select(({ r }) => ({ path: r.path, type: r.type, field: r.field }));
137
+ }
138
+ function groups(q, stage, shape, limit) {
139
+ const keys = shape.groupBy.map((_, index) => `key${index}`);
140
+ const from = q.from({ m: stage });
141
+ let query = from.groupBy(({ m }) => keys.map((key) => m[key]));
142
+ for (const { by, direction } of shape.orderBy) {
143
+ query = query.orderBy(({ m }) => (by === 'count' ? count(m.key) : m[keys[by]]), direction);
144
+ }
145
+ if (limit !== null && shape.orderBy.length > 0)
146
+ query = query.limit(limit);
147
+ return query.select(({ m }) => ({
148
+ ...Object.fromEntries(keys.map((key) => [key, m[key]])),
149
+ count: count(m.key),
150
+ }));
151
+ }
152
+ function built(rows, plan) {
153
+ const shape = plan.shape;
154
+ if (shape.kind === 'documents') {
155
+ const collection = live((q) => documents(q, rows, plan, shape));
156
+ return { live: collection, dispose: () => void collection.cleanup() };
157
+ }
158
+ const stage = live((q) => filtered(q, rows, plan).select(({ d }) => ({
159
+ key: d.key,
160
+ ...Object.fromEntries(shape.groupBy.map((key, index) => [`key${index}`, keyed(key, d)])),
161
+ })));
162
+ const collection = live((q) => groups(q, stage, shape, plan.limit));
163
+ return {
164
+ live: collection,
165
+ dispose: () => {
166
+ void collection.cleanup();
167
+ void stage.cleanup();
168
+ },
169
+ };
170
+ }
171
+ function present(entries) {
172
+ return Object.fromEntries(entries.filter(([, value]) => value !== undefined));
173
+ }
174
+ function part(row, prefix, names, whole) {
175
+ if (names === 'all')
176
+ return whole;
177
+ return present(names.map((name, index) => [name, row[`${prefix}${index}`]]));
178
+ }
179
+ function documentOf(row, shape) {
180
+ if (row.type === null)
181
+ return { room: row.room, path: row.path };
182
+ return {
183
+ room: row.room,
184
+ path: row.path,
185
+ type: row.type,
186
+ ...(shape.fields === null
187
+ ? {}
188
+ : { fields: part(row, 'field', shape.fields, row.fields) }),
189
+ ...(shape.sections === null
190
+ ? {}
191
+ : {
192
+ sections: part(row, 'section', shape.sections, row.sections),
193
+ }),
194
+ ...(shape.preamble ? { preamble: row.preamble } : {}),
195
+ ...(shape.markdown ? { markdown: row.markdown } : {}),
196
+ ...(shape.backlinks
197
+ ? {
198
+ backlinks: row.backlinks.map(({ path, type, field }) => ({ path, type, field })),
199
+ }
200
+ : {}),
201
+ ...(shape.change ? { change: row.change } : {}),
202
+ };
203
+ }
204
+ function keyedOf(plan, entries) {
205
+ const shape = plan.shape;
206
+ if (shape.kind === 'documents') {
207
+ return {
208
+ result: structuredClone({ documents: entries.map(([, row]) => documentOf(row, shape)) }),
209
+ keys: entries.map(([key]) => key),
210
+ };
211
+ }
212
+ const bounded = shape.orderBy.length === 0 && plan.limit !== null ? entries.slice(0, plan.limit) : entries;
213
+ return {
214
+ result: structuredClone({
215
+ groups: bounded.map(([, row]) => ({
216
+ key: shape.groupBy.map((_, index) => row[`key${index}`]),
217
+ count: row.count,
218
+ })),
219
+ }),
220
+ keys: bounded.map(([key]) => key),
221
+ };
222
+ }
223
+ export function reconciled(previous, next) {
224
+ const before = rowsOf(previous.result);
225
+ const held = new Map(previous.keys.map((key, index) => [key, before[index]]));
226
+ const changed = [];
227
+ const rows = rowsOf(next.result).map((row, index) => {
228
+ const key = next.keys[index];
229
+ const kept = held.get(key);
230
+ if (kept !== undefined && deepEquals(kept, row))
231
+ return kept;
232
+ changed.push([key, row]);
233
+ return row;
234
+ });
235
+ const present = new Set(next.keys);
236
+ const survivors = previous.keys.filter((key) => present.has(key));
237
+ const removed = previous.keys.filter((key) => !present.has(key));
238
+ const moved = survivors.length !== next.keys.length ||
239
+ survivors.some((key, index) => key !== next.keys[index]);
240
+ if (changed.length === 0 && removed.length === 0 && !moved)
241
+ return null;
242
+ return {
243
+ keyed: {
244
+ result: 'groups' in next.result
245
+ ? { groups: rows }
246
+ : { documents: rows },
247
+ keys: next.keys,
248
+ },
249
+ change: { changed, removed, order: moved ? next.keys : null },
250
+ };
251
+ }
252
+ export function createReadSide() {
253
+ configureIndexDevMode({ enabled: false });
254
+ let sync;
255
+ const rows = createCollection({
256
+ getKey: (row) => row.key,
257
+ gcTime: 0,
258
+ startSync: true,
259
+ autoIndex: 'eager',
260
+ defaultIndexType: BasicIndex,
261
+ sync: {
262
+ rowUpdateMode: 'full',
263
+ sync: (params) => {
264
+ sync = params;
265
+ params.markReady();
266
+ },
267
+ },
268
+ });
269
+ const shared = new Map();
270
+ function searched(row) {
271
+ const body = rows.get(row.body);
272
+ return {
273
+ room: row.room,
274
+ path: row.path,
275
+ type: row.type,
276
+ fields: row.fields,
277
+ preamble: body.preamble,
278
+ sections: body.sections,
279
+ };
280
+ }
281
+ function publish(entry) {
282
+ const next = reconciled(entry.result, keyedOf(entry.plan, [...entry.built.live.entries()]));
283
+ if (next === null)
284
+ return;
285
+ entry.result = next.keyed;
286
+ for (const listener of entry.listeners)
287
+ listener(next.change);
288
+ }
289
+ return {
290
+ apply(set) {
291
+ const next = new Map();
292
+ for (const change of set.documents) {
293
+ for (const row of change.before)
294
+ next.set(row.key, null);
295
+ for (const row of change.after)
296
+ next.set(row.key, row);
297
+ }
298
+ if (next.size === 0)
299
+ return;
300
+ sync.begin();
301
+ for (const [key, row] of next) {
302
+ if (row === null) {
303
+ if (rows.has(key))
304
+ sync.write({ type: 'delete', key });
305
+ continue;
306
+ }
307
+ sync.write({ type: rows.has(key) ? 'update' : 'insert', value: row });
308
+ }
309
+ sync.commit();
310
+ },
311
+ async query(plan) {
312
+ const { live: collection, dispose } = built(rows, plan);
313
+ try {
314
+ await collection.toArrayWhenReady();
315
+ return keyedOf(plan, [...collection.entries()]).result;
316
+ }
317
+ finally {
318
+ dispose();
319
+ }
320
+ },
321
+ watch(plan, identity, deliver) {
322
+ let entry = shared.get(identity);
323
+ if (entry === undefined) {
324
+ const created = {
325
+ plan,
326
+ built: built(rows, plan),
327
+ result: undefined,
328
+ listeners: new Set(),
329
+ subscription: null,
330
+ };
331
+ created.built.live.on('status:change', ({ status }) => {
332
+ if (status === 'cleaned-up' && created.listeners.size === 0)
333
+ shared.delete(identity);
334
+ });
335
+ shared.set(identity, created);
336
+ entry = created;
337
+ }
338
+ const observed = entry;
339
+ observed.listeners.add(deliver);
340
+ observed.built.live.onFirstReady(() => {
341
+ if (!observed.listeners.has(deliver))
342
+ return;
343
+ if (observed.subscription === null) {
344
+ observed.result = keyedOf(observed.plan, [...observed.built.live.entries()]);
345
+ observed.subscription = observed.built.live.subscribeChanges(() => {
346
+ publish(observed);
347
+ }, { includeInitialState: false });
348
+ }
349
+ deliver(observed.result);
350
+ });
351
+ return () => {
352
+ if (!observed.listeners.delete(deliver) || observed.listeners.size > 0)
353
+ return;
354
+ observed.subscription?.unsubscribe();
355
+ observed.subscription = null;
356
+ };
357
+ },
358
+ documents() {
359
+ return rows.toArray
360
+ .filter((row) => row.kind === 'document' && row.type !== null)
361
+ .map(searched);
362
+ },
363
+ document(room, path) {
364
+ const row = rows.get(documentKey(room, path));
365
+ return row === undefined || row.type === null ? null : searched(row);
366
+ },
367
+ close() {
368
+ for (const entry of shared.values()) {
369
+ entry.listeners.clear();
370
+ entry.subscription?.unsubscribe();
371
+ entry.built.dispose();
372
+ }
373
+ shared.clear();
374
+ void rows.cleanup();
375
+ },
376
+ };
377
+ }
package/dist/refresh.d.ts CHANGED
@@ -1,7 +1,7 @@
1
- import type { AttachedModelBinding } from './model.js';
2
- import type { Adapter, AdapterFailure, Authorization } from './adapter.js';
1
+ import type { Adapter, AdapterFailure, Authorization, RetryableRefusal } from './adapter.js';
2
+ import type { Remodel } from './bootstrap.js';
3
3
  import { type ConfirmedLayer } from './replica.js';
4
- import { type Unconfirmed } from './transfer.js';
4
+ import { type Refused, type Unconfirmed } from './transfer.js';
5
5
  export type RefreshOutcome = {
6
6
  kind: 'still-current';
7
7
  } | {
@@ -9,6 +9,6 @@ export type RefreshOutcome = {
9
9
  confirmed: ConfirmedLayer;
10
10
  } | {
11
11
  kind: 'beyond-horizon';
12
- } | Unconfirmed | AdapterFailure;
13
- export declare function refreshRoom(adapter: Adapter, authorization: Authorization, confirmed: ConfirmedLayer, binding: AttachedModelBinding | null): Promise<RefreshOutcome>;
12
+ } | RetryableRefusal | Unconfirmed | Refused | AdapterFailure;
13
+ export declare function refreshRoom(adapter: Adapter, authorization: Authorization, confirmed: ConfirmedLayer, remodel: Remodel, continuing: () => boolean): Promise<RefreshOutcome>;
14
14
  //# sourceMappingURL=refresh.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"refresh.d.ts","sourceRoot":"","sources":["../src/refresh.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,YAAY,CAAC;AACvD,OAAO,KAAK,EAAE,OAAO,EAAE,cAAc,EAAE,aAAa,EAAe,MAAM,cAAc,CAAC;AACxF,OAAO,EAAc,KAAK,cAAc,EAAE,MAAM,cAAc,CAAC;AAC/D,OAAO,EAA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1,65 +1,84 @@
1
+ import { declaredAttachments } from './attachment.js';
1
2
  import { readStaged } from './replica.js';
2
3
  import { unconfirmed } from './transfer.js';
3
- export async function refreshRoom(adapter, authorization, confirmed, binding) {
4
+ export async function refreshRoom(adapter, authorization, confirmed, remodel, continuing) {
4
5
  const locator = confirmed.record.locator;
5
6
  const fromRoomVersion = confirmed.record.roomVersion;
6
7
  const first = await adapter.refresh(authorization, {
7
8
  locator,
8
9
  selection: confirmed.record.replica.selection,
9
- fromRoomVersion
10
+ fromRoomVersion,
10
11
  });
11
- if (first.kind !== 'net-advancement') return first;
12
+ if (first.kind !== 'net-advancement')
13
+ return first;
12
14
  const roomVersion = first.roomVersion;
13
- const documents = {
14
- ...confirmed.record.documents
15
- };
16
- const revisions = {
17
- ...confirmed.record.revisions
18
- };
15
+ const models = remodel(first.models, {
16
+ collection: confirmed.record.model,
17
+ selection: confirmed.record.selectionModel,
18
+ });
19
+ const documents = { ...confirmed.record.documents };
20
+ const revisions = { ...confirmed.record.revisions };
21
+ const changes = { ...confirmed.record.changes };
22
+ const written = { ...confirmed.record.written };
19
23
  const binaryTargets = new Set(confirmed.record.binaryTargets);
20
- const stage = (page)=>{
24
+ const attachments = [];
25
+ const stage = (page) => {
26
+ if (page.attachments !== undefined)
27
+ attachments.push(page.attachments);
21
28
  Object.assign(documents, page.documents);
22
29
  Object.assign(revisions, page.revisions);
23
- for (const path of Object.keys(page.documents))binaryTargets.delete(path);
24
- for (const target of page.binaryTargets){
30
+ for (const path of Object.keys(page.documents)) {
31
+ binaryTargets.delete(path);
32
+ delete changes[path];
33
+ written[path] = roomVersion;
34
+ }
35
+ for (const target of page.binaryTargets) {
25
36
  delete documents[target];
26
37
  delete revisions[target];
38
+ delete changes[target];
39
+ delete written[target];
27
40
  binaryTargets.add(target);
28
41
  }
29
- for (const path of page.removals){
42
+ Object.assign(changes, page.changes);
43
+ for (const path of page.removals) {
30
44
  delete documents[path];
31
45
  delete revisions[path];
46
+ delete changes[path];
47
+ delete written[path];
32
48
  binaryTargets.delete(path);
33
49
  }
34
50
  };
35
51
  stage(first);
36
52
  let pageToken = first.pageToken;
37
- while(pageToken !== undefined){
53
+ while (pageToken !== undefined) {
54
+ if (!continuing())
55
+ return unconfirmed;
38
56
  const page = await adapter.refresh(authorization, {
39
57
  locator,
40
58
  selection: confirmed.record.replica.selection,
41
59
  fromRoomVersion,
42
- pageToken
60
+ pageToken,
43
61
  });
44
- if (page.kind === 'still-current' || page.kind === 'beyond-horizon') return unconfirmed;
45
- if (page.kind !== 'net-advancement') return page;
46
- if (page.roomVersion !== roomVersion) return unconfirmed;
62
+ if (page.kind === 'still-current' || page.kind === 'beyond-horizon')
63
+ return unconfirmed;
64
+ if (page.kind !== 'net-advancement')
65
+ return page;
66
+ if (page.roomVersion !== roomVersion)
67
+ return unconfirmed;
47
68
  stage(page);
48
69
  pageToken = page.pageToken;
49
70
  }
50
- const advanced = readStaged({
71
+ const staged = readStaged({
51
72
  ...confirmed.record,
73
+ model: models.collection,
74
+ selectionModel: models.selection,
52
75
  roomVersion,
53
76
  documents,
54
- binaryTargets: [
55
- ...binaryTargets
56
- ].sort(),
57
- revisions
58
- }, binding);
59
- return advanced === null ? unconfirmed : {
60
- kind: 'advanced',
61
- confirmed: advanced
62
- };
77
+ binaryTargets: [...binaryTargets].sort(),
78
+ revisions,
79
+ changes,
80
+ written,
81
+ attachments: attachments.length === 0 ? confirmed.record.attachments : declaredAttachments(attachments),
82
+ }, confirmed);
83
+ return staged.kind === 'staged' ? { kind: 'advanced', confirmed: staged.confirmed } : staged;
63
84
  }
64
-
65
- //# sourceMappingURL=refresh.js.map
package/dist/replica.d.ts CHANGED
@@ -1,8 +1,9 @@
1
- import type { AttachedModelBinding } from './model.js';
2
1
  import { type ChangeBatch } from './change.js';
3
2
  import { type Collection, type Composition } from './collection.js';
4
3
  import { type Layer } from './overlay.js';
4
+ import type { RoomAttachment } from './adapter.js';
5
5
  import type { ReplicaRecord } from './store.js';
6
+ import type { Refused } from './transfer.js';
6
7
  export type ConfirmedLayer = {
7
8
  record: ReplicaRecord;
8
9
  collection: Collection;
@@ -10,14 +11,33 @@ export type ConfirmedLayer = {
10
11
  export type VisibleLayer = Layer & {
11
12
  collection: Collection;
12
13
  };
14
+ export type Staged = {
15
+ kind: 'staged';
16
+ confirmed: ConfirmedLayer;
17
+ } | Refused;
13
18
  export type Replica = {
14
19
  confirmed: ConfirmedLayer;
15
20
  queue: readonly ChangeBatch[];
16
21
  visible: VisibleLayer;
22
+ replay: readonly ReplayStep[];
23
+ };
24
+ type ReplayStep = {
25
+ id: string;
26
+ before: Layer;
27
+ after: Layer;
28
+ effective: ChangeBatch;
29
+ collection?: Collection;
17
30
  };
18
31
  export declare function compositionOf(record: ReplicaRecord): Composition;
19
- export declare function readConfirmed(record: ReplicaRecord): ConfirmedLayer | null;
20
- export declare function readStaged(record: ReplicaRecord, binding: AttachedModelBinding | null): ConfirmedLayer | null;
21
- export declare function applyReturned(confirmed: ConfirmedLayer, documents: Readonly<Record<string, string>>, revisions: Readonly<Record<string, string>>, removals: readonly string[], roomVersion: string, binding: AttachedModelBinding | null): ConfirmedLayer | null;
22
- export declare function replicaOf(confirmed: ConfirmedLayer, queue: readonly ChangeBatch[]): Replica;
32
+ export declare function readConfirmed(record: ReplicaRecord, previous?: ConfirmedLayer): Staged;
33
+ export declare function sameMembership(record: ReplicaRecord, other: ReplicaRecord): boolean;
34
+ export declare function readStaged(record: ReplicaRecord, previous?: ConfirmedLayer): Staged;
35
+ export declare function readReclassified(record: ReplicaRecord, previous: ConfirmedLayer): Staged | {
36
+ kind: 'deferred';
37
+ };
38
+ export declare function applyReturned(confirmed: ConfirmedLayer, documents: Readonly<Record<string, string>>, revisions: Readonly<Record<string, string>>, removals: readonly string[], attachments: readonly RoomAttachment[] | undefined, roomVersion: string, writtenAt: string): Staged;
39
+ export declare function replicaOf(confirmed: ConfirmedLayer, queue: readonly ChangeBatch[], previous?: Replica): Replica;
40
+ export declare function landed(batch: ChangeBatch, confirmed: ConfirmedLayer): boolean;
41
+ export declare function withdrawals(confirmed: ConfirmedLayer, queue: readonly ChangeBatch[], withdrawable: (batch: ChangeBatch) => boolean): readonly ChangeBatch[];
42
+ export {};
23
43
  //# sourceMappingURL=replica.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"replica.d.ts","sourceRoot":"","sources":["../src/replica.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,YAAY,CAAC;AACvD,OAAO,EAAa,KAAK,WAAW,EAAE,MAAM,aAAa,CAAC;AAC1D,OAAO,EAIL,KAAK,UAAU,EACf,KAAK,WAAW,EACjB,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAAW,KAAK,KAAK,EAAE,MAAM,cAAc,CAAC;AACnD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAEhD,MAAM,MAAM,cAAc,GAAG;IAC3B,MAAM,EAAE,aAAa,CAAC;IACtB,UAAU,EAAE,UAAU,CAAC;CACxB,CAAC;AAEF,MAAM,MAAM,YAAY,GAAG,KAAK,GAAG;IACjC,UAAU,EAAE,UAAU,CAAC;CACxB,CAAC;AAEF,MAAM,MAAM,OAAO,GAAG;IACpB,SAAS,EAAE,cAAc,CAAC;IAC1B,KAAK,EAAE,SAAS,WAAW,EAAE,CAAC;IAC9B,OAAO,EAAE,YAAY,CAAC;CACvB,CAAC;AAEF,wBAAgB,aAAa,CAAC,MAAM,EAAE,aAAa,GAAG,WAAW,CAShE;AAED,wBAAgB,aAAa,CAAC,MAAM,EAAE,aAAa,GAAG,cAAc,GAAG,IAAI,CAG1E;AAED,wBAAgB,UAAU,CACxB,MAAM,EAAE,aAAa,EACrB,OAAO,EAAE,oBAAoB,GAAG,IAAI,GACnC,cAAc,GAAG,IAAI,CAcvB;AAED,wBAAgB,aAAa,CAC3B,SAAS,EAAE,cAAc,EACzB,SAAS,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC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