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