@get-bb/plugin-sdk 0.6.65 → 0.6.68
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/bundled-types/bb-plugin-sdk-app.d.ts +136 -86
- package/bundled-types/bb-plugin-sdk-internal-composer-handle.d.ts +60 -35
- package/bundled-types/bb-plugin-sdk-internal-plugin-app-collector.d.ts +5 -11
- package/bundled-types/bb-plugin-sdk-testing-app.d.ts +1 -0
- package/bundled-types/bb-plugin-sdk-testing.d.ts +18 -2
- package/bundled-types/bb-plugin-sdk.d.ts +231 -87
- package/dist/internal/composer-handle.js +13 -14
- package/dist/internal/plugin-app-collector.js +90 -87
- package/dist/internal/rpc-query-host.js +833 -181
- package/dist/provider-bridge-acp-next.js +2599 -2627
- package/dist/provider-bridge-acp.js +2599 -2627
- package/dist/provider-bridge-testing.js +1210 -1238
- package/dist/provider-bridge.js +1258 -1286
- package/dist/testing/app.js +919 -284
- package/dist/testing/index.js +55 -0
- package/package.json +1 -1
|
@@ -98,24 +98,454 @@ import {
|
|
|
98
98
|
useQueryClient,
|
|
99
99
|
useMutation,
|
|
100
100
|
useMutationState,
|
|
101
|
-
replaceEqualDeep as
|
|
101
|
+
replaceEqualDeep as replaceEqualDeep3
|
|
102
102
|
} from "@tanstack/react-query";
|
|
103
103
|
|
|
104
|
-
// ../query-tags/src/
|
|
104
|
+
// ../query-tags/src/clock.ts
|
|
105
|
+
var order = 0;
|
|
106
|
+
function tick() {
|
|
107
|
+
return ++order;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// ../query-tags/src/keyed-store.ts
|
|
105
111
|
import {
|
|
106
112
|
notifyManager,
|
|
107
113
|
replaceEqualDeep
|
|
108
114
|
} from "@tanstack/react-query";
|
|
115
|
+
var KeyedCollection = class {
|
|
116
|
+
constructor(name) {
|
|
117
|
+
this.name = name;
|
|
118
|
+
}
|
|
119
|
+
name;
|
|
120
|
+
};
|
|
121
|
+
var stores = /* @__PURE__ */ new WeakMap();
|
|
122
|
+
function isObject(value) {
|
|
123
|
+
return typeof value === "object" && value !== null;
|
|
124
|
+
}
|
|
125
|
+
function collectionOf(store, token) {
|
|
126
|
+
let collection = store.collections.get(token);
|
|
127
|
+
if (collection === void 0) {
|
|
128
|
+
collection = {
|
|
129
|
+
base: /* @__PURE__ */ new Map(),
|
|
130
|
+
visible: /* @__PURE__ */ new Map(),
|
|
131
|
+
layers: [],
|
|
132
|
+
acknowledgedAt: /* @__PURE__ */ new Map(),
|
|
133
|
+
views: /* @__PURE__ */ new Map()
|
|
134
|
+
};
|
|
135
|
+
store.collections.set(token, collection);
|
|
136
|
+
}
|
|
137
|
+
return collection;
|
|
138
|
+
}
|
|
139
|
+
function readEntity(collection, id) {
|
|
140
|
+
const cached = collection.visible.get(id);
|
|
141
|
+
if (cached !== void 0) return cached;
|
|
142
|
+
let entity = collection.base.get(id);
|
|
143
|
+
if (entity === void 0) return void 0;
|
|
144
|
+
for (const layer of collection.layers)
|
|
145
|
+
if (layer.ids.has(id)) entity = layer.apply(entity, id);
|
|
146
|
+
collection.visible.set(id, entity);
|
|
147
|
+
return entity;
|
|
148
|
+
}
|
|
149
|
+
function changed(store, collection, ids) {
|
|
150
|
+
const dirty = new Set(ids);
|
|
151
|
+
for (const id of dirty) collection.visible.delete(id);
|
|
152
|
+
if (store.batching > 0) {
|
|
153
|
+
const pending = store.dirtyIds.get(collection) ?? /* @__PURE__ */ new Set();
|
|
154
|
+
for (const id of dirty) pending.add(id);
|
|
155
|
+
store.dirtyIds.set(collection, pending);
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
const views = /* @__PURE__ */ new Map();
|
|
159
|
+
for (const id of dirty)
|
|
160
|
+
for (const view of collection.views.get(id) ?? []) {
|
|
161
|
+
const viewIds = views.get(view) ?? /* @__PURE__ */ new Set();
|
|
162
|
+
viewIds.add(id);
|
|
163
|
+
views.set(view, viewIds);
|
|
164
|
+
}
|
|
165
|
+
notifyManager.batch(() => {
|
|
166
|
+
for (const [view, viewIds] of views) rematerialize(store, view, viewIds);
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
function writeBase(store, collection, id, update) {
|
|
170
|
+
const current = collection.base.get(id);
|
|
171
|
+
const next = update(current);
|
|
172
|
+
if (next === void 0 || next === current) return;
|
|
173
|
+
collection.base.set(id, next);
|
|
174
|
+
changed(store, collection, [id]);
|
|
175
|
+
}
|
|
176
|
+
function ingest(store, skeleton, id, item, readStart) {
|
|
177
|
+
const fresh = readStart > (skeleton.collection.acknowledgedAt.get(id) ?? -1);
|
|
178
|
+
writeBase(
|
|
179
|
+
store,
|
|
180
|
+
skeleton.collection,
|
|
181
|
+
id,
|
|
182
|
+
(current) => skeleton.view.read(item, current, fresh)
|
|
183
|
+
);
|
|
184
|
+
}
|
|
185
|
+
function materializeItem(store, view, item, entity) {
|
|
186
|
+
if (!isObject(item) || !isObject(entity)) return view.render(item, entity);
|
|
187
|
+
let byEntity = store.rows.get(item);
|
|
188
|
+
if (byEntity === void 0) {
|
|
189
|
+
byEntity = /* @__PURE__ */ new WeakMap();
|
|
190
|
+
store.rows.set(item, byEntity);
|
|
191
|
+
}
|
|
192
|
+
const cached = byEntity.get(entity);
|
|
193
|
+
if (cached !== void 0) return cached;
|
|
194
|
+
const row = view.render(item, entity);
|
|
195
|
+
byEntity.set(entity, row);
|
|
196
|
+
return row;
|
|
197
|
+
}
|
|
198
|
+
function rematerialize(store, query, dirty) {
|
|
199
|
+
const skeleton = store.skeletons.get(query);
|
|
200
|
+
if (skeleton === void 0) return;
|
|
201
|
+
const { view, collection } = skeleton;
|
|
202
|
+
const previous = store.materialized.get(query);
|
|
203
|
+
const incremental = dirty !== void 0 && previous !== void 0;
|
|
204
|
+
const slotKeys = /* @__PURE__ */ new Set();
|
|
205
|
+
if (incremental) {
|
|
206
|
+
for (const id of dirty)
|
|
207
|
+
for (const slot of skeleton.slotsById.get(id) ?? []) slotKeys.add(slot);
|
|
208
|
+
} else for (const slot of skeleton.slots.keys()) slotKeys.add(slot);
|
|
209
|
+
if (slotKeys.size === 0) return;
|
|
210
|
+
const items = /* @__PURE__ */ new Map();
|
|
211
|
+
const materialized = new Map(previous);
|
|
212
|
+
for (const slot of slotKeys) {
|
|
213
|
+
const prior = previous?.get(slot);
|
|
214
|
+
const visible = /* @__PURE__ */ new Map();
|
|
215
|
+
const rows = [];
|
|
216
|
+
for (const entry of skeleton.slots.get(slot) ?? []) {
|
|
217
|
+
if (entry.id === null) {
|
|
218
|
+
rows.push(entry.item);
|
|
219
|
+
continue;
|
|
220
|
+
}
|
|
221
|
+
const entity = readEntity(collection, entry.id);
|
|
222
|
+
if (entity !== void 0 && view.shows !== void 0 && !view.shows(entity, query.queryKey))
|
|
223
|
+
continue;
|
|
224
|
+
const previousRow = incremental ? prior?.get(entry.id) : void 0;
|
|
225
|
+
const row = previousRow !== void 0 && dirty?.has(entry.id) === false ? previousRow : replaceEqualDeep(
|
|
226
|
+
previousRow,
|
|
227
|
+
entity === void 0 ? entry.item : materializeItem(store, view, entry.item, entity)
|
|
228
|
+
);
|
|
229
|
+
visible.set(entry.id, row);
|
|
230
|
+
rows.push(row);
|
|
231
|
+
}
|
|
232
|
+
const unchanged = prior !== void 0 && prior.size === rows.length && [...prior.values()].every((row, index) => row === rows[index]);
|
|
233
|
+
if (incremental && unchanged) continue;
|
|
234
|
+
items.set(slot, rows);
|
|
235
|
+
materialized.set(slot, visible);
|
|
236
|
+
}
|
|
237
|
+
if (items.size === 0) return;
|
|
238
|
+
let next = view.rebuild(
|
|
239
|
+
incremental ? query.state.data : skeleton.data,
|
|
240
|
+
items
|
|
241
|
+
);
|
|
242
|
+
if (!incremental) {
|
|
243
|
+
next = replaceEqualDeep(query.state.data, next);
|
|
244
|
+
for (const slot of view.slots(next) ?? []) {
|
|
245
|
+
const visible = /* @__PURE__ */ new Map();
|
|
246
|
+
for (const item of slot.items) {
|
|
247
|
+
const id = view.keyOf(item);
|
|
248
|
+
if (id !== void 0) visible.set(id, item);
|
|
249
|
+
}
|
|
250
|
+
materialized.set(slot.key, visible);
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
store.materialized.set(query, materialized);
|
|
254
|
+
if (next !== query.state.data) query.setState({ data: next });
|
|
255
|
+
}
|
|
256
|
+
function skeletonIds(skeleton) {
|
|
257
|
+
const ids = /* @__PURE__ */ new Set();
|
|
258
|
+
for (const entries of skeleton?.slots.values() ?? [])
|
|
259
|
+
for (const { id } of entries) if (id !== null) ids.add(id);
|
|
260
|
+
return ids;
|
|
261
|
+
}
|
|
262
|
+
function setSkeleton(store, query, skeleton) {
|
|
263
|
+
const previous = store.skeletons.get(query);
|
|
264
|
+
const previousIds = skeletonIds(previous);
|
|
265
|
+
const nextIds = skeletonIds(skeleton);
|
|
266
|
+
if (skeleton === void 0) {
|
|
267
|
+
store.skeletons.delete(query);
|
|
268
|
+
store.materialized.delete(query);
|
|
269
|
+
} else store.skeletons.set(query, skeleton);
|
|
270
|
+
const collection = skeleton?.collection ?? previous?.collection;
|
|
271
|
+
if (collection === void 0) return;
|
|
272
|
+
for (const id of nextIds) {
|
|
273
|
+
let views = collection.views.get(id);
|
|
274
|
+
if (views === void 0) {
|
|
275
|
+
views = /* @__PURE__ */ new Set();
|
|
276
|
+
collection.views.set(id, views);
|
|
277
|
+
}
|
|
278
|
+
views.add(query);
|
|
279
|
+
}
|
|
280
|
+
for (const id of previousIds) {
|
|
281
|
+
if (nextIds.has(id)) continue;
|
|
282
|
+
const views = collection.views.get(id);
|
|
283
|
+
views?.delete(query);
|
|
284
|
+
if (views === void 0 || views.size === 0) {
|
|
285
|
+
collection.views.delete(id);
|
|
286
|
+
collection.base.delete(id);
|
|
287
|
+
collection.visible.delete(id);
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
function priorItem(materialized, slot, id) {
|
|
292
|
+
const same = materialized?.get(slot)?.get(id);
|
|
293
|
+
if (same !== void 0) return same;
|
|
294
|
+
for (const visible of materialized?.values() ?? []) {
|
|
295
|
+
const item = visible.get(id);
|
|
296
|
+
if (item !== void 0) return item;
|
|
297
|
+
}
|
|
298
|
+
return void 0;
|
|
299
|
+
}
|
|
300
|
+
function isHidden(entry, visible) {
|
|
301
|
+
return entry.id !== null && !visible.has(entry.id);
|
|
302
|
+
}
|
|
303
|
+
function keepHiddenEntries(previous, next, visible) {
|
|
304
|
+
if (!previous.some((entry) => isHidden(entry, visible))) return next;
|
|
305
|
+
const result = [...next];
|
|
306
|
+
let insertAt = result.length;
|
|
307
|
+
for (let index = previous.length - 1; index >= 0; index--) {
|
|
308
|
+
const entry = previous[index];
|
|
309
|
+
const existing = entry.id === null ? -1 : result.findIndex(({ id }) => id === entry.id);
|
|
310
|
+
if (existing >= 0) insertAt = existing;
|
|
311
|
+
else if (isHidden(entry, visible)) result.splice(insertAt, 0, entry);
|
|
312
|
+
}
|
|
313
|
+
return result;
|
|
314
|
+
}
|
|
315
|
+
function keptInsertions(store, query, slot, readStart) {
|
|
316
|
+
const insertions = (store.insertions.get(query) ?? []).filter(
|
|
317
|
+
(insertion) => insertion.at > readStart
|
|
318
|
+
);
|
|
319
|
+
store.insertions.set(query, insertions);
|
|
320
|
+
return insertions.filter((insertion) => insertion.slot === slot);
|
|
321
|
+
}
|
|
322
|
+
function batch(store, run) {
|
|
323
|
+
store.batching++;
|
|
324
|
+
try {
|
|
325
|
+
notifyManager.batch(run);
|
|
326
|
+
} finally {
|
|
327
|
+
store.batching--;
|
|
328
|
+
}
|
|
329
|
+
if (store.batching > 0) return;
|
|
330
|
+
const full = new Set(store.dirtyViews);
|
|
331
|
+
const partial = /* @__PURE__ */ new Map();
|
|
332
|
+
for (const [collection, ids] of store.dirtyIds)
|
|
333
|
+
for (const id of ids)
|
|
334
|
+
for (const view of collection.views.get(id) ?? []) {
|
|
335
|
+
if (full.has(view)) continue;
|
|
336
|
+
const viewIds = partial.get(view) ?? /* @__PURE__ */ new Set();
|
|
337
|
+
viewIds.add(id);
|
|
338
|
+
partial.set(view, viewIds);
|
|
339
|
+
}
|
|
340
|
+
store.dirtyIds.clear();
|
|
341
|
+
store.dirtyViews.clear();
|
|
342
|
+
notifyManager.batch(() => {
|
|
343
|
+
for (const view of full) rematerialize(store, view);
|
|
344
|
+
for (const [view, ids] of partial) rematerialize(store, view, ids);
|
|
345
|
+
});
|
|
346
|
+
}
|
|
347
|
+
function onViewWritten(store, query, view, manual) {
|
|
348
|
+
const data = query.state.data;
|
|
349
|
+
const slots = data === void 0 ? void 0 : view.slots(data);
|
|
350
|
+
if (slots === void 0) {
|
|
351
|
+
setSkeleton(store, query, void 0);
|
|
352
|
+
return;
|
|
353
|
+
}
|
|
354
|
+
const collection = collectionOf(store, view.collection);
|
|
355
|
+
const target = { view, collection };
|
|
356
|
+
const previous = store.skeletons.get(query);
|
|
357
|
+
const materialized = manual ? store.materialized.get(query) : void 0;
|
|
358
|
+
const readStart = manual ? -1 : store.fetchStartedAt.get(query) ?? 0;
|
|
359
|
+
const receipt = manual ? void 0 : store.receipts.get(query);
|
|
360
|
+
if (receipt !== void 0) store.receipts.delete(query);
|
|
361
|
+
batch(store, () => {
|
|
362
|
+
const nextSlots = /* @__PURE__ */ new Map();
|
|
363
|
+
const slotsById = /* @__PURE__ */ new Map();
|
|
364
|
+
for (const slot of slots) {
|
|
365
|
+
const entries = [];
|
|
366
|
+
for (const item of slot.items) {
|
|
367
|
+
const id = view.keyOf(item);
|
|
368
|
+
entries.push({ id: id ?? null, item });
|
|
369
|
+
if (id === void 0) continue;
|
|
370
|
+
const prior = priorItem(materialized, slot.key, id);
|
|
371
|
+
if (prior !== void 0) {
|
|
372
|
+
if (prior !== item)
|
|
373
|
+
writeBase(
|
|
374
|
+
store,
|
|
375
|
+
collection,
|
|
376
|
+
id,
|
|
377
|
+
(current) => current === void 0 ? current : view.write(item, prior, current)
|
|
378
|
+
);
|
|
379
|
+
continue;
|
|
380
|
+
}
|
|
381
|
+
const start = receipt === void 0 ? readStart : receipt.get(id);
|
|
382
|
+
if (start !== void 0) ingest(store, target, id, item, start);
|
|
383
|
+
else if (!collection.base.has(id))
|
|
384
|
+
ingest(store, target, id, item, readStart);
|
|
385
|
+
}
|
|
386
|
+
const previousEntries = previous?.slots.get(slot.key);
|
|
387
|
+
const visible = materialized?.get(slot.key);
|
|
388
|
+
const kept = previousEntries !== void 0 && visible !== void 0 ? keepHiddenEntries(previousEntries, entries, visible) : entries;
|
|
389
|
+
if (!manual) {
|
|
390
|
+
for (const insertion of keptInsertions(
|
|
391
|
+
store,
|
|
392
|
+
query,
|
|
393
|
+
slot.key,
|
|
394
|
+
readStart
|
|
395
|
+
))
|
|
396
|
+
if (!kept.some(({ id }) => id === insertion.entry.id))
|
|
397
|
+
kept.push(insertion.entry);
|
|
398
|
+
}
|
|
399
|
+
nextSlots.set(slot.key, kept);
|
|
400
|
+
for (const { id } of kept) {
|
|
401
|
+
if (id === null) continue;
|
|
402
|
+
const slotKeys = slotsById.get(id) ?? /* @__PURE__ */ new Set();
|
|
403
|
+
slotKeys.add(slot.key);
|
|
404
|
+
slotsById.set(id, slotKeys);
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
setSkeleton(store, query, {
|
|
408
|
+
view,
|
|
409
|
+
collection,
|
|
410
|
+
data,
|
|
411
|
+
slots: nextSlots,
|
|
412
|
+
slotsById
|
|
413
|
+
});
|
|
414
|
+
store.dirtyViews.add(query);
|
|
415
|
+
});
|
|
416
|
+
}
|
|
417
|
+
function viewOf(store, query) {
|
|
418
|
+
const exact = store.exact.get(query.queryHash);
|
|
419
|
+
if (exact !== void 0) return exact;
|
|
420
|
+
for (const resolve of store.resolvers) {
|
|
421
|
+
const view = resolve(query.queryKey);
|
|
422
|
+
if (view !== void 0) return view;
|
|
423
|
+
}
|
|
424
|
+
return void 0;
|
|
425
|
+
}
|
|
426
|
+
function storeOf(client) {
|
|
427
|
+
const existing = stores.get(client);
|
|
428
|
+
if (existing !== void 0) return existing;
|
|
429
|
+
const store = {
|
|
430
|
+
client,
|
|
431
|
+
collections: /* @__PURE__ */ new Map(),
|
|
432
|
+
resolvers: [],
|
|
433
|
+
exact: /* @__PURE__ */ new Map(),
|
|
434
|
+
skeletons: /* @__PURE__ */ new Map(),
|
|
435
|
+
insertions: /* @__PURE__ */ new WeakMap(),
|
|
436
|
+
materialized: /* @__PURE__ */ new WeakMap(),
|
|
437
|
+
fetchStartedAt: /* @__PURE__ */ new WeakMap(),
|
|
438
|
+
receipts: /* @__PURE__ */ new WeakMap(),
|
|
439
|
+
rows: /* @__PURE__ */ new WeakMap(),
|
|
440
|
+
batching: 0,
|
|
441
|
+
dirtyIds: /* @__PURE__ */ new Map(),
|
|
442
|
+
dirtyViews: /* @__PURE__ */ new Set()
|
|
443
|
+
};
|
|
444
|
+
stores.set(client, store);
|
|
445
|
+
client.getQueryCache().subscribe((event) => {
|
|
446
|
+
const { query } = event;
|
|
447
|
+
if (event.type === "removed") {
|
|
448
|
+
if (store.skeletons.has(query)) setSkeleton(store, query, void 0);
|
|
449
|
+
return;
|
|
450
|
+
}
|
|
451
|
+
if (event.type !== "updated") return;
|
|
452
|
+
const { type } = event.action;
|
|
453
|
+
if (type !== "fetch" && type !== "success") return;
|
|
454
|
+
const view = viewOf(store, query);
|
|
455
|
+
if (view === void 0) return;
|
|
456
|
+
if (event.action.type === "fetch") {
|
|
457
|
+
store.fetchStartedAt.set(query, tick());
|
|
458
|
+
return;
|
|
459
|
+
}
|
|
460
|
+
if (event.action.type === "success")
|
|
461
|
+
onViewWritten(store, query, view, event.action.manual === true);
|
|
462
|
+
});
|
|
463
|
+
return store;
|
|
464
|
+
}
|
|
465
|
+
function adopt(store, matches) {
|
|
466
|
+
for (const query of store.client.getQueryCache().getAll()) {
|
|
467
|
+
const view = matches(query);
|
|
468
|
+
if (view !== void 0 && query.state.data !== void 0)
|
|
469
|
+
onViewWritten(store, query, view, true);
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
function registerKeyedView(client, queryKey, view) {
|
|
473
|
+
const store = storeOf(client);
|
|
474
|
+
const { queryHash } = client.defaultQueryOptions({ queryKey });
|
|
475
|
+
if (store.exact.has(queryHash)) return;
|
|
476
|
+
store.exact.set(queryHash, view);
|
|
477
|
+
adopt(store, (query) => query.queryHash === queryHash ? view : void 0);
|
|
478
|
+
}
|
|
479
|
+
function recordKeyedRead(client, queryKey, startedAt) {
|
|
480
|
+
const query = client.getQueryCache().get(client.defaultQueryOptions({ queryKey }).queryHash);
|
|
481
|
+
if (query !== void 0) storeOf(client).receipts.set(query, startedAt);
|
|
482
|
+
}
|
|
483
|
+
function bakeSettledLayers(collection) {
|
|
484
|
+
const baked = /* @__PURE__ */ new Set();
|
|
485
|
+
const pending = /* @__PURE__ */ new Set();
|
|
486
|
+
collection.layers = collection.layers.filter((layer) => {
|
|
487
|
+
const blocked = [...layer.ids].some((id) => pending.has(id));
|
|
488
|
+
if (!layer.settled || blocked) {
|
|
489
|
+
for (const id of layer.ids) pending.add(id);
|
|
490
|
+
return true;
|
|
491
|
+
}
|
|
492
|
+
for (const id of layer.ids) {
|
|
493
|
+
const current = collection.base.get(id);
|
|
494
|
+
if (current !== void 0)
|
|
495
|
+
collection.base.set(id, layer.apply(current, id));
|
|
496
|
+
baked.add(id);
|
|
497
|
+
}
|
|
498
|
+
return false;
|
|
499
|
+
});
|
|
500
|
+
return baked;
|
|
501
|
+
}
|
|
502
|
+
function predictKeyed(client, token, ids, apply) {
|
|
503
|
+
const store = storeOf(client);
|
|
504
|
+
const collection = collectionOf(store, token);
|
|
505
|
+
const layer = { ids, apply, settled: false };
|
|
506
|
+
collection.layers.push(layer);
|
|
507
|
+
changed(store, collection, ids);
|
|
508
|
+
const live = () => !layer.settled && collection.layers.includes(layer);
|
|
509
|
+
return {
|
|
510
|
+
accept() {
|
|
511
|
+
if (!live()) return;
|
|
512
|
+
const at = tick();
|
|
513
|
+
for (const id of ids) collection.acknowledgedAt.set(id, at);
|
|
514
|
+
layer.settled = true;
|
|
515
|
+
changed(store, collection, [...ids, ...bakeSettledLayers(collection)]);
|
|
516
|
+
},
|
|
517
|
+
remove() {
|
|
518
|
+
if (!live()) return;
|
|
519
|
+
collection.layers = collection.layers.filter((other) => other !== layer);
|
|
520
|
+
changed(store, collection, [...ids, ...bakeSettledLayers(collection)]);
|
|
521
|
+
}
|
|
522
|
+
};
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
// ../query-tags/src/optimistic.ts
|
|
526
|
+
import {
|
|
527
|
+
matchQuery,
|
|
528
|
+
notifyManager as notifyManager2,
|
|
529
|
+
replaceEqualDeep as replaceEqualDeep2
|
|
530
|
+
} from "@tanstack/react-query";
|
|
531
|
+
function optimisticKeyedUpdate(collection, key, update) {
|
|
532
|
+
return {
|
|
533
|
+
collection,
|
|
534
|
+
key,
|
|
535
|
+
update: (current, result) => update(current, result)
|
|
536
|
+
};
|
|
537
|
+
}
|
|
109
538
|
function optimisticQueryUpdate(queryKey, update, path = []) {
|
|
110
539
|
return {
|
|
111
540
|
queryKey,
|
|
112
541
|
path: [...path],
|
|
113
|
-
update: (current) => update(current)
|
|
542
|
+
update: (current, result) => update(current, result)
|
|
114
543
|
};
|
|
115
544
|
}
|
|
116
545
|
var predictions = /* @__PURE__ */ new WeakMap();
|
|
117
546
|
var receipts = /* @__PURE__ */ new WeakMap();
|
|
118
|
-
var
|
|
547
|
+
var taggedMeta = /* @__PURE__ */ new WeakSet();
|
|
548
|
+
var syncedClients = /* @__PURE__ */ new WeakMap();
|
|
119
549
|
function valueAt(data, path) {
|
|
120
550
|
let value = data;
|
|
121
551
|
for (const key of path) {
|
|
@@ -164,6 +594,22 @@ function confirmedValue(current, predicted, confirmed) {
|
|
|
164
594
|
}
|
|
165
595
|
return next;
|
|
166
596
|
}
|
|
597
|
+
function rebase(base, visible, next) {
|
|
598
|
+
if (Object.is(visible, next)) return base;
|
|
599
|
+
if (typeof base !== "object" || base === null || typeof visible !== "object" || visible === null || typeof next !== "object" || next === null || Array.isArray(base) || Array.isArray(visible) || Array.isArray(next))
|
|
600
|
+
return next;
|
|
601
|
+
const result = {};
|
|
602
|
+
for (const key of Object.keys(base))
|
|
603
|
+
if (!Object.hasOwn(visible, key) && !Object.hasOwn(next, key))
|
|
604
|
+
result[key] = Reflect.get(base, key);
|
|
605
|
+
for (const key of Object.keys(next))
|
|
606
|
+
result[key] = Object.hasOwn(visible, key) && Object.hasOwn(base, key) ? rebase(
|
|
607
|
+
Reflect.get(base, key),
|
|
608
|
+
Reflect.get(visible, key),
|
|
609
|
+
Reflect.get(next, key)
|
|
610
|
+
) : Reflect.get(next, key);
|
|
611
|
+
return result;
|
|
612
|
+
}
|
|
167
613
|
function projected(state) {
|
|
168
614
|
return state.layers.reduce((data, layer) => {
|
|
169
615
|
const current = valueAt(data, layer.path);
|
|
@@ -189,38 +635,98 @@ function subscribeOptimisticQuery(query, listener) {
|
|
|
189
635
|
if (listeners.size === 0) predictionListeners.delete(query);
|
|
190
636
|
};
|
|
191
637
|
}
|
|
638
|
+
function isTaggedQuery(query) {
|
|
639
|
+
return query.meta !== void 0 && taggedMeta.has(query.meta);
|
|
640
|
+
}
|
|
641
|
+
function applyLayer(data, layer) {
|
|
642
|
+
const current = valueAt(data, layer.path);
|
|
643
|
+
return replaceAt(
|
|
644
|
+
data,
|
|
645
|
+
layer.path,
|
|
646
|
+
layer.update(current === void 0 ? layer.initial : current.value)
|
|
647
|
+
);
|
|
648
|
+
}
|
|
649
|
+
function confirmLayers(query, state, ready) {
|
|
650
|
+
const tagged = isTaggedQuery(query);
|
|
651
|
+
while (state.layers[0] !== void 0 && ready(state.layers[0])) {
|
|
652
|
+
const layer = state.layers.shift();
|
|
653
|
+
state.base = applyLayer(state.base, layer);
|
|
654
|
+
if (tagged && !layer.fresh) state.guards.push(layer);
|
|
655
|
+
}
|
|
656
|
+
}
|
|
657
|
+
function confirmBeforeWrite(query, state) {
|
|
658
|
+
confirmLayers(query, state, (layer) => layer.acceptedAt !== null);
|
|
659
|
+
}
|
|
660
|
+
function isIdle(state) {
|
|
661
|
+
return state.layers.length === 0 && state.guards.length === 0;
|
|
662
|
+
}
|
|
663
|
+
function untrack(query, state) {
|
|
664
|
+
predictions.delete(query);
|
|
665
|
+
const synced = syncedClients.get(state.client);
|
|
666
|
+
if (synced === void 0) return;
|
|
667
|
+
synced.queries.delete(query);
|
|
668
|
+
if (synced.queries.size === 0) {
|
|
669
|
+
synced.unsubscribe();
|
|
670
|
+
syncedClients.delete(state.client);
|
|
671
|
+
}
|
|
672
|
+
}
|
|
673
|
+
function track(query, state) {
|
|
674
|
+
predictions.set(query, state);
|
|
675
|
+
let synced = syncedClients.get(state.client);
|
|
676
|
+
if (synced === void 0) {
|
|
677
|
+
const queries = /* @__PURE__ */ new Set();
|
|
678
|
+
const unsubscribe = state.client.getQueryCache().subscribe((event) => {
|
|
679
|
+
if (event.type !== "updated" || event.action.type !== "success") return;
|
|
680
|
+
const current = predictions.get(event.query);
|
|
681
|
+
if (current === void 0 || isTaggedQuery(event.query)) return;
|
|
682
|
+
const data = event.query.state.data;
|
|
683
|
+
if (event.action.manual) {
|
|
684
|
+
confirmBeforeWrite(event.query, current);
|
|
685
|
+
current.base = rebase(current.base, current.visible, data);
|
|
686
|
+
} else {
|
|
687
|
+
current.base = data;
|
|
688
|
+
}
|
|
689
|
+
publish(event.query, current);
|
|
690
|
+
});
|
|
691
|
+
synced = { queries, unsubscribe };
|
|
692
|
+
syncedClients.set(state.client, synced);
|
|
693
|
+
}
|
|
694
|
+
synced.queries.add(query);
|
|
695
|
+
}
|
|
192
696
|
function publish(query, state) {
|
|
193
|
-
query
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
697
|
+
if (!isTaggedQuery(query))
|
|
698
|
+
confirmLayers(query, state, (layer) => layer.settled);
|
|
699
|
+
const data = replaceEqualDeep2(query.state.data, projected(state));
|
|
700
|
+
state.visible = data;
|
|
701
|
+
query.setState({ data });
|
|
702
|
+
if (isIdle(state)) untrack(query, state);
|
|
197
703
|
for (const listener of predictionListeners.get(query) ?? []) listener();
|
|
198
704
|
}
|
|
199
|
-
function
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
705
|
+
function markTaggedQueryMeta(meta) {
|
|
706
|
+
taggedMeta.add(meta);
|
|
707
|
+
}
|
|
708
|
+
function commitTaggedReadReceipt(query, data, started) {
|
|
709
|
+
receipts.set(query, { data, started });
|
|
710
|
+
}
|
|
711
|
+
function consumeReceipt(query, next) {
|
|
712
|
+
const receipt = receipts.get(query);
|
|
713
|
+
if (receipt === void 0 || !Object.is(receipt.data, next)) return;
|
|
714
|
+
receipts.delete(query);
|
|
715
|
+
return receipt;
|
|
716
|
+
}
|
|
717
|
+
function refreshedBy(receipt, layer) {
|
|
718
|
+
return layer.acceptedAt !== null && receipt.started > layer.acceptedAt;
|
|
210
719
|
}
|
|
211
720
|
function taggedStructuralSharing(client, queryKey, previous, next) {
|
|
212
721
|
const query = client.getQueryCache().get(client.defaultQueryOptions({ queryKey }).queryHash);
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
const
|
|
216
|
-
if (
|
|
217
|
-
|
|
218
|
-
if (state !== void 0) {
|
|
722
|
+
if (query !== void 0 && Object.is(previous, query.state.data)) {
|
|
723
|
+
const receipt = consumeReceipt(query, next);
|
|
724
|
+
const state = predictions.get(query);
|
|
725
|
+
if (state !== void 0) {
|
|
726
|
+
if (receipt !== void 0) {
|
|
219
727
|
const refreshed = /* @__PURE__ */ new Set();
|
|
220
728
|
for (const layer of state.layers) {
|
|
221
|
-
if (layer
|
|
222
|
-
({ path, started }) => started > layer.acceptedAt && prefix(path, layer.path)
|
|
223
|
-
)) {
|
|
729
|
+
if (refreshedBy(receipt, layer)) {
|
|
224
730
|
layer.fresh = true;
|
|
225
731
|
refreshed.add(layer);
|
|
226
732
|
}
|
|
@@ -229,60 +735,129 @@ function taggedStructuralSharing(client, queryKey, previous, next) {
|
|
|
229
735
|
(layer) => !layer.settled || !refreshed.has(layer) || olderPending(state, layer)
|
|
230
736
|
);
|
|
231
737
|
}
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
738
|
+
if (receipt !== void 0) {
|
|
739
|
+
state.guards = state.guards.filter(
|
|
740
|
+
(guard) => !refreshedBy(receipt, guard)
|
|
741
|
+
);
|
|
742
|
+
state.base = state.guards.reduce(applyLayer, next);
|
|
743
|
+
} else {
|
|
744
|
+
confirmBeforeWrite(query, state);
|
|
745
|
+
state.base = rebase(state.base, previous, next);
|
|
746
|
+
}
|
|
235
747
|
next = projected(state);
|
|
236
|
-
|
|
748
|
+
state.visible = next;
|
|
749
|
+
if (isIdle(state)) untrack(query, state);
|
|
237
750
|
}
|
|
238
751
|
}
|
|
239
752
|
const sharing = client.defaultQueryOptions({ queryKey }).structuralSharing;
|
|
240
|
-
return sharing === false ? next : typeof sharing === "function" ? sharing(previous, next) :
|
|
753
|
+
return sharing === false ? next : typeof sharing === "function" ? sharing(previous, next) : replaceEqualDeep2(previous, next);
|
|
241
754
|
}
|
|
242
755
|
function getTaggedReadData(client, queryKey) {
|
|
243
756
|
const query = client.getQueryCache().get(client.defaultQueryOptions({ queryKey }).queryHash);
|
|
244
757
|
const state = query === void 0 ? void 0 : predictions.get(query);
|
|
245
758
|
return state === void 0 ? query?.state.data : state.base;
|
|
246
759
|
}
|
|
247
|
-
function
|
|
760
|
+
function isKeyedUpdate(update) {
|
|
761
|
+
return "collection" in update;
|
|
762
|
+
}
|
|
763
|
+
function beginKeyedPredictions(client, updates, result) {
|
|
764
|
+
const byCollection = /* @__PURE__ */ new Map();
|
|
765
|
+
for (const update of updates) {
|
|
766
|
+
const byKey = byCollection.get(update.collection) ?? /* @__PURE__ */ new Map();
|
|
767
|
+
byKey.set(update.key, [...byKey.get(update.key) ?? [], update]);
|
|
768
|
+
byCollection.set(update.collection, byKey);
|
|
769
|
+
}
|
|
770
|
+
return [...byCollection].map(
|
|
771
|
+
([collection, byKey]) => predictKeyed(
|
|
772
|
+
client,
|
|
773
|
+
collection,
|
|
774
|
+
new Set(byKey.keys()),
|
|
775
|
+
(entity, key) => (byKey.get(key) ?? []).reduce(
|
|
776
|
+
(current, update) => update.update(current, result()),
|
|
777
|
+
entity
|
|
778
|
+
)
|
|
779
|
+
)
|
|
780
|
+
);
|
|
781
|
+
}
|
|
782
|
+
function beginOptimisticMutation(client, allUpdates) {
|
|
248
783
|
const owner = {};
|
|
249
|
-
const mutationOrder =
|
|
250
|
-
let accepted
|
|
784
|
+
const mutationOrder = tick();
|
|
785
|
+
let accepted;
|
|
786
|
+
let done = false;
|
|
787
|
+
const updates = allUpdates.filter(
|
|
788
|
+
(update) => !isKeyedUpdate(update)
|
|
789
|
+
);
|
|
790
|
+
const keyed = beginKeyedPredictions(
|
|
791
|
+
client,
|
|
792
|
+
allUpdates.filter(isKeyedUpdate),
|
|
793
|
+
() => accepted?.result.value === void 0 ? void 0 : accepted.result
|
|
794
|
+
);
|
|
251
795
|
const affected = /* @__PURE__ */ new Map();
|
|
252
|
-
const
|
|
253
|
-
|
|
796
|
+
const attached = new Map(
|
|
797
|
+
updates.map((update) => [update, /* @__PURE__ */ new Set()])
|
|
798
|
+
);
|
|
799
|
+
const attach = (query, update) => {
|
|
800
|
+
const queries = attached.get(update);
|
|
801
|
+
if (queries.has(query) || query.state.data === void 0) return;
|
|
802
|
+
const state = predictions.get(query) ?? {
|
|
803
|
+
client,
|
|
804
|
+
base: query.state.data,
|
|
805
|
+
visible: query.state.data,
|
|
806
|
+
layers: [],
|
|
807
|
+
guards: []
|
|
808
|
+
};
|
|
809
|
+
const current = valueAt(projected(state), update.path);
|
|
810
|
+
if (current === void 0) return;
|
|
811
|
+
queries.add(query);
|
|
812
|
+
const layer = {
|
|
813
|
+
owner,
|
|
814
|
+
order: mutationOrder,
|
|
815
|
+
path: update.path,
|
|
816
|
+
initial: current.value,
|
|
817
|
+
update: (value) => update.update(value, accepted?.result),
|
|
818
|
+
acceptedAt: accepted?.at ?? null,
|
|
819
|
+
fresh: false,
|
|
820
|
+
settled: false
|
|
821
|
+
};
|
|
822
|
+
const index = state.layers.findIndex((other) => other.order > layer.order);
|
|
823
|
+
if (index < 0) state.layers.push(layer);
|
|
824
|
+
else state.layers.splice(index, 0, layer);
|
|
825
|
+
track(query, state);
|
|
826
|
+
affected.set(query, state);
|
|
827
|
+
publish(query, state);
|
|
828
|
+
};
|
|
829
|
+
const unsubscribe = updates.length === 0 ? () => {
|
|
830
|
+
} : client.getQueryCache().subscribe((event) => {
|
|
831
|
+
if (event.type !== "updated" || event.action.type !== "success")
|
|
832
|
+
return;
|
|
833
|
+
for (const update of updates)
|
|
834
|
+
if (matchQuery(
|
|
835
|
+
{ queryKey: update.queryKey, exact: true },
|
|
836
|
+
event.query
|
|
837
|
+
))
|
|
838
|
+
attach(event.query, update);
|
|
839
|
+
});
|
|
840
|
+
const finish = () => {
|
|
841
|
+
done = true;
|
|
842
|
+
unsubscribe();
|
|
843
|
+
};
|
|
844
|
+
const rollback = () => notifyManager2.batch(() => {
|
|
845
|
+
if (accepted !== void 0 || done) return;
|
|
846
|
+
finish();
|
|
847
|
+
for (const prediction of keyed) prediction.remove();
|
|
254
848
|
for (const [query, state] of affected) {
|
|
255
849
|
state.layers = state.layers.filter((layer) => layer.owner !== owner);
|
|
256
850
|
publish(query, state);
|
|
257
851
|
}
|
|
258
852
|
});
|
|
259
853
|
try {
|
|
260
|
-
|
|
261
|
-
for (const update of updates)
|
|
262
|
-
const query
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
base: query.state.data,
|
|
268
|
-
layers: []
|
|
269
|
-
};
|
|
270
|
-
const current = valueAt(projected(state), update.path);
|
|
271
|
-
if (current === void 0) continue;
|
|
272
|
-
state.layers.push({
|
|
273
|
-
owner,
|
|
274
|
-
order: mutationOrder,
|
|
275
|
-
path: update.path,
|
|
276
|
-
initial: current.value,
|
|
277
|
-
update: update.update,
|
|
278
|
-
acceptedAt: null,
|
|
279
|
-
fresh: false,
|
|
280
|
-
settled: false
|
|
281
|
-
});
|
|
282
|
-
predictions.set(query, state);
|
|
283
|
-
affected.set(query, state);
|
|
284
|
-
publish(query, state);
|
|
285
|
-
}
|
|
854
|
+
notifyManager2.batch(() => {
|
|
855
|
+
for (const update of updates)
|
|
856
|
+
for (const query of client.getQueryCache().findAll({
|
|
857
|
+
queryKey: update.queryKey,
|
|
858
|
+
exact: true
|
|
859
|
+
}))
|
|
860
|
+
attach(query, update);
|
|
286
861
|
});
|
|
287
862
|
} catch (error) {
|
|
288
863
|
rollback();
|
|
@@ -290,19 +865,27 @@ function beginOptimisticMutation(client, updates) {
|
|
|
290
865
|
}
|
|
291
866
|
return {
|
|
292
867
|
rollback,
|
|
293
|
-
accept() {
|
|
294
|
-
accepted =
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
for (const
|
|
298
|
-
|
|
868
|
+
accept(result) {
|
|
869
|
+
accepted = { at: tick(), result: { value: result } };
|
|
870
|
+
notifyManager2.batch(() => {
|
|
871
|
+
for (const prediction of keyed) prediction.accept();
|
|
872
|
+
for (const [query, state] of affected) {
|
|
873
|
+
for (const layer of state.layers)
|
|
874
|
+
if (layer.owner === owner) layer.acceptedAt = accepted.at;
|
|
875
|
+
publish(query, state);
|
|
876
|
+
}
|
|
877
|
+
});
|
|
299
878
|
},
|
|
300
879
|
settle() {
|
|
301
|
-
|
|
880
|
+
if (done) return;
|
|
881
|
+
finish();
|
|
882
|
+
notifyManager2.batch(() => {
|
|
302
883
|
for (const [query, state] of affected) {
|
|
884
|
+
const tagged = isTaggedQuery(query);
|
|
303
885
|
state.layers = state.layers.filter((layer) => {
|
|
304
886
|
if (layer.owner !== owner) return true;
|
|
305
887
|
layer.settled = true;
|
|
888
|
+
if (!tagged) return true;
|
|
306
889
|
if (!layer.fresh) return true;
|
|
307
890
|
if (!olderPending(state, layer)) return false;
|
|
308
891
|
const current = valueAt(state.base, layer.path);
|
|
@@ -339,6 +922,7 @@ var registrations = /* @__PURE__ */ new WeakMap();
|
|
|
339
922
|
var reads = /* @__PURE__ */ new WeakMap();
|
|
340
923
|
function taggedQueryOptions(client, queryKey, registration) {
|
|
341
924
|
const meta = {};
|
|
925
|
+
markTaggedQueryMeta(meta);
|
|
342
926
|
registrations.set(meta, {
|
|
343
927
|
scope: registration.scope,
|
|
344
928
|
tagsOf: (data) => registration.tagsOf(data),
|
|
@@ -352,22 +936,20 @@ function taggedQueryOptions(client, queryKey, registration) {
|
|
|
352
936
|
function registrationOf(query) {
|
|
353
937
|
return query.meta === void 0 ? void 0 : registrations.get(query.meta);
|
|
354
938
|
}
|
|
355
|
-
async function runTaggedRead(client, queryKey, read
|
|
939
|
+
async function runTaggedRead(client, queryKey, read) {
|
|
356
940
|
const query = client.getQueryCache().get(client.defaultQueryOptions({ queryKey }).queryHash);
|
|
357
941
|
if (query === void 0) return read();
|
|
358
942
|
const registration = registrationOf(query);
|
|
359
|
-
const receipt = taggedReadReceipt(query);
|
|
360
943
|
for (; ; ) {
|
|
361
944
|
const pending = { tags: /* @__PURE__ */ new Set(), restart: false };
|
|
362
945
|
reads.set(query, pending);
|
|
363
|
-
const started =
|
|
946
|
+
const started = tick();
|
|
364
947
|
try {
|
|
365
948
|
const result = await read();
|
|
366
|
-
receipt.record(started, paths());
|
|
367
949
|
const overlap = registration?.tagsOf(result).some((tag) => pending.tags.has(tag));
|
|
368
950
|
if (overlap) registration?.onInvalidate?.(result, [...pending.tags]);
|
|
369
951
|
if (pending.restart || overlap) continue;
|
|
370
|
-
|
|
952
|
+
commitTaggedReadReceipt(query, result, started);
|
|
371
953
|
return result;
|
|
372
954
|
} catch (error) {
|
|
373
955
|
if (pending.restart) continue;
|
|
@@ -415,10 +997,11 @@ async function invalidateTags(client, scope, tags) {
|
|
|
415
997
|
}
|
|
416
998
|
async function executeTaggedMutation(invalidate, call, prediction) {
|
|
417
999
|
const { result, invalidated } = await call();
|
|
418
|
-
prediction?.accept();
|
|
1000
|
+
prediction?.accept(result);
|
|
419
1001
|
const refresh = async () => {
|
|
420
1002
|
try {
|
|
421
|
-
if (invalidated.length > 0)
|
|
1003
|
+
if (invalidate !== void 0 && invalidated.length > 0)
|
|
1004
|
+
await invalidate(invalidated);
|
|
422
1005
|
} finally {
|
|
423
1006
|
prediction?.settle();
|
|
424
1007
|
}
|
|
@@ -432,7 +1015,13 @@ function restartTaggedRead(query) {
|
|
|
432
1015
|
const pending = reads.get(query);
|
|
433
1016
|
if (pending) pending.restart = true;
|
|
434
1017
|
}
|
|
435
|
-
function useTaggedMutation(
|
|
1018
|
+
function useTaggedMutation({
|
|
1019
|
+
client,
|
|
1020
|
+
invalidate,
|
|
1021
|
+
call,
|
|
1022
|
+
optimistic,
|
|
1023
|
+
meta
|
|
1024
|
+
}) {
|
|
436
1025
|
const [owner] = useState(() => ({}));
|
|
437
1026
|
const predict = useRef(optimistic);
|
|
438
1027
|
useEffect(() => {
|
|
@@ -440,7 +1029,7 @@ function useTaggedMutation(client, invalidate, call, optimistic) {
|
|
|
440
1029
|
}, [optimistic]);
|
|
441
1030
|
const mutation = useMutation(
|
|
442
1031
|
{
|
|
443
|
-
meta: { taggedMutationOwner: owner },
|
|
1032
|
+
meta: { ...meta, taggedMutationOwner: owner },
|
|
444
1033
|
mutationFn: ({ input, prediction }) => executeTaggedMutation(invalidate, () => call(input), prediction)
|
|
445
1034
|
},
|
|
446
1035
|
client
|
|
@@ -477,8 +1066,33 @@ function useTaggedMutation(client, invalidate, call, optimistic) {
|
|
|
477
1066
|
reset: mutation.reset
|
|
478
1067
|
};
|
|
479
1068
|
}
|
|
1069
|
+
function hasRows(data) {
|
|
1070
|
+
return typeof data === "object" && data !== null && "result" in data && typeof data.result === "object" && data.result !== null;
|
|
1071
|
+
}
|
|
1072
|
+
var rowKey = (item) => Array.isArray(item) && typeof item[0] === "string" ? item[0] : void 0;
|
|
1073
|
+
var rowValue = (item) => Array.isArray(item) ? item[1] : void 0;
|
|
1074
|
+
function keyedRowsView(collection) {
|
|
1075
|
+
return {
|
|
1076
|
+
collection,
|
|
1077
|
+
slots: (data) => hasRows(data) ? [{ key: "result", items: Object.entries(data.result) }] : void 0,
|
|
1078
|
+
rebuild: (data, items) => hasRows(data) ? {
|
|
1079
|
+
...data,
|
|
1080
|
+
result: Object.fromEntries(
|
|
1081
|
+
(items.get("result") ?? []).flatMap((item) => {
|
|
1082
|
+
const key = rowKey(item);
|
|
1083
|
+
return key === void 0 ? [] : [[key, rowValue(item)]];
|
|
1084
|
+
})
|
|
1085
|
+
)
|
|
1086
|
+
} : data,
|
|
1087
|
+
keyOf: rowKey,
|
|
1088
|
+
read: (item, current, fresh) => current === void 0 || fresh ? rowValue(item) : current,
|
|
1089
|
+
write: rowValue,
|
|
1090
|
+
render: (item, entity) => Object.is(rowValue(item), entity) ? item : [rowKey(item), entity]
|
|
1091
|
+
};
|
|
1092
|
+
}
|
|
480
1093
|
function createTaggedKeyedQueryOwner(options) {
|
|
481
|
-
const { client, queryKey, scope, read } = options;
|
|
1094
|
+
const { client, queryKey, scope, collection, read } = options;
|
|
1095
|
+
registerKeyedView(client, queryKey, keyedRowsView(collection));
|
|
482
1096
|
const keys = /* @__PURE__ */ new Map();
|
|
483
1097
|
let readingKeys = /* @__PURE__ */ new Set();
|
|
484
1098
|
let missingOnly = false;
|
|
@@ -506,76 +1120,73 @@ function createTaggedKeyedQueryOwner(options) {
|
|
|
506
1120
|
}),
|
|
507
1121
|
queryFn: () => {
|
|
508
1122
|
let previous = getTaggedReadData(client, queryKey);
|
|
509
|
-
|
|
1123
|
+
const startedAt = /* @__PURE__ */ new Map();
|
|
510
1124
|
let refresh = !missingOnly && dirtyKeys.size === 0;
|
|
511
1125
|
missingOnly = false;
|
|
512
1126
|
const refreshedKeys = /* @__PURE__ */ new Set();
|
|
513
1127
|
let refreshedAll = false;
|
|
514
|
-
return runTaggedRead(
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
1128
|
+
return runTaggedRead(client, queryKey, async () => {
|
|
1129
|
+
const subscribed = await collectSubscribedKeys();
|
|
1130
|
+
const all = refreshAll || refresh;
|
|
1131
|
+
refresh = false;
|
|
1132
|
+
const dirty = new Set(dirtyKeys);
|
|
1133
|
+
for (const key of dirty) refreshedKeys.add(key);
|
|
1134
|
+
refreshedAll ||= all;
|
|
1135
|
+
dirtyKeys.clear();
|
|
1136
|
+
refreshAll = false;
|
|
1137
|
+
const requested = subscribed.filter(
|
|
1138
|
+
(key) => all || dirty.has(key) || !Object.hasOwn(previous?.result ?? {}, key)
|
|
1139
|
+
);
|
|
1140
|
+
readingKeys = new Set(requested);
|
|
1141
|
+
const started = tick();
|
|
1142
|
+
try {
|
|
1143
|
+
const results = await Promise.all(
|
|
1144
|
+
Array.from(
|
|
1145
|
+
{ length: Math.ceil(requested.length / 200) },
|
|
1146
|
+
async (_, i) => {
|
|
1147
|
+
const batch2 = requested.slice(i * 200, (i + 1) * 200);
|
|
1148
|
+
const response = await read(batch2);
|
|
1149
|
+
for (const key of batch2)
|
|
1150
|
+
if (!Object.hasOwn(response.result, key))
|
|
1151
|
+
throw new Error(`Keyed read omitted key ${key}`);
|
|
1152
|
+
return response;
|
|
1153
|
+
}
|
|
1154
|
+
)
|
|
528
1155
|
);
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
)
|
|
543
|
-
);
|
|
544
|
-
const retained = (key) => !all && !dirty.has(key) && !readingKeys.has(key);
|
|
545
|
-
loaded = requested;
|
|
546
|
-
previous = {
|
|
547
|
-
result: Object.fromEntries([
|
|
548
|
-
...Object.entries(previous?.result ?? {}).filter(
|
|
549
|
-
([key]) => retained(key)
|
|
550
|
-
),
|
|
551
|
-
...results.flatMap(
|
|
552
|
-
(response) => Object.entries(response.result)
|
|
553
|
-
)
|
|
554
|
-
]),
|
|
555
|
-
tags: [
|
|
556
|
-
.../* @__PURE__ */ new Set([
|
|
557
|
-
...all ? [] : previous?.tags ?? [],
|
|
558
|
-
...results.flatMap((r) => r.tags)
|
|
559
|
-
])
|
|
560
|
-
],
|
|
561
|
-
keyTags: Object.fromEntries([
|
|
562
|
-
...Object.entries(previous?.keyTags ?? {}).filter(
|
|
563
|
-
([key]) => retained(key)
|
|
564
|
-
),
|
|
565
|
-
...results.flatMap(
|
|
566
|
-
(response) => Object.entries(response.keyTags)
|
|
567
|
-
)
|
|
1156
|
+
const retained = (key) => !all && !dirty.has(key) && !readingKeys.has(key);
|
|
1157
|
+
for (const key of requested) startedAt.set(key, started);
|
|
1158
|
+
previous = {
|
|
1159
|
+
result: Object.fromEntries([
|
|
1160
|
+
...Object.entries(previous?.result ?? {}).filter(
|
|
1161
|
+
([key]) => retained(key)
|
|
1162
|
+
),
|
|
1163
|
+
...results.flatMap((response) => Object.entries(response.result))
|
|
1164
|
+
]),
|
|
1165
|
+
tags: [
|
|
1166
|
+
.../* @__PURE__ */ new Set([
|
|
1167
|
+
...all ? [] : previous?.tags ?? [],
|
|
1168
|
+
...results.flatMap((r) => r.tags)
|
|
568
1169
|
])
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
1170
|
+
],
|
|
1171
|
+
keyTags: Object.fromEntries([
|
|
1172
|
+
...Object.entries(previous?.keyTags ?? {}).filter(
|
|
1173
|
+
([key]) => retained(key)
|
|
1174
|
+
),
|
|
1175
|
+
...results.flatMap(
|
|
1176
|
+
(response) => Object.entries(response.keyTags)
|
|
1177
|
+
)
|
|
1178
|
+
])
|
|
1179
|
+
};
|
|
1180
|
+
return previous;
|
|
1181
|
+
} catch (error) {
|
|
1182
|
+
for (const key of refreshedKeys) dirtyKeys.add(key);
|
|
1183
|
+
refreshAll ||= refreshedAll;
|
|
1184
|
+
throw error;
|
|
1185
|
+
}
|
|
1186
|
+
}).then((data) => {
|
|
1187
|
+
recordKeyedRead(client, queryKey, startedAt);
|
|
1188
|
+
return data;
|
|
1189
|
+
});
|
|
579
1190
|
}
|
|
580
1191
|
});
|
|
581
1192
|
const listeners = /* @__PURE__ */ new Set();
|
|
@@ -634,7 +1245,7 @@ function useQuerySelection(subscribe, getSnapshot, select) {
|
|
|
634
1245
|
if (cached && Object.is(cached.snapshot, snapshot)) return cached.value;
|
|
635
1246
|
cached = {
|
|
636
1247
|
snapshot,
|
|
637
|
-
value:
|
|
1248
|
+
value: replaceEqualDeep3(
|
|
638
1249
|
cached?.value ?? committed.current?.value,
|
|
639
1250
|
select(snapshot)
|
|
640
1251
|
)
|
|
@@ -666,14 +1277,23 @@ var jsonObjectSchema = z.record(
|
|
|
666
1277
|
jsonValueSchema
|
|
667
1278
|
);
|
|
668
1279
|
|
|
669
|
-
// src/internal/rpc-
|
|
1280
|
+
// src/internal/rpc-tags.ts
|
|
670
1281
|
import { z as z2 } from "zod";
|
|
671
|
-
var
|
|
672
|
-
|
|
673
|
-
tags
|
|
674
|
-
keyTags
|
|
675
|
-
|
|
676
|
-
|
|
1282
|
+
var rpcInvalidationTags = z2.array(z2.string().min(1)).min(1);
|
|
1283
|
+
function rpcResponseTags(declarations, input, output) {
|
|
1284
|
+
const tags = z2.array(z2.string().min(1)).parse(declarations.tags?.(input, output) ?? []);
|
|
1285
|
+
const keyTags = {};
|
|
1286
|
+
if (declarations.keyTags && typeof output === "object" && output !== null && !Array.isArray(output)) {
|
|
1287
|
+
for (const [key, value] of Object.entries(output)) {
|
|
1288
|
+
keyTags[key] = z2.array(z2.string().min(1)).parse(declarations.keyTags(key, value));
|
|
1289
|
+
}
|
|
1290
|
+
}
|
|
1291
|
+
const invalidated = z2.array(z2.string().min(1)).parse(declarations.invalidates?.(input, output) ?? []);
|
|
1292
|
+
return { tags, keyTags, invalidated };
|
|
1293
|
+
}
|
|
1294
|
+
|
|
1295
|
+
// src/internal/rpc-query-host.ts
|
|
1296
|
+
import { z as z3 } from "zod";
|
|
677
1297
|
var rpcQueryKey = (pluginId, method, input) => [
|
|
678
1298
|
"plugin-rpc",
|
|
679
1299
|
pluginId,
|
|
@@ -681,47 +1301,70 @@ var rpcQueryKey = (pluginId, method, input) => [
|
|
|
681
1301
|
input
|
|
682
1302
|
];
|
|
683
1303
|
var keyedQueryKey = (pluginId, method) => rpcQueryKey(pluginId, method, { keyed: true });
|
|
1304
|
+
var keyedCollections = /* @__PURE__ */ new Map();
|
|
1305
|
+
function keyedCollection(pluginId, method) {
|
|
1306
|
+
const name = JSON.stringify([pluginId, method]);
|
|
1307
|
+
let collection = keyedCollections.get(name);
|
|
1308
|
+
if (!collection) {
|
|
1309
|
+
collection = new KeyedCollection(name);
|
|
1310
|
+
keyedCollections.set(name, collection);
|
|
1311
|
+
}
|
|
1312
|
+
return collection;
|
|
1313
|
+
}
|
|
1314
|
+
function parsedUpdate(update) {
|
|
1315
|
+
return (current) => jsonValueSchema.parse(update(current));
|
|
1316
|
+
}
|
|
684
1317
|
function optimisticCache(pluginId, updates) {
|
|
685
|
-
const add = (prediction) => {
|
|
686
|
-
updates.push({
|
|
687
|
-
...prediction,
|
|
688
|
-
update: (current) => jsonValueSchema.parse(prediction.update(current))
|
|
689
|
-
});
|
|
690
|
-
};
|
|
691
1318
|
return {
|
|
692
1319
|
update(method, input, update) {
|
|
693
|
-
|
|
1320
|
+
updates.push(
|
|
694
1321
|
optimisticQueryUpdate(
|
|
695
1322
|
rpcQueryKey(pluginId, method, jsonValueSchema.parse(input ?? null)),
|
|
696
|
-
update,
|
|
1323
|
+
parsedUpdate(update),
|
|
697
1324
|
["result"]
|
|
698
1325
|
)
|
|
699
1326
|
);
|
|
700
1327
|
},
|
|
701
1328
|
updateKey(method, key, update) {
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
key
|
|
706
|
-
|
|
1329
|
+
updates.push(
|
|
1330
|
+
optimisticKeyedUpdate(
|
|
1331
|
+
keyedCollection(pluginId, method),
|
|
1332
|
+
key,
|
|
1333
|
+
parsedUpdate(update)
|
|
1334
|
+
)
|
|
707
1335
|
);
|
|
708
1336
|
}
|
|
709
1337
|
};
|
|
710
1338
|
}
|
|
711
|
-
async function
|
|
1339
|
+
async function callRpc(host, pluginId, contract, method, input) {
|
|
712
1340
|
let raw;
|
|
713
1341
|
try {
|
|
714
1342
|
raw = await host.call(pluginId, method, input);
|
|
715
1343
|
} catch (error) {
|
|
716
1344
|
throw experimental_PluginRpcCallError.from(error);
|
|
717
1345
|
}
|
|
718
|
-
const
|
|
719
|
-
if (!
|
|
1346
|
+
const result = jsonValueSchema.safeParse(raw);
|
|
1347
|
+
if (!result.success)
|
|
720
1348
|
throw new experimental_PluginRpcCallError({
|
|
721
1349
|
code: "invalid_response",
|
|
722
1350
|
message: `Invalid RPC response for ${method}`
|
|
723
1351
|
});
|
|
724
|
-
|
|
1352
|
+
const methodContract = contract[method];
|
|
1353
|
+
if (methodContract === void 0)
|
|
1354
|
+
throw new experimental_PluginRpcCallError({
|
|
1355
|
+
code: "unknown_method",
|
|
1356
|
+
message: `rpc method "${method}" isn't in the contract`
|
|
1357
|
+
});
|
|
1358
|
+
const parsedInput = await methodContract.input["~standard"].validate(input);
|
|
1359
|
+
if (parsedInput.issues !== void 0)
|
|
1360
|
+
throw new experimental_PluginRpcCallError({
|
|
1361
|
+
code: "invalid_input",
|
|
1362
|
+
message: `rpc input validation failed for ${method}`
|
|
1363
|
+
});
|
|
1364
|
+
return {
|
|
1365
|
+
result: result.data,
|
|
1366
|
+
...rpcResponseTags(methodContract, parsedInput.value, result.data)
|
|
1367
|
+
};
|
|
725
1368
|
}
|
|
726
1369
|
var tagsOf = (data) => [
|
|
727
1370
|
...data.tags,
|
|
@@ -752,7 +1395,13 @@ function createRpcQueryHooks(useHost) {
|
|
|
752
1395
|
queryFn: () => runTaggedRead(
|
|
753
1396
|
host.queryClient,
|
|
754
1397
|
queryKey,
|
|
755
|
-
async () =>
|
|
1398
|
+
async () => callRpc(
|
|
1399
|
+
host,
|
|
1400
|
+
pluginId,
|
|
1401
|
+
options.contract,
|
|
1402
|
+
options.method,
|
|
1403
|
+
input
|
|
1404
|
+
)
|
|
756
1405
|
)
|
|
757
1406
|
},
|
|
758
1407
|
host.queryClient
|
|
@@ -776,7 +1425,7 @@ function createRpcQueryHooks(useHost) {
|
|
|
776
1425
|
const enabled = options.enabled ?? true;
|
|
777
1426
|
const identity = JSON.stringify([...new Set(options.keys)].sort());
|
|
778
1427
|
const keys = useMemo2(
|
|
779
|
-
() =>
|
|
1428
|
+
() => z3.array(z3.string()).parse(JSON.parse(identity)),
|
|
780
1429
|
[identity]
|
|
781
1430
|
);
|
|
782
1431
|
let cache = owners.get(host.queryClient);
|
|
@@ -791,16 +1440,18 @@ function createRpcQueryHooks(useHost) {
|
|
|
791
1440
|
client: host.queryClient,
|
|
792
1441
|
queryKey: keyedQueryKey(pluginId, options.method),
|
|
793
1442
|
scope: pluginTagScope(pluginId),
|
|
1443
|
+
collection: keyedCollection(pluginId, options.method),
|
|
794
1444
|
read: async (keys2) => {
|
|
795
|
-
const response = await
|
|
1445
|
+
const response = await callRpc(
|
|
796
1446
|
host,
|
|
797
1447
|
pluginId,
|
|
1448
|
+
options.contract,
|
|
798
1449
|
options.method,
|
|
799
1450
|
{ keys: [...keys2] }
|
|
800
1451
|
);
|
|
801
1452
|
return {
|
|
802
1453
|
...response,
|
|
803
|
-
result:
|
|
1454
|
+
result: z3.record(z3.string(), jsonValueSchema).parse(response.result)
|
|
804
1455
|
};
|
|
805
1456
|
}
|
|
806
1457
|
});
|
|
@@ -848,13 +1499,14 @@ function createRpcQueryHooks(useHost) {
|
|
|
848
1499
|
function experimental_useRpcMutation(options) {
|
|
849
1500
|
const host = useHost();
|
|
850
1501
|
const pluginId = options.pluginId ?? host.pluginId;
|
|
851
|
-
const mutation = useTaggedMutation(
|
|
852
|
-
host.queryClient,
|
|
853
|
-
(tags) => invalidateTags(host.queryClient, pluginTagScope(pluginId), tags),
|
|
854
|
-
async (input) => {
|
|
855
|
-
const response = await
|
|
1502
|
+
const mutation = useTaggedMutation({
|
|
1503
|
+
client: host.queryClient,
|
|
1504
|
+
invalidate: (tags) => invalidateTags(host.queryClient, pluginTagScope(pluginId), tags),
|
|
1505
|
+
call: async (input) => {
|
|
1506
|
+
const response = await callRpc(
|
|
856
1507
|
host,
|
|
857
1508
|
pluginId,
|
|
1509
|
+
options.contract,
|
|
858
1510
|
options.method,
|
|
859
1511
|
jsonValueSchema.parse(input)
|
|
860
1512
|
);
|
|
@@ -863,7 +1515,7 @@ function createRpcQueryHooks(useHost) {
|
|
|
863
1515
|
result: response.result
|
|
864
1516
|
};
|
|
865
1517
|
},
|
|
866
|
-
(input) => {
|
|
1518
|
+
optimistic: (input) => {
|
|
867
1519
|
const updates = [];
|
|
868
1520
|
options.optimistic?.(
|
|
869
1521
|
input,
|
|
@@ -871,7 +1523,7 @@ function createRpcQueryHooks(useHost) {
|
|
|
871
1523
|
);
|
|
872
1524
|
return updates;
|
|
873
1525
|
}
|
|
874
|
-
);
|
|
1526
|
+
});
|
|
875
1527
|
const { mutate } = mutation;
|
|
876
1528
|
return {
|
|
877
1529
|
...mutation,
|