@contello/store 4.4.1 → 4.5.1
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 +0 -2
- package/dist/fragments.d.ts.map +1 -1
- package/dist/fragments.js +1 -2
- package/dist/index.cjs +114 -71
- package/dist/index.d.cts +7 -20
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.ts +7 -20
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +114 -72
- package/dist/index.js.map +1 -1
- package/package.json +7 -7
package/dist/index.js
CHANGED
|
@@ -2,9 +2,10 @@ import { BUILT_IN_MUTATIONS, createContelloClient, createSourceMutation, createS
|
|
|
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";
|
|
5
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
5
6
|
import { maybeAll, maybeThen } from "@entwico/dash";
|
|
6
7
|
import { LRUCache } from "lru-cache";
|
|
7
|
-
|
|
8
|
+
// src/generated/graphql.ts
|
|
8
9
|
const MediaAssetFragmentSchema = `fragment MediaAsset on ContelloAsset {
|
|
9
10
|
id
|
|
10
11
|
original {
|
|
@@ -217,11 +218,9 @@ const schema = {
|
|
|
217
218
|
},
|
|
218
219
|
models: { dummy: "DummyEntity" }
|
|
219
220
|
};
|
|
220
|
-
|
|
221
|
-
//#region src/telemetry.ts
|
|
221
|
+
// src/telemetry.ts
|
|
222
222
|
const { wrap } = createOperationTelemetry("@contello/store");
|
|
223
|
-
|
|
224
|
-
//#region src/utils.ts
|
|
223
|
+
// src/utils.ts
|
|
225
224
|
/**
|
|
226
225
|
* default `cache.ttl` applied across all store kinds. eager stores (collections, singletons)
|
|
227
226
|
* use it as the interval for a periodic full refresh; lazy stores use it as the per-item
|
|
@@ -237,6 +236,22 @@ function resolveTtl(ttl) {
|
|
|
237
236
|
if (ttl === false || ttl <= 0) return void 0;
|
|
238
237
|
return ttl;
|
|
239
238
|
}
|
|
239
|
+
function createRefreshRetry() {
|
|
240
|
+
const controller = new AbortController();
|
|
241
|
+
return {
|
|
242
|
+
async run(fn) {
|
|
243
|
+
try {
|
|
244
|
+
await retryWithBackoff(fn, { signal: controller.signal });
|
|
245
|
+
return true;
|
|
246
|
+
} catch {
|
|
247
|
+
return false;
|
|
248
|
+
}
|
|
249
|
+
},
|
|
250
|
+
stop() {
|
|
251
|
+
controller.abort();
|
|
252
|
+
}
|
|
253
|
+
};
|
|
254
|
+
}
|
|
240
255
|
/**
|
|
241
256
|
* creates a coalescing refresh scheduler with exponential-backoff retry on failure.
|
|
242
257
|
*
|
|
@@ -245,11 +260,11 @@ function resolveTtl(ttl) {
|
|
|
245
260
|
* scheduleRefresh. once the in-flight refresh completes, at most one queued refresh
|
|
246
261
|
* starts, with that latest kind — collapsing any number of intermediate requests into one.
|
|
247
262
|
*
|
|
248
|
-
* on error, retries
|
|
263
|
+
* on error, retries with exponential backoff until `retry` is stopped. the projected value keeps serving
|
|
249
264
|
* the stale cached value throughout (SWR). the `onRefreshed` callback receives the kind that
|
|
250
265
|
* triggered the just-completed refresh.
|
|
251
266
|
*/
|
|
252
|
-
function createRefresher(fn, onRefreshed,
|
|
267
|
+
function createRefresher(fn, onRefreshed, retry) {
|
|
253
268
|
let refreshing = false;
|
|
254
269
|
let inFlightKind;
|
|
255
270
|
let pendingKind;
|
|
@@ -260,8 +275,8 @@ function createRefresher(fn, onRefreshed, onStart) {
|
|
|
260
275
|
}
|
|
261
276
|
refreshing = true;
|
|
262
277
|
inFlightKind = kind;
|
|
263
|
-
|
|
264
|
-
|
|
278
|
+
retry.run(fn).then((refreshed) => {
|
|
279
|
+
if (!refreshed) return;
|
|
265
280
|
const finished = inFlightKind;
|
|
266
281
|
refreshing = false;
|
|
267
282
|
inFlightKind = void 0;
|
|
@@ -288,17 +303,21 @@ function createRefreshChannel(onRefresh) {
|
|
|
288
303
|
};
|
|
289
304
|
}
|
|
290
305
|
function createTtlOrchestrator(options) {
|
|
306
|
+
const runInOwnContext = AsyncLocalStorage.snapshot();
|
|
291
307
|
let timer;
|
|
308
|
+
let cleared = false;
|
|
292
309
|
return {
|
|
293
310
|
mark() {
|
|
311
|
+
if (cleared) return;
|
|
294
312
|
if (timer !== void 0) {
|
|
295
313
|
clearTimeout(timer);
|
|
296
314
|
timer = void 0;
|
|
297
315
|
}
|
|
298
316
|
if (options.ttl === void 0) return;
|
|
299
|
-
timer = setTimeout(options.run, options.ttl);
|
|
317
|
+
timer = runInOwnContext(() => setTimeout(options.run, options.ttl));
|
|
300
318
|
},
|
|
301
319
|
clear() {
|
|
320
|
+
cleared = true;
|
|
302
321
|
if (timer === void 0) return;
|
|
303
322
|
clearTimeout(timer);
|
|
304
323
|
timer = void 0;
|
|
@@ -327,8 +346,7 @@ function createRefreshByTtlQueue() {
|
|
|
327
346
|
next();
|
|
328
347
|
} };
|
|
329
348
|
}
|
|
330
|
-
|
|
331
|
-
//#region src/write-buffer.ts
|
|
349
|
+
// src/write-buffer.ts
|
|
332
350
|
function createWriteBuffer() {
|
|
333
351
|
const parked = /* @__PURE__ */ new Map();
|
|
334
352
|
const awaited = /* @__PURE__ */ new Map();
|
|
@@ -373,8 +391,7 @@ function createWriteBuffer() {
|
|
|
373
391
|
}
|
|
374
392
|
};
|
|
375
393
|
}
|
|
376
|
-
|
|
377
|
-
//#region src/assets.ts
|
|
394
|
+
// src/assets.ts
|
|
378
395
|
function mapFile$1(raw) {
|
|
379
396
|
return {
|
|
380
397
|
uid: raw.uid,
|
|
@@ -400,6 +417,7 @@ function createAssetsCollection(options, client, updates$, refreshByTtl) {
|
|
|
400
417
|
eviction: opts.cache?.eviction ?? "refresh"
|
|
401
418
|
};
|
|
402
419
|
const channel = createRefreshChannel(opts.onRefresh);
|
|
420
|
+
const retry = createRefreshRetry();
|
|
403
421
|
const ttl = createTtlOrchestrator({
|
|
404
422
|
ttl: cache.ttl,
|
|
405
423
|
run: () => runFullRefresh("ttl")
|
|
@@ -443,7 +461,7 @@ function createAssetsCollection(options, client, updates$, refreshByTtl) {
|
|
|
443
461
|
});
|
|
444
462
|
}
|
|
445
463
|
function runFullRefresh(kind) {
|
|
446
|
-
refreshByTtl.enqueue(() =>
|
|
464
|
+
refreshByTtl.enqueue(() => retry.run(async () => {
|
|
447
465
|
emit((await projected.refresh()).keys().toArray(), kind);
|
|
448
466
|
ttl.mark();
|
|
449
467
|
}));
|
|
@@ -455,7 +473,7 @@ function createAssetsCollection(options, client, updates$, refreshByTtl) {
|
|
|
455
473
|
return;
|
|
456
474
|
}
|
|
457
475
|
const settled = writeBuffer.awaitingUpstream(refreshIds);
|
|
458
|
-
|
|
476
|
+
retry.run(async () => {
|
|
459
477
|
await projected.refresh(refreshIds);
|
|
460
478
|
emit(changedIds, "upstream-update");
|
|
461
479
|
}).finally(settled);
|
|
@@ -529,6 +547,7 @@ function createAssetsCollection(options, client, updates$, refreshByTtl) {
|
|
|
529
547
|
},
|
|
530
548
|
destroy() {
|
|
531
549
|
unsubUpdates();
|
|
550
|
+
retry.stop();
|
|
532
551
|
ttl.clear();
|
|
533
552
|
channel.complete();
|
|
534
553
|
}
|
|
@@ -550,8 +569,7 @@ function createAssetsSyncCollection(options, client, updates$, refreshByTtl) {
|
|
|
550
569
|
destroy
|
|
551
570
|
};
|
|
552
571
|
}
|
|
553
|
-
|
|
554
|
-
//#region src/dependency-collector.ts
|
|
572
|
+
// src/dependency-collector.ts
|
|
555
573
|
const EMPTY_KEYS = /* @__PURE__ */ new Set();
|
|
556
574
|
function createDependencyKey(target, id) {
|
|
557
575
|
return `${target}\0${id}`;
|
|
@@ -698,8 +716,7 @@ var DependencyCollector = class {
|
|
|
698
716
|
for (const key of this.forwardIndex.keys()) if (!keys.has(key)) this.clearItem(key);
|
|
699
717
|
}
|
|
700
718
|
};
|
|
701
|
-
|
|
702
|
-
//#region src/collection.ts
|
|
719
|
+
// src/collection.ts
|
|
703
720
|
function fetchCollection(source, client, ids) {
|
|
704
721
|
return concatAsync(mapAsync(client.subscribe(createSourceSubscription(source), { ids }), (r) => r.source));
|
|
705
722
|
}
|
|
@@ -729,6 +746,7 @@ function createCollection(source, options, client, updates$, resolver, refreshBy
|
|
|
729
746
|
};
|
|
730
747
|
const dependencyCollector = new DependencyCollector(_def.model, resolver);
|
|
731
748
|
const channel = createRefreshChannel(opts.onRefresh);
|
|
749
|
+
const retry = createRefreshRetry();
|
|
732
750
|
const ttl = createTtlOrchestrator({
|
|
733
751
|
ttl: _def.cache.ttl,
|
|
734
752
|
run: () => runFullRefresh("ttl")
|
|
@@ -774,7 +792,7 @@ function createCollection(source, options, client, updates$, resolver, refreshBy
|
|
|
774
792
|
});
|
|
775
793
|
}
|
|
776
794
|
function runFullRefresh(kind) {
|
|
777
|
-
refreshByTtl.enqueue(() =>
|
|
795
|
+
refreshByTtl.enqueue(() => retry.run(async () => {
|
|
778
796
|
emit((await projected.refresh()).keys().toArray(), kind);
|
|
779
797
|
ttl.mark();
|
|
780
798
|
}));
|
|
@@ -786,7 +804,7 @@ function createCollection(source, options, client, updates$, resolver, refreshBy
|
|
|
786
804
|
return;
|
|
787
805
|
}
|
|
788
806
|
const settled = writeBuffer.awaitingUpstream(refreshIds);
|
|
789
|
-
|
|
807
|
+
retry.run(async () => {
|
|
790
808
|
await projected.refresh(refreshIds);
|
|
791
809
|
emit(changedIds, "upstream-update");
|
|
792
810
|
}).finally(settled);
|
|
@@ -872,6 +890,7 @@ function createCollection(source, options, client, updates$, resolver, refreshBy
|
|
|
872
890
|
},
|
|
873
891
|
destroy() {
|
|
874
892
|
unsubUpdates();
|
|
893
|
+
retry.stop();
|
|
875
894
|
ttl.clear();
|
|
876
895
|
channel.complete();
|
|
877
896
|
}
|
|
@@ -896,8 +915,7 @@ function createCollectionSync(source, options, client, updates$, resolver, refre
|
|
|
896
915
|
destroy
|
|
897
916
|
};
|
|
898
917
|
}
|
|
899
|
-
|
|
900
|
-
//#region src/i18n.ts
|
|
918
|
+
// src/i18n.ts
|
|
901
919
|
function toGqlMessageInput(msg) {
|
|
902
920
|
return {
|
|
903
921
|
token: msg.token,
|
|
@@ -920,6 +938,7 @@ function toGqlMessageInput(msg) {
|
|
|
920
938
|
}
|
|
921
939
|
function createI18nMessagesCollection(def, client, updates$, refreshByTtl) {
|
|
922
940
|
const channel = createRefreshChannel(def.onRefresh);
|
|
941
|
+
const retry = createRefreshRetry();
|
|
923
942
|
const ttl = createTtlOrchestrator({
|
|
924
943
|
ttl: resolveTtl(def.cache?.ttl),
|
|
925
944
|
run: () => runTtlRefresh()
|
|
@@ -957,7 +976,7 @@ function createI18nMessagesCollection(def, client, updates$, refreshByTtl) {
|
|
|
957
976
|
}
|
|
958
977
|
function runTtlRefresh() {
|
|
959
978
|
refreshByTtl.enqueue(async () => {
|
|
960
|
-
await
|
|
979
|
+
if (!await retry.run(() => projected.refresh())) return;
|
|
961
980
|
emitWithCurrentIds("ttl");
|
|
962
981
|
ttl.mark();
|
|
963
982
|
});
|
|
@@ -965,7 +984,7 @@ function createI18nMessagesCollection(def, client, updates$, refreshByTtl) {
|
|
|
965
984
|
const scheduleRefresh = createRefresher(() => projected.refresh(), (kind) => {
|
|
966
985
|
emitWithCurrentIds(kind);
|
|
967
986
|
ttl.mark();
|
|
968
|
-
},
|
|
987
|
+
}, retry);
|
|
969
988
|
function runPartialRefresh(upsertedIds, deletedIds) {
|
|
970
989
|
const changedIds = [.../* @__PURE__ */ new Set([...upsertedIds, ...deletedIds])];
|
|
971
990
|
if (changedIds.length === 0) return;
|
|
@@ -973,7 +992,7 @@ function createI18nMessagesCollection(def, client, updates$, refreshByTtl) {
|
|
|
973
992
|
emit(changedIds, "upstream-update");
|
|
974
993
|
return;
|
|
975
994
|
}
|
|
976
|
-
|
|
995
|
+
retry.run(async () => {
|
|
977
996
|
await projected.refresh(upsertedIds);
|
|
978
997
|
emit(changedIds, "upstream-update");
|
|
979
998
|
});
|
|
@@ -1009,13 +1028,13 @@ function createI18nMessagesCollection(def, client, updates$, refreshByTtl) {
|
|
|
1009
1028
|
},
|
|
1010
1029
|
destroy() {
|
|
1011
1030
|
unsubUpdates();
|
|
1031
|
+
retry.stop();
|
|
1012
1032
|
ttl.clear();
|
|
1013
1033
|
channel.complete();
|
|
1014
1034
|
}
|
|
1015
1035
|
};
|
|
1016
1036
|
}
|
|
1017
|
-
|
|
1018
|
-
//#region src/lru.ts
|
|
1037
|
+
// src/lru.ts
|
|
1019
1038
|
function createLruCache(options) {
|
|
1020
1039
|
const { max, ttl, onEvict: dispose } = options;
|
|
1021
1040
|
const lru = new LRUCache({
|
|
@@ -1041,8 +1060,7 @@ function createLruCache(options) {
|
|
|
1041
1060
|
keys: () => lru.keys().toArray()
|
|
1042
1061
|
};
|
|
1043
1062
|
}
|
|
1044
|
-
|
|
1045
|
-
//#region src/lazy-assets.ts
|
|
1063
|
+
// src/lazy-assets.ts
|
|
1046
1064
|
function mapFile(raw) {
|
|
1047
1065
|
return {
|
|
1048
1066
|
uid: raw.uid,
|
|
@@ -1087,13 +1105,14 @@ function createLazyAssetsCollection(def, client, updates$) {
|
|
|
1087
1105
|
});
|
|
1088
1106
|
}
|
|
1089
1107
|
let lastRefreshKeys = [];
|
|
1108
|
+
const retry = createRefreshRetry();
|
|
1090
1109
|
const scheduleRefresh = createRefresher(async () => {
|
|
1091
1110
|
lastRefreshKeys = cache.keys();
|
|
1092
1111
|
if (lastRefreshKeys.length === 0) return;
|
|
1093
1112
|
await projected.refresh(lastRefreshKeys);
|
|
1094
1113
|
}, (kind) => {
|
|
1095
1114
|
if (lastRefreshKeys.length > 0) emit(lastRefreshKeys, kind);
|
|
1096
|
-
},
|
|
1115
|
+
}, retry);
|
|
1097
1116
|
const unsubUpdates = updates$.subscribe((batch) => {
|
|
1098
1117
|
if (batch.asset.length === 0) return;
|
|
1099
1118
|
for (const event of batch.asset) projected.delete(event.id);
|
|
@@ -1123,12 +1142,12 @@ function createLazyAssetsCollection(def, client, updates$) {
|
|
|
1123
1142
|
},
|
|
1124
1143
|
destroy() {
|
|
1125
1144
|
unsubUpdates();
|
|
1145
|
+
retry.stop();
|
|
1126
1146
|
channel.complete();
|
|
1127
1147
|
}
|
|
1128
1148
|
};
|
|
1129
1149
|
}
|
|
1130
|
-
|
|
1131
|
-
//#region src/lazy-collection.ts
|
|
1150
|
+
// src/lazy-collection.ts
|
|
1132
1151
|
function createLazyCollection(source, options, client, updates$, resolver) {
|
|
1133
1152
|
const opts = options ?? {};
|
|
1134
1153
|
const mapFn = opts.map ?? ((item) => item);
|
|
@@ -1163,6 +1182,7 @@ function createLazyCollection(source, options, client, updates$, resolver) {
|
|
|
1163
1182
|
refresh$.next(event);
|
|
1164
1183
|
opts.onRefresh?.(event);
|
|
1165
1184
|
}
|
|
1185
|
+
const retry = createRefreshRetry();
|
|
1166
1186
|
let lastRefreshKeys = [];
|
|
1167
1187
|
const scheduleRefresh = createRefresher(async () => {
|
|
1168
1188
|
lastRefreshKeys = cache.keys();
|
|
@@ -1170,7 +1190,7 @@ function createLazyCollection(source, options, client, updates$, resolver) {
|
|
|
1170
1190
|
await projected.refresh(lastRefreshKeys);
|
|
1171
1191
|
}, (kind) => {
|
|
1172
1192
|
if (lastRefreshKeys.length > 0) emit(lastRefreshKeys, kind);
|
|
1173
|
-
},
|
|
1193
|
+
}, retry);
|
|
1174
1194
|
const unsubUpdates = updates$.subscribe((batch) => {
|
|
1175
1195
|
const evicted = /* @__PURE__ */ new Set();
|
|
1176
1196
|
const ownModelEvents = batch.entity.get(_def.model);
|
|
@@ -1200,12 +1220,35 @@ function createLazyCollection(source, options, client, updates$, resolver) {
|
|
|
1200
1220
|
},
|
|
1201
1221
|
destroy() {
|
|
1202
1222
|
unsubUpdates();
|
|
1223
|
+
retry.stop();
|
|
1203
1224
|
refresh$.complete();
|
|
1204
1225
|
}
|
|
1205
1226
|
};
|
|
1206
1227
|
}
|
|
1207
|
-
|
|
1208
|
-
|
|
1228
|
+
// src/route-path.ts
|
|
1229
|
+
const ESCAPES = /(?:%[\dA-Fa-f]{2})+/g;
|
|
1230
|
+
const STAYS_ENCODED = /[/%?#\s\p{Cc}]/u;
|
|
1231
|
+
function decodeRun(run) {
|
|
1232
|
+
let decoded;
|
|
1233
|
+
try {
|
|
1234
|
+
decoded = decodeURIComponent(run);
|
|
1235
|
+
} catch {
|
|
1236
|
+
return run.toUpperCase();
|
|
1237
|
+
}
|
|
1238
|
+
let result = "";
|
|
1239
|
+
for (const char of decoded) result += STAYS_ENCODED.test(char) ? encodeURIComponent(char) : char;
|
|
1240
|
+
return result;
|
|
1241
|
+
}
|
|
1242
|
+
/**
|
|
1243
|
+
* The one form a route path is stored and looked up in: percent escapes decoded and unicode
|
|
1244
|
+
* composed (NFC), as core normalizes it. A request arrives percent-encoded (`/%C3%BCber-uns`),
|
|
1245
|
+
* the stored path is decoded (`/über-uns`); both spellings must hit the same cache entry. An
|
|
1246
|
+
* escape of a character a path cannot hold decoded stays encoded, with uppercase hex.
|
|
1247
|
+
*/
|
|
1248
|
+
function normalizeRoutePath(path) {
|
|
1249
|
+
return path.replaceAll(ESCAPES, decodeRun).normalize("NFC");
|
|
1250
|
+
}
|
|
1251
|
+
// src/routes-mapping.ts
|
|
1209
1252
|
function mapRoute(raw, resolver) {
|
|
1210
1253
|
const base = {
|
|
1211
1254
|
id: raw.id,
|
|
@@ -1255,8 +1298,7 @@ function mapRoute(raw, resolver) {
|
|
|
1255
1298
|
default: return;
|
|
1256
1299
|
}
|
|
1257
1300
|
}
|
|
1258
|
-
|
|
1259
|
-
//#region src/lazy-routes.ts
|
|
1301
|
+
// src/lazy-routes.ts
|
|
1260
1302
|
const ID_PREFIX = "1\0";
|
|
1261
1303
|
const PATH_PREFIX = "2\0";
|
|
1262
1304
|
const CACHE_KEY = Symbol("cacheKey");
|
|
@@ -1284,7 +1326,7 @@ function createRoutesCache(max, ttl) {
|
|
|
1284
1326
|
max,
|
|
1285
1327
|
ttl,
|
|
1286
1328
|
onEvict: (route) => {
|
|
1287
|
-
pathToId.delete(route.path);
|
|
1329
|
+
pathToId.delete(normalizeRoutePath(route.path));
|
|
1288
1330
|
idToPath.delete(route.id);
|
|
1289
1331
|
}
|
|
1290
1332
|
});
|
|
@@ -1301,8 +1343,8 @@ function createRoutesCache(max, ttl) {
|
|
|
1301
1343
|
get: (key) => lru.get(resolveKey(key)),
|
|
1302
1344
|
set: (_key, value) => {
|
|
1303
1345
|
const previousPath = idToPath.get(value.id);
|
|
1304
|
-
if (previousPath !== void 0 && previousPath !== value.path) pathToId.delete(previousPath);
|
|
1305
|
-
pathToId.set(value.path, value.id);
|
|
1346
|
+
if (previousPath !== void 0 && previousPath !== value.path) pathToId.delete(normalizeRoutePath(previousPath));
|
|
1347
|
+
pathToId.set(normalizeRoutePath(value.path), value.id);
|
|
1306
1348
|
idToPath.set(value.id, value.path);
|
|
1307
1349
|
lru.set(ID_PREFIX + value.id, value);
|
|
1308
1350
|
},
|
|
@@ -1310,7 +1352,7 @@ function createRoutesCache(max, ttl) {
|
|
|
1310
1352
|
const resolved = resolveKey(key);
|
|
1311
1353
|
const value = lru.get(resolved);
|
|
1312
1354
|
if (value) {
|
|
1313
|
-
pathToId.delete(value.path);
|
|
1355
|
+
pathToId.delete(normalizeRoutePath(value.path));
|
|
1314
1356
|
idToPath.delete(value.id);
|
|
1315
1357
|
}
|
|
1316
1358
|
lru.delete(resolved);
|
|
@@ -1350,7 +1392,7 @@ function createLazyRoutesCollection(def, client, updates$, resolver) {
|
|
|
1350
1392
|
}
|
|
1351
1393
|
const [rawByIds, rawByPaths] = await Promise.all([ids.length > 0 ? fetchRoutes({ ids }) : Promise.resolve([]), paths.length > 0 ? fetchRoutes({ paths }) : Promise.resolve([])]);
|
|
1352
1394
|
const byIds = collectRoutes(rawByIds, (r) => ID_PREFIX + r.id, resolver);
|
|
1353
|
-
const byPaths = collectRoutes(rawByPaths, (r) => PATH_PREFIX + r.path, resolver);
|
|
1395
|
+
const byPaths = collectRoutes(rawByPaths, (r) => PATH_PREFIX + normalizeRoutePath(r.path), resolver);
|
|
1354
1396
|
const found = [...byIds, ...byPaths];
|
|
1355
1397
|
const foundKeys = new Set(found.map((route) => route[CACHE_KEY]));
|
|
1356
1398
|
for (const key of prefixedKeys) if (!foundKeys.has(key)) negatives.set(key, true);
|
|
@@ -1366,13 +1408,14 @@ function createLazyRoutesCollection(def, client, updates$, resolver) {
|
|
|
1366
1408
|
});
|
|
1367
1409
|
}
|
|
1368
1410
|
let lastRefreshKeys = [];
|
|
1411
|
+
const retry = createRefreshRetry();
|
|
1369
1412
|
const scheduleRefresh = createRefresher(async () => {
|
|
1370
1413
|
lastRefreshKeys = cache.keys();
|
|
1371
1414
|
if (lastRefreshKeys.length === 0) return;
|
|
1372
1415
|
await projected.refresh(lastRefreshKeys);
|
|
1373
1416
|
}, (kind) => {
|
|
1374
1417
|
if (lastRefreshKeys.length > 0) emit(lastRefreshKeys.map((key) => key.slice(2)), kind);
|
|
1375
|
-
},
|
|
1418
|
+
}, retry);
|
|
1376
1419
|
const unsubUpdates = updates$.subscribe((batch) => {
|
|
1377
1420
|
const evicted = [];
|
|
1378
1421
|
for (const event of batch.route) {
|
|
@@ -1382,7 +1425,7 @@ function createLazyRoutesCollection(def, client, updates$, resolver) {
|
|
|
1382
1425
|
evicted.push(event.id);
|
|
1383
1426
|
} else {
|
|
1384
1427
|
negatives.delete(idKey);
|
|
1385
|
-
negatives.delete(PATH_PREFIX + event.after.path);
|
|
1428
|
+
negatives.delete(PATH_PREFIX + normalizeRoutePath(event.after.path));
|
|
1386
1429
|
if (cache.has(idKey)) cache.set(idKey, event.after);
|
|
1387
1430
|
evicted.push(event.after.path);
|
|
1388
1431
|
}
|
|
@@ -1406,8 +1449,8 @@ function createLazyRoutesCollection(def, client, updates$, resolver) {
|
|
|
1406
1449
|
return lookupOne(ID_PREFIX + idOrIds);
|
|
1407
1450
|
},
|
|
1408
1451
|
getByPath(pathOrPaths) {
|
|
1409
|
-
if (Array.isArray(pathOrPaths)) return lookupMany(pathOrPaths.map((p) => PATH_PREFIX + p));
|
|
1410
|
-
return lookupOne(PATH_PREFIX + pathOrPaths);
|
|
1452
|
+
if (Array.isArray(pathOrPaths)) return lookupMany(pathOrPaths.map((p) => PATH_PREFIX + normalizeRoutePath(p)));
|
|
1453
|
+
return lookupOne(PATH_PREFIX + normalizeRoutePath(pathOrPaths));
|
|
1411
1454
|
},
|
|
1412
1455
|
refresh() {
|
|
1413
1456
|
negatives.clear();
|
|
@@ -1420,12 +1463,12 @@ function createLazyRoutesCollection(def, client, updates$, resolver) {
|
|
|
1420
1463
|
},
|
|
1421
1464
|
destroy() {
|
|
1422
1465
|
unsubUpdates();
|
|
1466
|
+
retry.stop();
|
|
1423
1467
|
channel.complete();
|
|
1424
1468
|
}
|
|
1425
1469
|
};
|
|
1426
1470
|
}
|
|
1427
|
-
|
|
1428
|
-
//#region src/model-resolver.ts
|
|
1471
|
+
// src/model-resolver.ts
|
|
1429
1472
|
var ModelResolver = class {
|
|
1430
1473
|
_typeNames;
|
|
1431
1474
|
_toModel;
|
|
@@ -1462,8 +1505,7 @@ var ModelResolver = class {
|
|
|
1462
1505
|
return this._toModel.get(typeName) ?? typeName;
|
|
1463
1506
|
}
|
|
1464
1507
|
};
|
|
1465
|
-
|
|
1466
|
-
//#region src/routes.ts
|
|
1508
|
+
// src/routes.ts
|
|
1467
1509
|
function createRoutesCollection(options, client, updates$, resolver, refreshByTtl) {
|
|
1468
1510
|
const opts = options ?? {};
|
|
1469
1511
|
const cache = {
|
|
@@ -1471,6 +1513,7 @@ function createRoutesCollection(options, client, updates$, resolver, refreshByTt
|
|
|
1471
1513
|
eviction: opts.cache?.eviction ?? "refresh"
|
|
1472
1514
|
};
|
|
1473
1515
|
const channel = createRefreshChannel(opts.onRefresh);
|
|
1516
|
+
const retry = createRefreshRetry();
|
|
1474
1517
|
const ttl = createTtlOrchestrator({
|
|
1475
1518
|
ttl: cache.ttl,
|
|
1476
1519
|
run: () => runFullRefresh("ttl")
|
|
@@ -1510,7 +1553,7 @@ function createRoutesCollection(options, client, updates$, resolver, refreshByTt
|
|
|
1510
1553
|
idByPath.clear();
|
|
1511
1554
|
for (const r of items) {
|
|
1512
1555
|
pathById.set(r.id, r.path);
|
|
1513
|
-
idByPath.set(r.path, r.id);
|
|
1556
|
+
idByPath.set(normalizeRoutePath(r.path), r.id);
|
|
1514
1557
|
}
|
|
1515
1558
|
if (!loaded) {
|
|
1516
1559
|
loaded = true;
|
|
@@ -1523,14 +1566,14 @@ function createRoutesCollection(options, client, updates$, resolver, refreshByTt
|
|
|
1523
1566
|
if (returnedIds.has(id)) continue;
|
|
1524
1567
|
const oldPath = pathById.get(id);
|
|
1525
1568
|
if (oldPath === void 0) continue;
|
|
1526
|
-
idByPath.delete(oldPath);
|
|
1569
|
+
idByPath.delete(normalizeRoutePath(oldPath));
|
|
1527
1570
|
pathById.delete(id);
|
|
1528
1571
|
}
|
|
1529
1572
|
for (const r of items) {
|
|
1530
1573
|
const oldPath = pathById.get(r.id);
|
|
1531
|
-
if (oldPath !== void 0 && oldPath !== r.path) idByPath.delete(oldPath);
|
|
1574
|
+
if (oldPath !== void 0 && oldPath !== r.path) idByPath.delete(normalizeRoutePath(oldPath));
|
|
1532
1575
|
pathById.set(r.id, r.path);
|
|
1533
|
-
idByPath.set(r.path, r.id);
|
|
1576
|
+
idByPath.set(normalizeRoutePath(r.path), r.id);
|
|
1534
1577
|
}
|
|
1535
1578
|
}
|
|
1536
1579
|
return items;
|
|
@@ -1543,7 +1586,7 @@ function createRoutesCollection(options, client, updates$, resolver, refreshByTt
|
|
|
1543
1586
|
});
|
|
1544
1587
|
}
|
|
1545
1588
|
function runFullRefresh(kind) {
|
|
1546
|
-
refreshByTtl.enqueue(() =>
|
|
1589
|
+
refreshByTtl.enqueue(() => retry.run(async () => {
|
|
1547
1590
|
emit((await projected.refresh()).keys().toArray(), kind);
|
|
1548
1591
|
ttl.mark();
|
|
1549
1592
|
}));
|
|
@@ -1555,7 +1598,7 @@ function createRoutesCollection(options, client, updates$, resolver, refreshByTt
|
|
|
1555
1598
|
return;
|
|
1556
1599
|
}
|
|
1557
1600
|
const settled = writeBuffer.awaitingUpstream(refreshIds);
|
|
1558
|
-
|
|
1601
|
+
retry.run(async () => {
|
|
1559
1602
|
await projected.refresh(refreshIds);
|
|
1560
1603
|
emit(changedIds, "upstream-update");
|
|
1561
1604
|
}).finally(settled);
|
|
@@ -1577,7 +1620,7 @@ function createRoutesCollection(options, client, updates$, resolver, refreshByTt
|
|
|
1577
1620
|
for (const id of deleted) {
|
|
1578
1621
|
const oldPath = pathById.get(id);
|
|
1579
1622
|
if (oldPath === void 0) continue;
|
|
1580
|
-
idByPath.delete(oldPath);
|
|
1623
|
+
idByPath.delete(normalizeRoutePath(oldPath));
|
|
1581
1624
|
pathById.delete(id);
|
|
1582
1625
|
}
|
|
1583
1626
|
if (deleted.length > 0) projected.delete(deleted);
|
|
@@ -1591,7 +1634,7 @@ function createRoutesCollection(options, client, updates$, resolver, refreshByTt
|
|
|
1591
1634
|
}
|
|
1592
1635
|
function lookupByPathSingle(path) {
|
|
1593
1636
|
return maybeThen(ensureLoaded(), () => {
|
|
1594
|
-
const id = idByPath.get(path);
|
|
1637
|
+
const id = idByPath.get(normalizeRoutePath(path));
|
|
1595
1638
|
return id === void 0 ? void 0 : projected.get(id);
|
|
1596
1639
|
});
|
|
1597
1640
|
}
|
|
@@ -1599,7 +1642,7 @@ function createRoutesCollection(options, client, updates$, resolver, refreshByTt
|
|
|
1599
1642
|
return maybeThen(ensureLoaded(), () => {
|
|
1600
1643
|
const ids = [];
|
|
1601
1644
|
for (const p of paths) {
|
|
1602
|
-
const id = idByPath.get(p);
|
|
1645
|
+
const id = idByPath.get(normalizeRoutePath(p));
|
|
1603
1646
|
if (id !== void 0) ids.push(id);
|
|
1604
1647
|
}
|
|
1605
1648
|
return projected.get(ids);
|
|
@@ -1646,7 +1689,7 @@ function createRoutesCollection(options, client, updates$, resolver, refreshByTt
|
|
|
1646
1689
|
writeBuffer.release([id]);
|
|
1647
1690
|
const oldPath = pathById.get(id);
|
|
1648
1691
|
if (oldPath !== void 0) {
|
|
1649
|
-
idByPath.delete(oldPath);
|
|
1692
|
+
idByPath.delete(normalizeRoutePath(oldPath));
|
|
1650
1693
|
pathById.delete(id);
|
|
1651
1694
|
}
|
|
1652
1695
|
projected.delete([id]);
|
|
@@ -1668,6 +1711,7 @@ function createRoutesCollection(options, client, updates$, resolver, refreshByTt
|
|
|
1668
1711
|
},
|
|
1669
1712
|
destroy() {
|
|
1670
1713
|
unsubUpdates();
|
|
1714
|
+
retry.stop();
|
|
1671
1715
|
ttl.clear();
|
|
1672
1716
|
channel.complete();
|
|
1673
1717
|
}
|
|
@@ -1692,8 +1736,7 @@ function createRoutesSyncCollection(options, client, updates$, resolver, refresh
|
|
|
1692
1736
|
destroy
|
|
1693
1737
|
};
|
|
1694
1738
|
}
|
|
1695
|
-
|
|
1696
|
-
//#region src/singleton.ts
|
|
1739
|
+
// src/singleton.ts
|
|
1697
1740
|
function createSingleton(source, options, client, updates$, resolver, refreshByTtl) {
|
|
1698
1741
|
const opts = options ?? {};
|
|
1699
1742
|
const mapFn = opts.map ?? ((item) => item);
|
|
@@ -1708,6 +1751,7 @@ function createSingleton(source, options, client, updates$, resolver, refreshByT
|
|
|
1708
1751
|
const dependencyCollector = new DependencyCollector(_def.model, resolver);
|
|
1709
1752
|
const itemKey = `singleton:${_def.name}`;
|
|
1710
1753
|
const channel = createRefreshChannel(opts.onRefresh);
|
|
1754
|
+
const retry = createRefreshRetry();
|
|
1711
1755
|
const ttl = createTtlOrchestrator({
|
|
1712
1756
|
ttl: _def.cache.ttl,
|
|
1713
1757
|
run: () => runTtlRefresh()
|
|
@@ -1727,7 +1771,7 @@ function createSingleton(source, options, client, updates$, resolver, refreshByT
|
|
|
1727
1771
|
}
|
|
1728
1772
|
function runTtlRefresh() {
|
|
1729
1773
|
refreshByTtl.enqueue(async () => {
|
|
1730
|
-
await
|
|
1774
|
+
if (!await retry.run(() => projected.refresh())) return;
|
|
1731
1775
|
emit("ttl");
|
|
1732
1776
|
ttl.mark();
|
|
1733
1777
|
});
|
|
@@ -1735,7 +1779,7 @@ function createSingleton(source, options, client, updates$, resolver, refreshByT
|
|
|
1735
1779
|
const scheduleRefresh = createRefresher(() => projected.refresh(), (kind) => {
|
|
1736
1780
|
emit(kind);
|
|
1737
1781
|
ttl.mark();
|
|
1738
|
-
},
|
|
1782
|
+
}, retry);
|
|
1739
1783
|
const unsubUpdates = updates$.subscribe((batch) => {
|
|
1740
1784
|
if (!loaded) return;
|
|
1741
1785
|
const hasOwnModel = batch.entity.has(_def.model);
|
|
@@ -1764,6 +1808,7 @@ function createSingleton(source, options, client, updates$, resolver, refreshByT
|
|
|
1764
1808
|
},
|
|
1765
1809
|
destroy() {
|
|
1766
1810
|
unsubUpdates();
|
|
1811
|
+
retry.stop();
|
|
1767
1812
|
ttl.clear();
|
|
1768
1813
|
channel.complete();
|
|
1769
1814
|
}
|
|
@@ -1783,8 +1828,7 @@ function createSingletonSync(source, options, client, updates$, resolver, refres
|
|
|
1783
1828
|
destroy
|
|
1784
1829
|
};
|
|
1785
1830
|
}
|
|
1786
|
-
|
|
1787
|
-
//#region src/watcher.ts
|
|
1831
|
+
// src/watcher.ts
|
|
1788
1832
|
function addRouteByModel(map, entityType, event) {
|
|
1789
1833
|
let list = map.get(entityType);
|
|
1790
1834
|
if (!list) {
|
|
@@ -1929,8 +1973,7 @@ function createInternalWatcher(client, resolver) {
|
|
|
1929
1973
|
}
|
|
1930
1974
|
};
|
|
1931
1975
|
}
|
|
1932
|
-
|
|
1933
|
-
//#region src/store.ts
|
|
1976
|
+
// src/store.ts
|
|
1934
1977
|
var Store = class {
|
|
1935
1978
|
_client;
|
|
1936
1979
|
_resolver;
|
|
@@ -2048,7 +2091,6 @@ var Store = class {
|
|
|
2048
2091
|
function createStore(options) {
|
|
2049
2092
|
return new Store(options);
|
|
2050
2093
|
}
|
|
2051
|
-
|
|
2052
|
-
export { Store, createStore };
|
|
2094
|
+
export { Store, createStore, normalizeRoutePath };
|
|
2053
2095
|
|
|
2054
2096
|
//# sourceMappingURL=index.js.map
|