@contello/store 2.1.0 → 3.0.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 CHANGED
@@ -248,6 +248,7 @@ var schema = { operations, sources, models };
248
248
  // src/utils.ts
249
249
  var import_client = require("@contello/client");
250
250
  var DEFAULT_TTL_MS = 3 * 60 * 60 * 1e3;
251
+ var DEFAULT_LRU_MAX = 1e3;
251
252
  function resolveTtl(ttl) {
252
253
  if (ttl === void 0) return DEFAULT_TTL_MS;
253
254
  if (ttl === false || ttl <= 0) return void 0;
@@ -348,7 +349,7 @@ function mapAsset(raw) {
348
349
  id: raw.id,
349
350
  original: mapFile(raw.original),
350
351
  preview: raw.preview ? mapFile(raw.preview) : void 0,
351
- optimized: raw.optimized.map(mapFile)
352
+ optimized: raw.optimized.map((file) => mapFile(file))
352
353
  };
353
354
  }
354
355
  function createAssetsCollection(options, client, updates$, refreshByTtl) {
@@ -368,7 +369,7 @@ function createAssetsCollection(options, client, updates$, refreshByTtl) {
368
369
  () => (0, import_client2.collectAsync)(
369
370
  (0, import_client2.mapAsync)(client.subscribe(assetsSourceDoc, { ids }), (data) => data.source)
370
371
  ).then((rawItems) => {
371
- const items = rawItems.map(mapAsset);
372
+ const items = rawItems.map((item) => mapAsset(item));
372
373
  if (ids === void 0 && !loaded) {
373
374
  loaded = true;
374
375
  ttl.mark();
@@ -461,7 +462,7 @@ function createAssetsSyncCollection(options, client, updates$, refreshByTtl) {
461
462
  const { instance: base, destroy } = createAssetsCollection(options, client, updates$, refreshByTtl);
462
463
  function assertSync(value, method) {
463
464
  if (value instanceof Promise) {
464
- throw new Error(`assets.${method}() is not initialized yet \u2014 call assets.load() first`);
465
+ throw new TypeError(`assets.${method}() is not initialized yet \u2014 call assets.load() first`);
465
466
  }
466
467
  return value;
467
468
  }
@@ -481,6 +482,7 @@ var import_client3 = require("@contello/client");
481
482
  var import_projected2 = require("projected");
482
483
 
483
484
  // src/dependency-collector.ts
485
+ var EMPTY_KEYS = /* @__PURE__ */ new Set();
484
486
  function createDependencyKey(target, id) {
485
487
  return `${target}\0${id}`;
486
488
  }
@@ -513,12 +515,14 @@ var DependencyCollector = class {
513
515
  throw new Error(`track(): object with __typename "${obj.__typename}" has no "id" field`);
514
516
  }
515
517
  switch (obj.__typename) {
516
- case "ContelloAsset":
518
+ case "ContelloAsset": {
517
519
  deps.push(createDependencyKey("asset", obj.id));
518
520
  break;
519
- case "ContelloRoute":
521
+ }
522
+ case "ContelloRoute": {
520
523
  deps.push(createDependencyKey("route", obj.id));
521
524
  break;
525
+ }
522
526
  default: {
523
527
  const model = this.resolver.getModel(obj.__typename);
524
528
  if (!model) {
@@ -562,7 +566,7 @@ var DependencyCollector = class {
562
566
  */
563
567
  getAffectedKeys(event) {
564
568
  if (event.target === "entity") {
565
- return this.reverseIndex.get(createEntityDependencyKey(event.model, event.id)) ?? /* @__PURE__ */ new Set();
569
+ return this.reverseIndex.get(createEntityDependencyKey(event.model, event.id)) ?? EMPTY_KEYS;
566
570
  }
567
571
  let affected = this.reverseIndex.get(createDependencyKey(event.target, event.id));
568
572
  if (event.target === "route") {
@@ -573,7 +577,7 @@ var DependencyCollector = class {
573
577
  affected = this.mergeRouteTargetKeys(affected, event.before.model, event.before.entityId);
574
578
  }
575
579
  }
576
- return affected ?? /* @__PURE__ */ new Set();
580
+ return affected ?? EMPTY_KEYS;
577
581
  }
578
582
  mergeRouteTargetKeys(existing, entityType, entityId) {
579
583
  const items = this.reverseIndex.get(createRouteTargetKey(entityType, entityId));
@@ -664,11 +668,13 @@ function createCollection(source, options, client, updates$, resolver, refreshBy
664
668
  fetchCollection(source, client, ids),
665
669
  (rawItems) => Promise.all(
666
670
  rawItems.map(
667
- (item) => dependencyCollector.createContext(
668
- (ref, register) => (0, import_projected2.maybeThen)(mapFn(item, ref), (mapped) => {
669
- register(mapped.id);
670
- return mapped;
671
- })
671
+ (item) => Promise.resolve(
672
+ dependencyCollector.createContext(
673
+ (ref, register) => (0, import_projected2.maybeThen)(mapFn(item, ref), (mapped) => {
674
+ register(mapped.id);
675
+ return mapped;
676
+ })
677
+ )
672
678
  )
673
679
  )
674
680
  ).then((items) => {
@@ -796,7 +802,7 @@ function createCollectionSync(source, options, client, updates$, resolver, refre
796
802
  );
797
803
  function assertSync(value, method) {
798
804
  if (value instanceof Promise) {
799
- throw new Error(`collection "${base.name}".${method}() is not initialized yet \u2014 call collection.load() first`);
805
+ throw new TypeError(`collection "${base.name}".${method}() is not initialized yet \u2014 call collection.load() first`);
800
806
  }
801
807
  return value;
802
808
  }
@@ -935,7 +941,7 @@ function createI18nMessagesCollection(def, client, updates$, refreshByTtl) {
935
941
  `i18n-register:${def.collection}`,
936
942
  () => client.execute(storeRegisterI18nMessagesDocument, {
937
943
  collection: def.collection,
938
- messages: messages.map(toGqlMessageInput)
944
+ messages: messages.map((message) => toGqlMessageInput(message))
939
945
  }).then(() => {
940
946
  })
941
947
  );
@@ -993,13 +999,13 @@ function mapAsset2(raw) {
993
999
  id: raw.id,
994
1000
  original: mapFile2(raw.original),
995
1001
  preview: raw.preview ? mapFile2(raw.preview) : void 0,
996
- optimized: raw.optimized.map(mapFile2)
1002
+ optimized: raw.optimized.map((file) => mapFile2(file))
997
1003
  };
998
1004
  }
999
1005
  function createLazyAssetsCollection(def, client, updates$) {
1000
1006
  const _def = {
1001
1007
  cache: {
1002
- max: def?.cache?.max ?? 1e3,
1008
+ max: def?.cache?.max ?? DEFAULT_LRU_MAX,
1003
1009
  ttl: resolveTtl(def?.cache?.ttl)
1004
1010
  }
1005
1011
  };
@@ -1011,7 +1017,7 @@ function createLazyAssetsCollection(def, client, updates$) {
1011
1017
  "assets",
1012
1018
  () => (0, import_client5.collectAsync)(
1013
1019
  (0, import_client5.mapAsync)(client.subscribe(assetsSourceDoc, { ids }), (data) => data.source)
1014
- ).then((rawItems) => rawItems.map(mapAsset2))
1020
+ ).then((rawItems) => rawItems.map((item) => mapAsset2(item)))
1015
1021
  ),
1016
1022
  cache
1017
1023
  });
@@ -1085,7 +1091,7 @@ function createLazyCollection(source, options, client, updates$, resolver) {
1085
1091
  name: opts.name ?? source.__model,
1086
1092
  model: source.__model,
1087
1093
  cache: {
1088
- max: opts.cache?.max ?? 1e3,
1094
+ max: opts.cache?.max ?? DEFAULT_LRU_MAX,
1089
1095
  ttl: resolveTtl(opts.cache?.ttl)
1090
1096
  }
1091
1097
  };
@@ -1110,11 +1116,13 @@ function createLazyCollection(source, options, client, updates$, resolver) {
1110
1116
  ),
1111
1117
  (rawItems) => Promise.all(
1112
1118
  rawItems.map(
1113
- (item) => dependencyCollector.createContext(
1114
- (ref, register) => (0, import_projected5.maybeThen)(mapFn(item, ref), (mapped) => {
1115
- register(mapped.id);
1116
- return mapped;
1117
- })
1119
+ (item) => Promise.resolve(
1120
+ dependencyCollector.createContext(
1121
+ (ref, register) => (0, import_projected5.maybeThen)(mapFn(item, ref), (mapped) => {
1122
+ register(mapped.id);
1123
+ return mapped;
1124
+ })
1125
+ )
1118
1126
  )
1119
1127
  )
1120
1128
  )
@@ -1200,21 +1208,23 @@ function mapRoute(raw, resolver) {
1200
1208
  customHeaders: raw.customHeaders.map((h) => ({ name: h.name, value: h.value }))
1201
1209
  };
1202
1210
  switch (raw.target.__typename) {
1203
- case "ContelloRouteTargetRedirect":
1211
+ case "ContelloRouteTargetRedirect": {
1204
1212
  return {
1205
1213
  ...base,
1206
1214
  type: "redirect",
1207
1215
  location: raw.target.location,
1208
1216
  status: raw.target.responseCode
1209
1217
  };
1210
- case "ContelloRouteTargetText":
1218
+ }
1219
+ case "ContelloRouteTargetText": {
1211
1220
  return {
1212
1221
  ...base,
1213
1222
  type: "text",
1214
1223
  content: raw.target.content,
1215
1224
  mimeType: raw.target.mimeType
1216
1225
  };
1217
- case "ContelloRouteTargetAsset":
1226
+ }
1227
+ case "ContelloRouteTargetAsset": {
1218
1228
  return {
1219
1229
  ...base,
1220
1230
  type: "asset",
@@ -1223,6 +1233,7 @@ function mapRoute(raw, resolver) {
1223
1233
  contentDisposition: raw.target.contentDisposition === "INLINE" ? "inline" : "attachment",
1224
1234
  mimeType: raw.target.asset.original.mimeType
1225
1235
  };
1236
+ }
1226
1237
  case "ContelloRouteTargetEntity": {
1227
1238
  if (!raw.target.entity) {
1228
1239
  return void 0;
@@ -1236,8 +1247,9 @@ function mapRoute(raw, resolver) {
1236
1247
  entityId: raw.target.entity.id
1237
1248
  };
1238
1249
  }
1239
- default:
1250
+ default: {
1240
1251
  return void 0;
1252
+ }
1241
1253
  }
1242
1254
  }
1243
1255
 
@@ -1257,11 +1269,13 @@ function collectRoutes(rawItems, keyFn, resolver) {
1257
1269
  }
1258
1270
  function createRoutesCache(max, ttl) {
1259
1271
  const pathToId = /* @__PURE__ */ new Map();
1272
+ const idToPath = /* @__PURE__ */ new Map();
1260
1273
  const lru = createLruCache({
1261
1274
  max,
1262
1275
  ttl,
1263
1276
  onEvict: (route) => {
1264
1277
  pathToId.delete(route.path);
1278
+ idToPath.delete(route.id);
1265
1279
  }
1266
1280
  });
1267
1281
  function resolveKey(key) {
@@ -1278,7 +1292,12 @@ function createRoutesCache(max, ttl) {
1278
1292
  has: (key) => lru.has(resolveKey(key)),
1279
1293
  get: (key) => lru.get(resolveKey(key)),
1280
1294
  set: (_key, value) => {
1295
+ const previousPath = idToPath.get(value.id);
1296
+ if (previousPath !== void 0 && previousPath !== value.path) {
1297
+ pathToId.delete(previousPath);
1298
+ }
1281
1299
  pathToId.set(value.path, value.id);
1300
+ idToPath.set(value.id, value.path);
1282
1301
  lru.set(ID_PREFIX + value.id, value);
1283
1302
  },
1284
1303
  delete: (key) => {
@@ -1286,11 +1305,13 @@ function createRoutesCache(max, ttl) {
1286
1305
  const value = lru.get(resolved);
1287
1306
  if (value) {
1288
1307
  pathToId.delete(value.path);
1308
+ idToPath.delete(value.id);
1289
1309
  }
1290
1310
  lru.delete(resolved);
1291
1311
  },
1292
1312
  clear: () => {
1293
1313
  pathToId.clear();
1314
+ idToPath.clear();
1294
1315
  lru.clear();
1295
1316
  },
1296
1317
  keys: () => lru.keys()
@@ -1299,7 +1320,7 @@ function createRoutesCache(max, ttl) {
1299
1320
  function createLazyRoutesCollection(def, client, updates$, resolver) {
1300
1321
  const _def = {
1301
1322
  cache: {
1302
- max: def?.cache?.max ?? 1e3,
1323
+ max: def?.cache?.max ?? DEFAULT_LRU_MAX,
1303
1324
  ttl: resolveTtl(def?.cache?.ttl)
1304
1325
  }
1305
1326
  };
@@ -1432,7 +1453,7 @@ var ModelResolver = class {
1432
1453
  return this._toTypeName.get(model) ?? model;
1433
1454
  }
1434
1455
  hasModel(model) {
1435
- return !this._toTypeName.size || this._toTypeName.has(model);
1456
+ return this._toTypeName.size === 0 || this._toTypeName.has(model);
1436
1457
  }
1437
1458
  resolveModel(typeName) {
1438
1459
  return this._toModel.get(typeName) ?? typeName;
@@ -1588,7 +1609,8 @@ function createRoutesCollection(options, client, updates$, resolver, refreshByTt
1588
1609
  if (loaded) {
1589
1610
  return void 0;
1590
1611
  }
1591
- return (0, import_projected7.maybeThen)(projected.getAllAsMap(), () => void 0);
1612
+ return (0, import_projected7.maybeThen)(projected.getAllAsMap(), () => {
1613
+ });
1592
1614
  }
1593
1615
  const instance = {
1594
1616
  refresh$: channel.stream$,
@@ -1598,10 +1620,10 @@ function createRoutesCollection(options, client, updates$, resolver, refreshByTt
1598
1620
  }
1599
1621
  },
1600
1622
  get(idOrIds) {
1601
- return Array.isArray(idOrIds) ? getByIds(idOrIds) : getByIdSingle(idOrIds);
1623
+ return typeof idOrIds === "string" ? getByIdSingle(idOrIds) : getByIds(idOrIds);
1602
1624
  },
1603
1625
  getByPath(pathOrPaths) {
1604
- return Array.isArray(pathOrPaths) ? lookupByPaths(pathOrPaths) : lookupByPathSingle(pathOrPaths);
1626
+ return typeof pathOrPaths === "string" ? lookupByPathSingle(pathOrPaths) : lookupByPaths(pathOrPaths);
1605
1627
  },
1606
1628
  refresh() {
1607
1629
  runFullRefresh("on-demand");
@@ -1620,7 +1642,7 @@ function createRoutesSyncCollection(options, client, updates$, resolver, refresh
1620
1642
  const { instance: base, destroy } = createRoutesCollection(options, client, updates$, resolver, refreshByTtl);
1621
1643
  function assertSync(value, method) {
1622
1644
  if (value instanceof Promise) {
1623
- throw new Error(`routes.${method}() is not initialized yet \u2014 call routes.load() first`);
1645
+ throw new TypeError(`routes.${method}() is not initialized yet \u2014 call routes.load() first`);
1624
1646
  }
1625
1647
  return value;
1626
1648
  }
@@ -1755,7 +1777,7 @@ function createSingletonSync(source, options, client, updates$, resolver, refres
1755
1777
  get() {
1756
1778
  const result = base.get();
1757
1779
  if (result instanceof Promise) {
1758
- throw new Error(`singleton "${base.name}" is not initialized yet \u2014 call singleton.load() first`);
1780
+ throw new TypeError(`singleton "${base.name}" is not initialized yet \u2014 call singleton.load() first`);
1759
1781
  }
1760
1782
  return result;
1761
1783
  }
@@ -1782,7 +1804,7 @@ function createUpdateBatch(events) {
1782
1804
  const routeByEntityModel = /* @__PURE__ */ new Map();
1783
1805
  for (const event of events) {
1784
1806
  switch (event.target) {
1785
- case "route":
1807
+ case "route": {
1786
1808
  route.push(event);
1787
1809
  if ("after" in event && event.after.type === "entity") {
1788
1810
  addRouteByModel(routeByEntityModel, event.after.model, event);
@@ -1791,12 +1813,15 @@ function createUpdateBatch(events) {
1791
1813
  addRouteByModel(routeByEntityModel, event.before.model, event);
1792
1814
  }
1793
1815
  break;
1794
- case "asset":
1816
+ }
1817
+ case "asset": {
1795
1818
  asset.push(event);
1796
1819
  break;
1797
- case "i18nMessage":
1820
+ }
1821
+ case "i18nMessage": {
1798
1822
  i18nMessage.push(event);
1799
1823
  break;
1824
+ }
1800
1825
  case "entity": {
1801
1826
  let list = entity.get(event.model);
1802
1827
  if (!list) {
@@ -1812,12 +1837,15 @@ function createUpdateBatch(events) {
1812
1837
  }
1813
1838
  function castMutationType(type) {
1814
1839
  switch (type) {
1815
- case "CREATE":
1840
+ case "CREATE": {
1816
1841
  return "create";
1817
- case "UPDATE":
1842
+ }
1843
+ case "UPDATE": {
1818
1844
  return "update";
1819
- case "DELETE":
1845
+ }
1846
+ case "DELETE": {
1820
1847
  return "delete";
1848
+ }
1821
1849
  }
1822
1850
  }
1823
1851
  function mapEvent(raw, resolver) {
@@ -1842,10 +1870,12 @@ function mapEvent(raw, resolver) {
1842
1870
  }
1843
1871
  return { id, mutation, target: "route", after, before };
1844
1872
  }
1845
- case "ContelloAsset":
1873
+ case "ContelloAsset": {
1846
1874
  return { id, mutation, target: "asset" };
1847
- case "ContelloI18nMessage":
1875
+ }
1876
+ case "ContelloI18nMessage": {
1848
1877
  return { id, mutation, target: "i18nMessage", token: target.token };
1878
+ }
1849
1879
  default: {
1850
1880
  if (!__typename || !resolver.hasTypeName(__typename)) {
1851
1881
  return void 0;
@@ -1854,6 +1884,13 @@ function mapEvent(raw, resolver) {
1854
1884
  }
1855
1885
  }
1856
1886
  }
1887
+ function safeMapEvent(raw, resolver) {
1888
+ try {
1889
+ return mapEvent(raw, resolver);
1890
+ } catch {
1891
+ return void 0;
1892
+ }
1893
+ }
1857
1894
  function createInternalWatcher(client, resolver) {
1858
1895
  const updates$ = (0, import_client10.createAsyncIterableSubject)();
1859
1896
  let controller;
@@ -1873,7 +1910,7 @@ function createInternalWatcher(client, resolver) {
1873
1910
  );
1874
1911
  const events = (0, import_client10.mapAsync)(
1875
1912
  source,
1876
- (data) => (data.contelloUpdatesBatch?.events ?? []).map((e) => mapEvent(e, resolver)).filter((e) => e !== void 0)
1913
+ (data) => (data.contelloUpdatesBatch?.events ?? []).map((e) => safeMapEvent(e, resolver)).filter((e) => e !== void 0)
1877
1914
  );
1878
1915
  for await (const list of events) {
1879
1916
  if (signal.aborted) {
@@ -1882,9 +1919,13 @@ function createInternalWatcher(client, resolver) {
1882
1919
  if (list.length === 0) {
1883
1920
  continue;
1884
1921
  }
1885
- updates$.next(createUpdateBatch(list));
1922
+ try {
1923
+ updates$.next(createUpdateBatch(list));
1924
+ } catch {
1925
+ }
1886
1926
  }
1887
- })();
1927
+ })().catch(() => {
1928
+ });
1888
1929
  });
1889
1930
  },
1890
1931
  stop() {
package/dist/index.d.cts CHANGED
@@ -114,7 +114,8 @@ type SourceAt<TSchema, TKey> = TSchema extends {
114
114
  } ? TKey extends keyof TSources ? TSources[TKey] : never : never;
115
115
  /**
116
116
  * For `define*(arg, options?)` — accepts either a string key (looked up in `schema.sources`) or a SourceDef directly.
117
- * `ResolveSource` projects the argument to the underlying SourceDef so `ExtractSourceResult` can extract the fragment type.
117
+ * `ResolveSource` projects the argument to the underlying SourceDef so `ExtractSourceResult`
118
+ * can extract the fragment type.
118
119
  */
119
120
  type ResolveSource<TSchema, T> = T extends string ? SourceAt<TSchema, T> : T extends SourceDef<any, any, any> ? T : never;
120
121
  type CreateStoreOptions<TSchema extends Schema | undefined = undefined> = {
@@ -161,7 +162,10 @@ type CacheOptions = {
161
162
  */
162
163
  eviction?: 'refresh' | 'clear' | undefined;
163
164
  };
164
- /** Cache options for sync collections and singletons. `eviction` is omitted — clearing the cache would break the sync guarantee. */
165
+ /**
166
+ * Cache options for sync collections and singletons. `eviction` is omitted — clearing
167
+ * the cache would break the sync guarantee.
168
+ */
165
169
  type SyncCacheOptions = {
166
170
  /**
167
171
  * Periodic full refresh — guards against stale data from missed update events.
@@ -235,7 +239,7 @@ type Collection<T> = {
235
239
  readonly refresh$: AsyncIterable<RefreshEvent>;
236
240
  load(): Promise<void>;
237
241
  get(id: string): MaybePromise<ReadonlyDeep<T> | undefined>;
238
- get(ids: string[]): MaybePromise<ReadonlyArray<ReadonlyDeep<T>>>;
242
+ get(ids: readonly string[]): MaybePromise<ReadonlyArray<ReadonlyDeep<T>>>;
239
243
  getAll(): MaybePromise<ReadonlyArray<ReadonlyDeep<T>>>;
240
244
  refresh(): void;
241
245
  };
@@ -244,7 +248,7 @@ type CollectionSync<T> = {
244
248
  readonly refresh$: AsyncIterable<RefreshEvent>;
245
249
  load(): Promise<void>;
246
250
  get(id: string): ReadonlyDeep<T> | undefined;
247
- get(ids: string[]): ReadonlyArray<ReadonlyDeep<T>>;
251
+ get(ids: readonly string[]): ReadonlyArray<ReadonlyDeep<T>>;
248
252
  getAll(): ReadonlyArray<ReadonlyDeep<T>>;
249
253
  refresh(): void;
250
254
  };
@@ -260,7 +264,7 @@ type LazyCollection<T> = {
260
264
  readonly name: string;
261
265
  readonly refresh$: AsyncIterable<RefreshEvent>;
262
266
  get(id: string): MaybePromise<ReadonlyDeep<T> | undefined>;
263
- get(ids: string[]): MaybePromise<ReadonlyArray<ReadonlyDeep<T>>>;
267
+ get(ids: readonly string[]): MaybePromise<ReadonlyArray<ReadonlyDeep<T>>>;
264
268
  refresh(): void;
265
269
  clear(): void;
266
270
  };
@@ -287,7 +291,7 @@ type LazyAssetsOptions = {
287
291
  type LazyAssets = {
288
292
  readonly refresh$: AsyncIterable<RefreshEvent>;
289
293
  get(id: string): Promise<ReadonlyDeep<StoreAsset> | undefined>;
290
- get(ids: string[]): Promise<ReadonlyDeep<StoreAsset[]>>;
294
+ get(ids: readonly string[]): Promise<ReadonlyDeep<StoreAsset[]>>;
291
295
  upload(data: UploadData, meta: UploadMetadata, options?: UploadOptions | undefined): Promise<string>;
292
296
  download(fileId: string): Promise<DownloadResult>;
293
297
  proxyHls(path: string, signal?: AbortSignal | undefined): Promise<ProxyResult>;
@@ -309,7 +313,7 @@ type Assets = {
309
313
  readonly refresh$: AsyncIterable<RefreshEvent>;
310
314
  load(): Promise<void>;
311
315
  get(id: string): MaybePromise<ReadonlyDeep<StoreAsset> | undefined>;
312
- get(ids: string[]): MaybePromise<ReadonlyArray<ReadonlyDeep<StoreAsset>>>;
316
+ get(ids: readonly string[]): MaybePromise<ReadonlyArray<ReadonlyDeep<StoreAsset>>>;
313
317
  upload(data: UploadData, meta: UploadMetadata, options?: UploadOptions | undefined): Promise<string>;
314
318
  download(fileId: string): Promise<DownloadResult>;
315
319
  proxyHls(path: string, signal?: AbortSignal | undefined): Promise<ProxyResult>;
@@ -319,7 +323,7 @@ type AssetsSync = {
319
323
  readonly refresh$: AsyncIterable<RefreshEvent>;
320
324
  load(): Promise<void>;
321
325
  get(id: string): ReadonlyDeep<StoreAsset> | undefined;
322
- get(ids: string[]): ReadonlyArray<ReadonlyDeep<StoreAsset>>;
326
+ get(ids: readonly string[]): ReadonlyArray<ReadonlyDeep<StoreAsset>>;
323
327
  upload(data: UploadData, meta: UploadMetadata, options?: UploadOptions | undefined): Promise<string>;
324
328
  download(fileId: string): Promise<DownloadResult>;
325
329
  proxyHls(path: string, signal?: AbortSignal | undefined): Promise<ProxyResult>;
@@ -362,7 +366,7 @@ type I18nMessage = {
362
366
  type I18nMessages = {
363
367
  readonly refresh$: AsyncIterable<RefreshEvent>;
364
368
  get(id: string): MaybePromise<ReadonlyDeep<I18nMessage> | undefined>;
365
- get(ids: string[]): MaybePromise<ReadonlyDeep<I18nMessage[]>>;
369
+ get(ids: readonly string[]): MaybePromise<ReadonlyDeep<I18nMessage[]>>;
366
370
  getAll(): MaybePromise<ReadonlyDeep<I18nMessage[]>>;
367
371
  register(messages: I18nMessageRegistrationDefinition[]): Promise<void>;
368
372
  refresh(): void;
@@ -377,9 +381,9 @@ type LazyRoutesOptions = {
377
381
  type LazyRoutes = {
378
382
  readonly refresh$: AsyncIterable<RefreshEvent>;
379
383
  get(id: string): Promise<ReadonlyDeep<StoreRoute> | undefined>;
380
- get(ids: string[]): Promise<ReadonlyDeep<StoreRoute[]>>;
384
+ get(ids: readonly string[]): Promise<ReadonlyDeep<StoreRoute[]>>;
381
385
  getByPath(path: string): Promise<ReadonlyDeep<StoreRoute> | undefined>;
382
- getByPath(paths: string[]): Promise<ReadonlyDeep<StoreRoute[]>>;
386
+ getByPath(paths: readonly string[]): Promise<ReadonlyDeep<StoreRoute[]>>;
383
387
  refresh(): void;
384
388
  clear(): void;
385
389
  };
@@ -398,18 +402,18 @@ type Routes = {
398
402
  readonly refresh$: AsyncIterable<RefreshEvent>;
399
403
  load(): Promise<void>;
400
404
  get(id: string): MaybePromise<ReadonlyDeep<StoreRoute> | undefined>;
401
- get(ids: string[]): MaybePromise<ReadonlyArray<ReadonlyDeep<StoreRoute>>>;
405
+ get(ids: readonly string[]): MaybePromise<ReadonlyArray<ReadonlyDeep<StoreRoute>>>;
402
406
  getByPath(path: string): MaybePromise<ReadonlyDeep<StoreRoute> | undefined>;
403
- getByPath(paths: string[]): MaybePromise<ReadonlyArray<ReadonlyDeep<StoreRoute>>>;
407
+ getByPath(paths: readonly string[]): MaybePromise<ReadonlyArray<ReadonlyDeep<StoreRoute>>>;
404
408
  refresh(): void;
405
409
  };
406
410
  type RoutesSync = {
407
411
  readonly refresh$: AsyncIterable<RefreshEvent>;
408
412
  load(): Promise<void>;
409
413
  get(id: string): ReadonlyDeep<StoreRoute> | undefined;
410
- get(ids: string[]): ReadonlyArray<ReadonlyDeep<StoreRoute>>;
414
+ get(ids: readonly string[]): ReadonlyArray<ReadonlyDeep<StoreRoute>>;
411
415
  getByPath(path: string): ReadonlyDeep<StoreRoute> | undefined;
412
- getByPath(paths: string[]): ReadonlyArray<ReadonlyDeep<StoreRoute>>;
416
+ getByPath(paths: readonly string[]): ReadonlyArray<ReadonlyDeep<StoreRoute>>;
413
417
  refresh(): void;
414
418
  };
415
419
 
package/dist/index.d.ts CHANGED
@@ -114,7 +114,8 @@ type SourceAt<TSchema, TKey> = TSchema extends {
114
114
  } ? TKey extends keyof TSources ? TSources[TKey] : never : never;
115
115
  /**
116
116
  * For `define*(arg, options?)` — accepts either a string key (looked up in `schema.sources`) or a SourceDef directly.
117
- * `ResolveSource` projects the argument to the underlying SourceDef so `ExtractSourceResult` can extract the fragment type.
117
+ * `ResolveSource` projects the argument to the underlying SourceDef so `ExtractSourceResult`
118
+ * can extract the fragment type.
118
119
  */
119
120
  type ResolveSource<TSchema, T> = T extends string ? SourceAt<TSchema, T> : T extends SourceDef<any, any, any> ? T : never;
120
121
  type CreateStoreOptions<TSchema extends Schema | undefined = undefined> = {
@@ -161,7 +162,10 @@ type CacheOptions = {
161
162
  */
162
163
  eviction?: 'refresh' | 'clear' | undefined;
163
164
  };
164
- /** Cache options for sync collections and singletons. `eviction` is omitted — clearing the cache would break the sync guarantee. */
165
+ /**
166
+ * Cache options for sync collections and singletons. `eviction` is omitted — clearing
167
+ * the cache would break the sync guarantee.
168
+ */
165
169
  type SyncCacheOptions = {
166
170
  /**
167
171
  * Periodic full refresh — guards against stale data from missed update events.
@@ -235,7 +239,7 @@ type Collection<T> = {
235
239
  readonly refresh$: AsyncIterable<RefreshEvent>;
236
240
  load(): Promise<void>;
237
241
  get(id: string): MaybePromise<ReadonlyDeep<T> | undefined>;
238
- get(ids: string[]): MaybePromise<ReadonlyArray<ReadonlyDeep<T>>>;
242
+ get(ids: readonly string[]): MaybePromise<ReadonlyArray<ReadonlyDeep<T>>>;
239
243
  getAll(): MaybePromise<ReadonlyArray<ReadonlyDeep<T>>>;
240
244
  refresh(): void;
241
245
  };
@@ -244,7 +248,7 @@ type CollectionSync<T> = {
244
248
  readonly refresh$: AsyncIterable<RefreshEvent>;
245
249
  load(): Promise<void>;
246
250
  get(id: string): ReadonlyDeep<T> | undefined;
247
- get(ids: string[]): ReadonlyArray<ReadonlyDeep<T>>;
251
+ get(ids: readonly string[]): ReadonlyArray<ReadonlyDeep<T>>;
248
252
  getAll(): ReadonlyArray<ReadonlyDeep<T>>;
249
253
  refresh(): void;
250
254
  };
@@ -260,7 +264,7 @@ type LazyCollection<T> = {
260
264
  readonly name: string;
261
265
  readonly refresh$: AsyncIterable<RefreshEvent>;
262
266
  get(id: string): MaybePromise<ReadonlyDeep<T> | undefined>;
263
- get(ids: string[]): MaybePromise<ReadonlyArray<ReadonlyDeep<T>>>;
267
+ get(ids: readonly string[]): MaybePromise<ReadonlyArray<ReadonlyDeep<T>>>;
264
268
  refresh(): void;
265
269
  clear(): void;
266
270
  };
@@ -287,7 +291,7 @@ type LazyAssetsOptions = {
287
291
  type LazyAssets = {
288
292
  readonly refresh$: AsyncIterable<RefreshEvent>;
289
293
  get(id: string): Promise<ReadonlyDeep<StoreAsset> | undefined>;
290
- get(ids: string[]): Promise<ReadonlyDeep<StoreAsset[]>>;
294
+ get(ids: readonly string[]): Promise<ReadonlyDeep<StoreAsset[]>>;
291
295
  upload(data: UploadData, meta: UploadMetadata, options?: UploadOptions | undefined): Promise<string>;
292
296
  download(fileId: string): Promise<DownloadResult>;
293
297
  proxyHls(path: string, signal?: AbortSignal | undefined): Promise<ProxyResult>;
@@ -309,7 +313,7 @@ type Assets = {
309
313
  readonly refresh$: AsyncIterable<RefreshEvent>;
310
314
  load(): Promise<void>;
311
315
  get(id: string): MaybePromise<ReadonlyDeep<StoreAsset> | undefined>;
312
- get(ids: string[]): MaybePromise<ReadonlyArray<ReadonlyDeep<StoreAsset>>>;
316
+ get(ids: readonly string[]): MaybePromise<ReadonlyArray<ReadonlyDeep<StoreAsset>>>;
313
317
  upload(data: UploadData, meta: UploadMetadata, options?: UploadOptions | undefined): Promise<string>;
314
318
  download(fileId: string): Promise<DownloadResult>;
315
319
  proxyHls(path: string, signal?: AbortSignal | undefined): Promise<ProxyResult>;
@@ -319,7 +323,7 @@ type AssetsSync = {
319
323
  readonly refresh$: AsyncIterable<RefreshEvent>;
320
324
  load(): Promise<void>;
321
325
  get(id: string): ReadonlyDeep<StoreAsset> | undefined;
322
- get(ids: string[]): ReadonlyArray<ReadonlyDeep<StoreAsset>>;
326
+ get(ids: readonly string[]): ReadonlyArray<ReadonlyDeep<StoreAsset>>;
323
327
  upload(data: UploadData, meta: UploadMetadata, options?: UploadOptions | undefined): Promise<string>;
324
328
  download(fileId: string): Promise<DownloadResult>;
325
329
  proxyHls(path: string, signal?: AbortSignal | undefined): Promise<ProxyResult>;
@@ -362,7 +366,7 @@ type I18nMessage = {
362
366
  type I18nMessages = {
363
367
  readonly refresh$: AsyncIterable<RefreshEvent>;
364
368
  get(id: string): MaybePromise<ReadonlyDeep<I18nMessage> | undefined>;
365
- get(ids: string[]): MaybePromise<ReadonlyDeep<I18nMessage[]>>;
369
+ get(ids: readonly string[]): MaybePromise<ReadonlyDeep<I18nMessage[]>>;
366
370
  getAll(): MaybePromise<ReadonlyDeep<I18nMessage[]>>;
367
371
  register(messages: I18nMessageRegistrationDefinition[]): Promise<void>;
368
372
  refresh(): void;
@@ -377,9 +381,9 @@ type LazyRoutesOptions = {
377
381
  type LazyRoutes = {
378
382
  readonly refresh$: AsyncIterable<RefreshEvent>;
379
383
  get(id: string): Promise<ReadonlyDeep<StoreRoute> | undefined>;
380
- get(ids: string[]): Promise<ReadonlyDeep<StoreRoute[]>>;
384
+ get(ids: readonly string[]): Promise<ReadonlyDeep<StoreRoute[]>>;
381
385
  getByPath(path: string): Promise<ReadonlyDeep<StoreRoute> | undefined>;
382
- getByPath(paths: string[]): Promise<ReadonlyDeep<StoreRoute[]>>;
386
+ getByPath(paths: readonly string[]): Promise<ReadonlyDeep<StoreRoute[]>>;
383
387
  refresh(): void;
384
388
  clear(): void;
385
389
  };
@@ -398,18 +402,18 @@ type Routes = {
398
402
  readonly refresh$: AsyncIterable<RefreshEvent>;
399
403
  load(): Promise<void>;
400
404
  get(id: string): MaybePromise<ReadonlyDeep<StoreRoute> | undefined>;
401
- get(ids: string[]): MaybePromise<ReadonlyArray<ReadonlyDeep<StoreRoute>>>;
405
+ get(ids: readonly string[]): MaybePromise<ReadonlyArray<ReadonlyDeep<StoreRoute>>>;
402
406
  getByPath(path: string): MaybePromise<ReadonlyDeep<StoreRoute> | undefined>;
403
- getByPath(paths: string[]): MaybePromise<ReadonlyArray<ReadonlyDeep<StoreRoute>>>;
407
+ getByPath(paths: readonly string[]): MaybePromise<ReadonlyArray<ReadonlyDeep<StoreRoute>>>;
404
408
  refresh(): void;
405
409
  };
406
410
  type RoutesSync = {
407
411
  readonly refresh$: AsyncIterable<RefreshEvent>;
408
412
  load(): Promise<void>;
409
413
  get(id: string): ReadonlyDeep<StoreRoute> | undefined;
410
- get(ids: string[]): ReadonlyArray<ReadonlyDeep<StoreRoute>>;
414
+ get(ids: readonly string[]): ReadonlyArray<ReadonlyDeep<StoreRoute>>;
411
415
  getByPath(path: string): ReadonlyDeep<StoreRoute> | undefined;
412
- getByPath(paths: string[]): ReadonlyArray<ReadonlyDeep<StoreRoute>>;
416
+ getByPath(paths: readonly string[]): ReadonlyArray<ReadonlyDeep<StoreRoute>>;
413
417
  refresh(): void;
414
418
  };
415
419
 
package/dist/index.js CHANGED
@@ -214,6 +214,7 @@ var schema = { operations, sources, models };
214
214
  // src/utils.ts
215
215
  import { createAsyncIterableSubject, runWithBackoff } from "@contello/client";
216
216
  var DEFAULT_TTL_MS = 3 * 60 * 60 * 1e3;
217
+ var DEFAULT_LRU_MAX = 1e3;
217
218
  function resolveTtl(ttl) {
218
219
  if (ttl === void 0) return DEFAULT_TTL_MS;
219
220
  if (ttl === false || ttl <= 0) return void 0;
@@ -314,7 +315,7 @@ function mapAsset(raw) {
314
315
  id: raw.id,
315
316
  original: mapFile(raw.original),
316
317
  preview: raw.preview ? mapFile(raw.preview) : void 0,
317
- optimized: raw.optimized.map(mapFile)
318
+ optimized: raw.optimized.map((file) => mapFile(file))
318
319
  };
319
320
  }
320
321
  function createAssetsCollection(options, client, updates$, refreshByTtl) {
@@ -334,7 +335,7 @@ function createAssetsCollection(options, client, updates$, refreshByTtl) {
334
335
  () => collectAsync(
335
336
  mapAsync(client.subscribe(assetsSourceDoc, { ids }), (data) => data.source)
336
337
  ).then((rawItems) => {
337
- const items = rawItems.map(mapAsset);
338
+ const items = rawItems.map((item) => mapAsset(item));
338
339
  if (ids === void 0 && !loaded) {
339
340
  loaded = true;
340
341
  ttl.mark();
@@ -427,7 +428,7 @@ function createAssetsSyncCollection(options, client, updates$, refreshByTtl) {
427
428
  const { instance: base, destroy } = createAssetsCollection(options, client, updates$, refreshByTtl);
428
429
  function assertSync(value, method) {
429
430
  if (value instanceof Promise) {
430
- throw new Error(`assets.${method}() is not initialized yet \u2014 call assets.load() first`);
431
+ throw new TypeError(`assets.${method}() is not initialized yet \u2014 call assets.load() first`);
431
432
  }
432
433
  return value;
433
434
  }
@@ -452,6 +453,7 @@ import {
452
453
  import { ProjectedMap as ProjectedMap2, maybeThen } from "projected";
453
454
 
454
455
  // src/dependency-collector.ts
456
+ var EMPTY_KEYS = /* @__PURE__ */ new Set();
455
457
  function createDependencyKey(target, id) {
456
458
  return `${target}\0${id}`;
457
459
  }
@@ -484,12 +486,14 @@ var DependencyCollector = class {
484
486
  throw new Error(`track(): object with __typename "${obj.__typename}" has no "id" field`);
485
487
  }
486
488
  switch (obj.__typename) {
487
- case "ContelloAsset":
489
+ case "ContelloAsset": {
488
490
  deps.push(createDependencyKey("asset", obj.id));
489
491
  break;
490
- case "ContelloRoute":
492
+ }
493
+ case "ContelloRoute": {
491
494
  deps.push(createDependencyKey("route", obj.id));
492
495
  break;
496
+ }
493
497
  default: {
494
498
  const model = this.resolver.getModel(obj.__typename);
495
499
  if (!model) {
@@ -533,7 +537,7 @@ var DependencyCollector = class {
533
537
  */
534
538
  getAffectedKeys(event) {
535
539
  if (event.target === "entity") {
536
- return this.reverseIndex.get(createEntityDependencyKey(event.model, event.id)) ?? /* @__PURE__ */ new Set();
540
+ return this.reverseIndex.get(createEntityDependencyKey(event.model, event.id)) ?? EMPTY_KEYS;
537
541
  }
538
542
  let affected = this.reverseIndex.get(createDependencyKey(event.target, event.id));
539
543
  if (event.target === "route") {
@@ -544,7 +548,7 @@ var DependencyCollector = class {
544
548
  affected = this.mergeRouteTargetKeys(affected, event.before.model, event.before.entityId);
545
549
  }
546
550
  }
547
- return affected ?? /* @__PURE__ */ new Set();
551
+ return affected ?? EMPTY_KEYS;
548
552
  }
549
553
  mergeRouteTargetKeys(existing, entityType, entityId) {
550
554
  const items = this.reverseIndex.get(createRouteTargetKey(entityType, entityId));
@@ -635,11 +639,13 @@ function createCollection(source, options, client, updates$, resolver, refreshBy
635
639
  fetchCollection(source, client, ids),
636
640
  (rawItems) => Promise.all(
637
641
  rawItems.map(
638
- (item) => dependencyCollector.createContext(
639
- (ref, register) => maybeThen(mapFn(item, ref), (mapped) => {
640
- register(mapped.id);
641
- return mapped;
642
- })
642
+ (item) => Promise.resolve(
643
+ dependencyCollector.createContext(
644
+ (ref, register) => maybeThen(mapFn(item, ref), (mapped) => {
645
+ register(mapped.id);
646
+ return mapped;
647
+ })
648
+ )
643
649
  )
644
650
  )
645
651
  ).then((items) => {
@@ -767,7 +773,7 @@ function createCollectionSync(source, options, client, updates$, resolver, refre
767
773
  );
768
774
  function assertSync(value, method) {
769
775
  if (value instanceof Promise) {
770
- throw new Error(`collection "${base.name}".${method}() is not initialized yet \u2014 call collection.load() first`);
776
+ throw new TypeError(`collection "${base.name}".${method}() is not initialized yet \u2014 call collection.load() first`);
771
777
  }
772
778
  return value;
773
779
  }
@@ -911,7 +917,7 @@ function createI18nMessagesCollection(def, client, updates$, refreshByTtl) {
911
917
  `i18n-register:${def.collection}`,
912
918
  () => client.execute(storeRegisterI18nMessagesDocument, {
913
919
  collection: def.collection,
914
- messages: messages.map(toGqlMessageInput)
920
+ messages: messages.map((message) => toGqlMessageInput(message))
915
921
  }).then(() => {
916
922
  })
917
923
  );
@@ -973,13 +979,13 @@ function mapAsset2(raw) {
973
979
  id: raw.id,
974
980
  original: mapFile2(raw.original),
975
981
  preview: raw.preview ? mapFile2(raw.preview) : void 0,
976
- optimized: raw.optimized.map(mapFile2)
982
+ optimized: raw.optimized.map((file) => mapFile2(file))
977
983
  };
978
984
  }
979
985
  function createLazyAssetsCollection(def, client, updates$) {
980
986
  const _def = {
981
987
  cache: {
982
- max: def?.cache?.max ?? 1e3,
988
+ max: def?.cache?.max ?? DEFAULT_LRU_MAX,
983
989
  ttl: resolveTtl(def?.cache?.ttl)
984
990
  }
985
991
  };
@@ -991,7 +997,7 @@ function createLazyAssetsCollection(def, client, updates$) {
991
997
  "assets",
992
998
  () => collectAsync4(
993
999
  mapAsync4(client.subscribe(assetsSourceDoc, { ids }), (data) => data.source)
994
- ).then((rawItems) => rawItems.map(mapAsset2))
1000
+ ).then((rawItems) => rawItems.map((item) => mapAsset2(item)))
995
1001
  ),
996
1002
  cache
997
1003
  });
@@ -1070,7 +1076,7 @@ function createLazyCollection(source, options, client, updates$, resolver) {
1070
1076
  name: opts.name ?? source.__model,
1071
1077
  model: source.__model,
1072
1078
  cache: {
1073
- max: opts.cache?.max ?? 1e3,
1079
+ max: opts.cache?.max ?? DEFAULT_LRU_MAX,
1074
1080
  ttl: resolveTtl(opts.cache?.ttl)
1075
1081
  }
1076
1082
  };
@@ -1095,11 +1101,13 @@ function createLazyCollection(source, options, client, updates$, resolver) {
1095
1101
  ),
1096
1102
  (rawItems) => Promise.all(
1097
1103
  rawItems.map(
1098
- (item) => dependencyCollector.createContext(
1099
- (ref, register) => maybeThen3(mapFn(item, ref), (mapped) => {
1100
- register(mapped.id);
1101
- return mapped;
1102
- })
1104
+ (item) => Promise.resolve(
1105
+ dependencyCollector.createContext(
1106
+ (ref, register) => maybeThen3(mapFn(item, ref), (mapped) => {
1107
+ register(mapped.id);
1108
+ return mapped;
1109
+ })
1110
+ )
1103
1111
  )
1104
1112
  )
1105
1113
  )
@@ -1189,21 +1197,23 @@ function mapRoute(raw, resolver) {
1189
1197
  customHeaders: raw.customHeaders.map((h) => ({ name: h.name, value: h.value }))
1190
1198
  };
1191
1199
  switch (raw.target.__typename) {
1192
- case "ContelloRouteTargetRedirect":
1200
+ case "ContelloRouteTargetRedirect": {
1193
1201
  return {
1194
1202
  ...base,
1195
1203
  type: "redirect",
1196
1204
  location: raw.target.location,
1197
1205
  status: raw.target.responseCode
1198
1206
  };
1199
- case "ContelloRouteTargetText":
1207
+ }
1208
+ case "ContelloRouteTargetText": {
1200
1209
  return {
1201
1210
  ...base,
1202
1211
  type: "text",
1203
1212
  content: raw.target.content,
1204
1213
  mimeType: raw.target.mimeType
1205
1214
  };
1206
- case "ContelloRouteTargetAsset":
1215
+ }
1216
+ case "ContelloRouteTargetAsset": {
1207
1217
  return {
1208
1218
  ...base,
1209
1219
  type: "asset",
@@ -1212,6 +1222,7 @@ function mapRoute(raw, resolver) {
1212
1222
  contentDisposition: raw.target.contentDisposition === "INLINE" ? "inline" : "attachment",
1213
1223
  mimeType: raw.target.asset.original.mimeType
1214
1224
  };
1225
+ }
1215
1226
  case "ContelloRouteTargetEntity": {
1216
1227
  if (!raw.target.entity) {
1217
1228
  return void 0;
@@ -1225,8 +1236,9 @@ function mapRoute(raw, resolver) {
1225
1236
  entityId: raw.target.entity.id
1226
1237
  };
1227
1238
  }
1228
- default:
1239
+ default: {
1229
1240
  return void 0;
1241
+ }
1230
1242
  }
1231
1243
  }
1232
1244
 
@@ -1246,11 +1258,13 @@ function collectRoutes(rawItems, keyFn, resolver) {
1246
1258
  }
1247
1259
  function createRoutesCache(max, ttl) {
1248
1260
  const pathToId = /* @__PURE__ */ new Map();
1261
+ const idToPath = /* @__PURE__ */ new Map();
1249
1262
  const lru = createLruCache({
1250
1263
  max,
1251
1264
  ttl,
1252
1265
  onEvict: (route) => {
1253
1266
  pathToId.delete(route.path);
1267
+ idToPath.delete(route.id);
1254
1268
  }
1255
1269
  });
1256
1270
  function resolveKey(key) {
@@ -1267,7 +1281,12 @@ function createRoutesCache(max, ttl) {
1267
1281
  has: (key) => lru.has(resolveKey(key)),
1268
1282
  get: (key) => lru.get(resolveKey(key)),
1269
1283
  set: (_key, value) => {
1284
+ const previousPath = idToPath.get(value.id);
1285
+ if (previousPath !== void 0 && previousPath !== value.path) {
1286
+ pathToId.delete(previousPath);
1287
+ }
1270
1288
  pathToId.set(value.path, value.id);
1289
+ idToPath.set(value.id, value.path);
1271
1290
  lru.set(ID_PREFIX + value.id, value);
1272
1291
  },
1273
1292
  delete: (key) => {
@@ -1275,11 +1294,13 @@ function createRoutesCache(max, ttl) {
1275
1294
  const value = lru.get(resolved);
1276
1295
  if (value) {
1277
1296
  pathToId.delete(value.path);
1297
+ idToPath.delete(value.id);
1278
1298
  }
1279
1299
  lru.delete(resolved);
1280
1300
  },
1281
1301
  clear: () => {
1282
1302
  pathToId.clear();
1303
+ idToPath.clear();
1283
1304
  lru.clear();
1284
1305
  },
1285
1306
  keys: () => lru.keys()
@@ -1288,7 +1309,7 @@ function createRoutesCache(max, ttl) {
1288
1309
  function createLazyRoutesCollection(def, client, updates$, resolver) {
1289
1310
  const _def = {
1290
1311
  cache: {
1291
- max: def?.cache?.max ?? 1e3,
1312
+ max: def?.cache?.max ?? DEFAULT_LRU_MAX,
1292
1313
  ttl: resolveTtl(def?.cache?.ttl)
1293
1314
  }
1294
1315
  };
@@ -1421,7 +1442,7 @@ var ModelResolver = class {
1421
1442
  return this._toTypeName.get(model) ?? model;
1422
1443
  }
1423
1444
  hasModel(model) {
1424
- return !this._toTypeName.size || this._toTypeName.has(model);
1445
+ return this._toTypeName.size === 0 || this._toTypeName.has(model);
1425
1446
  }
1426
1447
  resolveModel(typeName) {
1427
1448
  return this._toModel.get(typeName) ?? typeName;
@@ -1582,7 +1603,8 @@ function createRoutesCollection(options, client, updates$, resolver, refreshByTt
1582
1603
  if (loaded) {
1583
1604
  return void 0;
1584
1605
  }
1585
- return maybeThen4(projected.getAllAsMap(), () => void 0);
1606
+ return maybeThen4(projected.getAllAsMap(), () => {
1607
+ });
1586
1608
  }
1587
1609
  const instance = {
1588
1610
  refresh$: channel.stream$,
@@ -1592,10 +1614,10 @@ function createRoutesCollection(options, client, updates$, resolver, refreshByTt
1592
1614
  }
1593
1615
  },
1594
1616
  get(idOrIds) {
1595
- return Array.isArray(idOrIds) ? getByIds(idOrIds) : getByIdSingle(idOrIds);
1617
+ return typeof idOrIds === "string" ? getByIdSingle(idOrIds) : getByIds(idOrIds);
1596
1618
  },
1597
1619
  getByPath(pathOrPaths) {
1598
- return Array.isArray(pathOrPaths) ? lookupByPaths(pathOrPaths) : lookupByPathSingle(pathOrPaths);
1620
+ return typeof pathOrPaths === "string" ? lookupByPathSingle(pathOrPaths) : lookupByPaths(pathOrPaths);
1599
1621
  },
1600
1622
  refresh() {
1601
1623
  runFullRefresh("on-demand");
@@ -1614,7 +1636,7 @@ function createRoutesSyncCollection(options, client, updates$, resolver, refresh
1614
1636
  const { instance: base, destroy } = createRoutesCollection(options, client, updates$, resolver, refreshByTtl);
1615
1637
  function assertSync(value, method) {
1616
1638
  if (value instanceof Promise) {
1617
- throw new Error(`routes.${method}() is not initialized yet \u2014 call routes.load() first`);
1639
+ throw new TypeError(`routes.${method}() is not initialized yet \u2014 call routes.load() first`);
1618
1640
  }
1619
1641
  return value;
1620
1642
  }
@@ -1754,7 +1776,7 @@ function createSingletonSync(source, options, client, updates$, resolver, refres
1754
1776
  get() {
1755
1777
  const result = base.get();
1756
1778
  if (result instanceof Promise) {
1757
- throw new Error(`singleton "${base.name}" is not initialized yet \u2014 call singleton.load() first`);
1779
+ throw new TypeError(`singleton "${base.name}" is not initialized yet \u2014 call singleton.load() first`);
1758
1780
  }
1759
1781
  return result;
1760
1782
  }
@@ -1785,7 +1807,7 @@ function createUpdateBatch(events) {
1785
1807
  const routeByEntityModel = /* @__PURE__ */ new Map();
1786
1808
  for (const event of events) {
1787
1809
  switch (event.target) {
1788
- case "route":
1810
+ case "route": {
1789
1811
  route.push(event);
1790
1812
  if ("after" in event && event.after.type === "entity") {
1791
1813
  addRouteByModel(routeByEntityModel, event.after.model, event);
@@ -1794,12 +1816,15 @@ function createUpdateBatch(events) {
1794
1816
  addRouteByModel(routeByEntityModel, event.before.model, event);
1795
1817
  }
1796
1818
  break;
1797
- case "asset":
1819
+ }
1820
+ case "asset": {
1798
1821
  asset.push(event);
1799
1822
  break;
1800
- case "i18nMessage":
1823
+ }
1824
+ case "i18nMessage": {
1801
1825
  i18nMessage.push(event);
1802
1826
  break;
1827
+ }
1803
1828
  case "entity": {
1804
1829
  let list = entity.get(event.model);
1805
1830
  if (!list) {
@@ -1815,12 +1840,15 @@ function createUpdateBatch(events) {
1815
1840
  }
1816
1841
  function castMutationType(type) {
1817
1842
  switch (type) {
1818
- case "CREATE":
1843
+ case "CREATE": {
1819
1844
  return "create";
1820
- case "UPDATE":
1845
+ }
1846
+ case "UPDATE": {
1821
1847
  return "update";
1822
- case "DELETE":
1848
+ }
1849
+ case "DELETE": {
1823
1850
  return "delete";
1851
+ }
1824
1852
  }
1825
1853
  }
1826
1854
  function mapEvent(raw, resolver) {
@@ -1845,10 +1873,12 @@ function mapEvent(raw, resolver) {
1845
1873
  }
1846
1874
  return { id, mutation, target: "route", after, before };
1847
1875
  }
1848
- case "ContelloAsset":
1876
+ case "ContelloAsset": {
1849
1877
  return { id, mutation, target: "asset" };
1850
- case "ContelloI18nMessage":
1878
+ }
1879
+ case "ContelloI18nMessage": {
1851
1880
  return { id, mutation, target: "i18nMessage", token: target.token };
1881
+ }
1852
1882
  default: {
1853
1883
  if (!__typename || !resolver.hasTypeName(__typename)) {
1854
1884
  return void 0;
@@ -1857,6 +1887,13 @@ function mapEvent(raw, resolver) {
1857
1887
  }
1858
1888
  }
1859
1889
  }
1890
+ function safeMapEvent(raw, resolver) {
1891
+ try {
1892
+ return mapEvent(raw, resolver);
1893
+ } catch {
1894
+ return void 0;
1895
+ }
1896
+ }
1860
1897
  function createInternalWatcher(client, resolver) {
1861
1898
  const updates$ = createAsyncIterableSubject3();
1862
1899
  let controller;
@@ -1876,7 +1913,7 @@ function createInternalWatcher(client, resolver) {
1876
1913
  );
1877
1914
  const events = mapAsync9(
1878
1915
  source,
1879
- (data) => (data.contelloUpdatesBatch?.events ?? []).map((e) => mapEvent(e, resolver)).filter((e) => e !== void 0)
1916
+ (data) => (data.contelloUpdatesBatch?.events ?? []).map((e) => safeMapEvent(e, resolver)).filter((e) => e !== void 0)
1880
1917
  );
1881
1918
  for await (const list of events) {
1882
1919
  if (signal.aborted) {
@@ -1885,9 +1922,13 @@ function createInternalWatcher(client, resolver) {
1885
1922
  if (list.length === 0) {
1886
1923
  continue;
1887
1924
  }
1888
- updates$.next(createUpdateBatch(list));
1925
+ try {
1926
+ updates$.next(createUpdateBatch(list));
1927
+ } catch {
1928
+ }
1889
1929
  }
1890
- })();
1930
+ })().catch(() => {
1931
+ });
1891
1932
  });
1892
1933
  },
1893
1934
  stop() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contello/store",
3
- "version": "2.1.0",
3
+ "version": "3.0.0",
4
4
  "description": "Framework-agnostic store for Contello CMS with reactive collections and automatic dependency tracking",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -11,16 +11,16 @@
11
11
  "directory": "packages/store"
12
12
  },
13
13
  "peerDependencies": {
14
- "@contello/client": "^2.2.0",
15
- "@contello/media": "^1.0.1"
14
+ "@contello/client": "^2.2.2",
15
+ "@contello/media": "^2.0.0"
16
16
  },
17
17
  "dependencies": {
18
18
  "lru-cache": "^11.3.5",
19
- "projected": "^3.0.1"
19
+ "projected": "^3.0.2"
20
20
  },
21
21
  "devDependencies": {
22
- "@contello/media": "^1.0.1",
23
- "@contello/client": "^2.2.0"
22
+ "@contello/client": "^2.2.2",
23
+ "@contello/media": "^2.0.0"
24
24
  },
25
25
  "files": [
26
26
  "dist",
@@ -44,7 +44,7 @@
44
44
  "scripts": {
45
45
  "build": "tsup",
46
46
  "generate": "node --env-file=../../.env ../client/dist/cli.js generate",
47
- "lint": "eslint .",
47
+ "lint": "eslint . --cache",
48
48
  "lint:fix": "eslint . --fix",
49
49
  "typecheck": "tsc --noEmit"
50
50
  }