@contello/store 4.1.1 → 4.3.0
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/dist/index.cjs +234 -21
- package/dist/index.d.cts +107 -57
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.ts +107 -57
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +244 -19
- package/dist/index.js.map +1 -1
- package/package.json +6 -6
package/dist/index.cjs
CHANGED
|
@@ -278,6 +278,52 @@ function createRefreshByTtlQueue() {
|
|
|
278
278
|
} };
|
|
279
279
|
}
|
|
280
280
|
//#endregion
|
|
281
|
+
//#region src/write-buffer.ts
|
|
282
|
+
function createWriteBuffer() {
|
|
283
|
+
const parked = /* @__PURE__ */ new Map();
|
|
284
|
+
const awaited = /* @__PURE__ */ new Map();
|
|
285
|
+
return {
|
|
286
|
+
park(id, entity) {
|
|
287
|
+
if (!awaited.has(id)) parked.set(id, entity);
|
|
288
|
+
},
|
|
289
|
+
release(ids) {
|
|
290
|
+
for (const id of ids) parked.delete(id);
|
|
291
|
+
},
|
|
292
|
+
clear() {
|
|
293
|
+
parked.clear();
|
|
294
|
+
},
|
|
295
|
+
take(ids) {
|
|
296
|
+
const written = [];
|
|
297
|
+
const missing = [];
|
|
298
|
+
for (const id of ids) {
|
|
299
|
+
const entity = parked.get(id);
|
|
300
|
+
if (entity === void 0) missing.push(id);
|
|
301
|
+
else {
|
|
302
|
+
parked.delete(id);
|
|
303
|
+
written.push(entity);
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
return {
|
|
307
|
+
written,
|
|
308
|
+
missing
|
|
309
|
+
};
|
|
310
|
+
},
|
|
311
|
+
awaitingUpstream(ids) {
|
|
312
|
+
for (const id of ids) {
|
|
313
|
+
parked.delete(id);
|
|
314
|
+
awaited.set(id, (awaited.get(id) ?? 0) + 1);
|
|
315
|
+
}
|
|
316
|
+
return () => {
|
|
317
|
+
for (const id of ids) {
|
|
318
|
+
const count = awaited.get(id) ?? 0;
|
|
319
|
+
if (count > 1) awaited.set(id, count - 1);
|
|
320
|
+
else awaited.delete(id);
|
|
321
|
+
}
|
|
322
|
+
};
|
|
323
|
+
}
|
|
324
|
+
};
|
|
325
|
+
}
|
|
326
|
+
//#endregion
|
|
281
327
|
//#region src/assets.ts
|
|
282
328
|
function mapFile$1(raw) {
|
|
283
329
|
return {
|
|
@@ -309,11 +355,29 @@ function createAssetsCollection(options, client, updates$, refreshByTtl) {
|
|
|
309
355
|
run: () => runFullRefresh("ttl")
|
|
310
356
|
});
|
|
311
357
|
let loaded = false;
|
|
312
|
-
const
|
|
358
|
+
const assetSource = {
|
|
359
|
+
...schema.sources.storeAsset,
|
|
360
|
+
mutations: _contello_client.BUILT_IN_MUTATIONS.asset
|
|
361
|
+
};
|
|
362
|
+
const assetsSourceDoc = (0, _contello_client.createSourceSubscription)(assetSource);
|
|
363
|
+
const writeBuffer = createWriteBuffer();
|
|
364
|
+
function fetchAssets(ids) {
|
|
365
|
+
return (0, _entwico_dash_async.concatAsync)((0, _entwico_dash_async.mapAsync)(client.subscribe(assetsSourceDoc, { ids }), (data) => data.source));
|
|
366
|
+
}
|
|
367
|
+
async function fetchOrTakeWritten(ids) {
|
|
368
|
+
if (ids === void 0) {
|
|
369
|
+
writeBuffer.clear();
|
|
370
|
+
return fetchAssets(void 0);
|
|
371
|
+
}
|
|
372
|
+
const { written, missing } = writeBuffer.take(ids);
|
|
373
|
+
if (missing.length === 0) return written;
|
|
374
|
+
const fetched = await fetchAssets(missing);
|
|
375
|
+
return written.length === 0 ? fetched : [...written, ...fetched];
|
|
376
|
+
}
|
|
313
377
|
const projected = new _entwico_projected.ProjectedMap({
|
|
314
378
|
key: (asset) => asset.id,
|
|
315
379
|
values: (ids) => wrap("assets", async () => {
|
|
316
|
-
const items = (await (
|
|
380
|
+
const items = (await fetchOrTakeWritten(ids)).map((item) => mapAsset$1(item));
|
|
317
381
|
if (ids === void 0 && !loaded) {
|
|
318
382
|
loaded = true;
|
|
319
383
|
ttl.mark();
|
|
@@ -340,10 +404,11 @@ function createAssetsCollection(options, client, updates$, refreshByTtl) {
|
|
|
340
404
|
emit(changedIds, "upstream-update");
|
|
341
405
|
return;
|
|
342
406
|
}
|
|
407
|
+
const settled = writeBuffer.awaitingUpstream(refreshIds);
|
|
343
408
|
(0, _entwico_dash_async.retryWithBackoff)(async () => {
|
|
344
409
|
await projected.refresh(refreshIds);
|
|
345
410
|
emit(changedIds, "upstream-update");
|
|
346
|
-
});
|
|
411
|
+
}).finally(settled);
|
|
347
412
|
}
|
|
348
413
|
const unsubUpdates = updates$.subscribe((batch) => {
|
|
349
414
|
if (!loaded || batch.asset.length === 0) return;
|
|
@@ -352,14 +417,46 @@ function createAssetsCollection(options, client, updates$, refreshByTtl) {
|
|
|
352
417
|
for (const event of batch.asset) if (event.mutation === "delete") deleted.push(event.id);
|
|
353
418
|
else upserted.add(event.id);
|
|
354
419
|
if (cache.eviction === "clear") {
|
|
420
|
+
writeBuffer.clear();
|
|
355
421
|
projected.clear();
|
|
356
422
|
return;
|
|
357
423
|
}
|
|
424
|
+
writeBuffer.release(deleted);
|
|
358
425
|
if (deleted.length > 0) projected.delete(deleted);
|
|
359
426
|
runPartialRefresh([...upserted], deleted);
|
|
360
427
|
});
|
|
428
|
+
async function runMutation(kind, values) {
|
|
429
|
+
const binding = assetSource.mutations?.[kind];
|
|
430
|
+
if (!binding) throw new Error(`@contello/store: assets cannot ${kind} — the schema exposes no ${kind} mutation for assets`);
|
|
431
|
+
const response = await client.execute((0, _contello_client.createSourceMutation)(assetSource, kind), (0, _contello_client.createSourceMutationVariables)(binding, values));
|
|
432
|
+
if (response?.result === void 0 || response.result === null) throw new Error(`@contello/store: ${kind} on assets returned nothing`);
|
|
433
|
+
return response.result;
|
|
434
|
+
}
|
|
361
435
|
return {
|
|
362
436
|
instance: {
|
|
437
|
+
async update(input) {
|
|
438
|
+
const raw = await runMutation("update", { input });
|
|
439
|
+
writeBuffer.park(raw.id, raw);
|
|
440
|
+
let map;
|
|
441
|
+
try {
|
|
442
|
+
map = await projected.refresh([raw.id]);
|
|
443
|
+
} finally {
|
|
444
|
+
writeBuffer.release([raw.id]);
|
|
445
|
+
}
|
|
446
|
+
emit([raw.id], "write");
|
|
447
|
+
const asset = map.get(raw.id);
|
|
448
|
+
if (!asset) throw new Error(`@contello/store: wrote asset "${raw.id}" but it is not in the cache — a concurrent full refresh did not return it`);
|
|
449
|
+
return asset;
|
|
450
|
+
},
|
|
451
|
+
async delete(id) {
|
|
452
|
+
await runMutation("delete", {
|
|
453
|
+
input: { id },
|
|
454
|
+
id
|
|
455
|
+
});
|
|
456
|
+
writeBuffer.release([id]);
|
|
457
|
+
projected.delete([id]);
|
|
458
|
+
emit([id], "write");
|
|
459
|
+
},
|
|
363
460
|
refresh$: channel.stream$,
|
|
364
461
|
async load() {
|
|
365
462
|
if (!loaded) await projected.getAllAsMap();
|
|
@@ -557,6 +654,19 @@ var DependencyCollector = class {
|
|
|
557
654
|
function fetchCollection(source, client, ids) {
|
|
558
655
|
return (0, _entwico_dash_async.concatAsync)((0, _entwico_dash_async.mapAsync)(client.subscribe((0, _contello_client.createSourceSubscription)(source), { ids }), (r) => r.source));
|
|
559
656
|
}
|
|
657
|
+
/**
|
|
658
|
+
* Runs one write mutation of the source. A create/update selects the source's fragment, so the
|
|
659
|
+
* answer is a raw entity in exactly the shape the source subscription yields — the cache takes it
|
|
660
|
+
* from there. A delete only carries an id.
|
|
661
|
+
*/
|
|
662
|
+
async function runMutation(source, client, name, kind, values) {
|
|
663
|
+
const binding = source.mutations?.[kind];
|
|
664
|
+
if (!binding) throw new Error(`@contello/store: collection "${name}" cannot ${kind} — the schema exposes no ${kind} mutation for model "${source.__model}"`);
|
|
665
|
+
const document = (0, _contello_client.createSourceMutation)(source, kind);
|
|
666
|
+
const response = await client.execute(document, (0, _contello_client.createSourceMutationVariables)(binding, values));
|
|
667
|
+
if (!response?.result) throw new Error(`@contello/store: ${kind} on collection "${name}" returned no entity`);
|
|
668
|
+
return response.result;
|
|
669
|
+
}
|
|
560
670
|
function createCollection(source, options, client, updates$, resolver, refreshByTtl) {
|
|
561
671
|
const opts = options ?? {};
|
|
562
672
|
const mapFn = opts.map ?? ((item) => item);
|
|
@@ -575,9 +685,20 @@ function createCollection(source, options, client, updates$, resolver, refreshBy
|
|
|
575
685
|
run: () => runFullRefresh("ttl")
|
|
576
686
|
});
|
|
577
687
|
let loaded = false;
|
|
688
|
+
const writeBuffer = createWriteBuffer();
|
|
689
|
+
function fetchOrTakeWritten(ids) {
|
|
690
|
+
if (ids === void 0) {
|
|
691
|
+
writeBuffer.clear();
|
|
692
|
+
return fetchCollection(source, client, void 0);
|
|
693
|
+
}
|
|
694
|
+
const { written, missing } = writeBuffer.take(ids);
|
|
695
|
+
if (missing.length === 0) return written;
|
|
696
|
+
if (written.length === 0) return fetchCollection(source, client, missing);
|
|
697
|
+
return (0, _entwico_dash.maybeThen)(fetchCollection(source, client, missing), (fetched) => [...written, ...fetched]);
|
|
698
|
+
}
|
|
578
699
|
const projected = new _entwico_projected.ProjectedMap({
|
|
579
700
|
key: (item) => item.id,
|
|
580
|
-
values: (ids) => wrap(`collection:${_def.name}`, () => (0, _entwico_dash.maybeThen)(
|
|
701
|
+
values: (ids) => wrap(`collection:${_def.name}`, () => (0, _entwico_dash.maybeThen)(fetchOrTakeWritten(ids), async (rawItems) => {
|
|
581
702
|
const items = await (0, _entwico_dash.maybeAll)(rawItems.map((item) => dependencyCollector.createContext((ref, register) => (0, _entwico_dash.maybeThen)(mapFn(item, ref), (mapped) => {
|
|
582
703
|
register(mapped.id);
|
|
583
704
|
return mapped;
|
|
@@ -615,10 +736,11 @@ function createCollection(source, options, client, updates$, resolver, refreshBy
|
|
|
615
736
|
emit(changedIds, "upstream-update");
|
|
616
737
|
return;
|
|
617
738
|
}
|
|
739
|
+
const settled = writeBuffer.awaitingUpstream(refreshIds);
|
|
618
740
|
(0, _entwico_dash_async.retryWithBackoff)(async () => {
|
|
619
741
|
await projected.refresh(refreshIds);
|
|
620
742
|
emit(changedIds, "upstream-update");
|
|
621
|
-
});
|
|
743
|
+
}).finally(settled);
|
|
622
744
|
}
|
|
623
745
|
const unsubUpdates = updates$.subscribe((batch) => {
|
|
624
746
|
if (!loaded) return;
|
|
@@ -632,15 +754,55 @@ function createCollection(source, options, client, updates$, resolver, refreshBy
|
|
|
632
754
|
if (deleted.length === 0 && upserted.size === 0 && depAffected.size === 0) return;
|
|
633
755
|
if (_def.cache.eviction === "clear") {
|
|
634
756
|
dependencyCollector.clear();
|
|
757
|
+
writeBuffer.clear();
|
|
635
758
|
projected.clear();
|
|
636
759
|
return;
|
|
637
760
|
}
|
|
761
|
+
writeBuffer.release(deleted);
|
|
638
762
|
for (const id of deleted) dependencyCollector.removeItem(id);
|
|
639
763
|
if (deleted.length > 0) projected.delete(deleted);
|
|
640
764
|
const refreshIds = upserted.union(depAffected);
|
|
641
765
|
for (const id of deleted) refreshIds.delete(id);
|
|
642
766
|
runPartialRefresh([...refreshIds], deleted);
|
|
643
767
|
});
|
|
768
|
+
/** Puts a written entity into the cache through the normal refresh path and returns it mapped. */
|
|
769
|
+
async function takeIntoCache(entity) {
|
|
770
|
+
writeBuffer.park(entity.id, entity);
|
|
771
|
+
let map;
|
|
772
|
+
try {
|
|
773
|
+
map = await projected.refresh([entity.id]);
|
|
774
|
+
} finally {
|
|
775
|
+
writeBuffer.release([entity.id]);
|
|
776
|
+
}
|
|
777
|
+
emit([entity.id], "write");
|
|
778
|
+
const item = map.get(entity.id);
|
|
779
|
+
if (!item) throw new Error(`@contello/store: collection "${_def.name}" wrote entity "${entity.id}" but it is not in the cache — a concurrent full refresh did not return it`);
|
|
780
|
+
return item;
|
|
781
|
+
}
|
|
782
|
+
/**
|
|
783
|
+
* One method per mutation the source binds — a model the schema exposes no `createX` for has
|
|
784
|
+
* no `create`, matching what `CollectionWrites` types. Permission is not decided here: the
|
|
785
|
+
* server rejects a write the token may not run.
|
|
786
|
+
*/
|
|
787
|
+
function createWrites() {
|
|
788
|
+
const out = {};
|
|
789
|
+
if (source.mutations?.create) out["create"] = async (input) => takeIntoCache(await runMutation(source, client, _def.name, "create", { input }));
|
|
790
|
+
if (source.mutations?.update) out["update"] = async (input) => takeIntoCache(await runMutation(source, client, _def.name, "update", { input }));
|
|
791
|
+
if (source.mutations?.delete) out["delete"] = async (id, options) => {
|
|
792
|
+
await runMutation(source, client, _def.name, "delete", {
|
|
793
|
+
input: {
|
|
794
|
+
...options,
|
|
795
|
+
id
|
|
796
|
+
},
|
|
797
|
+
id
|
|
798
|
+
});
|
|
799
|
+
writeBuffer.release([id]);
|
|
800
|
+
dependencyCollector.removeItem(id);
|
|
801
|
+
projected.delete([id]);
|
|
802
|
+
emit([id], "write");
|
|
803
|
+
};
|
|
804
|
+
return out;
|
|
805
|
+
}
|
|
644
806
|
return {
|
|
645
807
|
instance: {
|
|
646
808
|
name: _def.name,
|
|
@@ -656,7 +818,8 @@ function createCollection(source, options, client, updates$, resolver, refreshBy
|
|
|
656
818
|
},
|
|
657
819
|
async load() {
|
|
658
820
|
if (!loaded) await projected.getAllAsMap();
|
|
659
|
-
}
|
|
821
|
+
},
|
|
822
|
+
...createWrites()
|
|
660
823
|
},
|
|
661
824
|
destroy() {
|
|
662
825
|
unsubUpdates();
|
|
@@ -1262,11 +1425,29 @@ function createRoutesCollection(options, client, updates$, resolver, refreshByTt
|
|
|
1262
1425
|
const pathById = /* @__PURE__ */ new Map();
|
|
1263
1426
|
const idByPath = /* @__PURE__ */ new Map();
|
|
1264
1427
|
let loaded = false;
|
|
1265
|
-
const
|
|
1428
|
+
const routeSource = {
|
|
1429
|
+
...schema.sources.storeRoute,
|
|
1430
|
+
mutations: _contello_client.BUILT_IN_MUTATIONS.route
|
|
1431
|
+
};
|
|
1432
|
+
const routesSourceDoc = (0, _contello_client.createSourceSubscription)(routeSource);
|
|
1433
|
+
const writeBuffer = createWriteBuffer();
|
|
1434
|
+
function fetchRoutes(ids) {
|
|
1435
|
+
return (0, _entwico_dash_async.concatAsync)((0, _entwico_dash_async.mapAsync)(client.subscribe(routesSourceDoc, { ids }), (data) => data.source));
|
|
1436
|
+
}
|
|
1437
|
+
async function fetchOrTakeWritten(ids) {
|
|
1438
|
+
if (ids === void 0) {
|
|
1439
|
+
writeBuffer.clear();
|
|
1440
|
+
return fetchRoutes(void 0);
|
|
1441
|
+
}
|
|
1442
|
+
const { written, missing } = writeBuffer.take(ids);
|
|
1443
|
+
if (missing.length === 0) return written;
|
|
1444
|
+
const fetched = await fetchRoutes(missing);
|
|
1445
|
+
return written.length === 0 ? fetched : [...written, ...fetched];
|
|
1446
|
+
}
|
|
1266
1447
|
const projected = new _entwico_projected.ProjectedMap({
|
|
1267
1448
|
key: (route) => route.id,
|
|
1268
1449
|
values: (ids) => wrap("routes", async () => {
|
|
1269
|
-
const items = (await (
|
|
1450
|
+
const items = (await fetchOrTakeWritten(ids)).reduce((acc, raw) => {
|
|
1270
1451
|
const mapped = mapRoute(raw, resolver);
|
|
1271
1452
|
if (mapped) acc.push(mapped);
|
|
1272
1453
|
return acc;
|
|
@@ -1321,10 +1502,11 @@ function createRoutesCollection(options, client, updates$, resolver, refreshByTt
|
|
|
1321
1502
|
emit(changedIds, "upstream-update");
|
|
1322
1503
|
return;
|
|
1323
1504
|
}
|
|
1505
|
+
const settled = writeBuffer.awaitingUpstream(refreshIds);
|
|
1324
1506
|
(0, _entwico_dash_async.retryWithBackoff)(async () => {
|
|
1325
1507
|
await projected.refresh(refreshIds);
|
|
1326
1508
|
emit(changedIds, "upstream-update");
|
|
1327
|
-
});
|
|
1509
|
+
}).finally(settled);
|
|
1328
1510
|
}
|
|
1329
1511
|
const unsubUpdates = updates$.subscribe((batch) => {
|
|
1330
1512
|
if (!loaded || batch.route.length === 0) return;
|
|
@@ -1335,9 +1517,11 @@ function createRoutesCollection(options, client, updates$, resolver, refreshByTt
|
|
|
1335
1517
|
if (cache.eviction === "clear") {
|
|
1336
1518
|
pathById.clear();
|
|
1337
1519
|
idByPath.clear();
|
|
1520
|
+
writeBuffer.clear();
|
|
1338
1521
|
projected.clear();
|
|
1339
1522
|
return;
|
|
1340
1523
|
}
|
|
1524
|
+
writeBuffer.release(deleted);
|
|
1341
1525
|
for (const id of deleted) {
|
|
1342
1526
|
const oldPath = pathById.get(id);
|
|
1343
1527
|
if (oldPath !== void 0) {
|
|
@@ -1374,8 +1558,49 @@ function createRoutesCollection(options, client, updates$, resolver, refreshByTt
|
|
|
1374
1558
|
if (loaded) return;
|
|
1375
1559
|
return (0, _entwico_dash.maybeThen)(projected.getAllAsMap(), () => {});
|
|
1376
1560
|
}
|
|
1561
|
+
async function runMutation(kind, values) {
|
|
1562
|
+
const binding = routeSource.mutations?.[kind];
|
|
1563
|
+
if (!binding) throw new Error(`@contello/store: routes cannot ${kind} — the schema exposes no ${kind} mutation for routes`);
|
|
1564
|
+
const response = await client.execute((0, _contello_client.createSourceMutation)(routeSource, kind), (0, _contello_client.createSourceMutationVariables)(binding, values));
|
|
1565
|
+
if (response?.result === void 0 || response.result === null) throw new Error(`@contello/store: ${kind} on routes returned nothing`);
|
|
1566
|
+
return response.result;
|
|
1567
|
+
}
|
|
1568
|
+
/** Takes a written route into the cache through the normal refresh path, keeping both indexes. */
|
|
1569
|
+
async function takeIntoCache(raw) {
|
|
1570
|
+
writeBuffer.park(raw.id, raw);
|
|
1571
|
+
let map;
|
|
1572
|
+
try {
|
|
1573
|
+
map = await projected.refresh([raw.id]);
|
|
1574
|
+
} finally {
|
|
1575
|
+
writeBuffer.release([raw.id]);
|
|
1576
|
+
}
|
|
1577
|
+
emit([raw.id], "write");
|
|
1578
|
+
const route = map.get(raw.id);
|
|
1579
|
+
if (!route) throw new Error(`@contello/store: wrote route "${raw.id}" but it is not in the cache — its target may not have resolved, or a concurrent full refresh did not return it`);
|
|
1580
|
+
return route;
|
|
1581
|
+
}
|
|
1377
1582
|
return {
|
|
1378
1583
|
instance: {
|
|
1584
|
+
async create(input) {
|
|
1585
|
+
return takeIntoCache(await runMutation("create", { input }));
|
|
1586
|
+
},
|
|
1587
|
+
async update(input) {
|
|
1588
|
+
return takeIntoCache(await runMutation("update", { input }));
|
|
1589
|
+
},
|
|
1590
|
+
async delete(id) {
|
|
1591
|
+
await runMutation("delete", {
|
|
1592
|
+
input: { id },
|
|
1593
|
+
id
|
|
1594
|
+
});
|
|
1595
|
+
writeBuffer.release([id]);
|
|
1596
|
+
const oldPath = pathById.get(id);
|
|
1597
|
+
if (oldPath !== void 0) {
|
|
1598
|
+
idByPath.delete(oldPath);
|
|
1599
|
+
pathById.delete(id);
|
|
1600
|
+
}
|
|
1601
|
+
projected.delete([id]);
|
|
1602
|
+
emit([id], "write");
|
|
1603
|
+
},
|
|
1379
1604
|
refresh$: channel.stream$,
|
|
1380
1605
|
async load() {
|
|
1381
1606
|
if (!loaded) await projected.getAllAsMap();
|
|
@@ -1774,15 +1999,3 @@ function createStore(options) {
|
|
|
1774
1999
|
//#endregion
|
|
1775
2000
|
exports.Store = Store;
|
|
1776
2001
|
exports.createStore = createStore;
|
|
1777
|
-
Object.defineProperty(exports, "maybeCatch", {
|
|
1778
|
-
enumerable: true,
|
|
1779
|
-
get: function() {
|
|
1780
|
-
return _entwico_dash.maybeCatch;
|
|
1781
|
-
}
|
|
1782
|
-
});
|
|
1783
|
-
Object.defineProperty(exports, "maybeThen", {
|
|
1784
|
-
enumerable: true,
|
|
1785
|
-
get: function() {
|
|
1786
|
-
return _entwico_dash.maybeThen;
|
|
1787
|
-
}
|
|
1788
|
-
});
|