@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.
- package/AGENTS.md +227 -0
- package/README.md +44 -0
- package/dist/adapter.d.ts +184 -34
- package/dist/adapter.d.ts.map +1 -1
- package/dist/adapter.js +1 -10
- package/dist/attached-model.d.ts +11 -0
- package/dist/attached-model.d.ts.map +1 -0
- package/dist/attached-model.js +34 -0
- package/dist/attachment.d.ts +3 -0
- package/dist/attachment.d.ts.map +1 -0
- package/dist/attachment.js +8 -0
- package/dist/bootstrap.d.ts +7 -9
- package/dist/bootstrap.d.ts.map +1 -1
- package/dist/bootstrap.js +29 -28
- package/dist/change-set.d.ts +33 -0
- package/dist/change-set.d.ts.map +1 -0
- package/dist/change-set.js +83 -0
- package/dist/change.d.ts +9 -0
- package/dist/change.d.ts.map +1 -1
- package/dist/change.js +6 -5
- package/dist/client.d.ts +38 -32
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +114 -183
- package/dist/collection.d.ts +13 -16
- package/dist/collection.d.ts.map +1 -1
- package/dist/collection.js +45 -88
- package/dist/connection.d.ts +47 -44
- package/dist/connection.d.ts.map +1 -1
- package/dist/connection.js +1542 -1158
- package/dist/conversation.d.ts +44 -22
- package/dist/conversation.d.ts.map +1 -1
- package/dist/conversation.js +540 -328
- package/dist/durability.js +6 -6
- package/dist/file.d.ts +2 -2
- package/dist/file.d.ts.map +1 -1
- package/dist/file.js +48 -71
- package/dist/host.d.ts +12 -2
- package/dist/host.d.ts.map +1 -1
- package/dist/host.js +97 -119
- package/dist/identity.d.ts +1 -0
- package/dist/identity.d.ts.map +1 -1
- package/dist/identity.js +10 -9
- package/dist/index.d.ts +12 -11
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -3
- package/dist/log.js +5 -6
- package/dist/memory.d.ts +15 -7
- package/dist/memory.d.ts.map +1 -1
- package/dist/memory.js +255 -220
- package/dist/model.d.ts +10 -29
- package/dist/model.d.ts.map +1 -1
- package/dist/model.js +18 -21
- package/dist/observation.d.ts +27 -0
- package/dist/observation.d.ts.map +1 -0
- package/dist/observation.js +95 -0
- package/dist/outcome.d.ts +10 -29
- package/dist/outcome.d.ts.map +1 -1
- package/dist/outcome.js +1 -23
- package/dist/overlay.d.ts +9 -2
- package/dist/overlay.d.ts.map +1 -1
- package/dist/overlay.js +187 -43
- package/dist/paths.d.ts +0 -1
- package/dist/paths.d.ts.map +1 -1
- package/dist/paths.js +12 -19
- package/dist/query.d.ts +215 -10
- package/dist/query.d.ts.map +1 -1
- package/dist/query.js +421 -15
- package/dist/reach.d.ts +40 -0
- package/dist/reach.d.ts.map +1 -0
- package/dist/reach.js +277 -0
- package/dist/react.d.ts +30 -0
- package/dist/react.d.ts.map +1 -0
- package/dist/react.js +62 -0
- package/dist/read-side.d.ts +18 -0
- package/dist/read-side.d.ts.map +1 -0
- package/dist/read-side.js +377 -0
- package/dist/refresh.d.ts +5 -5
- package/dist/refresh.d.ts.map +1 -1
- package/dist/refresh.js +49 -30
- package/dist/replica.d.ts +25 -5
- package/dist/replica.d.ts.map +1 -1
- package/dist/replica.js +227 -38
- package/dist/room-draft.d.ts +127 -0
- package/dist/room-draft.d.ts.map +1 -0
- package/dist/room-draft.js +329 -0
- package/dist/rows.d.ts +41 -0
- package/dist/rows.d.ts.map +1 -0
- package/dist/rows.js +100 -0
- package/dist/save.d.ts +6 -4
- package/dist/save.d.ts.map +1 -1
- package/dist/save.js +262 -350
- package/dist/search.d.ts +61 -0
- package/dist/search.d.ts.map +1 -0
- package/dist/search.js +125 -0
- package/dist/status.d.ts +4 -6
- package/dist/status.d.ts.map +1 -1
- package/dist/status.js +9 -13
- package/dist/store.d.ts +59 -32
- package/dist/store.d.ts.map +1 -1
- package/dist/store.js +338 -134
- package/dist/transfer.d.ts +5 -0
- package/dist/transfer.d.ts.map +1 -1
- package/dist/transfer.js +1 -5
- package/dist/wire.d.ts +51 -16
- package/dist/wire.d.ts.map +1 -1
- package/dist/wire.js +77 -108
- package/package.json +17 -11
- package/dist/adapter.js.map +0 -1
- package/dist/bootstrap.js.map +0 -1
- package/dist/change.js.map +0 -1
- package/dist/client.js.map +0 -1
- package/dist/collection.js.map +0 -1
- package/dist/connection.js.map +0 -1
- package/dist/conversation.js.map +0 -1
- package/dist/durability.js.map +0 -1
- package/dist/file.js.map +0 -1
- package/dist/host.js.map +0 -1
- package/dist/identity.js.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/log.js.map +0 -1
- package/dist/memory.js.map +0 -1
- package/dist/model.js.map +0 -1
- package/dist/outcome.js.map +0 -1
- package/dist/overlay.js.map +0 -1
- package/dist/paths.js.map +0 -1
- package/dist/query.js.map +0 -1
- package/dist/refresh.js.map +0 -1
- package/dist/replica.js.map +0 -1
- package/dist/save.js.map +0 -1
- package/dist/status.js.map +0 -1
- package/dist/store.js.map +0 -1
- package/dist/transfer.js.map +0 -1
- package/dist/wire.js.map +0 -1
package/dist/reach.js
ADDED
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
import { unheldView } from './conversation.js';
|
|
2
|
+
import { modelTypes, NO_MODEL } from './model.js';
|
|
3
|
+
import { applied, observing } from './observation.js';
|
|
4
|
+
import { beneath } from './paths.js';
|
|
5
|
+
import { reconciled } from './read-side.js';
|
|
6
|
+
import { moveTarget, readParts } from './room-draft.js';
|
|
7
|
+
import { documentKey } from './rows.js';
|
|
8
|
+
const UNHELD_TYPES = { collection: modelTypes(NO_MODEL), typedSelection: null };
|
|
9
|
+
const UNDECLARED = { kind: 'undeclared' };
|
|
10
|
+
function undeclared() {
|
|
11
|
+
return Promise.resolve(UNDECLARED);
|
|
12
|
+
}
|
|
13
|
+
const NO_CONVERSATION = {
|
|
14
|
+
list: () => Promise.resolve([]),
|
|
15
|
+
read: (conversation) => Promise.resolve(unheldView(conversation)),
|
|
16
|
+
create: undeclared,
|
|
17
|
+
send: undeclared,
|
|
18
|
+
answer: undeclared,
|
|
19
|
+
interrupt: undeclared,
|
|
20
|
+
kill: undeclared,
|
|
21
|
+
cancel: undeclared,
|
|
22
|
+
delete: undeclared,
|
|
23
|
+
};
|
|
24
|
+
const NO_CHAT_DRAFT = {
|
|
25
|
+
write: () => Promise.resolve(),
|
|
26
|
+
discard: () => Promise.resolve(),
|
|
27
|
+
};
|
|
28
|
+
export function holds(reach, room) {
|
|
29
|
+
return (reach === null ||
|
|
30
|
+
reach.rooms.includes(room) ||
|
|
31
|
+
reach.documents.some((document) => document.room === room));
|
|
32
|
+
}
|
|
33
|
+
function wholly(reach, room) {
|
|
34
|
+
return reach === null || reach.rooms.includes(room);
|
|
35
|
+
}
|
|
36
|
+
export function governs(reach, room) {
|
|
37
|
+
if (wholly(reach, room.locator)) {
|
|
38
|
+
return { collection: true, selection: room.typedSelection !== null };
|
|
39
|
+
}
|
|
40
|
+
const selected = (path) => room.typedSelection?.roots.some((root) => beneath(root, path)) ?? false;
|
|
41
|
+
const paths = reach.documents
|
|
42
|
+
.filter((document) => document.room === room.locator)
|
|
43
|
+
.map(({ path }) => path);
|
|
44
|
+
return { collection: paths.some((path) => !selected(path)), selection: paths.some(selected) };
|
|
45
|
+
}
|
|
46
|
+
function covers(reach, room, path) {
|
|
47
|
+
return (reach === null ||
|
|
48
|
+
reach.rooms.includes(room) ||
|
|
49
|
+
reach.documents.some((document) => document.room === room && document.path === path));
|
|
50
|
+
}
|
|
51
|
+
function conversing(reach) {
|
|
52
|
+
return reach === null || reach.conversations;
|
|
53
|
+
}
|
|
54
|
+
export function planReach(reach) {
|
|
55
|
+
return reach === null
|
|
56
|
+
? null
|
|
57
|
+
: {
|
|
58
|
+
rooms: [...new Set(reach.rooms)].sort(),
|
|
59
|
+
documents: [
|
|
60
|
+
...new Set(reach.documents.map(({ room, path }) => documentKey(room, path))),
|
|
61
|
+
].sort(),
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
function roomsWithin(reach, rooms) {
|
|
65
|
+
if (reach === null)
|
|
66
|
+
return rooms;
|
|
67
|
+
return rooms
|
|
68
|
+
.filter((room) => holds(reach, room.locator))
|
|
69
|
+
.map((room) => {
|
|
70
|
+
if (wholly(reach, room.locator))
|
|
71
|
+
return room;
|
|
72
|
+
const governed = governs(reach, room);
|
|
73
|
+
return {
|
|
74
|
+
...room,
|
|
75
|
+
model: governed.collection ? room.model : modelTypes(NO_MODEL),
|
|
76
|
+
typedSelection: governed.selection ? room.typedSelection : null,
|
|
77
|
+
roomVersion: null,
|
|
78
|
+
attachments: [],
|
|
79
|
+
};
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
function typesWithin(reach, room, types) {
|
|
83
|
+
if (room === undefined)
|
|
84
|
+
return structuredClone(UNHELD_TYPES);
|
|
85
|
+
const governed = governs(reach, room);
|
|
86
|
+
return {
|
|
87
|
+
collection: governed.collection ? types.collection : modelTypes(NO_MODEL),
|
|
88
|
+
typedSelection: governed.selection ? types.typedSelection : null,
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
function moves(reach, room, move) {
|
|
92
|
+
return move === undefined || covers(reach, room, moveTarget(move));
|
|
93
|
+
}
|
|
94
|
+
function draftWithin(reach, draft) {
|
|
95
|
+
return covers(reach, draft.room, draft.path) && moves(reach, draft.room, draft.parts.move);
|
|
96
|
+
}
|
|
97
|
+
function draftsWithin(reach, drafts) {
|
|
98
|
+
return drafts.filter((draft) => draftWithin(reach, draft));
|
|
99
|
+
}
|
|
100
|
+
function shownDraft(reach, draft) {
|
|
101
|
+
return draft !== null && draftWithin(reach, draft) ? draft : null;
|
|
102
|
+
}
|
|
103
|
+
function writeWithin(reach, write) {
|
|
104
|
+
return write.outcome === 'written'
|
|
105
|
+
? { outcome: 'written', roomDraft: shownDraft(reach, write.roomDraft) }
|
|
106
|
+
: { outcome: 'stale', current: shownDraft(reach, write.current) };
|
|
107
|
+
}
|
|
108
|
+
function savedWithin(reach, room, result) {
|
|
109
|
+
if (reach === null || result.outcome === 'saved')
|
|
110
|
+
return result;
|
|
111
|
+
const { file, location, referrers, ...refusal } = result;
|
|
112
|
+
const hidden = file !== undefined && !covers(reach, room, file);
|
|
113
|
+
return {
|
|
114
|
+
...refusal,
|
|
115
|
+
...(file === undefined || hidden ? {} : { file }),
|
|
116
|
+
...(location === undefined || hidden ? {} : { location }),
|
|
117
|
+
...(referrers === undefined
|
|
118
|
+
? {}
|
|
119
|
+
: { referrers: referrers.filter((path) => covers(reach, room, path)) }),
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
function statusWithin(reach, status) {
|
|
123
|
+
return {
|
|
124
|
+
...status,
|
|
125
|
+
attentionRequired: status.attentionRequired.filter((entry) => covers(reach, entry.room, entry.path)),
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
function queueWithin(reach, room, entries) {
|
|
129
|
+
return entries.filter(({ batch }) => batch.changes.every((change) => covers(reach, room, change.path) &&
|
|
130
|
+
(change.kind !== 'move' || covers(reach, room, change.to))));
|
|
131
|
+
}
|
|
132
|
+
function unreached(room) {
|
|
133
|
+
return new Error(`No Local Replica for ${room}`);
|
|
134
|
+
}
|
|
135
|
+
export function narrowed(runtime, given) {
|
|
136
|
+
const { md, watch } = runtime;
|
|
137
|
+
let reach = given;
|
|
138
|
+
const open = new Set();
|
|
139
|
+
function following(begin) {
|
|
140
|
+
let stop = begin();
|
|
141
|
+
const restart = () => {
|
|
142
|
+
stop();
|
|
143
|
+
stop = begin();
|
|
144
|
+
};
|
|
145
|
+
open.add(restart);
|
|
146
|
+
return () => {
|
|
147
|
+
open.delete(restart);
|
|
148
|
+
stop();
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
const within = (topic, input, deliver) => {
|
|
152
|
+
if (topic === 'rooms') {
|
|
153
|
+
return following(() => watch(topic, input, (rooms) => deliver(roomsWithin(reach, rooms))));
|
|
154
|
+
}
|
|
155
|
+
if (topic === 'sync-status') {
|
|
156
|
+
return following(() => watch(topic, input, (status) => deliver(statusWithin(reach, status))));
|
|
157
|
+
}
|
|
158
|
+
if (topic === 'room-drafts') {
|
|
159
|
+
return following(() => watch(topic, input, (drafts) => deliver(draftsWithin(reach, drafts))));
|
|
160
|
+
}
|
|
161
|
+
if (topic === 'queue') {
|
|
162
|
+
const room = input[0];
|
|
163
|
+
return following(() => {
|
|
164
|
+
if (!holds(reach, room)) {
|
|
165
|
+
deliver([]);
|
|
166
|
+
return () => { };
|
|
167
|
+
}
|
|
168
|
+
return watch(topic, input, (entries) => deliver(queueWithin(reach, room, entries)));
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
if (topic === 'query') {
|
|
172
|
+
const plan = runtime.plan(input[0], reach);
|
|
173
|
+
let held = null;
|
|
174
|
+
return following(() => {
|
|
175
|
+
let first = true;
|
|
176
|
+
return runtime.watchPlan({ ...plan, reach: planReach(reach) }, (message) => {
|
|
177
|
+
if (!first) {
|
|
178
|
+
held = applied(held, message);
|
|
179
|
+
deliver(message);
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
first = false;
|
|
183
|
+
if (held === null) {
|
|
184
|
+
held = message;
|
|
185
|
+
deliver(message);
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
const next = reconciled(held, message);
|
|
189
|
+
if (next === null)
|
|
190
|
+
return;
|
|
191
|
+
held = next.keyed;
|
|
192
|
+
deliver(next.change);
|
|
193
|
+
});
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
if (topic === 'conversation') {
|
|
197
|
+
return following(() => {
|
|
198
|
+
if (conversing(reach))
|
|
199
|
+
return watch(topic, input, deliver);
|
|
200
|
+
deliver(unheldView(input[0]));
|
|
201
|
+
return () => { };
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
return watch(topic, input, deliver);
|
|
205
|
+
};
|
|
206
|
+
const conversation = () => (conversing(reach) ? md.conversation : NO_CONVERSATION);
|
|
207
|
+
const chatDraft = () => (conversing(reach) ? md.chatDraft : NO_CHAT_DRAFT);
|
|
208
|
+
const view = {
|
|
209
|
+
...md,
|
|
210
|
+
rooms: () => roomsWithin(reach, md.rooms()),
|
|
211
|
+
query: ((input) => runtime.query(runtime.plan(input, reach))),
|
|
212
|
+
search: (input) => runtime.search(input, reach),
|
|
213
|
+
types(room) {
|
|
214
|
+
if (wholly(reach, room))
|
|
215
|
+
return md.types(room);
|
|
216
|
+
if (!holds(reach, room))
|
|
217
|
+
return structuredClone(UNHELD_TYPES);
|
|
218
|
+
const listed = md.rooms().find(({ locator }) => locator === room);
|
|
219
|
+
return typesWithin(reach, listed, md.types(room));
|
|
220
|
+
},
|
|
221
|
+
async save(room, roomDrafts, writtenBy) {
|
|
222
|
+
if (!holds(reach, room))
|
|
223
|
+
throw unreached(room);
|
|
224
|
+
const outside = roomDrafts.find(({ path }) => !covers(reach, room, path) ||
|
|
225
|
+
!moves(reach, room, md.roomDraft.read(room, path)?.parts.move));
|
|
226
|
+
if (outside !== undefined)
|
|
227
|
+
return { outcome: 'refused', rule: 'stale', file: outside.path };
|
|
228
|
+
return savedWithin(reach, room, await md.save(room, roomDrafts, writtenBy));
|
|
229
|
+
},
|
|
230
|
+
history: (room, path, query) => covers(reach, room, path)
|
|
231
|
+
? md.history(room, path, query)
|
|
232
|
+
: Promise.resolve({ kind: 'room-not-readable', locator: room }),
|
|
233
|
+
reached: (room, roomVersion, path) => (path === undefined ? wholly(reach, room) : covers(reach, room, path)) &&
|
|
234
|
+
md.reached(room, roomVersion, path),
|
|
235
|
+
log: () => (reach === null ? md.log() : ''),
|
|
236
|
+
roomDraft: {
|
|
237
|
+
list: () => md.roomDraft.list().then((drafts) => draftsWithin(reach, drafts)),
|
|
238
|
+
read: (room, path) => covers(reach, room, path) ? shownDraft(reach, md.roomDraft.read(room, path)) : null,
|
|
239
|
+
async write(room, path, revision, parts) {
|
|
240
|
+
if (!covers(reach, room, path) || !moves(reach, room, readParts(parts).move)) {
|
|
241
|
+
throw unreached(room);
|
|
242
|
+
}
|
|
243
|
+
return writeWithin(reach, await md.roomDraft.write(room, path, revision, parts));
|
|
244
|
+
},
|
|
245
|
+
async discard(room, path, revision) {
|
|
246
|
+
if (!covers(reach, room, path))
|
|
247
|
+
return { outcome: 'stale', current: null };
|
|
248
|
+
return writeWithin(reach, await md.roomDraft.discard(room, path, revision));
|
|
249
|
+
},
|
|
250
|
+
},
|
|
251
|
+
conversation: {
|
|
252
|
+
list: () => conversation().list(),
|
|
253
|
+
read: (id, options) => conversation().read(id, options),
|
|
254
|
+
create: (input) => conversation().create(input),
|
|
255
|
+
send: (id) => conversation().send(id),
|
|
256
|
+
answer: (id, answer) => conversation().answer(id, answer),
|
|
257
|
+
interrupt: (id) => conversation().interrupt(id),
|
|
258
|
+
kill: (id) => conversation().kill(id),
|
|
259
|
+
cancel: (id, turn) => conversation().cancel(id, turn),
|
|
260
|
+
delete: (id) => conversation().delete(id),
|
|
261
|
+
},
|
|
262
|
+
chatDraft: {
|
|
263
|
+
write: (id, draft) => chatDraft().write(id, draft),
|
|
264
|
+
discard: (id) => chatDraft().discard(id),
|
|
265
|
+
},
|
|
266
|
+
observe: observing(within),
|
|
267
|
+
};
|
|
268
|
+
return {
|
|
269
|
+
md: view,
|
|
270
|
+
watch: within,
|
|
271
|
+
narrow(next) {
|
|
272
|
+
reach = next;
|
|
273
|
+
for (const restart of [...open])
|
|
274
|
+
restart();
|
|
275
|
+
},
|
|
276
|
+
};
|
|
277
|
+
}
|
package/dist/react.d.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { MdRuntimeClient } from './client.js';
|
|
2
|
+
import type { AppRoom, ConnectionRefusal, SaveResult } from './connection.js';
|
|
3
|
+
import type { ServiceMode } from './adapter.js';
|
|
4
|
+
import type { ConversationOptions, ConversationView } from './conversation.js';
|
|
5
|
+
import type { QueueEntry } from './outcome.js';
|
|
6
|
+
import type { DocumentQuery, DocumentResult, GroupQuery, GroupResult } from './query.js';
|
|
7
|
+
import type { RoomDraft, RoomDraftParts } from './room-draft.js';
|
|
8
|
+
import type { SyncStatus } from './status.js';
|
|
9
|
+
type Client = MdRuntimeClient | null;
|
|
10
|
+
export declare function useObserve(client: Client, topic: 'deployment-facts'): unknown;
|
|
11
|
+
export declare function useObserve(client: Client, topic: 'rooms'): readonly AppRoom[] | undefined;
|
|
12
|
+
export declare function useObserve(client: Client, topic: 'connection-refusal'): ConnectionRefusal | null | undefined;
|
|
13
|
+
export declare function useObserve(client: Client, topic: 'storage-loss'): string | null | undefined;
|
|
14
|
+
export declare function useObserve(client: Client, topic: 'sync-status'): SyncStatus | undefined;
|
|
15
|
+
export declare function useObserve(client: Client, topic: 'service-mode'): ServiceMode | undefined;
|
|
16
|
+
export declare function useObserve(client: Client, topic: 'query', query: DocumentQuery): DocumentResult | undefined;
|
|
17
|
+
export declare function useObserve(client: Client, topic: 'query', query: GroupQuery): GroupResult | undefined;
|
|
18
|
+
export declare function useObserve(client: Client, topic: 'queue', locator: string): readonly QueueEntry[] | undefined;
|
|
19
|
+
export declare function useObserve(client: Client, topic: 'room-drafts'): readonly RoomDraft[] | undefined;
|
|
20
|
+
export declare function useObserve(client: Client, topic: 'presentation'): unknown;
|
|
21
|
+
export declare function useObserve(client: Client, topic: 'conversation', conversation: string, options: ConversationOptions): ConversationView | undefined;
|
|
22
|
+
export type Saving = {
|
|
23
|
+
readonly busy: boolean;
|
|
24
|
+
readonly outcome: SaveResult | null;
|
|
25
|
+
readonly failure: Error | null;
|
|
26
|
+
save(room: string, roomDrafts: ReadonlyMap<string, RoomDraftParts>): Promise<SaveResult>;
|
|
27
|
+
};
|
|
28
|
+
export declare function useSave(client: MdRuntimeClient): Saving;
|
|
29
|
+
export {};
|
|
30
|
+
//# sourceMappingURL=react.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"react.d.ts","sourceRoot":"","sources":["../src/react.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AACnD,OAAO,KAAK,EAAE,OAAO,EAAE,iBAAiB,EAAkB,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAC9F,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAChD,OAAO,KAAK,EAAE,mBAAmB,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAC;AAE/E,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAC/C,OAAO,KAAK,EAAE,aAAa,EAAE,cAAc,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AACzF,OAAO,KAAK,EAAE,SAAS,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AACjE,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAG9C,KAAK,MAAM,GAAG,eAAe,GAAG,IAAI,CAAC;AAIrC,wBAAgB,UAAU,CAAC,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,kBAAkB,GAAG,OAAO,CAAC;AAC/E,wBAAgB,UAAU,CAAC,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,GAAG,SAAS,OAAO,EAAE,GAAG,SAAS,CAAC;AAC3F,wBAAgB,UAAU,CACxB,MAAM,EAAE,MAAM,EACd,KAAK,EAAE,oBAAoB,GAC1B,iBAAiB,GAAG,IAAI,GAAG,SAAS,CAAC;AACxC,wBAAgB,UAAU,CAAC,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,cAAc,GAAG,MAAM,GAAG,IAAI,GAAG,SAAS,CAAC;AAC7F,wBAAgB,UAAU,CAAC,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,aAAa,GAAG,UAAU,GAAG,SAAS,CAAC;AACzF,wBAAgB,UAAU,CAAC,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,cAAc,GAAG,WAAW,GAAG,SAAS,CAAC;AAC3F,wBAAgB,UAAU,CACxB,MAAM,EAAE,MAAM,EACd,KAAK,EAAE,OAAO,EACd,KAAK,EAAE,aAAa,GACnB,cAAc,GAAG,SAAS,CAAC;AAC9B,wBAAgB,UAAU,CACxB,MAAM,EAAE,MAAM,EACd,KAAK,EAAE,OAAO,EACd,KAAK,EAAE,UAAU,GAChB,WAAW,GAAG,SAAS,CAAC;AAC3B,wBAAgB,UAAU,CACxB,MAAM,EAAE,MAAM,EACd,KAAK,EAAE,OAAO,EACd,OAAO,EAAE,MAAM,GACd,SAAS,UAAU,EAAE,GAAG,SAAS,CAAC;AACrC,wBAAgB,UAAU,CAAC,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,aAAa,GAAG,SAAS,SAAS,EAAE,GAAG,SAAS,CAAC;AACnG,wBAAgB,UAAU,CAAC,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,cAAc,GAAG,OAAO,CAAC;AAC3E,wBAAgB,UAAU,CACxB,MAAM,EAAE,MAAM,EACd,KAAK,EAAE,cAAc,EACrB,YAAY,EAAE,MAAM,EACpB,OAAO,EAAE,mBAAmB,GAC3B,gBAAgB,GAAG,SAAS,CAAC;AAkChC,MAAM,MAAM,MAAM,GAAG;IACnB,QAAQ,CAAC,IAAI,EAAE,OAAO,CAAC;IACvB,QAAQ,CAAC,OAAO,EAAE,UAAU,GAAG,IAAI,CAAC;IACpC,QAAQ,CAAC,OAAO,EAAE,KAAK,GAAG,IAAI,CAAC;IAC/B,IAAI,CAAC,IAAI,EAAE,MAAM,EAAE,UAAU,EAAE,WAAW,CAAC,MAAM,EAAE,cAAc,CAAC,GAAG,OAAO,CAAC,UAAU,CAAC,CAAC;CAC1F,CAAC;AAuBF,wBAAgB,OAAO,CAAC,MAAM,EAAE,eAAe,GAAG,MAAM,CAkBvD"}
|
package/dist/react.js
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { useCallback, useEffect, useRef, useState } from 'react';
|
|
2
|
+
export function useObserve(client, topic, ...input) {
|
|
3
|
+
const key = JSON.stringify([topic, ...input]);
|
|
4
|
+
const [held, setHeld] = useState(null);
|
|
5
|
+
const opened = useRef(input);
|
|
6
|
+
opened.current = input;
|
|
7
|
+
useEffect(() => {
|
|
8
|
+
if (client === null)
|
|
9
|
+
return;
|
|
10
|
+
let live = true;
|
|
11
|
+
let observation = null;
|
|
12
|
+
const observe = client.observe;
|
|
13
|
+
void observe(topic, ...opened.current).then((open) => {
|
|
14
|
+
if (!live) {
|
|
15
|
+
open.close();
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
observation = open;
|
|
19
|
+
const take = (value) => {
|
|
20
|
+
if (live)
|
|
21
|
+
setHeld({ client, key, value });
|
|
22
|
+
};
|
|
23
|
+
take(open.current());
|
|
24
|
+
open.subscribe(take);
|
|
25
|
+
});
|
|
26
|
+
return () => {
|
|
27
|
+
live = false;
|
|
28
|
+
observation?.close();
|
|
29
|
+
};
|
|
30
|
+
}, [client, key]);
|
|
31
|
+
return held !== null && held.client === client && held.key === key ? held.value : undefined;
|
|
32
|
+
}
|
|
33
|
+
const IDLE = { busy: false, outcome: null, failure: null };
|
|
34
|
+
async function written(client, room, roomDrafts) {
|
|
35
|
+
const kept = [];
|
|
36
|
+
for (const [path, parts] of roomDrafts) {
|
|
37
|
+
const prior = await client.roomDraft.read(room, path);
|
|
38
|
+
const write = await client.roomDraft.write(room, path, prior?.revision ?? null, parts);
|
|
39
|
+
if (write.outcome !== 'written' || write.roomDraft === null) {
|
|
40
|
+
throw new Error(`the Room Draft at ${path} was not written`);
|
|
41
|
+
}
|
|
42
|
+
kept.push({ path, revision: write.roomDraft.revision });
|
|
43
|
+
}
|
|
44
|
+
return client.save(room, kept);
|
|
45
|
+
}
|
|
46
|
+
export function useSave(client) {
|
|
47
|
+
const [state, setState] = useState(IDLE);
|
|
48
|
+
const save = useCallback(async (room, roomDrafts) => {
|
|
49
|
+
setState({ busy: true, outcome: null, failure: null });
|
|
50
|
+
try {
|
|
51
|
+
const outcome = await written(client, room, roomDrafts);
|
|
52
|
+
setState({ busy: false, outcome, failure: null });
|
|
53
|
+
return outcome;
|
|
54
|
+
}
|
|
55
|
+
catch (error) {
|
|
56
|
+
const failure = error instanceof Error ? error : new Error(String(error));
|
|
57
|
+
setState({ busy: false, outcome: null, failure });
|
|
58
|
+
throw failure;
|
|
59
|
+
}
|
|
60
|
+
}, [client]);
|
|
61
|
+
return { ...state, save };
|
|
62
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { ChangeSet } from './change-set.js';
|
|
2
|
+
import { type KeyedResult, type QueryChange } from './observation.js';
|
|
3
|
+
import type { Plan, QueryResult } from './query.js';
|
|
4
|
+
import type { SearchedDocument } from './search.js';
|
|
5
|
+
export type ReadSide = {
|
|
6
|
+
apply(set: ChangeSet): void;
|
|
7
|
+
query(plan: Plan): Promise<QueryResult>;
|
|
8
|
+
watch(plan: Plan, identity: string, deliver: (message: KeyedResult | QueryChange) => void): () => void;
|
|
9
|
+
documents(): readonly SearchedDocument[];
|
|
10
|
+
document(room: string, path: string): SearchedDocument | null;
|
|
11
|
+
close(): void;
|
|
12
|
+
};
|
|
13
|
+
export declare function reconciled(previous: KeyedResult, next: KeyedResult): {
|
|
14
|
+
keyed: KeyedResult;
|
|
15
|
+
change: QueryChange;
|
|
16
|
+
} | null;
|
|
17
|
+
export declare function createReadSide(): ReadSide;
|
|
18
|
+
//# sourceMappingURL=read-side.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
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"}
|