@contello/store 4.2.0 → 4.4.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/fragments.d.ts.map +1 -1
- package/dist/fragments.js.map +1 -1
- package/dist/index.cjs +294 -11
- package/dist/index.d.cts +99 -27
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.ts +99 -28
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +295 -12
- package/dist/index.js.map +1 -1
- package/fragments/route.gql +3 -0
- package/package.json +6 -6
package/dist/fragments.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fragments.d.ts","names":[],"sources":["../src/fragments.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"fragments.d.ts","names":[],"sources":["../src/fragments.ts"],"mappings":";;;;;;;cAYa"}
|
package/dist/fragments.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fragments.js","names":["mediaFragments"],"sources":["../src/fragments.ts"],"sourcesContent":["import { fileURLToPath } from 'node:url';\n\nimport { fragments as mediaFragments } from '@contello/media/fragments';\n\nconst packageRoot = fileURLToPath(new URL('..', import.meta.url));\n\n/**\n * resolved absolute paths to the `.gql` fragment files that store-backed\n * codegen should include. combines media's primitives with store's own\n * `StoreAsset` / `StoreRoute` fragments. intended for use in a project's\n * codegen `documents` config.\n */\nexport const fragments: string[] = [\n ...mediaFragments,\n `${packageRoot}fragments/asset.gql`,\n `${packageRoot}fragments/route.gql`,\n];\n"],"mappings":";;;AAIA,MAAM,cAAc,cAAc,IAAI,IAAI,MAAM,
|
|
1
|
+
{"version":3,"file":"fragments.js","names":["mediaFragments"],"sources":["../src/fragments.ts"],"sourcesContent":["import { fileURLToPath } from 'node:url';\n\nimport { fragments as mediaFragments } from '@contello/media/fragments';\n\nconst packageRoot = fileURLToPath(new URL('..', import.meta.url));\n\n/**\n * resolved absolute paths to the `.gql` fragment files that store-backed\n * codegen should include. combines media's primitives with store's own\n * `StoreAsset` / `StoreRoute` fragments. intended for use in a project's\n * codegen `documents` config.\n */\nexport const fragments: string[] = [\n ...mediaFragments,\n `${packageRoot}fragments/asset.gql`,\n `${packageRoot}fragments/route.gql`,\n];\n"],"mappings":";;;AAIA,MAAM,cAAc,cAAc,IAAI,IAAI,MAAM,YAAY,GAAG,CAAC;;;;;;;AAQhE,MAAa,YAAsB;CACjC,GAAGA;CACH,GAAG,YAAY;CACf,GAAG,YAAY;AACjB"}
|
package/dist/index.cjs
CHANGED
|
@@ -83,6 +83,9 @@ const StoreRouteFragmentSchema = `fragment StoreRoute on ContelloRoute {
|
|
|
83
83
|
content
|
|
84
84
|
mimeType
|
|
85
85
|
}
|
|
86
|
+
... on ContelloRouteTargetSitemap {
|
|
87
|
+
sitemapId
|
|
88
|
+
}
|
|
86
89
|
}
|
|
87
90
|
customHeaders {
|
|
88
91
|
name
|
|
@@ -146,6 +149,26 @@ const schema = {
|
|
|
146
149
|
document: `${MediaFileFragmentSchema}\n${MediaAssetFragmentSchema}\n${StoreAssetFragmentSchema}`,
|
|
147
150
|
fragment: "StoreAsset",
|
|
148
151
|
subscription: "contelloAssetsBatch",
|
|
152
|
+
mutations: {
|
|
153
|
+
update: {
|
|
154
|
+
field: "updateContelloAsset",
|
|
155
|
+
arguments: [{
|
|
156
|
+
name: "request",
|
|
157
|
+
type: "ContelloAssetUpdateInput!",
|
|
158
|
+
from: "input"
|
|
159
|
+
}],
|
|
160
|
+
result: "entity"
|
|
161
|
+
},
|
|
162
|
+
delete: {
|
|
163
|
+
field: "deleteContelloAsset",
|
|
164
|
+
arguments: [{
|
|
165
|
+
name: "id",
|
|
166
|
+
type: "String!",
|
|
167
|
+
from: "id"
|
|
168
|
+
}],
|
|
169
|
+
result: "idObject"
|
|
170
|
+
}
|
|
171
|
+
},
|
|
149
172
|
__model: "storeAsset",
|
|
150
173
|
__cardinality: "asset"
|
|
151
174
|
},
|
|
@@ -160,6 +183,35 @@ const schema = {
|
|
|
160
183
|
document: StoreRouteFragmentSchema,
|
|
161
184
|
fragment: "StoreRoute",
|
|
162
185
|
subscription: "contelloRoutesBatch",
|
|
186
|
+
mutations: {
|
|
187
|
+
create: {
|
|
188
|
+
field: "createContelloRoute",
|
|
189
|
+
arguments: [{
|
|
190
|
+
name: "route",
|
|
191
|
+
type: "ContelloRouteInput!",
|
|
192
|
+
from: "input"
|
|
193
|
+
}],
|
|
194
|
+
result: "entity"
|
|
195
|
+
},
|
|
196
|
+
update: {
|
|
197
|
+
field: "updateContelloRoute",
|
|
198
|
+
arguments: [{
|
|
199
|
+
name: "route",
|
|
200
|
+
type: "ContelloRouteInput!",
|
|
201
|
+
from: "input"
|
|
202
|
+
}],
|
|
203
|
+
result: "entity"
|
|
204
|
+
},
|
|
205
|
+
delete: {
|
|
206
|
+
field: "deleteContelloRoute",
|
|
207
|
+
arguments: [{
|
|
208
|
+
name: "id",
|
|
209
|
+
type: "String",
|
|
210
|
+
from: "id"
|
|
211
|
+
}],
|
|
212
|
+
result: "idScalar"
|
|
213
|
+
}
|
|
214
|
+
},
|
|
163
215
|
__model: "storeRoute",
|
|
164
216
|
__cardinality: "route"
|
|
165
217
|
}
|
|
@@ -176,7 +228,7 @@ const { wrap } = (0, _contello_opentelemetry.createOperationTelemetry)("@contell
|
|
|
176
228
|
* use it as the interval for a periodic full refresh; lazy stores use it as the per-item
|
|
177
229
|
* LRU eviction window. 3 hours.
|
|
178
230
|
*/
|
|
179
|
-
const DEFAULT_TTL_MS =
|
|
231
|
+
const DEFAULT_TTL_MS = 108e5;
|
|
180
232
|
/**
|
|
181
233
|
* resolves the consumer-supplied `ttl` (number | false | undefined) into a runtime
|
|
182
234
|
* value: undefined → default, false or 0 → disabled (undefined), positive → as-is.
|
|
@@ -278,6 +330,52 @@ function createRefreshByTtlQueue() {
|
|
|
278
330
|
} };
|
|
279
331
|
}
|
|
280
332
|
//#endregion
|
|
333
|
+
//#region src/write-buffer.ts
|
|
334
|
+
function createWriteBuffer() {
|
|
335
|
+
const parked = /* @__PURE__ */ new Map();
|
|
336
|
+
const awaited = /* @__PURE__ */ new Map();
|
|
337
|
+
return {
|
|
338
|
+
park(id, entity) {
|
|
339
|
+
if (!awaited.has(id)) parked.set(id, entity);
|
|
340
|
+
},
|
|
341
|
+
release(ids) {
|
|
342
|
+
for (const id of ids) parked.delete(id);
|
|
343
|
+
},
|
|
344
|
+
clear() {
|
|
345
|
+
parked.clear();
|
|
346
|
+
},
|
|
347
|
+
take(ids) {
|
|
348
|
+
const written = [];
|
|
349
|
+
const missing = [];
|
|
350
|
+
for (const id of ids) {
|
|
351
|
+
const entity = parked.get(id);
|
|
352
|
+
if (entity === void 0) missing.push(id);
|
|
353
|
+
else {
|
|
354
|
+
parked.delete(id);
|
|
355
|
+
written.push(entity);
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
return {
|
|
359
|
+
written,
|
|
360
|
+
missing
|
|
361
|
+
};
|
|
362
|
+
},
|
|
363
|
+
awaitingUpstream(ids) {
|
|
364
|
+
for (const id of ids) {
|
|
365
|
+
parked.delete(id);
|
|
366
|
+
awaited.set(id, (awaited.get(id) ?? 0) + 1);
|
|
367
|
+
}
|
|
368
|
+
return () => {
|
|
369
|
+
for (const id of ids) {
|
|
370
|
+
const count = awaited.get(id) ?? 0;
|
|
371
|
+
if (count > 1) awaited.set(id, count - 1);
|
|
372
|
+
else awaited.delete(id);
|
|
373
|
+
}
|
|
374
|
+
};
|
|
375
|
+
}
|
|
376
|
+
};
|
|
377
|
+
}
|
|
378
|
+
//#endregion
|
|
281
379
|
//#region src/assets.ts
|
|
282
380
|
function mapFile$1(raw) {
|
|
283
381
|
return {
|
|
@@ -309,11 +407,29 @@ function createAssetsCollection(options, client, updates$, refreshByTtl) {
|
|
|
309
407
|
run: () => runFullRefresh("ttl")
|
|
310
408
|
});
|
|
311
409
|
let loaded = false;
|
|
312
|
-
const
|
|
410
|
+
const assetSource = {
|
|
411
|
+
...schema.sources.storeAsset,
|
|
412
|
+
mutations: _contello_client.BUILT_IN_MUTATIONS.asset
|
|
413
|
+
};
|
|
414
|
+
const assetsSourceDoc = (0, _contello_client.createSourceSubscription)(assetSource);
|
|
415
|
+
const writeBuffer = createWriteBuffer();
|
|
416
|
+
function fetchAssets(ids) {
|
|
417
|
+
return (0, _entwico_dash_async.concatAsync)((0, _entwico_dash_async.mapAsync)(client.subscribe(assetsSourceDoc, { ids }), (data) => data.source));
|
|
418
|
+
}
|
|
419
|
+
async function fetchOrTakeWritten(ids) {
|
|
420
|
+
if (ids === void 0) {
|
|
421
|
+
writeBuffer.clear();
|
|
422
|
+
return fetchAssets(void 0);
|
|
423
|
+
}
|
|
424
|
+
const { written, missing } = writeBuffer.take(ids);
|
|
425
|
+
if (missing.length === 0) return written;
|
|
426
|
+
const fetched = await fetchAssets(missing);
|
|
427
|
+
return written.length === 0 ? fetched : [...written, ...fetched];
|
|
428
|
+
}
|
|
313
429
|
const projected = new _entwico_projected.ProjectedMap({
|
|
314
430
|
key: (asset) => asset.id,
|
|
315
431
|
values: (ids) => wrap("assets", async () => {
|
|
316
|
-
const items = (await (
|
|
432
|
+
const items = (await fetchOrTakeWritten(ids)).map((item) => mapAsset$1(item));
|
|
317
433
|
if (ids === void 0 && !loaded) {
|
|
318
434
|
loaded = true;
|
|
319
435
|
ttl.mark();
|
|
@@ -340,10 +456,11 @@ function createAssetsCollection(options, client, updates$, refreshByTtl) {
|
|
|
340
456
|
emit(changedIds, "upstream-update");
|
|
341
457
|
return;
|
|
342
458
|
}
|
|
459
|
+
const settled = writeBuffer.awaitingUpstream(refreshIds);
|
|
343
460
|
(0, _entwico_dash_async.retryWithBackoff)(async () => {
|
|
344
461
|
await projected.refresh(refreshIds);
|
|
345
462
|
emit(changedIds, "upstream-update");
|
|
346
|
-
});
|
|
463
|
+
}).finally(settled);
|
|
347
464
|
}
|
|
348
465
|
const unsubUpdates = updates$.subscribe((batch) => {
|
|
349
466
|
if (!loaded || batch.asset.length === 0) return;
|
|
@@ -352,14 +469,46 @@ function createAssetsCollection(options, client, updates$, refreshByTtl) {
|
|
|
352
469
|
for (const event of batch.asset) if (event.mutation === "delete") deleted.push(event.id);
|
|
353
470
|
else upserted.add(event.id);
|
|
354
471
|
if (cache.eviction === "clear") {
|
|
472
|
+
writeBuffer.clear();
|
|
355
473
|
projected.clear();
|
|
356
474
|
return;
|
|
357
475
|
}
|
|
476
|
+
writeBuffer.release(deleted);
|
|
358
477
|
if (deleted.length > 0) projected.delete(deleted);
|
|
359
478
|
runPartialRefresh([...upserted], deleted);
|
|
360
479
|
});
|
|
480
|
+
async function runMutation(kind, values) {
|
|
481
|
+
const binding = assetSource.mutations?.[kind];
|
|
482
|
+
if (!binding) throw new Error(`@contello/store: assets cannot ${kind} — the schema exposes no ${kind} mutation for assets`);
|
|
483
|
+
const response = await client.execute((0, _contello_client.createSourceMutation)(assetSource, kind), (0, _contello_client.createSourceMutationVariables)(binding, values));
|
|
484
|
+
if (response?.result === void 0 || response.result === null) throw new Error(`@contello/store: ${kind} on assets returned nothing`);
|
|
485
|
+
return response.result;
|
|
486
|
+
}
|
|
361
487
|
return {
|
|
362
488
|
instance: {
|
|
489
|
+
async update(input) {
|
|
490
|
+
const raw = await runMutation("update", { input });
|
|
491
|
+
writeBuffer.park(raw.id, raw);
|
|
492
|
+
let map;
|
|
493
|
+
try {
|
|
494
|
+
map = await projected.refresh([raw.id]);
|
|
495
|
+
} finally {
|
|
496
|
+
writeBuffer.release([raw.id]);
|
|
497
|
+
}
|
|
498
|
+
emit([raw.id], "write");
|
|
499
|
+
const asset = map.get(raw.id);
|
|
500
|
+
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`);
|
|
501
|
+
return asset;
|
|
502
|
+
},
|
|
503
|
+
async delete(id) {
|
|
504
|
+
await runMutation("delete", {
|
|
505
|
+
input: { id },
|
|
506
|
+
id
|
|
507
|
+
});
|
|
508
|
+
writeBuffer.release([id]);
|
|
509
|
+
projected.delete([id]);
|
|
510
|
+
emit([id], "write");
|
|
511
|
+
},
|
|
363
512
|
refresh$: channel.stream$,
|
|
364
513
|
async load() {
|
|
365
514
|
if (!loaded) await projected.getAllAsMap();
|
|
@@ -557,6 +706,19 @@ var DependencyCollector = class {
|
|
|
557
706
|
function fetchCollection(source, client, ids) {
|
|
558
707
|
return (0, _entwico_dash_async.concatAsync)((0, _entwico_dash_async.mapAsync)(client.subscribe((0, _contello_client.createSourceSubscription)(source), { ids }), (r) => r.source));
|
|
559
708
|
}
|
|
709
|
+
/**
|
|
710
|
+
* Runs one write mutation of the source. A create/update selects the source's fragment, so the
|
|
711
|
+
* answer is a raw entity in exactly the shape the source subscription yields — the cache takes it
|
|
712
|
+
* from there. A delete only carries an id.
|
|
713
|
+
*/
|
|
714
|
+
async function runMutation(source, client, name, kind, values) {
|
|
715
|
+
const binding = source.mutations?.[kind];
|
|
716
|
+
if (!binding) throw new Error(`@contello/store: collection "${name}" cannot ${kind} — the schema exposes no ${kind} mutation for model "${source.__model}"`);
|
|
717
|
+
const document = (0, _contello_client.createSourceMutation)(source, kind);
|
|
718
|
+
const response = await client.execute(document, (0, _contello_client.createSourceMutationVariables)(binding, values));
|
|
719
|
+
if (!response?.result) throw new Error(`@contello/store: ${kind} on collection "${name}" returned no entity`);
|
|
720
|
+
return response.result;
|
|
721
|
+
}
|
|
560
722
|
function createCollection(source, options, client, updates$, resolver, refreshByTtl) {
|
|
561
723
|
const opts = options ?? {};
|
|
562
724
|
const mapFn = opts.map ?? ((item) => item);
|
|
@@ -575,9 +737,20 @@ function createCollection(source, options, client, updates$, resolver, refreshBy
|
|
|
575
737
|
run: () => runFullRefresh("ttl")
|
|
576
738
|
});
|
|
577
739
|
let loaded = false;
|
|
740
|
+
const writeBuffer = createWriteBuffer();
|
|
741
|
+
function fetchOrTakeWritten(ids) {
|
|
742
|
+
if (ids === void 0) {
|
|
743
|
+
writeBuffer.clear();
|
|
744
|
+
return fetchCollection(source, client, void 0);
|
|
745
|
+
}
|
|
746
|
+
const { written, missing } = writeBuffer.take(ids);
|
|
747
|
+
if (missing.length === 0) return written;
|
|
748
|
+
if (written.length === 0) return fetchCollection(source, client, missing);
|
|
749
|
+
return (0, _entwico_dash.maybeThen)(fetchCollection(source, client, missing), (fetched) => [...written, ...fetched]);
|
|
750
|
+
}
|
|
578
751
|
const projected = new _entwico_projected.ProjectedMap({
|
|
579
752
|
key: (item) => item.id,
|
|
580
|
-
values: (ids) => wrap(`collection:${_def.name}`, () => (0, _entwico_dash.maybeThen)(
|
|
753
|
+
values: (ids) => wrap(`collection:${_def.name}`, () => (0, _entwico_dash.maybeThen)(fetchOrTakeWritten(ids), async (rawItems) => {
|
|
581
754
|
const items = await (0, _entwico_dash.maybeAll)(rawItems.map((item) => dependencyCollector.createContext((ref, register) => (0, _entwico_dash.maybeThen)(mapFn(item, ref), (mapped) => {
|
|
582
755
|
register(mapped.id);
|
|
583
756
|
return mapped;
|
|
@@ -615,10 +788,11 @@ function createCollection(source, options, client, updates$, resolver, refreshBy
|
|
|
615
788
|
emit(changedIds, "upstream-update");
|
|
616
789
|
return;
|
|
617
790
|
}
|
|
791
|
+
const settled = writeBuffer.awaitingUpstream(refreshIds);
|
|
618
792
|
(0, _entwico_dash_async.retryWithBackoff)(async () => {
|
|
619
793
|
await projected.refresh(refreshIds);
|
|
620
794
|
emit(changedIds, "upstream-update");
|
|
621
|
-
});
|
|
795
|
+
}).finally(settled);
|
|
622
796
|
}
|
|
623
797
|
const unsubUpdates = updates$.subscribe((batch) => {
|
|
624
798
|
if (!loaded) return;
|
|
@@ -632,15 +806,55 @@ function createCollection(source, options, client, updates$, resolver, refreshBy
|
|
|
632
806
|
if (deleted.length === 0 && upserted.size === 0 && depAffected.size === 0) return;
|
|
633
807
|
if (_def.cache.eviction === "clear") {
|
|
634
808
|
dependencyCollector.clear();
|
|
809
|
+
writeBuffer.clear();
|
|
635
810
|
projected.clear();
|
|
636
811
|
return;
|
|
637
812
|
}
|
|
813
|
+
writeBuffer.release(deleted);
|
|
638
814
|
for (const id of deleted) dependencyCollector.removeItem(id);
|
|
639
815
|
if (deleted.length > 0) projected.delete(deleted);
|
|
640
816
|
const refreshIds = upserted.union(depAffected);
|
|
641
817
|
for (const id of deleted) refreshIds.delete(id);
|
|
642
818
|
runPartialRefresh([...refreshIds], deleted);
|
|
643
819
|
});
|
|
820
|
+
/** Puts a written entity into the cache through the normal refresh path and returns it mapped. */
|
|
821
|
+
async function takeIntoCache(entity) {
|
|
822
|
+
writeBuffer.park(entity.id, entity);
|
|
823
|
+
let map;
|
|
824
|
+
try {
|
|
825
|
+
map = await projected.refresh([entity.id]);
|
|
826
|
+
} finally {
|
|
827
|
+
writeBuffer.release([entity.id]);
|
|
828
|
+
}
|
|
829
|
+
emit([entity.id], "write");
|
|
830
|
+
const item = map.get(entity.id);
|
|
831
|
+
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`);
|
|
832
|
+
return item;
|
|
833
|
+
}
|
|
834
|
+
/**
|
|
835
|
+
* One method per mutation the source binds — a model the schema exposes no `createX` for has
|
|
836
|
+
* no `create`, matching what `CollectionWrites` types. Permission is not decided here: the
|
|
837
|
+
* server rejects a write the token may not run.
|
|
838
|
+
*/
|
|
839
|
+
function createWrites() {
|
|
840
|
+
const out = {};
|
|
841
|
+
if (source.mutations?.create) out["create"] = async (input) => takeIntoCache(await runMutation(source, client, _def.name, "create", { input }));
|
|
842
|
+
if (source.mutations?.update) out["update"] = async (input) => takeIntoCache(await runMutation(source, client, _def.name, "update", { input }));
|
|
843
|
+
if (source.mutations?.delete) out["delete"] = async (id, options) => {
|
|
844
|
+
await runMutation(source, client, _def.name, "delete", {
|
|
845
|
+
input: {
|
|
846
|
+
...options,
|
|
847
|
+
id
|
|
848
|
+
},
|
|
849
|
+
id
|
|
850
|
+
});
|
|
851
|
+
writeBuffer.release([id]);
|
|
852
|
+
dependencyCollector.removeItem(id);
|
|
853
|
+
projected.delete([id]);
|
|
854
|
+
emit([id], "write");
|
|
855
|
+
};
|
|
856
|
+
return out;
|
|
857
|
+
}
|
|
644
858
|
return {
|
|
645
859
|
instance: {
|
|
646
860
|
name: _def.name,
|
|
@@ -656,7 +870,8 @@ function createCollection(source, options, client, updates$, resolver, refreshBy
|
|
|
656
870
|
},
|
|
657
871
|
async load() {
|
|
658
872
|
if (!loaded) await projected.getAllAsMap();
|
|
659
|
-
}
|
|
873
|
+
},
|
|
874
|
+
...createWrites()
|
|
660
875
|
},
|
|
661
876
|
destroy() {
|
|
662
877
|
unsubUpdates();
|
|
@@ -1036,6 +1251,11 @@ function mapRoute(raw, resolver) {
|
|
|
1036
1251
|
entityId: raw.target.entity.id
|
|
1037
1252
|
};
|
|
1038
1253
|
}
|
|
1254
|
+
case "ContelloRouteTargetSitemap": return {
|
|
1255
|
+
...base,
|
|
1256
|
+
type: "sitemap",
|
|
1257
|
+
sitemapId: raw.target.sitemapId
|
|
1258
|
+
};
|
|
1039
1259
|
default: return;
|
|
1040
1260
|
}
|
|
1041
1261
|
}
|
|
@@ -1262,11 +1482,29 @@ function createRoutesCollection(options, client, updates$, resolver, refreshByTt
|
|
|
1262
1482
|
const pathById = /* @__PURE__ */ new Map();
|
|
1263
1483
|
const idByPath = /* @__PURE__ */ new Map();
|
|
1264
1484
|
let loaded = false;
|
|
1265
|
-
const
|
|
1485
|
+
const routeSource = {
|
|
1486
|
+
...schema.sources.storeRoute,
|
|
1487
|
+
mutations: _contello_client.BUILT_IN_MUTATIONS.route
|
|
1488
|
+
};
|
|
1489
|
+
const routesSourceDoc = (0, _contello_client.createSourceSubscription)(routeSource);
|
|
1490
|
+
const writeBuffer = createWriteBuffer();
|
|
1491
|
+
function fetchRoutes(ids) {
|
|
1492
|
+
return (0, _entwico_dash_async.concatAsync)((0, _entwico_dash_async.mapAsync)(client.subscribe(routesSourceDoc, { ids }), (data) => data.source));
|
|
1493
|
+
}
|
|
1494
|
+
async function fetchOrTakeWritten(ids) {
|
|
1495
|
+
if (ids === void 0) {
|
|
1496
|
+
writeBuffer.clear();
|
|
1497
|
+
return fetchRoutes(void 0);
|
|
1498
|
+
}
|
|
1499
|
+
const { written, missing } = writeBuffer.take(ids);
|
|
1500
|
+
if (missing.length === 0) return written;
|
|
1501
|
+
const fetched = await fetchRoutes(missing);
|
|
1502
|
+
return written.length === 0 ? fetched : [...written, ...fetched];
|
|
1503
|
+
}
|
|
1266
1504
|
const projected = new _entwico_projected.ProjectedMap({
|
|
1267
1505
|
key: (route) => route.id,
|
|
1268
1506
|
values: (ids) => wrap("routes", async () => {
|
|
1269
|
-
const items = (await (
|
|
1507
|
+
const items = (await fetchOrTakeWritten(ids)).reduce((acc, raw) => {
|
|
1270
1508
|
const mapped = mapRoute(raw, resolver);
|
|
1271
1509
|
if (mapped) acc.push(mapped);
|
|
1272
1510
|
return acc;
|
|
@@ -1321,10 +1559,11 @@ function createRoutesCollection(options, client, updates$, resolver, refreshByTt
|
|
|
1321
1559
|
emit(changedIds, "upstream-update");
|
|
1322
1560
|
return;
|
|
1323
1561
|
}
|
|
1562
|
+
const settled = writeBuffer.awaitingUpstream(refreshIds);
|
|
1324
1563
|
(0, _entwico_dash_async.retryWithBackoff)(async () => {
|
|
1325
1564
|
await projected.refresh(refreshIds);
|
|
1326
1565
|
emit(changedIds, "upstream-update");
|
|
1327
|
-
});
|
|
1566
|
+
}).finally(settled);
|
|
1328
1567
|
}
|
|
1329
1568
|
const unsubUpdates = updates$.subscribe((batch) => {
|
|
1330
1569
|
if (!loaded || batch.route.length === 0) return;
|
|
@@ -1335,9 +1574,11 @@ function createRoutesCollection(options, client, updates$, resolver, refreshByTt
|
|
|
1335
1574
|
if (cache.eviction === "clear") {
|
|
1336
1575
|
pathById.clear();
|
|
1337
1576
|
idByPath.clear();
|
|
1577
|
+
writeBuffer.clear();
|
|
1338
1578
|
projected.clear();
|
|
1339
1579
|
return;
|
|
1340
1580
|
}
|
|
1581
|
+
writeBuffer.release(deleted);
|
|
1341
1582
|
for (const id of deleted) {
|
|
1342
1583
|
const oldPath = pathById.get(id);
|
|
1343
1584
|
if (oldPath !== void 0) {
|
|
@@ -1374,8 +1615,49 @@ function createRoutesCollection(options, client, updates$, resolver, refreshByTt
|
|
|
1374
1615
|
if (loaded) return;
|
|
1375
1616
|
return (0, _entwico_dash.maybeThen)(projected.getAllAsMap(), () => {});
|
|
1376
1617
|
}
|
|
1618
|
+
async function runMutation(kind, values) {
|
|
1619
|
+
const binding = routeSource.mutations?.[kind];
|
|
1620
|
+
if (!binding) throw new Error(`@contello/store: routes cannot ${kind} — the schema exposes no ${kind} mutation for routes`);
|
|
1621
|
+
const response = await client.execute((0, _contello_client.createSourceMutation)(routeSource, kind), (0, _contello_client.createSourceMutationVariables)(binding, values));
|
|
1622
|
+
if (response?.result === void 0 || response.result === null) throw new Error(`@contello/store: ${kind} on routes returned nothing`);
|
|
1623
|
+
return response.result;
|
|
1624
|
+
}
|
|
1625
|
+
/** Takes a written route into the cache through the normal refresh path, keeping both indexes. */
|
|
1626
|
+
async function takeIntoCache(raw) {
|
|
1627
|
+
writeBuffer.park(raw.id, raw);
|
|
1628
|
+
let map;
|
|
1629
|
+
try {
|
|
1630
|
+
map = await projected.refresh([raw.id]);
|
|
1631
|
+
} finally {
|
|
1632
|
+
writeBuffer.release([raw.id]);
|
|
1633
|
+
}
|
|
1634
|
+
emit([raw.id], "write");
|
|
1635
|
+
const route = map.get(raw.id);
|
|
1636
|
+
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`);
|
|
1637
|
+
return route;
|
|
1638
|
+
}
|
|
1377
1639
|
return {
|
|
1378
1640
|
instance: {
|
|
1641
|
+
async create(input) {
|
|
1642
|
+
return takeIntoCache(await runMutation("create", { input }));
|
|
1643
|
+
},
|
|
1644
|
+
async update(input) {
|
|
1645
|
+
return takeIntoCache(await runMutation("update", { input }));
|
|
1646
|
+
},
|
|
1647
|
+
async delete(id) {
|
|
1648
|
+
await runMutation("delete", {
|
|
1649
|
+
input: { id },
|
|
1650
|
+
id
|
|
1651
|
+
});
|
|
1652
|
+
writeBuffer.release([id]);
|
|
1653
|
+
const oldPath = pathById.get(id);
|
|
1654
|
+
if (oldPath !== void 0) {
|
|
1655
|
+
idByPath.delete(oldPath);
|
|
1656
|
+
pathById.delete(id);
|
|
1657
|
+
}
|
|
1658
|
+
projected.delete([id]);
|
|
1659
|
+
emit([id], "write");
|
|
1660
|
+
},
|
|
1379
1661
|
refresh$: channel.stream$,
|
|
1380
1662
|
async load() {
|
|
1381
1663
|
if (!loaded) await projected.getAllAsMap();
|
|
@@ -1634,7 +1916,8 @@ function createInternalWatcher(client, resolver) {
|
|
|
1634
1916
|
const signal = controller.signal;
|
|
1635
1917
|
wrap("watcher:start", () => {
|
|
1636
1918
|
(async () => {
|
|
1637
|
-
const
|
|
1919
|
+
const source = (0, _entwico_dash_async.keepalive)(() => client.subscribe(storeWatchUpdatesDocument), { signal });
|
|
1920
|
+
const events = (0, _entwico_dash_async.mapAsync)(source, (data) => (data.contelloUpdatesBatch?.events ?? []).map((e) => safeMapEvent(e, resolver)).filter((e) => e !== void 0));
|
|
1638
1921
|
for await (const list of events) {
|
|
1639
1922
|
if (signal.aborted) return;
|
|
1640
1923
|
if (list.length === 0) continue;
|