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