@agentshouse/mdruntime 0.1.0-cf6041a06849c → 0.1.0-cf6e25bcd52f6
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.
- package/README.md +23 -10
- package/dist/adapter.d.ts +144 -29
- package/dist/adapter.d.ts.map +1 -1
- package/dist/attached-model.d.ts +5 -1
- package/dist/attached-model.d.ts.map +1 -1
- package/dist/attached-model.js +53 -6
- package/dist/attempt.d.ts +16 -0
- package/dist/attempt.d.ts.map +1 -0
- package/dist/attempt.js +70 -0
- package/dist/binary.d.ts +18 -0
- package/dist/binary.d.ts.map +1 -0
- package/dist/binary.js +8 -0
- package/dist/bootstrap.d.ts +41 -6
- package/dist/bootstrap.d.ts.map +1 -1
- package/dist/bootstrap.js +224 -45
- package/dist/chain.d.ts +11 -0
- package/dist/chain.d.ts.map +1 -0
- package/dist/chain.js +64 -0
- package/dist/change-set.d.ts +9 -14
- package/dist/change-set.d.ts.map +1 -1
- package/dist/change-set.js +11 -56
- package/dist/change.d.ts +45 -0
- package/dist/change.d.ts.map +1 -1
- package/dist/change.js +34 -0
- package/dist/client.d.ts +35 -14
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +156 -19
- package/dist/collection.d.ts +14 -13
- package/dist/collection.d.ts.map +1 -1
- package/dist/collection.js +66 -54
- package/dist/compile.d.ts +16 -0
- package/dist/compile.d.ts.map +1 -0
- package/dist/compile.js +765 -0
- package/dist/connection.d.ts +63 -20
- package/dist/connection.d.ts.map +1 -1
- package/dist/connection.js +3889 -743
- package/dist/conversation.d.ts +42 -12
- package/dist/conversation.d.ts.map +1 -1
- package/dist/conversation.js +642 -188
- package/dist/digest.d.ts +7 -0
- package/dist/digest.d.ts.map +1 -0
- package/dist/digest.js +113 -0
- package/dist/durability.d.ts +5 -1
- package/dist/durability.d.ts.map +1 -1
- package/dist/durability.js +5 -0
- package/dist/entry.d.ts +19 -0
- package/dist/entry.d.ts.map +1 -0
- package/dist/entry.js +151 -0
- package/dist/expectation.d.ts +21 -0
- package/dist/expectation.d.ts.map +1 -0
- package/dist/expectation.js +70 -0
- package/dist/failure.d.ts +13 -0
- package/dist/failure.d.ts.map +1 -0
- package/dist/failure.js +19 -0
- package/dist/fence.d.ts +6 -0
- package/dist/fence.d.ts.map +1 -0
- package/dist/fence.js +10 -0
- package/dist/file.d.ts +2 -19
- package/dist/file.d.ts.map +1 -1
- package/dist/file.js +7 -122
- package/dist/fulltext.d.ts +27 -0
- package/dist/fulltext.d.ts.map +1 -0
- package/dist/fulltext.js +368 -0
- package/dist/host.d.ts +15 -2
- package/dist/host.d.ts.map +1 -1
- package/dist/host.js +134 -21
- package/dist/identity.d.ts +4 -0
- package/dist/identity.d.ts.map +1 -1
- package/dist/identity.js +16 -1
- package/dist/index.d.ts +19 -9
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +8 -0
- package/dist/live.d.ts +22 -0
- package/dist/live.d.ts.map +1 -0
- package/dist/live.js +124 -0
- package/dist/media.d.ts +8 -0
- package/dist/media.d.ts.map +1 -0
- package/dist/media.js +208 -0
- package/dist/memory.d.ts +13 -5
- package/dist/memory.d.ts.map +1 -1
- package/dist/memory.js +140 -31
- package/dist/messages.d.ts +28 -0
- package/dist/messages.d.ts.map +1 -0
- package/dist/messages.js +90 -0
- package/dist/model.d.ts +20 -4
- package/dist/model.d.ts.map +1 -1
- package/dist/model.js +19 -2
- package/dist/name.d.ts +4 -0
- package/dist/name.d.ts.map +1 -0
- package/dist/name.js +122 -0
- package/dist/observation.d.ts +20 -2
- package/dist/observation.d.ts.map +1 -1
- package/dist/observation.js +113 -12
- package/dist/open.d.ts +2 -0
- package/dist/open.d.ts.map +1 -0
- package/dist/open.js +1 -0
- package/dist/opened.d.ts +22 -0
- package/dist/opened.d.ts.map +1 -0
- package/dist/opened.js +107 -0
- package/dist/outcome.d.ts +73 -3
- package/dist/outcome.d.ts.map +1 -1
- package/dist/outcome.js +81 -1
- package/dist/overlay.d.ts +15 -5
- package/dist/overlay.d.ts.map +1 -1
- package/dist/overlay.js +136 -180
- package/dist/paths.d.ts +1 -1
- package/dist/paths.d.ts.map +1 -1
- package/dist/paths.js +4 -16
- package/dist/query.d.ts +71 -5
- package/dist/query.d.ts.map +1 -1
- package/dist/query.js +100 -26
- package/dist/reach.d.ts +18 -4
- package/dist/reach.d.ts.map +1 -1
- package/dist/reach.js +247 -84
- package/dist/react.d.ts +31 -19
- package/dist/react.d.ts.map +1 -1
- package/dist/react.js +13 -21
- package/dist/readiness.d.ts +64 -0
- package/dist/readiness.d.ts.map +1 -0
- package/dist/readiness.js +33 -0
- package/dist/reads.d.ts +11 -0
- package/dist/reads.d.ts.map +1 -0
- package/dist/reads.js +100 -0
- package/dist/recovery.d.ts +25 -0
- package/dist/recovery.d.ts.map +1 -0
- package/dist/recovery.js +198 -0
- package/dist/refresh.d.ts +19 -8
- package/dist/refresh.d.ts.map +1 -1
- package/dist/refresh.js +125 -57
- package/dist/replica.d.ts +59 -33
- package/dist/replica.d.ts.map +1 -1
- package/dist/replica.js +322 -155
- package/dist/room-draft.d.ts +41 -9
- package/dist/room-draft.d.ts.map +1 -1
- package/dist/room-draft.js +186 -37
- package/dist/rows.d.ts +39 -34
- package/dist/rows.d.ts.map +1 -1
- package/dist/rows.js +197 -80
- package/dist/save.d.ts +74 -4
- package/dist/save.d.ts.map +1 -1
- package/dist/save.js +900 -178
- package/dist/search.d.ts +41 -29
- package/dist/search.d.ts.map +1 -1
- package/dist/search.js +37 -105
- package/dist/seed.d.ts +16 -0
- package/dist/seed.d.ts.map +1 -0
- package/dist/seed.js +75 -0
- package/dist/shared.d.ts +16 -0
- package/dist/shared.d.ts.map +1 -0
- package/dist/shared.js +431 -0
- package/dist/status.d.ts +11 -2
- package/dist/status.d.ts.map +1 -1
- package/dist/status.js +3 -9
- package/dist/store.d.ts +132 -58
- package/dist/store.d.ts.map +1 -1
- package/dist/store.js +379 -344
- package/dist/stored.d.ts +154 -0
- package/dist/stored.d.ts.map +1 -0
- package/dist/stored.js +578 -0
- package/dist/tables.d.ts +362 -0
- package/dist/tables.d.ts.map +1 -0
- package/dist/tables.js +627 -0
- package/dist/turn.d.ts +9 -0
- package/dist/turn.d.ts.map +1 -0
- package/dist/turn.js +75 -0
- package/dist/validation.d.ts +30 -0
- package/dist/validation.d.ts.map +1 -0
- package/dist/validation.js +162 -0
- package/dist/windows.d.ts +22 -0
- package/dist/windows.d.ts.map +1 -0
- package/dist/windows.js +89 -0
- package/dist/wire.d.ts +173 -33
- package/dist/wire.d.ts.map +1 -1
- package/dist/wire.js +69 -31
- package/dist/words.d.ts +19 -0
- package/dist/words.d.ts.map +1 -0
- package/dist/words.js +156 -0
- package/package.json +26 -15
- package/AGENTS.md +0 -231
- package/dist/attachment.d.ts +0 -3
- package/dist/attachment.d.ts.map +0 -1
- package/dist/attachment.js +0 -8
- package/dist/read-side.d.ts +0 -18
- package/dist/read-side.d.ts.map +0 -1
- package/dist/read-side.js +0 -377
package/dist/read-side.d.ts.map
DELETED
|
@@ -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,EAIJ,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;AA8RF,wBAAgB,UAAU,CACxB,QAAQ,EAAE,WAAW,EACrB,IAAI,EAAE,WAAW,GAChB;IAAE,KAAK,EAAE,WAAW,CAAC;IAAC,MAAM,EAAE,WAAW,CAAA;CAAE,GAAG,IAAI,CA4BpD;AAED,wBAAgB,cAAc,IAAI,QAAQ,CA4HzC"}
|
package/dist/read-side.js
DELETED
|
@@ -1,377 +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 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
|
-
}
|