@contello/store 2.0.1 → 2.1.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/index.cjs +880 -325
- package/dist/index.d.cts +135 -35
- package/dist/index.d.ts +135 -35
- package/dist/index.js +908 -337
- package/package.json +7 -7
package/dist/index.cjs
CHANGED
|
@@ -32,13 +32,13 @@ var index_exports = {};
|
|
|
32
32
|
__export(index_exports, {
|
|
33
33
|
Store: () => Store,
|
|
34
34
|
createStore: () => createStore,
|
|
35
|
-
maybeCatch: () =>
|
|
36
|
-
maybeThen: () =>
|
|
35
|
+
maybeCatch: () => import_projected9.maybeCatch,
|
|
36
|
+
maybeThen: () => import_projected9.maybeThen
|
|
37
37
|
});
|
|
38
38
|
module.exports = __toCommonJS(index_exports);
|
|
39
39
|
|
|
40
40
|
// src/store.ts
|
|
41
|
-
var
|
|
41
|
+
var import_client11 = require("@contello/client");
|
|
42
42
|
|
|
43
43
|
// src/assets.ts
|
|
44
44
|
var import_client2 = require("@contello/client");
|
|
@@ -170,20 +170,6 @@ var StoreRouteFragmentSchema = `fragment StoreRoute on ContelloRoute {
|
|
|
170
170
|
value
|
|
171
171
|
}
|
|
172
172
|
}`;
|
|
173
|
-
var storeGetAssetsDocument = `${MediaFileFragmentSchema}
|
|
174
|
-
${MediaAssetFragmentSchema}
|
|
175
|
-
${StoreAssetFragmentSchema}
|
|
176
|
-
query StoreGetAssets($filter: ContelloAssetsFilterInput!) {
|
|
177
|
-
contelloAssets(filter: $filter) {
|
|
178
|
-
...StoreAsset
|
|
179
|
-
}
|
|
180
|
-
}`;
|
|
181
|
-
var storeGetI18nMessagesDocument = `${StoreI18nMessageFragmentSchema}
|
|
182
|
-
subscription StoreGetI18nMessages($collection: String!) {
|
|
183
|
-
contelloI18nMessagesBatch(collectionReferenceName: $collection) {
|
|
184
|
-
...StoreI18nMessage
|
|
185
|
-
}
|
|
186
|
-
}`;
|
|
187
173
|
var storeRegisterI18nMessagesDocument = `mutation StoreRegisterI18nMessages($collection: String!, $messages: [ContelloI18nMessageInput!]!) {
|
|
188
174
|
registerContelloI18nMessages(
|
|
189
175
|
collectionReferenceName: $collection
|
|
@@ -193,12 +179,6 @@ var storeRegisterI18nMessagesDocument = `mutation StoreRegisterI18nMessages($col
|
|
|
193
179
|
ids
|
|
194
180
|
}
|
|
195
181
|
}`;
|
|
196
|
-
var storeGetRoutesDocument = `${StoreRouteFragmentSchema}
|
|
197
|
-
query StoreGetRoutes($request: ContelloRoutesRequestInput!) {
|
|
198
|
-
contelloRoutes(request: $request) {
|
|
199
|
-
...StoreRoute
|
|
200
|
-
}
|
|
201
|
-
}`;
|
|
202
182
|
var storeWatchUpdatesDocument = `${StoreRouteFragmentSchema}
|
|
203
183
|
subscription StoreWatchUpdates {
|
|
204
184
|
contelloUpdatesBatch {
|
|
@@ -231,6 +211,10 @@ subscription StoreWatchUpdates {
|
|
|
231
211
|
}
|
|
232
212
|
}
|
|
233
213
|
}`;
|
|
214
|
+
var operations = {
|
|
215
|
+
storeRegisterI18nMessages: { document: storeRegisterI18nMessagesDocument, kind: "mutation" },
|
|
216
|
+
storeWatchUpdates: { document: storeWatchUpdatesDocument, kind: "subscription" }
|
|
217
|
+
};
|
|
234
218
|
var sources = {
|
|
235
219
|
storeAsset: {
|
|
236
220
|
document: `${MediaFileFragmentSchema}
|
|
@@ -256,55 +240,101 @@ ${StoreAssetFragmentSchema}`,
|
|
|
256
240
|
__cardinality: "route"
|
|
257
241
|
}
|
|
258
242
|
};
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
const { max, ttl, onEvict: dispose } = options;
|
|
264
|
-
const lru = new import_lru_cache.LRUCache({
|
|
265
|
-
max,
|
|
266
|
-
...ttl !== void 0 && { ttl, ttlAutopurge: false },
|
|
267
|
-
...dispose !== void 0 && { dispose }
|
|
268
|
-
});
|
|
269
|
-
return {
|
|
270
|
-
has: (key) => lru.has(key),
|
|
271
|
-
get: (key) => lru.get(key),
|
|
272
|
-
set: (key, value) => {
|
|
273
|
-
lru.set(key, value);
|
|
274
|
-
},
|
|
275
|
-
delete: (key) => {
|
|
276
|
-
lru.delete(key);
|
|
277
|
-
},
|
|
278
|
-
clear: () => {
|
|
279
|
-
lru.clear();
|
|
280
|
-
},
|
|
281
|
-
keys: () => [...lru.keys()]
|
|
282
|
-
};
|
|
283
|
-
}
|
|
243
|
+
var models = {
|
|
244
|
+
dummy: "DummyEntity"
|
|
245
|
+
};
|
|
246
|
+
var schema = { operations, sources, models };
|
|
284
247
|
|
|
285
248
|
// src/utils.ts
|
|
286
249
|
var import_client = require("@contello/client");
|
|
250
|
+
var DEFAULT_TTL_MS = 3 * 60 * 60 * 1e3;
|
|
251
|
+
var DEFAULT_LRU_MAX = 1e3;
|
|
252
|
+
function resolveTtl(ttl) {
|
|
253
|
+
if (ttl === void 0) return DEFAULT_TTL_MS;
|
|
254
|
+
if (ttl === false || ttl <= 0) return void 0;
|
|
255
|
+
return ttl;
|
|
256
|
+
}
|
|
287
257
|
function createRefresher(fn, onRefreshed, onStart2) {
|
|
288
258
|
let refreshing = false;
|
|
289
|
-
let
|
|
290
|
-
|
|
259
|
+
let inFlightKind;
|
|
260
|
+
let pendingKind;
|
|
261
|
+
function scheduleRefresh(kind) {
|
|
291
262
|
if (refreshing) {
|
|
292
|
-
|
|
263
|
+
pendingKind = { kind };
|
|
293
264
|
return;
|
|
294
265
|
}
|
|
295
266
|
refreshing = true;
|
|
267
|
+
inFlightKind = kind;
|
|
296
268
|
onStart2?.();
|
|
297
269
|
void (0, import_client.runWithBackoff)(fn).then(() => {
|
|
270
|
+
const finished = inFlightKind;
|
|
298
271
|
refreshing = false;
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
272
|
+
inFlightKind = void 0;
|
|
273
|
+
onRefreshed(finished);
|
|
274
|
+
if (pendingKind) {
|
|
275
|
+
const { kind: next } = pendingKind;
|
|
276
|
+
pendingKind = void 0;
|
|
277
|
+
scheduleRefresh(next);
|
|
303
278
|
}
|
|
304
279
|
});
|
|
305
280
|
}
|
|
306
281
|
return scheduleRefresh;
|
|
307
282
|
}
|
|
283
|
+
function createRefreshChannel(onRefresh) {
|
|
284
|
+
const stream$ = (0, import_client.createAsyncIterableSubject)();
|
|
285
|
+
return {
|
|
286
|
+
stream$,
|
|
287
|
+
emit(event) {
|
|
288
|
+
stream$.next(event);
|
|
289
|
+
onRefresh?.(event);
|
|
290
|
+
},
|
|
291
|
+
complete() {
|
|
292
|
+
stream$.complete();
|
|
293
|
+
}
|
|
294
|
+
};
|
|
295
|
+
}
|
|
296
|
+
function createTtlOrchestrator(options) {
|
|
297
|
+
let timer;
|
|
298
|
+
return {
|
|
299
|
+
mark() {
|
|
300
|
+
if (timer !== void 0) {
|
|
301
|
+
clearTimeout(timer);
|
|
302
|
+
timer = void 0;
|
|
303
|
+
}
|
|
304
|
+
if (options.ttl === void 0) {
|
|
305
|
+
return;
|
|
306
|
+
}
|
|
307
|
+
timer = setTimeout(options.run, options.ttl);
|
|
308
|
+
},
|
|
309
|
+
clear() {
|
|
310
|
+
if (timer !== void 0) {
|
|
311
|
+
clearTimeout(timer);
|
|
312
|
+
timer = void 0;
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
};
|
|
316
|
+
}
|
|
317
|
+
function createRefreshByTtlQueue() {
|
|
318
|
+
const waiting = [];
|
|
319
|
+
let running = false;
|
|
320
|
+
function next() {
|
|
321
|
+
if (running) return;
|
|
322
|
+
const task = waiting.shift();
|
|
323
|
+
if (!task) return;
|
|
324
|
+
running = true;
|
|
325
|
+
Promise.resolve().then(task).catch(() => {
|
|
326
|
+
}).finally(() => {
|
|
327
|
+
running = false;
|
|
328
|
+
next();
|
|
329
|
+
});
|
|
330
|
+
}
|
|
331
|
+
return {
|
|
332
|
+
enqueue(task) {
|
|
333
|
+
waiting.push(task);
|
|
334
|
+
next();
|
|
335
|
+
}
|
|
336
|
+
};
|
|
337
|
+
}
|
|
308
338
|
|
|
309
339
|
// src/assets.ts
|
|
310
340
|
function mapFile(raw) {
|
|
@@ -319,83 +349,131 @@ function mapAsset(raw) {
|
|
|
319
349
|
id: raw.id,
|
|
320
350
|
original: mapFile(raw.original),
|
|
321
351
|
preview: raw.preview ? mapFile(raw.preview) : void 0,
|
|
322
|
-
optimized: raw.optimized.map(mapFile)
|
|
352
|
+
optimized: raw.optimized.map((file) => mapFile(file))
|
|
323
353
|
};
|
|
324
354
|
}
|
|
325
|
-
function createAssetsCollection(
|
|
326
|
-
const
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
}
|
|
355
|
+
function createAssetsCollection(options, client, updates$, refreshByTtl) {
|
|
356
|
+
const opts = options ?? {};
|
|
357
|
+
const cache = {
|
|
358
|
+
ttl: resolveTtl(opts.cache?.ttl),
|
|
359
|
+
eviction: opts.cache?.eviction ?? "refresh"
|
|
331
360
|
};
|
|
332
|
-
const
|
|
333
|
-
const
|
|
361
|
+
const channel = createRefreshChannel(opts.onRefresh);
|
|
362
|
+
const ttl = createTtlOrchestrator({ ttl: cache.ttl, run: () => runFullRefresh("ttl") });
|
|
363
|
+
let loaded = false;
|
|
364
|
+
const assetsSourceDoc = (0, import_client2.createSourceSubscription)(schema.sources.storeAsset);
|
|
365
|
+
const projected = new import_projected.ProjectedMap({
|
|
334
366
|
key: (asset) => asset.id,
|
|
335
367
|
values: (ids) => wrap(
|
|
336
368
|
"assets",
|
|
337
|
-
() =>
|
|
338
|
-
(
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
369
|
+
() => (0, import_client2.collectAsync)(
|
|
370
|
+
(0, import_client2.mapAsync)(client.subscribe(assetsSourceDoc, { ids }), (data) => data.source)
|
|
371
|
+
).then((rawItems) => {
|
|
372
|
+
const items = rawItems.map((item) => mapAsset(item));
|
|
373
|
+
if (ids === void 0 && !loaded) {
|
|
374
|
+
loaded = true;
|
|
375
|
+
ttl.mark();
|
|
376
|
+
opts.onLoad?.(items.map((a) => a.id));
|
|
377
|
+
}
|
|
378
|
+
return items;
|
|
379
|
+
})
|
|
380
|
+
)
|
|
381
|
+
});
|
|
382
|
+
function emit(ids, kind) {
|
|
383
|
+
channel.emit({ ids, kind });
|
|
384
|
+
}
|
|
385
|
+
function runFullRefresh(kind) {
|
|
386
|
+
refreshByTtl.enqueue(
|
|
387
|
+
() => (0, import_client2.runWithBackoff)(
|
|
388
|
+
() => projected.refresh().then((map) => {
|
|
389
|
+
emit([...map.keys()], kind);
|
|
390
|
+
ttl.mark();
|
|
391
|
+
})
|
|
344
392
|
)
|
|
345
|
-
)
|
|
346
|
-
|
|
393
|
+
);
|
|
394
|
+
}
|
|
395
|
+
function runPartialRefresh(refreshIds, deletedIds) {
|
|
396
|
+
const changedIds = [.../* @__PURE__ */ new Set([...refreshIds, ...deletedIds])];
|
|
397
|
+
if (refreshIds.length === 0) {
|
|
398
|
+
emit(changedIds, "upstream-update");
|
|
399
|
+
return;
|
|
400
|
+
}
|
|
401
|
+
void (0, import_client2.runWithBackoff)(
|
|
402
|
+
() => projected.refresh(refreshIds).then(() => {
|
|
403
|
+
emit(changedIds, "upstream-update");
|
|
404
|
+
})
|
|
405
|
+
);
|
|
406
|
+
}
|
|
407
|
+
const unsubUpdates = updates$.subscribe((batch) => {
|
|
408
|
+
if (!loaded || batch.asset.length === 0) {
|
|
409
|
+
return;
|
|
410
|
+
}
|
|
411
|
+
const deleted = [];
|
|
412
|
+
const upserted = /* @__PURE__ */ new Set();
|
|
413
|
+
for (const event of batch.asset) {
|
|
414
|
+
if (event.mutation === "delete") {
|
|
415
|
+
deleted.push(event.id);
|
|
416
|
+
} else {
|
|
417
|
+
upserted.add(event.id);
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
if (cache.eviction === "clear") {
|
|
421
|
+
projected.clear();
|
|
422
|
+
return;
|
|
423
|
+
}
|
|
424
|
+
if (deleted.length > 0) {
|
|
425
|
+
projected.delete(deleted);
|
|
426
|
+
}
|
|
427
|
+
runPartialRefresh([...upserted], deleted);
|
|
347
428
|
});
|
|
348
|
-
const
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
if (lastRefreshKeys.length === 0) {
|
|
354
|
-
return;
|
|
429
|
+
const instance = {
|
|
430
|
+
refresh$: channel.stream$,
|
|
431
|
+
async load() {
|
|
432
|
+
if (!loaded) {
|
|
433
|
+
await projected.getAllAsMap();
|
|
355
434
|
}
|
|
356
|
-
await projected.refresh(lastRefreshKeys);
|
|
357
435
|
},
|
|
358
|
-
()
|
|
359
|
-
|
|
360
|
-
refresh$.next(lastRefreshKeys);
|
|
361
|
-
}
|
|
436
|
+
get(idOrIds) {
|
|
437
|
+
return projected.get(idOrIds);
|
|
362
438
|
},
|
|
363
|
-
()
|
|
439
|
+
upload(data, meta, options2) {
|
|
440
|
+
return client.upload(data, meta, options2);
|
|
441
|
+
},
|
|
442
|
+
download(fileId) {
|
|
443
|
+
return client.download(fileId);
|
|
444
|
+
},
|
|
445
|
+
proxyHls(path, signal) {
|
|
446
|
+
return client.proxyHls(path, signal);
|
|
447
|
+
},
|
|
448
|
+
refresh() {
|
|
449
|
+
runFullRefresh("on-demand");
|
|
364
450
|
}
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
451
|
+
};
|
|
452
|
+
return {
|
|
453
|
+
instance,
|
|
454
|
+
destroy() {
|
|
455
|
+
unsubUpdates();
|
|
456
|
+
ttl.clear();
|
|
457
|
+
channel.complete();
|
|
370
458
|
}
|
|
371
|
-
|
|
372
|
-
|
|
459
|
+
};
|
|
460
|
+
}
|
|
461
|
+
function createAssetsSyncCollection(options, client, updates$, refreshByTtl) {
|
|
462
|
+
const { instance: base, destroy } = createAssetsCollection(options, client, updates$, refreshByTtl);
|
|
463
|
+
function assertSync(value, method) {
|
|
464
|
+
if (value instanceof Promise) {
|
|
465
|
+
throw new TypeError(`assets.${method}() is not initialized yet \u2014 call assets.load() first`);
|
|
466
|
+
}
|
|
467
|
+
return value;
|
|
468
|
+
}
|
|
373
469
|
return {
|
|
374
470
|
instance: {
|
|
375
|
-
|
|
471
|
+
...base,
|
|
376
472
|
get(idOrIds) {
|
|
377
|
-
return
|
|
378
|
-
},
|
|
379
|
-
upload(data, meta, options) {
|
|
380
|
-
return client.upload(data, meta, options);
|
|
381
|
-
},
|
|
382
|
-
download(fileId) {
|
|
383
|
-
return client.download(fileId);
|
|
384
|
-
},
|
|
385
|
-
proxyHls(path, signal) {
|
|
386
|
-
return client.proxyHls(path, signal);
|
|
387
|
-
},
|
|
388
|
-
refresh() {
|
|
389
|
-
scheduleRefresh();
|
|
390
|
-
},
|
|
391
|
-
clear() {
|
|
392
|
-
projected.clear();
|
|
473
|
+
return assertSync(base.get(idOrIds), "get");
|
|
393
474
|
}
|
|
394
475
|
},
|
|
395
|
-
destroy
|
|
396
|
-
unsubUpdates();
|
|
397
|
-
refresh$.complete();
|
|
398
|
-
}
|
|
476
|
+
destroy
|
|
399
477
|
};
|
|
400
478
|
}
|
|
401
479
|
|
|
@@ -436,12 +514,14 @@ var DependencyCollector = class {
|
|
|
436
514
|
throw new Error(`track(): object with __typename "${obj.__typename}" has no "id" field`);
|
|
437
515
|
}
|
|
438
516
|
switch (obj.__typename) {
|
|
439
|
-
case "ContelloAsset":
|
|
517
|
+
case "ContelloAsset": {
|
|
440
518
|
deps.push(createDependencyKey("asset", obj.id));
|
|
441
519
|
break;
|
|
442
|
-
|
|
520
|
+
}
|
|
521
|
+
case "ContelloRoute": {
|
|
443
522
|
deps.push(createDependencyKey("route", obj.id));
|
|
444
523
|
break;
|
|
524
|
+
}
|
|
445
525
|
default: {
|
|
446
526
|
const model = this.resolver.getModel(obj.__typename);
|
|
447
527
|
if (!model) {
|
|
@@ -564,19 +644,21 @@ function fetchCollection(source, client, ids) {
|
|
|
564
644
|
)
|
|
565
645
|
);
|
|
566
646
|
}
|
|
567
|
-
function createCollection(source, options, client, updates$, resolver) {
|
|
647
|
+
function createCollection(source, options, client, updates$, resolver, refreshByTtl) {
|
|
568
648
|
const opts = options ?? {};
|
|
569
649
|
const mapFn = opts.map ?? ((item) => item);
|
|
570
650
|
const _def = {
|
|
571
651
|
name: opts.name ?? source.__model,
|
|
572
652
|
model: source.__model,
|
|
573
653
|
cache: {
|
|
574
|
-
ttl: opts.cache?.ttl,
|
|
654
|
+
ttl: resolveTtl(opts.cache?.ttl),
|
|
575
655
|
eviction: opts.cache?.eviction ?? "refresh"
|
|
576
656
|
}
|
|
577
657
|
};
|
|
578
658
|
const dependencyCollector = new DependencyCollector(_def.model, resolver);
|
|
579
|
-
|
|
659
|
+
const channel = createRefreshChannel(opts.onRefresh);
|
|
660
|
+
const ttl = createTtlOrchestrator({ ttl: _def.cache.ttl, run: () => runFullRefresh("ttl") });
|
|
661
|
+
let loaded = false;
|
|
580
662
|
const projected = new import_projected2.ProjectedMap({
|
|
581
663
|
key: (item) => item.id,
|
|
582
664
|
values: (ids) => wrap(
|
|
@@ -585,16 +667,23 @@ function createCollection(source, options, client, updates$, resolver) {
|
|
|
585
667
|
fetchCollection(source, client, ids),
|
|
586
668
|
(rawItems) => Promise.all(
|
|
587
669
|
rawItems.map(
|
|
588
|
-
(item) =>
|
|
589
|
-
|
|
590
|
-
register(
|
|
591
|
-
|
|
592
|
-
|
|
670
|
+
(item) => Promise.resolve(
|
|
671
|
+
dependencyCollector.createContext(
|
|
672
|
+
(ref, register) => (0, import_projected2.maybeThen)(mapFn(item, ref), (mapped) => {
|
|
673
|
+
register(mapped.id);
|
|
674
|
+
return mapped;
|
|
675
|
+
})
|
|
676
|
+
)
|
|
593
677
|
)
|
|
594
678
|
)
|
|
595
679
|
).then((items) => {
|
|
596
680
|
if (ids === void 0) {
|
|
597
681
|
dependencyCollector.retainOnly(new Set(items.map((item) => item.id)));
|
|
682
|
+
if (!loaded) {
|
|
683
|
+
loaded = true;
|
|
684
|
+
ttl.mark();
|
|
685
|
+
opts.onLoad?.(items.map((item) => item.id));
|
|
686
|
+
}
|
|
598
687
|
} else {
|
|
599
688
|
const returnedIds = new Set(items.map((item) => item.id));
|
|
600
689
|
for (const id of ids) {
|
|
@@ -609,38 +698,28 @@ function createCollection(source, options, client, updates$, resolver) {
|
|
|
609
698
|
),
|
|
610
699
|
sort: opts.sort
|
|
611
700
|
});
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
function scheduleTtl() {
|
|
615
|
-
if (_def.cache.ttl === void 0) {
|
|
616
|
-
return;
|
|
617
|
-
}
|
|
618
|
-
clearTimeout(ttlTimer);
|
|
619
|
-
ttlTimer = setTimeout(() => {
|
|
620
|
-
runFullRefresh();
|
|
621
|
-
}, _def.cache.ttl);
|
|
701
|
+
function emit(ids, kind) {
|
|
702
|
+
channel.emit({ ids, kind });
|
|
622
703
|
}
|
|
623
|
-
function runFullRefresh() {
|
|
624
|
-
|
|
625
|
-
() =>
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
704
|
+
function runFullRefresh(kind) {
|
|
705
|
+
refreshByTtl.enqueue(
|
|
706
|
+
() => (0, import_client3.runWithBackoff)(
|
|
707
|
+
() => projected.refresh().then((map) => {
|
|
708
|
+
emit([...map.keys()], kind);
|
|
709
|
+
ttl.mark();
|
|
710
|
+
})
|
|
711
|
+
)
|
|
631
712
|
);
|
|
632
713
|
}
|
|
633
714
|
function runPartialRefresh(refreshIds, deletedIds) {
|
|
634
715
|
const changedIds = [.../* @__PURE__ */ new Set([...refreshIds, ...deletedIds])];
|
|
635
716
|
if (refreshIds.length === 0) {
|
|
636
|
-
|
|
637
|
-
opts.onRefresh?.(changedIds);
|
|
717
|
+
emit(changedIds, "upstream-update");
|
|
638
718
|
return;
|
|
639
719
|
}
|
|
640
720
|
void (0, import_client3.runWithBackoff)(
|
|
641
721
|
() => projected.refresh(refreshIds).then(() => {
|
|
642
|
-
|
|
643
|
-
opts.onRefresh?.(changedIds);
|
|
722
|
+
emit(changedIds, "upstream-update");
|
|
644
723
|
})
|
|
645
724
|
);
|
|
646
725
|
}
|
|
@@ -686,7 +765,7 @@ function createCollection(source, options, client, updates$, resolver) {
|
|
|
686
765
|
});
|
|
687
766
|
const instance = {
|
|
688
767
|
name: _def.name,
|
|
689
|
-
refresh$,
|
|
768
|
+
refresh$: channel.stream$,
|
|
690
769
|
get(idOrIds) {
|
|
691
770
|
return projected.get(idOrIds);
|
|
692
771
|
},
|
|
@@ -694,35 +773,35 @@ function createCollection(source, options, client, updates$, resolver) {
|
|
|
694
773
|
return projected.getAll();
|
|
695
774
|
},
|
|
696
775
|
refresh() {
|
|
697
|
-
runFullRefresh();
|
|
776
|
+
runFullRefresh("on-demand");
|
|
698
777
|
},
|
|
699
778
|
async load() {
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
opts.onLoad?.([...map.keys()]);
|
|
779
|
+
if (!loaded) {
|
|
780
|
+
await projected.getAllAsMap();
|
|
781
|
+
}
|
|
704
782
|
}
|
|
705
783
|
};
|
|
706
784
|
return {
|
|
707
785
|
instance,
|
|
708
786
|
destroy() {
|
|
709
787
|
unsubUpdates();
|
|
710
|
-
|
|
711
|
-
|
|
788
|
+
ttl.clear();
|
|
789
|
+
channel.complete();
|
|
712
790
|
}
|
|
713
791
|
};
|
|
714
792
|
}
|
|
715
|
-
function createCollectionSync(source, options, client, updates$, resolver) {
|
|
793
|
+
function createCollectionSync(source, options, client, updates$, resolver, refreshByTtl) {
|
|
716
794
|
const { instance: base, destroy } = createCollection(
|
|
717
795
|
source,
|
|
718
796
|
options,
|
|
719
797
|
client,
|
|
720
798
|
updates$,
|
|
721
|
-
resolver
|
|
799
|
+
resolver,
|
|
800
|
+
refreshByTtl
|
|
722
801
|
);
|
|
723
802
|
function assertSync(value, method) {
|
|
724
803
|
if (value instanceof Promise) {
|
|
725
|
-
throw new
|
|
804
|
+
throw new TypeError(`collection "${base.name}".${method}() is not initialized yet \u2014 call collection.load() first`);
|
|
726
805
|
}
|
|
727
806
|
return value;
|
|
728
807
|
}
|
|
@@ -753,45 +832,103 @@ function toGqlMessageInput(msg) {
|
|
|
753
832
|
initialTranslations: msg.initialTranslations?.map((t) => ({ language: t.language, message: t.message }))
|
|
754
833
|
};
|
|
755
834
|
}
|
|
756
|
-
function createI18nMessagesCollection(def, client, updates
|
|
835
|
+
function createI18nMessagesCollection(def, client, updates$, refreshByTtl) {
|
|
836
|
+
const channel = createRefreshChannel(def.onRefresh);
|
|
837
|
+
const ttl = createTtlOrchestrator({ ttl: resolveTtl(def.cache?.ttl), run: () => runTtlRefresh() });
|
|
838
|
+
let loaded = false;
|
|
839
|
+
const i18nSourceDoc = (0, import_client4.createSourceSubscription)(schema.sources.storeI18nMessage);
|
|
757
840
|
const projected = new import_projected3.ProjectedMap({
|
|
758
841
|
key: (msg) => msg.id,
|
|
759
|
-
values: () => wrap(
|
|
842
|
+
values: (ids) => wrap(
|
|
760
843
|
`i18n:${def.collection}`,
|
|
761
844
|
() => (0, import_client4.collectAsync)(
|
|
762
845
|
(0, import_client4.mapAsync)(
|
|
763
|
-
client.subscribe(
|
|
764
|
-
collection: def.collection
|
|
846
|
+
client.subscribe(i18nSourceDoc, {
|
|
847
|
+
collection: def.collection,
|
|
848
|
+
ids
|
|
765
849
|
}),
|
|
766
|
-
(data) => data.
|
|
850
|
+
(data) => data.source
|
|
767
851
|
)
|
|
768
|
-
).then(
|
|
769
|
-
|
|
852
|
+
).then((msgs) => {
|
|
853
|
+
const items = msgs.map((msg) => ({
|
|
770
854
|
id: msg.id,
|
|
771
855
|
token: msg.token,
|
|
772
856
|
translations: new Map(msg.translations.map((t) => [t.language, t.value]))
|
|
773
|
-
}))
|
|
774
|
-
|
|
857
|
+
}));
|
|
858
|
+
if (ids === void 0 && !loaded) {
|
|
859
|
+
loaded = true;
|
|
860
|
+
ttl.mark();
|
|
861
|
+
}
|
|
862
|
+
return items;
|
|
863
|
+
})
|
|
775
864
|
)
|
|
776
865
|
});
|
|
777
|
-
|
|
866
|
+
function emit(ids, kind) {
|
|
867
|
+
channel.emit({ ids, kind });
|
|
868
|
+
}
|
|
869
|
+
function emitWithCurrentIds(kind) {
|
|
870
|
+
(0, import_projected3.maybeThen)(projected.getAll(), (msgs) => {
|
|
871
|
+
emit(
|
|
872
|
+
msgs.map((m) => m.id),
|
|
873
|
+
kind
|
|
874
|
+
);
|
|
875
|
+
});
|
|
876
|
+
}
|
|
877
|
+
function runTtlRefresh() {
|
|
878
|
+
refreshByTtl.enqueue(
|
|
879
|
+
() => (0, import_client4.runWithBackoff)(() => projected.refresh()).then(() => {
|
|
880
|
+
emitWithCurrentIds("ttl");
|
|
881
|
+
ttl.mark();
|
|
882
|
+
})
|
|
883
|
+
);
|
|
884
|
+
}
|
|
778
885
|
const scheduleRefresh = createRefresher(
|
|
779
886
|
() => projected.refresh(),
|
|
780
|
-
() => {
|
|
781
|
-
(
|
|
782
|
-
|
|
783
|
-
});
|
|
887
|
+
(kind) => {
|
|
888
|
+
emitWithCurrentIds(kind);
|
|
889
|
+
ttl.mark();
|
|
784
890
|
},
|
|
785
891
|
() => {
|
|
786
892
|
}
|
|
787
893
|
);
|
|
894
|
+
function runPartialRefresh(upsertedIds, deletedIds) {
|
|
895
|
+
const changedIds = [.../* @__PURE__ */ new Set([...upsertedIds, ...deletedIds])];
|
|
896
|
+
if (changedIds.length === 0) {
|
|
897
|
+
return;
|
|
898
|
+
}
|
|
899
|
+
if (upsertedIds.length === 0) {
|
|
900
|
+
emit(changedIds, "upstream-update");
|
|
901
|
+
return;
|
|
902
|
+
}
|
|
903
|
+
void (0, import_client4.runWithBackoff)(
|
|
904
|
+
() => projected.refresh(upsertedIds).then(() => {
|
|
905
|
+
emit(changedIds, "upstream-update");
|
|
906
|
+
})
|
|
907
|
+
);
|
|
908
|
+
}
|
|
788
909
|
const unsubUpdates = updates$.subscribe((batch) => {
|
|
789
|
-
if (
|
|
790
|
-
|
|
910
|
+
if (!loaded) {
|
|
911
|
+
return;
|
|
912
|
+
}
|
|
913
|
+
if (batch.i18nMessage.length === 0) {
|
|
914
|
+
return;
|
|
915
|
+
}
|
|
916
|
+
const deleted = [];
|
|
917
|
+
const upserted = /* @__PURE__ */ new Set();
|
|
918
|
+
for (const event of batch.i18nMessage) {
|
|
919
|
+
if (event.mutation === "delete") {
|
|
920
|
+
deleted.push(event.id);
|
|
921
|
+
} else {
|
|
922
|
+
upserted.add(event.id);
|
|
923
|
+
}
|
|
791
924
|
}
|
|
925
|
+
if (deleted.length > 0) {
|
|
926
|
+
projected.delete(deleted);
|
|
927
|
+
}
|
|
928
|
+
runPartialRefresh([...upserted], deleted);
|
|
792
929
|
});
|
|
793
930
|
return {
|
|
794
|
-
refresh$,
|
|
931
|
+
refresh$: channel.stream$,
|
|
795
932
|
get(idOrIds) {
|
|
796
933
|
return projected.get(idOrIds);
|
|
797
934
|
},
|
|
@@ -803,30 +940,158 @@ function createI18nMessagesCollection(def, client, updates$) {
|
|
|
803
940
|
`i18n-register:${def.collection}`,
|
|
804
941
|
() => client.execute(storeRegisterI18nMessagesDocument, {
|
|
805
942
|
collection: def.collection,
|
|
806
|
-
messages: messages.map(toGqlMessageInput)
|
|
943
|
+
messages: messages.map((message) => toGqlMessageInput(message))
|
|
807
944
|
}).then(() => {
|
|
808
945
|
})
|
|
809
946
|
);
|
|
810
947
|
},
|
|
948
|
+
refresh() {
|
|
949
|
+
scheduleRefresh("on-demand");
|
|
950
|
+
},
|
|
811
951
|
destroy() {
|
|
812
952
|
unsubUpdates();
|
|
813
|
-
|
|
953
|
+
ttl.clear();
|
|
954
|
+
channel.complete();
|
|
814
955
|
}
|
|
815
956
|
};
|
|
816
957
|
}
|
|
817
958
|
|
|
818
|
-
// src/lazy-
|
|
959
|
+
// src/lazy-assets.ts
|
|
819
960
|
var import_client5 = require("@contello/client");
|
|
820
961
|
var import_projected4 = require("projected");
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
962
|
+
|
|
963
|
+
// src/lru.ts
|
|
964
|
+
var import_lru_cache = require("lru-cache");
|
|
965
|
+
function createLruCache(options) {
|
|
966
|
+
const { max, ttl, onEvict: dispose } = options;
|
|
967
|
+
const lru = new import_lru_cache.LRUCache({
|
|
968
|
+
max,
|
|
969
|
+
...ttl !== void 0 && { ttl, ttlAutopurge: false },
|
|
970
|
+
...dispose !== void 0 && { dispose }
|
|
971
|
+
});
|
|
972
|
+
return {
|
|
973
|
+
has: (key) => lru.has(key),
|
|
974
|
+
get: (key) => lru.get(key),
|
|
975
|
+
set: (key, value) => {
|
|
976
|
+
lru.set(key, value);
|
|
977
|
+
},
|
|
978
|
+
delete: (key) => {
|
|
979
|
+
lru.delete(key);
|
|
980
|
+
},
|
|
981
|
+
clear: () => {
|
|
982
|
+
lru.clear();
|
|
983
|
+
},
|
|
984
|
+
keys: () => [...lru.keys()]
|
|
985
|
+
};
|
|
986
|
+
}
|
|
987
|
+
|
|
988
|
+
// src/lazy-assets.ts
|
|
989
|
+
function mapFile2(raw) {
|
|
990
|
+
return {
|
|
991
|
+
uid: raw.uid,
|
|
992
|
+
mimeType: raw.mimeType,
|
|
993
|
+
metadata: raw.metadata ? { width: raw.metadata.width, height: raw.metadata.height } : void 0
|
|
994
|
+
};
|
|
995
|
+
}
|
|
996
|
+
function mapAsset2(raw) {
|
|
997
|
+
return {
|
|
998
|
+
id: raw.id,
|
|
999
|
+
original: mapFile2(raw.original),
|
|
1000
|
+
preview: raw.preview ? mapFile2(raw.preview) : void 0,
|
|
1001
|
+
optimized: raw.optimized.map((file) => mapFile2(file))
|
|
1002
|
+
};
|
|
1003
|
+
}
|
|
1004
|
+
function createLazyAssetsCollection(def, client, updates$) {
|
|
824
1005
|
const _def = {
|
|
825
|
-
name: opts.name ?? source.__model,
|
|
826
|
-
model: source.__model,
|
|
827
1006
|
cache: {
|
|
828
|
-
max:
|
|
829
|
-
ttl:
|
|
1007
|
+
max: def?.cache?.max ?? DEFAULT_LRU_MAX,
|
|
1008
|
+
ttl: resolveTtl(def?.cache?.ttl)
|
|
1009
|
+
}
|
|
1010
|
+
};
|
|
1011
|
+
const cache = createLruCache({ max: _def.cache.max, ttl: _def.cache.ttl, onEvict: void 0 });
|
|
1012
|
+
const assetsSourceDoc = (0, import_client5.createSourceSubscription)(schema.sources.storeAsset);
|
|
1013
|
+
const projected = new import_projected4.ProjectedLazyMap({
|
|
1014
|
+
key: (asset) => asset.id,
|
|
1015
|
+
values: (ids) => wrap(
|
|
1016
|
+
"assets",
|
|
1017
|
+
() => (0, import_client5.collectAsync)(
|
|
1018
|
+
(0, import_client5.mapAsync)(client.subscribe(assetsSourceDoc, { ids }), (data) => data.source)
|
|
1019
|
+
).then((rawItems) => rawItems.map((item) => mapAsset2(item)))
|
|
1020
|
+
),
|
|
1021
|
+
cache
|
|
1022
|
+
});
|
|
1023
|
+
const channel = createRefreshChannel(def?.onRefresh);
|
|
1024
|
+
function emit(ids, kind) {
|
|
1025
|
+
channel.emit({ ids, kind });
|
|
1026
|
+
}
|
|
1027
|
+
let lastRefreshKeys = [];
|
|
1028
|
+
const scheduleRefresh = createRefresher(
|
|
1029
|
+
async () => {
|
|
1030
|
+
lastRefreshKeys = cache.keys();
|
|
1031
|
+
if (lastRefreshKeys.length === 0) {
|
|
1032
|
+
return;
|
|
1033
|
+
}
|
|
1034
|
+
await projected.refresh(lastRefreshKeys);
|
|
1035
|
+
},
|
|
1036
|
+
(kind) => {
|
|
1037
|
+
if (lastRefreshKeys.length > 0) {
|
|
1038
|
+
emit(lastRefreshKeys, kind);
|
|
1039
|
+
}
|
|
1040
|
+
},
|
|
1041
|
+
() => {
|
|
1042
|
+
}
|
|
1043
|
+
);
|
|
1044
|
+
const unsubUpdates = updates$.subscribe((batch) => {
|
|
1045
|
+
if (batch.asset.length === 0) return;
|
|
1046
|
+
for (const event of batch.asset) {
|
|
1047
|
+
projected.delete(event.id);
|
|
1048
|
+
}
|
|
1049
|
+
emit(
|
|
1050
|
+
batch.asset.map((e) => e.id),
|
|
1051
|
+
"upstream-update"
|
|
1052
|
+
);
|
|
1053
|
+
});
|
|
1054
|
+
return {
|
|
1055
|
+
instance: {
|
|
1056
|
+
refresh$: channel.stream$,
|
|
1057
|
+
get(idOrIds) {
|
|
1058
|
+
return projected.get(idOrIds);
|
|
1059
|
+
},
|
|
1060
|
+
upload(data, meta, options) {
|
|
1061
|
+
return client.upload(data, meta, options);
|
|
1062
|
+
},
|
|
1063
|
+
download(fileId) {
|
|
1064
|
+
return client.download(fileId);
|
|
1065
|
+
},
|
|
1066
|
+
proxyHls(path, signal) {
|
|
1067
|
+
return client.proxyHls(path, signal);
|
|
1068
|
+
},
|
|
1069
|
+
refresh() {
|
|
1070
|
+
scheduleRefresh("on-demand");
|
|
1071
|
+
},
|
|
1072
|
+
clear() {
|
|
1073
|
+
projected.clear();
|
|
1074
|
+
}
|
|
1075
|
+
},
|
|
1076
|
+
destroy() {
|
|
1077
|
+
unsubUpdates();
|
|
1078
|
+
channel.complete();
|
|
1079
|
+
}
|
|
1080
|
+
};
|
|
1081
|
+
}
|
|
1082
|
+
|
|
1083
|
+
// src/lazy-collection.ts
|
|
1084
|
+
var import_client6 = require("@contello/client");
|
|
1085
|
+
var import_projected5 = require("projected");
|
|
1086
|
+
function createLazyCollection(source, options, client, updates$, resolver) {
|
|
1087
|
+
const opts = options ?? {};
|
|
1088
|
+
const mapFn = opts.map ?? ((item) => item);
|
|
1089
|
+
const _def = {
|
|
1090
|
+
name: opts.name ?? source.__model,
|
|
1091
|
+
model: source.__model,
|
|
1092
|
+
cache: {
|
|
1093
|
+
max: opts.cache?.max ?? DEFAULT_LRU_MAX,
|
|
1094
|
+
ttl: resolveTtl(opts.cache?.ttl)
|
|
830
1095
|
}
|
|
831
1096
|
};
|
|
832
1097
|
const dependencyCollector = new DependencyCollector(_def.model, resolver);
|
|
@@ -835,14 +1100,14 @@ function createLazyCollection(source, options, client, updates$, resolver) {
|
|
|
835
1100
|
ttl: _def.cache.ttl,
|
|
836
1101
|
onEvict: (_value, key) => dependencyCollector.removeItem(key)
|
|
837
1102
|
});
|
|
838
|
-
const projected = new
|
|
1103
|
+
const projected = new import_projected5.ProjectedLazyMap({
|
|
839
1104
|
key: (item) => item.id,
|
|
840
1105
|
values: (keys) => wrap(
|
|
841
1106
|
`lazy-collection:${_def.name}`,
|
|
842
|
-
() => (0,
|
|
843
|
-
(0,
|
|
844
|
-
(0,
|
|
845
|
-
client.subscribe((0,
|
|
1107
|
+
() => (0, import_projected5.maybeThen)(
|
|
1108
|
+
(0, import_client6.collectAsync)(
|
|
1109
|
+
(0, import_client6.mapAsync)(
|
|
1110
|
+
client.subscribe((0, import_client6.createSourceSubscription)(source), {
|
|
846
1111
|
ids: keys
|
|
847
1112
|
}),
|
|
848
1113
|
(r) => r.source
|
|
@@ -850,11 +1115,13 @@ function createLazyCollection(source, options, client, updates$, resolver) {
|
|
|
850
1115
|
),
|
|
851
1116
|
(rawItems) => Promise.all(
|
|
852
1117
|
rawItems.map(
|
|
853
|
-
(item) =>
|
|
854
|
-
|
|
855
|
-
register(
|
|
856
|
-
|
|
857
|
-
|
|
1118
|
+
(item) => Promise.resolve(
|
|
1119
|
+
dependencyCollector.createContext(
|
|
1120
|
+
(ref, register) => (0, import_projected5.maybeThen)(mapFn(item, ref), (mapped) => {
|
|
1121
|
+
register(mapped.id);
|
|
1122
|
+
return mapped;
|
|
1123
|
+
})
|
|
1124
|
+
)
|
|
858
1125
|
)
|
|
859
1126
|
)
|
|
860
1127
|
)
|
|
@@ -862,7 +1129,12 @@ function createLazyCollection(source, options, client, updates$, resolver) {
|
|
|
862
1129
|
),
|
|
863
1130
|
cache
|
|
864
1131
|
});
|
|
865
|
-
const refresh$ = (0,
|
|
1132
|
+
const refresh$ = (0, import_client6.createAsyncIterableSubject)();
|
|
1133
|
+
function emit(ids, kind) {
|
|
1134
|
+
const event = { ids, kind };
|
|
1135
|
+
refresh$.next(event);
|
|
1136
|
+
opts.onRefresh?.(event);
|
|
1137
|
+
}
|
|
866
1138
|
let lastRefreshKeys = [];
|
|
867
1139
|
const scheduleRefresh = createRefresher(
|
|
868
1140
|
async () => {
|
|
@@ -872,10 +1144,9 @@ function createLazyCollection(source, options, client, updates$, resolver) {
|
|
|
872
1144
|
}
|
|
873
1145
|
await projected.refresh(lastRefreshKeys);
|
|
874
1146
|
},
|
|
875
|
-
() => {
|
|
1147
|
+
(kind) => {
|
|
876
1148
|
if (lastRefreshKeys.length > 0) {
|
|
877
|
-
|
|
878
|
-
opts.onRefresh?.(lastRefreshKeys);
|
|
1149
|
+
emit(lastRefreshKeys, kind);
|
|
879
1150
|
}
|
|
880
1151
|
},
|
|
881
1152
|
() => {
|
|
@@ -899,12 +1170,9 @@ function createLazyCollection(source, options, client, updates$, resolver) {
|
|
|
899
1170
|
dependencyCollector.removeItem(key);
|
|
900
1171
|
projected.delete(key);
|
|
901
1172
|
}
|
|
902
|
-
|
|
903
|
-
refresh$.next(ids);
|
|
904
|
-
opts.onRefresh?.(ids);
|
|
1173
|
+
emit([...evicted], "upstream-update");
|
|
905
1174
|
}
|
|
906
1175
|
});
|
|
907
|
-
void scheduleRefresh;
|
|
908
1176
|
return {
|
|
909
1177
|
instance: {
|
|
910
1178
|
name: _def.name,
|
|
@@ -913,7 +1181,7 @@ function createLazyCollection(source, options, client, updates$, resolver) {
|
|
|
913
1181
|
return projected.get(idOrIds);
|
|
914
1182
|
},
|
|
915
1183
|
refresh() {
|
|
916
|
-
scheduleRefresh();
|
|
1184
|
+
scheduleRefresh("on-demand");
|
|
917
1185
|
},
|
|
918
1186
|
clear() {
|
|
919
1187
|
dependencyCollector.clear();
|
|
@@ -927,47 +1195,9 @@ function createLazyCollection(source, options, client, updates$, resolver) {
|
|
|
927
1195
|
};
|
|
928
1196
|
}
|
|
929
1197
|
|
|
930
|
-
// src/
|
|
931
|
-
var
|
|
932
|
-
|
|
933
|
-
_toModel;
|
|
934
|
-
_toTypeName;
|
|
935
|
-
constructor(models) {
|
|
936
|
-
const toModel = /* @__PURE__ */ new Map();
|
|
937
|
-
const toTypeName = /* @__PURE__ */ new Map();
|
|
938
|
-
let typeNames;
|
|
939
|
-
if (models) {
|
|
940
|
-
typeNames = /* @__PURE__ */ new Set();
|
|
941
|
-
for (const [model, typeName] of Object.entries(models)) {
|
|
942
|
-
toModel.set(typeName, model);
|
|
943
|
-
toTypeName.set(model, typeName);
|
|
944
|
-
typeNames.add(typeName);
|
|
945
|
-
}
|
|
946
|
-
}
|
|
947
|
-
this._typeNames = typeNames;
|
|
948
|
-
this._toModel = toModel;
|
|
949
|
-
this._toTypeName = toTypeName;
|
|
950
|
-
}
|
|
951
|
-
hasTypeName(typeName) {
|
|
952
|
-
return !this._typeNames || this._typeNames.has(typeName);
|
|
953
|
-
}
|
|
954
|
-
getModel(typeName) {
|
|
955
|
-
return this._toModel.get(typeName);
|
|
956
|
-
}
|
|
957
|
-
getTypeName(model) {
|
|
958
|
-
return this._toTypeName.get(model) ?? model;
|
|
959
|
-
}
|
|
960
|
-
hasModel(model) {
|
|
961
|
-
return !this._toTypeName.size || this._toTypeName.has(model);
|
|
962
|
-
}
|
|
963
|
-
resolveModel(typeName) {
|
|
964
|
-
return this._toModel.get(typeName) ?? typeName;
|
|
965
|
-
}
|
|
966
|
-
};
|
|
967
|
-
|
|
968
|
-
// src/routes.ts
|
|
969
|
-
var import_client6 = require("@contello/client");
|
|
970
|
-
var import_projected5 = require("projected");
|
|
1198
|
+
// src/lazy-routes.ts
|
|
1199
|
+
var import_client7 = require("@contello/client");
|
|
1200
|
+
var import_projected6 = require("projected");
|
|
971
1201
|
|
|
972
1202
|
// src/routes-mapping.ts
|
|
973
1203
|
function mapRoute(raw, resolver) {
|
|
@@ -977,21 +1207,23 @@ function mapRoute(raw, resolver) {
|
|
|
977
1207
|
customHeaders: raw.customHeaders.map((h) => ({ name: h.name, value: h.value }))
|
|
978
1208
|
};
|
|
979
1209
|
switch (raw.target.__typename) {
|
|
980
|
-
case "ContelloRouteTargetRedirect":
|
|
1210
|
+
case "ContelloRouteTargetRedirect": {
|
|
981
1211
|
return {
|
|
982
1212
|
...base,
|
|
983
1213
|
type: "redirect",
|
|
984
1214
|
location: raw.target.location,
|
|
985
1215
|
status: raw.target.responseCode
|
|
986
1216
|
};
|
|
987
|
-
|
|
1217
|
+
}
|
|
1218
|
+
case "ContelloRouteTargetText": {
|
|
988
1219
|
return {
|
|
989
1220
|
...base,
|
|
990
1221
|
type: "text",
|
|
991
1222
|
content: raw.target.content,
|
|
992
1223
|
mimeType: raw.target.mimeType
|
|
993
1224
|
};
|
|
994
|
-
|
|
1225
|
+
}
|
|
1226
|
+
case "ContelloRouteTargetAsset": {
|
|
995
1227
|
return {
|
|
996
1228
|
...base,
|
|
997
1229
|
type: "asset",
|
|
@@ -1000,6 +1232,7 @@ function mapRoute(raw, resolver) {
|
|
|
1000
1232
|
contentDisposition: raw.target.contentDisposition === "INLINE" ? "inline" : "attachment",
|
|
1001
1233
|
mimeType: raw.target.asset.original.mimeType
|
|
1002
1234
|
};
|
|
1235
|
+
}
|
|
1003
1236
|
case "ContelloRouteTargetEntity": {
|
|
1004
1237
|
if (!raw.target.entity) {
|
|
1005
1238
|
return void 0;
|
|
@@ -1013,20 +1246,18 @@ function mapRoute(raw, resolver) {
|
|
|
1013
1246
|
entityId: raw.target.entity.id
|
|
1014
1247
|
};
|
|
1015
1248
|
}
|
|
1016
|
-
default:
|
|
1249
|
+
default: {
|
|
1017
1250
|
return void 0;
|
|
1251
|
+
}
|
|
1018
1252
|
}
|
|
1019
1253
|
}
|
|
1020
1254
|
|
|
1021
|
-
// src/routes.ts
|
|
1255
|
+
// src/lazy-routes.ts
|
|
1022
1256
|
var ID_PREFIX = "1\0";
|
|
1023
1257
|
var PATH_PREFIX = "2\0";
|
|
1024
1258
|
var CACHE_KEY = /* @__PURE__ */ Symbol("cacheKey");
|
|
1025
|
-
function collectRoutes(
|
|
1026
|
-
return
|
|
1027
|
-
if (!raw) {
|
|
1028
|
-
return acc;
|
|
1029
|
-
}
|
|
1259
|
+
function collectRoutes(rawItems, keyFn, resolver) {
|
|
1260
|
+
return rawItems.reduce((acc, raw) => {
|
|
1030
1261
|
const mapped = mapRoute(raw, resolver);
|
|
1031
1262
|
if (mapped) {
|
|
1032
1263
|
mapped[CACHE_KEY] = keyFn(mapped);
|
|
@@ -1076,15 +1307,21 @@ function createRoutesCache(max, ttl) {
|
|
|
1076
1307
|
keys: () => lru.keys()
|
|
1077
1308
|
};
|
|
1078
1309
|
}
|
|
1079
|
-
function
|
|
1310
|
+
function createLazyRoutesCollection(def, client, updates$, resolver) {
|
|
1080
1311
|
const _def = {
|
|
1081
1312
|
cache: {
|
|
1082
|
-
max: def?.cache?.max ??
|
|
1083
|
-
ttl: def?.cache?.ttl
|
|
1313
|
+
max: def?.cache?.max ?? DEFAULT_LRU_MAX,
|
|
1314
|
+
ttl: resolveTtl(def?.cache?.ttl)
|
|
1084
1315
|
}
|
|
1085
1316
|
};
|
|
1086
1317
|
const cache = createRoutesCache(_def.cache.max, _def.cache.ttl);
|
|
1087
|
-
const
|
|
1318
|
+
const routesSourceDoc = (0, import_client7.createSourceSubscription)(schema.sources.storeRoute);
|
|
1319
|
+
function fetchRoutes(vars) {
|
|
1320
|
+
return (0, import_client7.collectAsync)(
|
|
1321
|
+
(0, import_client7.mapAsync)(client.subscribe(routesSourceDoc, vars), (data) => data.source)
|
|
1322
|
+
);
|
|
1323
|
+
}
|
|
1324
|
+
const projected = new import_projected6.ProjectedLazyMap({
|
|
1088
1325
|
key: (route) => route[CACHE_KEY] ?? ID_PREFIX + route.id,
|
|
1089
1326
|
values: (prefixedKeys) => wrap("routes", () => {
|
|
1090
1327
|
const ids = [];
|
|
@@ -1098,13 +1335,16 @@ function createRoutesCollection(def, client, updates$, resolver) {
|
|
|
1098
1335
|
}
|
|
1099
1336
|
}
|
|
1100
1337
|
return Promise.all([
|
|
1101
|
-
ids.length > 0 ?
|
|
1102
|
-
paths.length > 0 ?
|
|
1338
|
+
ids.length > 0 ? fetchRoutes({ ids }).then((raw) => collectRoutes(raw, (r) => ID_PREFIX + r.id, resolver)) : Promise.resolve([]),
|
|
1339
|
+
paths.length > 0 ? fetchRoutes({ paths }).then((raw) => collectRoutes(raw, (r) => PATH_PREFIX + r.path, resolver)) : Promise.resolve([])
|
|
1103
1340
|
]).then(([byIds, byPaths]) => [...byIds, ...byPaths]);
|
|
1104
1341
|
}),
|
|
1105
1342
|
cache
|
|
1106
1343
|
});
|
|
1107
|
-
const
|
|
1344
|
+
const channel = createRefreshChannel(def?.onRefresh);
|
|
1345
|
+
function emit(ids, kind) {
|
|
1346
|
+
channel.emit({ ids, kind });
|
|
1347
|
+
}
|
|
1108
1348
|
let lastRefreshKeys = [];
|
|
1109
1349
|
const scheduleRefresh = createRefresher(
|
|
1110
1350
|
async () => {
|
|
@@ -1114,9 +1354,12 @@ function createRoutesCollection(def, client, updates$, resolver) {
|
|
|
1114
1354
|
}
|
|
1115
1355
|
await projected.refresh(lastRefreshKeys);
|
|
1116
1356
|
},
|
|
1117
|
-
() => {
|
|
1357
|
+
(kind) => {
|
|
1118
1358
|
if (lastRefreshKeys.length > 0) {
|
|
1119
|
-
|
|
1359
|
+
emit(
|
|
1360
|
+
lastRefreshKeys.map((key) => key.slice(2)),
|
|
1361
|
+
kind
|
|
1362
|
+
);
|
|
1120
1363
|
}
|
|
1121
1364
|
},
|
|
1122
1365
|
() => {
|
|
@@ -1137,12 +1380,12 @@ function createRoutesCollection(def, client, updates$, resolver) {
|
|
|
1137
1380
|
}
|
|
1138
1381
|
}
|
|
1139
1382
|
if (evicted.length > 0) {
|
|
1140
|
-
|
|
1383
|
+
emit(evicted, "upstream-update");
|
|
1141
1384
|
}
|
|
1142
1385
|
});
|
|
1143
1386
|
return {
|
|
1144
1387
|
instance: {
|
|
1145
|
-
refresh$,
|
|
1388
|
+
refresh$: channel.stream$,
|
|
1146
1389
|
get(idOrIds) {
|
|
1147
1390
|
if (Array.isArray(idOrIds)) {
|
|
1148
1391
|
return projected.get(idOrIds.map((id) => ID_PREFIX + id));
|
|
@@ -1156,7 +1399,7 @@ function createRoutesCollection(def, client, updates$, resolver) {
|
|
|
1156
1399
|
return projected.get(PATH_PREFIX + pathOrPaths);
|
|
1157
1400
|
},
|
|
1158
1401
|
refresh() {
|
|
1159
|
-
scheduleRefresh();
|
|
1402
|
+
scheduleRefresh("on-demand");
|
|
1160
1403
|
},
|
|
1161
1404
|
clear() {
|
|
1162
1405
|
projected.clear();
|
|
@@ -1164,60 +1407,312 @@ function createRoutesCollection(def, client, updates$, resolver) {
|
|
|
1164
1407
|
},
|
|
1165
1408
|
destroy() {
|
|
1166
1409
|
unsubUpdates();
|
|
1167
|
-
|
|
1410
|
+
channel.complete();
|
|
1168
1411
|
}
|
|
1169
1412
|
};
|
|
1170
1413
|
}
|
|
1171
1414
|
|
|
1415
|
+
// src/model-resolver.ts
|
|
1416
|
+
var ModelResolver = class {
|
|
1417
|
+
_typeNames;
|
|
1418
|
+
_toModel;
|
|
1419
|
+
_toTypeName;
|
|
1420
|
+
constructor(models2) {
|
|
1421
|
+
const toModel = /* @__PURE__ */ new Map();
|
|
1422
|
+
const toTypeName = /* @__PURE__ */ new Map();
|
|
1423
|
+
let typeNames;
|
|
1424
|
+
if (models2) {
|
|
1425
|
+
typeNames = /* @__PURE__ */ new Set();
|
|
1426
|
+
for (const [model, typeName] of Object.entries(models2)) {
|
|
1427
|
+
toModel.set(typeName, model);
|
|
1428
|
+
toTypeName.set(model, typeName);
|
|
1429
|
+
typeNames.add(typeName);
|
|
1430
|
+
}
|
|
1431
|
+
}
|
|
1432
|
+
this._typeNames = typeNames;
|
|
1433
|
+
this._toModel = toModel;
|
|
1434
|
+
this._toTypeName = toTypeName;
|
|
1435
|
+
}
|
|
1436
|
+
hasTypeName(typeName) {
|
|
1437
|
+
return !this._typeNames || this._typeNames.has(typeName);
|
|
1438
|
+
}
|
|
1439
|
+
getModel(typeName) {
|
|
1440
|
+
return this._toModel.get(typeName);
|
|
1441
|
+
}
|
|
1442
|
+
getTypeName(model) {
|
|
1443
|
+
return this._toTypeName.get(model) ?? model;
|
|
1444
|
+
}
|
|
1445
|
+
hasModel(model) {
|
|
1446
|
+
return this._toTypeName.size === 0 || this._toTypeName.has(model);
|
|
1447
|
+
}
|
|
1448
|
+
resolveModel(typeName) {
|
|
1449
|
+
return this._toModel.get(typeName) ?? typeName;
|
|
1450
|
+
}
|
|
1451
|
+
};
|
|
1452
|
+
|
|
1453
|
+
// src/routes.ts
|
|
1454
|
+
var import_client8 = require("@contello/client");
|
|
1455
|
+
var import_projected7 = require("projected");
|
|
1456
|
+
function createRoutesCollection(options, client, updates$, resolver, refreshByTtl) {
|
|
1457
|
+
const opts = options ?? {};
|
|
1458
|
+
const cache = {
|
|
1459
|
+
ttl: resolveTtl(opts.cache?.ttl),
|
|
1460
|
+
eviction: opts.cache?.eviction ?? "refresh"
|
|
1461
|
+
};
|
|
1462
|
+
const channel = createRefreshChannel(opts.onRefresh);
|
|
1463
|
+
const ttl = createTtlOrchestrator({ ttl: cache.ttl, run: () => runFullRefresh("ttl") });
|
|
1464
|
+
const pathById = /* @__PURE__ */ new Map();
|
|
1465
|
+
const idByPath = /* @__PURE__ */ new Map();
|
|
1466
|
+
let loaded = false;
|
|
1467
|
+
const routesSourceDoc = (0, import_client8.createSourceSubscription)(schema.sources.storeRoute);
|
|
1468
|
+
const projected = new import_projected7.ProjectedMap({
|
|
1469
|
+
key: (route) => route.id,
|
|
1470
|
+
values: (ids) => wrap(
|
|
1471
|
+
"routes",
|
|
1472
|
+
() => (0, import_client8.collectAsync)(
|
|
1473
|
+
(0, import_client8.mapAsync)(client.subscribe(routesSourceDoc, { ids }), (data) => data.source)
|
|
1474
|
+
).then((rawItems) => {
|
|
1475
|
+
const items = rawItems.reduce((acc, raw) => {
|
|
1476
|
+
const mapped = mapRoute(raw, resolver);
|
|
1477
|
+
if (mapped) {
|
|
1478
|
+
acc.push(mapped);
|
|
1479
|
+
}
|
|
1480
|
+
return acc;
|
|
1481
|
+
}, []);
|
|
1482
|
+
if (ids === void 0) {
|
|
1483
|
+
pathById.clear();
|
|
1484
|
+
idByPath.clear();
|
|
1485
|
+
for (const r of items) {
|
|
1486
|
+
pathById.set(r.id, r.path);
|
|
1487
|
+
idByPath.set(r.path, r.id);
|
|
1488
|
+
}
|
|
1489
|
+
if (!loaded) {
|
|
1490
|
+
loaded = true;
|
|
1491
|
+
ttl.mark();
|
|
1492
|
+
opts.onLoad?.(items.map((r) => r.id));
|
|
1493
|
+
}
|
|
1494
|
+
} else {
|
|
1495
|
+
const returnedIds = new Set(items.map((r) => r.id));
|
|
1496
|
+
for (const id of ids) {
|
|
1497
|
+
if (!returnedIds.has(id)) {
|
|
1498
|
+
const oldPath = pathById.get(id);
|
|
1499
|
+
if (oldPath !== void 0) {
|
|
1500
|
+
idByPath.delete(oldPath);
|
|
1501
|
+
pathById.delete(id);
|
|
1502
|
+
}
|
|
1503
|
+
}
|
|
1504
|
+
}
|
|
1505
|
+
for (const r of items) {
|
|
1506
|
+
const oldPath = pathById.get(r.id);
|
|
1507
|
+
if (oldPath !== void 0 && oldPath !== r.path) {
|
|
1508
|
+
idByPath.delete(oldPath);
|
|
1509
|
+
}
|
|
1510
|
+
pathById.set(r.id, r.path);
|
|
1511
|
+
idByPath.set(r.path, r.id);
|
|
1512
|
+
}
|
|
1513
|
+
}
|
|
1514
|
+
return items;
|
|
1515
|
+
})
|
|
1516
|
+
)
|
|
1517
|
+
});
|
|
1518
|
+
function emit(ids, kind) {
|
|
1519
|
+
channel.emit({ ids, kind });
|
|
1520
|
+
}
|
|
1521
|
+
function runFullRefresh(kind) {
|
|
1522
|
+
refreshByTtl.enqueue(
|
|
1523
|
+
() => (0, import_client8.runWithBackoff)(
|
|
1524
|
+
() => projected.refresh().then((map) => {
|
|
1525
|
+
emit([...map.keys()], kind);
|
|
1526
|
+
ttl.mark();
|
|
1527
|
+
})
|
|
1528
|
+
)
|
|
1529
|
+
);
|
|
1530
|
+
}
|
|
1531
|
+
function runPartialRefresh(refreshIds, deletedIds) {
|
|
1532
|
+
const changedIds = [.../* @__PURE__ */ new Set([...refreshIds, ...deletedIds])];
|
|
1533
|
+
if (refreshIds.length === 0) {
|
|
1534
|
+
emit(changedIds, "upstream-update");
|
|
1535
|
+
return;
|
|
1536
|
+
}
|
|
1537
|
+
void (0, import_client8.runWithBackoff)(
|
|
1538
|
+
() => projected.refresh(refreshIds).then(() => {
|
|
1539
|
+
emit(changedIds, "upstream-update");
|
|
1540
|
+
})
|
|
1541
|
+
);
|
|
1542
|
+
}
|
|
1543
|
+
const unsubUpdates = updates$.subscribe((batch) => {
|
|
1544
|
+
if (!loaded || batch.route.length === 0) {
|
|
1545
|
+
return;
|
|
1546
|
+
}
|
|
1547
|
+
const deleted = [];
|
|
1548
|
+
const upserted = /* @__PURE__ */ new Set();
|
|
1549
|
+
for (const event of batch.route) {
|
|
1550
|
+
if (event.mutation === "delete") {
|
|
1551
|
+
deleted.push(event.id);
|
|
1552
|
+
} else {
|
|
1553
|
+
upserted.add(event.id);
|
|
1554
|
+
}
|
|
1555
|
+
}
|
|
1556
|
+
if (cache.eviction === "clear") {
|
|
1557
|
+
pathById.clear();
|
|
1558
|
+
idByPath.clear();
|
|
1559
|
+
projected.clear();
|
|
1560
|
+
return;
|
|
1561
|
+
}
|
|
1562
|
+
for (const id of deleted) {
|
|
1563
|
+
const oldPath = pathById.get(id);
|
|
1564
|
+
if (oldPath !== void 0) {
|
|
1565
|
+
idByPath.delete(oldPath);
|
|
1566
|
+
pathById.delete(id);
|
|
1567
|
+
}
|
|
1568
|
+
}
|
|
1569
|
+
if (deleted.length > 0) {
|
|
1570
|
+
projected.delete(deleted);
|
|
1571
|
+
}
|
|
1572
|
+
runPartialRefresh([...upserted], deleted);
|
|
1573
|
+
});
|
|
1574
|
+
function getByIdSingle(id) {
|
|
1575
|
+
return projected.get(id);
|
|
1576
|
+
}
|
|
1577
|
+
function getByIds(ids) {
|
|
1578
|
+
return projected.get(ids);
|
|
1579
|
+
}
|
|
1580
|
+
function lookupByPathSingle(path) {
|
|
1581
|
+
return (0, import_projected7.maybeThen)(ensureLoaded(), () => {
|
|
1582
|
+
const id = idByPath.get(path);
|
|
1583
|
+
return id === void 0 ? void 0 : projected.get(id);
|
|
1584
|
+
});
|
|
1585
|
+
}
|
|
1586
|
+
function lookupByPaths(paths) {
|
|
1587
|
+
return (0, import_projected7.maybeThen)(ensureLoaded(), () => {
|
|
1588
|
+
const ids = [];
|
|
1589
|
+
for (const p of paths) {
|
|
1590
|
+
const id = idByPath.get(p);
|
|
1591
|
+
if (id !== void 0) {
|
|
1592
|
+
ids.push(id);
|
|
1593
|
+
}
|
|
1594
|
+
}
|
|
1595
|
+
return projected.get(ids);
|
|
1596
|
+
});
|
|
1597
|
+
}
|
|
1598
|
+
function ensureLoaded() {
|
|
1599
|
+
if (loaded) {
|
|
1600
|
+
return void 0;
|
|
1601
|
+
}
|
|
1602
|
+
return (0, import_projected7.maybeThen)(projected.getAllAsMap(), () => {
|
|
1603
|
+
});
|
|
1604
|
+
}
|
|
1605
|
+
const instance = {
|
|
1606
|
+
refresh$: channel.stream$,
|
|
1607
|
+
async load() {
|
|
1608
|
+
if (!loaded) {
|
|
1609
|
+
await projected.getAllAsMap();
|
|
1610
|
+
}
|
|
1611
|
+
},
|
|
1612
|
+
get(idOrIds) {
|
|
1613
|
+
return typeof idOrIds === "string" ? getByIdSingle(idOrIds) : getByIds(idOrIds);
|
|
1614
|
+
},
|
|
1615
|
+
getByPath(pathOrPaths) {
|
|
1616
|
+
return typeof pathOrPaths === "string" ? lookupByPathSingle(pathOrPaths) : lookupByPaths(pathOrPaths);
|
|
1617
|
+
},
|
|
1618
|
+
refresh() {
|
|
1619
|
+
runFullRefresh("on-demand");
|
|
1620
|
+
}
|
|
1621
|
+
};
|
|
1622
|
+
return {
|
|
1623
|
+
instance,
|
|
1624
|
+
destroy() {
|
|
1625
|
+
unsubUpdates();
|
|
1626
|
+
ttl.clear();
|
|
1627
|
+
channel.complete();
|
|
1628
|
+
}
|
|
1629
|
+
};
|
|
1630
|
+
}
|
|
1631
|
+
function createRoutesSyncCollection(options, client, updates$, resolver, refreshByTtl) {
|
|
1632
|
+
const { instance: base, destroy } = createRoutesCollection(options, client, updates$, resolver, refreshByTtl);
|
|
1633
|
+
function assertSync(value, method) {
|
|
1634
|
+
if (value instanceof Promise) {
|
|
1635
|
+
throw new TypeError(`routes.${method}() is not initialized yet \u2014 call routes.load() first`);
|
|
1636
|
+
}
|
|
1637
|
+
return value;
|
|
1638
|
+
}
|
|
1639
|
+
return {
|
|
1640
|
+
instance: {
|
|
1641
|
+
...base,
|
|
1642
|
+
get(idOrIds) {
|
|
1643
|
+
return assertSync(base.get(idOrIds), "get");
|
|
1644
|
+
},
|
|
1645
|
+
getByPath(pathOrPaths) {
|
|
1646
|
+
return assertSync(base.getByPath(pathOrPaths), "getByPath");
|
|
1647
|
+
}
|
|
1648
|
+
},
|
|
1649
|
+
destroy
|
|
1650
|
+
};
|
|
1651
|
+
}
|
|
1652
|
+
|
|
1172
1653
|
// src/singleton.ts
|
|
1173
|
-
var
|
|
1174
|
-
var
|
|
1175
|
-
function createSingleton(source, options, client, updates$, resolver) {
|
|
1654
|
+
var import_client9 = require("@contello/client");
|
|
1655
|
+
var import_projected8 = require("projected");
|
|
1656
|
+
function createSingleton(source, options, client, updates$, resolver, refreshByTtl) {
|
|
1176
1657
|
const opts = options ?? {};
|
|
1177
1658
|
const mapFn = opts.map ?? ((item) => item);
|
|
1178
1659
|
const _def = {
|
|
1179
1660
|
name: opts.name ?? source.__model,
|
|
1180
1661
|
model: source.__model,
|
|
1181
1662
|
cache: {
|
|
1182
|
-
ttl: opts.cache?.ttl,
|
|
1663
|
+
ttl: resolveTtl(opts.cache?.ttl),
|
|
1183
1664
|
eviction: opts.cache?.eviction ?? "refresh"
|
|
1184
1665
|
}
|
|
1185
1666
|
};
|
|
1186
1667
|
const dependencyCollector = new DependencyCollector(_def.model, resolver);
|
|
1187
1668
|
const itemKey = `singleton:${_def.name}`;
|
|
1188
|
-
|
|
1189
|
-
const
|
|
1669
|
+
const channel = createRefreshChannel(opts.onRefresh);
|
|
1670
|
+
const ttl = createTtlOrchestrator({ ttl: _def.cache.ttl, run: () => runTtlRefresh() });
|
|
1671
|
+
let loaded = false;
|
|
1672
|
+
const projected = new import_projected8.ProjectedValue({
|
|
1190
1673
|
value: () => wrap(
|
|
1191
1674
|
`singleton:${_def.name}`,
|
|
1192
|
-
() => (0,
|
|
1193
|
-
(0,
|
|
1194
|
-
(0,
|
|
1195
|
-
client.subscribe((0,
|
|
1675
|
+
() => (0, import_projected8.maybeThen)(
|
|
1676
|
+
(0, import_client9.firstAsync)(
|
|
1677
|
+
(0, import_client9.mapAsync)(
|
|
1678
|
+
client.subscribe((0, import_client9.createSourceSubscription)(source)),
|
|
1196
1679
|
(r) => r.source
|
|
1197
1680
|
)
|
|
1198
1681
|
),
|
|
1199
1682
|
(raw) => dependencyCollector.createContext(
|
|
1200
|
-
(ref, register) => (0,
|
|
1683
|
+
(ref, register) => (0, import_projected8.maybeThen)(mapFn(raw, ref), (mapped) => {
|
|
1201
1684
|
register(itemKey);
|
|
1685
|
+
if (!loaded) {
|
|
1686
|
+
loaded = true;
|
|
1687
|
+
ttl.mark();
|
|
1688
|
+
opts.onLoad?.();
|
|
1689
|
+
}
|
|
1202
1690
|
return mapped;
|
|
1203
1691
|
})
|
|
1204
1692
|
)
|
|
1205
1693
|
)
|
|
1206
1694
|
)
|
|
1207
1695
|
});
|
|
1208
|
-
|
|
1696
|
+
function emit(kind) {
|
|
1697
|
+
channel.emit({ kind });
|
|
1698
|
+
}
|
|
1699
|
+
function runTtlRefresh() {
|
|
1700
|
+
refreshByTtl.enqueue(
|
|
1701
|
+
() => (0, import_client9.runWithBackoff)(() => projected.refresh()).then(() => {
|
|
1702
|
+
emit("ttl");
|
|
1703
|
+
ttl.mark();
|
|
1704
|
+
})
|
|
1705
|
+
);
|
|
1706
|
+
}
|
|
1209
1707
|
const scheduleRefresh = createRefresher(
|
|
1210
1708
|
() => projected.refresh(),
|
|
1211
|
-
() => {
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
if (_def.cache.ttl !== void 0) {
|
|
1215
|
-
timer = setTimeout(scheduleRefresh, _def.cache.ttl);
|
|
1216
|
-
}
|
|
1709
|
+
(kind) => {
|
|
1710
|
+
emit(kind);
|
|
1711
|
+
ttl.mark();
|
|
1217
1712
|
},
|
|
1218
|
-
() =>
|
|
1713
|
+
() => {
|
|
1714
|
+
}
|
|
1219
1715
|
);
|
|
1220
|
-
let loaded = false;
|
|
1221
1716
|
const unsubUpdates = updates$.subscribe((batch) => {
|
|
1222
1717
|
if (!loaded) {
|
|
1223
1718
|
return;
|
|
@@ -1232,41 +1727,39 @@ function createSingleton(source, options, client, updates$, resolver) {
|
|
|
1232
1727
|
projected.clear();
|
|
1233
1728
|
return;
|
|
1234
1729
|
}
|
|
1235
|
-
scheduleRefresh();
|
|
1730
|
+
scheduleRefresh("upstream-update");
|
|
1236
1731
|
});
|
|
1237
1732
|
return {
|
|
1238
1733
|
instance: {
|
|
1239
1734
|
name: _def.name,
|
|
1240
|
-
refresh$,
|
|
1735
|
+
refresh$: channel.stream$,
|
|
1241
1736
|
get() {
|
|
1242
1737
|
return projected.get();
|
|
1243
1738
|
},
|
|
1244
1739
|
refresh() {
|
|
1245
|
-
scheduleRefresh();
|
|
1740
|
+
scheduleRefresh("on-demand");
|
|
1246
1741
|
},
|
|
1247
1742
|
async load() {
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
if (_def.cache.ttl !== void 0) {
|
|
1251
|
-
timer = setTimeout(scheduleRefresh, _def.cache.ttl);
|
|
1743
|
+
if (!loaded) {
|
|
1744
|
+
await projected.get();
|
|
1252
1745
|
}
|
|
1253
|
-
opts.onLoad?.();
|
|
1254
1746
|
}
|
|
1255
1747
|
},
|
|
1256
1748
|
destroy() {
|
|
1257
1749
|
unsubUpdates();
|
|
1258
|
-
|
|
1259
|
-
|
|
1750
|
+
ttl.clear();
|
|
1751
|
+
channel.complete();
|
|
1260
1752
|
}
|
|
1261
1753
|
};
|
|
1262
1754
|
}
|
|
1263
|
-
function createSingletonSync(source, options, client, updates$, resolver) {
|
|
1755
|
+
function createSingletonSync(source, options, client, updates$, resolver, refreshByTtl) {
|
|
1264
1756
|
const { instance: base, destroy } = createSingleton(
|
|
1265
1757
|
source,
|
|
1266
1758
|
options,
|
|
1267
1759
|
client,
|
|
1268
1760
|
updates$,
|
|
1269
|
-
resolver
|
|
1761
|
+
resolver,
|
|
1762
|
+
refreshByTtl
|
|
1270
1763
|
);
|
|
1271
1764
|
return {
|
|
1272
1765
|
instance: {
|
|
@@ -1274,7 +1767,7 @@ function createSingletonSync(source, options, client, updates$, resolver) {
|
|
|
1274
1767
|
get() {
|
|
1275
1768
|
const result = base.get();
|
|
1276
1769
|
if (result instanceof Promise) {
|
|
1277
|
-
throw new
|
|
1770
|
+
throw new TypeError(`singleton "${base.name}" is not initialized yet \u2014 call singleton.load() first`);
|
|
1278
1771
|
}
|
|
1279
1772
|
return result;
|
|
1280
1773
|
}
|
|
@@ -1284,7 +1777,7 @@ function createSingletonSync(source, options, client, updates$, resolver) {
|
|
|
1284
1777
|
}
|
|
1285
1778
|
|
|
1286
1779
|
// src/watcher.ts
|
|
1287
|
-
var
|
|
1780
|
+
var import_client10 = require("@contello/client");
|
|
1288
1781
|
function addRouteByModel(map, entityType, event) {
|
|
1289
1782
|
let list = map.get(entityType);
|
|
1290
1783
|
if (!list) {
|
|
@@ -1301,7 +1794,7 @@ function createUpdateBatch(events) {
|
|
|
1301
1794
|
const routeByEntityModel = /* @__PURE__ */ new Map();
|
|
1302
1795
|
for (const event of events) {
|
|
1303
1796
|
switch (event.target) {
|
|
1304
|
-
case "route":
|
|
1797
|
+
case "route": {
|
|
1305
1798
|
route.push(event);
|
|
1306
1799
|
if ("after" in event && event.after.type === "entity") {
|
|
1307
1800
|
addRouteByModel(routeByEntityModel, event.after.model, event);
|
|
@@ -1310,12 +1803,15 @@ function createUpdateBatch(events) {
|
|
|
1310
1803
|
addRouteByModel(routeByEntityModel, event.before.model, event);
|
|
1311
1804
|
}
|
|
1312
1805
|
break;
|
|
1313
|
-
|
|
1806
|
+
}
|
|
1807
|
+
case "asset": {
|
|
1314
1808
|
asset.push(event);
|
|
1315
1809
|
break;
|
|
1316
|
-
|
|
1810
|
+
}
|
|
1811
|
+
case "i18nMessage": {
|
|
1317
1812
|
i18nMessage.push(event);
|
|
1318
1813
|
break;
|
|
1814
|
+
}
|
|
1319
1815
|
case "entity": {
|
|
1320
1816
|
let list = entity.get(event.model);
|
|
1321
1817
|
if (!list) {
|
|
@@ -1331,12 +1827,15 @@ function createUpdateBatch(events) {
|
|
|
1331
1827
|
}
|
|
1332
1828
|
function castMutationType(type) {
|
|
1333
1829
|
switch (type) {
|
|
1334
|
-
case "CREATE":
|
|
1830
|
+
case "CREATE": {
|
|
1335
1831
|
return "create";
|
|
1336
|
-
|
|
1832
|
+
}
|
|
1833
|
+
case "UPDATE": {
|
|
1337
1834
|
return "update";
|
|
1338
|
-
|
|
1835
|
+
}
|
|
1836
|
+
case "DELETE": {
|
|
1339
1837
|
return "delete";
|
|
1838
|
+
}
|
|
1340
1839
|
}
|
|
1341
1840
|
}
|
|
1342
1841
|
function mapEvent(raw, resolver) {
|
|
@@ -1361,10 +1860,12 @@ function mapEvent(raw, resolver) {
|
|
|
1361
1860
|
}
|
|
1362
1861
|
return { id, mutation, target: "route", after, before };
|
|
1363
1862
|
}
|
|
1364
|
-
case "ContelloAsset":
|
|
1863
|
+
case "ContelloAsset": {
|
|
1365
1864
|
return { id, mutation, target: "asset" };
|
|
1366
|
-
|
|
1865
|
+
}
|
|
1866
|
+
case "ContelloI18nMessage": {
|
|
1367
1867
|
return { id, mutation, target: "i18nMessage", token: target.token };
|
|
1868
|
+
}
|
|
1368
1869
|
default: {
|
|
1369
1870
|
if (!__typename || !resolver.hasTypeName(__typename)) {
|
|
1370
1871
|
return void 0;
|
|
@@ -1374,7 +1875,7 @@ function mapEvent(raw, resolver) {
|
|
|
1374
1875
|
}
|
|
1375
1876
|
}
|
|
1376
1877
|
function createInternalWatcher(client, resolver) {
|
|
1377
|
-
const updates$ = (0,
|
|
1878
|
+
const updates$ = (0, import_client10.createAsyncIterableSubject)();
|
|
1378
1879
|
let controller;
|
|
1379
1880
|
return {
|
|
1380
1881
|
updates$,
|
|
@@ -1386,11 +1887,11 @@ function createInternalWatcher(client, resolver) {
|
|
|
1386
1887
|
const signal = controller.signal;
|
|
1387
1888
|
wrap("watcher:start", () => {
|
|
1388
1889
|
void (async () => {
|
|
1389
|
-
const source = (0,
|
|
1890
|
+
const source = (0, import_client10.asyncKeepalive)(
|
|
1390
1891
|
() => client.subscribe(storeWatchUpdatesDocument),
|
|
1391
1892
|
signal
|
|
1392
1893
|
);
|
|
1393
|
-
const events = (0,
|
|
1894
|
+
const events = (0, import_client10.mapAsync)(
|
|
1394
1895
|
source,
|
|
1395
1896
|
(data) => (data.contelloUpdatesBatch?.events ?? []).map((e) => mapEvent(e, resolver)).filter((e) => e !== void 0)
|
|
1396
1897
|
);
|
|
@@ -1421,26 +1922,27 @@ var Store = class {
|
|
|
1421
1922
|
_watcher;
|
|
1422
1923
|
_schema;
|
|
1423
1924
|
_cleanups = [];
|
|
1925
|
+
_refreshByTtl = createRefreshByTtlQueue();
|
|
1424
1926
|
/**
|
|
1425
1927
|
* Multicast update-batch stream from the watcher. Consume with `for await (const batch of updates$)`;
|
|
1426
1928
|
* each iteration starts an independent fan-out from the shared source.
|
|
1427
1929
|
*/
|
|
1428
1930
|
updates$;
|
|
1429
1931
|
constructor(options) {
|
|
1430
|
-
const { url, project, token, schema } = options;
|
|
1431
|
-
this._schema =
|
|
1432
|
-
this._client = (0,
|
|
1932
|
+
const { url, project, token, schema: schema2 } = options;
|
|
1933
|
+
this._schema = schema2;
|
|
1934
|
+
this._client = (0, import_client11.createContelloClient)({
|
|
1433
1935
|
url,
|
|
1434
1936
|
project,
|
|
1435
1937
|
token,
|
|
1436
|
-
schema,
|
|
1938
|
+
schema: schema2,
|
|
1437
1939
|
connections: options.connections,
|
|
1438
1940
|
onConnected: options.onConnected,
|
|
1439
1941
|
onReconnecting: options.onReconnecting,
|
|
1440
1942
|
onError: options.onError,
|
|
1441
1943
|
connectionEvents: options.connectionEvents
|
|
1442
1944
|
});
|
|
1443
|
-
this._resolver = new ModelResolver(
|
|
1945
|
+
this._resolver = new ModelResolver(schema2?.models);
|
|
1444
1946
|
this._watcher = createInternalWatcher(this._client, this._resolver);
|
|
1445
1947
|
this.updates$ = this._watcher.updates$;
|
|
1446
1948
|
this.ping = () => this._client.ping();
|
|
@@ -1482,7 +1984,8 @@ var Store = class {
|
|
|
1482
1984
|
options,
|
|
1483
1985
|
this._client,
|
|
1484
1986
|
this._watcher.updates$,
|
|
1485
|
-
this._resolver
|
|
1987
|
+
this._resolver,
|
|
1988
|
+
this._refreshByTtl
|
|
1486
1989
|
);
|
|
1487
1990
|
this._cleanups.push(destroy);
|
|
1488
1991
|
return instance;
|
|
@@ -1494,7 +1997,8 @@ var Store = class {
|
|
|
1494
1997
|
options,
|
|
1495
1998
|
this._client,
|
|
1496
1999
|
this._watcher.updates$,
|
|
1497
|
-
this._resolver
|
|
2000
|
+
this._resolver,
|
|
2001
|
+
this._refreshByTtl
|
|
1498
2002
|
);
|
|
1499
2003
|
this._cleanups.push(destroy);
|
|
1500
2004
|
return instance;
|
|
@@ -1507,7 +2011,8 @@ var Store = class {
|
|
|
1507
2011
|
options,
|
|
1508
2012
|
this._client,
|
|
1509
2013
|
this._watcher.updates$,
|
|
1510
|
-
this._resolver
|
|
2014
|
+
this._resolver,
|
|
2015
|
+
this._refreshByTtl
|
|
1511
2016
|
);
|
|
1512
2017
|
this._cleanups.push(destroy);
|
|
1513
2018
|
return instance;
|
|
@@ -1519,7 +2024,8 @@ var Store = class {
|
|
|
1519
2024
|
options,
|
|
1520
2025
|
this._client,
|
|
1521
2026
|
this._watcher.updates$,
|
|
1522
|
-
this._resolver
|
|
2027
|
+
this._resolver,
|
|
2028
|
+
this._refreshByTtl
|
|
1523
2029
|
);
|
|
1524
2030
|
this._cleanups.push(destroy);
|
|
1525
2031
|
return instance;
|
|
@@ -1538,18 +2044,67 @@ var Store = class {
|
|
|
1538
2044
|
return instance;
|
|
1539
2045
|
}
|
|
1540
2046
|
// --- assets / routes / i18n (no source — built-ins) ---
|
|
2047
|
+
// --- assets ---
|
|
1541
2048
|
defineAssets(options) {
|
|
1542
|
-
const { instance, destroy } = createAssetsCollection(
|
|
2049
|
+
const { instance, destroy } = createAssetsCollection(
|
|
2050
|
+
options,
|
|
2051
|
+
this._client,
|
|
2052
|
+
this._watcher.updates$,
|
|
2053
|
+
this._refreshByTtl
|
|
2054
|
+
);
|
|
2055
|
+
this._cleanups.push(destroy);
|
|
2056
|
+
return instance;
|
|
2057
|
+
}
|
|
2058
|
+
defineAssetsSync(options) {
|
|
2059
|
+
const { instance, destroy } = createAssetsSyncCollection(
|
|
2060
|
+
options,
|
|
2061
|
+
this._client,
|
|
2062
|
+
this._watcher.updates$,
|
|
2063
|
+
this._refreshByTtl
|
|
2064
|
+
);
|
|
2065
|
+
this._cleanups.push(destroy);
|
|
2066
|
+
return instance;
|
|
2067
|
+
}
|
|
2068
|
+
defineLazyAssets(options) {
|
|
2069
|
+
const { instance, destroy } = createLazyAssetsCollection(options, this._client, this._watcher.updates$);
|
|
1543
2070
|
this._cleanups.push(destroy);
|
|
1544
2071
|
return instance;
|
|
1545
2072
|
}
|
|
2073
|
+
// --- routes ---
|
|
1546
2074
|
defineRoutes(options) {
|
|
1547
|
-
const { instance, destroy } = createRoutesCollection(
|
|
2075
|
+
const { instance, destroy } = createRoutesCollection(
|
|
2076
|
+
options,
|
|
2077
|
+
this._client,
|
|
2078
|
+
this._watcher.updates$,
|
|
2079
|
+
this._resolver,
|
|
2080
|
+
this._refreshByTtl
|
|
2081
|
+
);
|
|
2082
|
+
this._cleanups.push(destroy);
|
|
2083
|
+
return instance;
|
|
2084
|
+
}
|
|
2085
|
+
defineRoutesSync(options) {
|
|
2086
|
+
const { instance, destroy } = createRoutesSyncCollection(
|
|
2087
|
+
options,
|
|
2088
|
+
this._client,
|
|
2089
|
+
this._watcher.updates$,
|
|
2090
|
+
this._resolver,
|
|
2091
|
+
this._refreshByTtl
|
|
2092
|
+
);
|
|
2093
|
+
this._cleanups.push(destroy);
|
|
2094
|
+
return instance;
|
|
2095
|
+
}
|
|
2096
|
+
defineLazyRoutes(options) {
|
|
2097
|
+
const { instance, destroy } = createLazyRoutesCollection(
|
|
2098
|
+
options,
|
|
2099
|
+
this._client,
|
|
2100
|
+
this._watcher.updates$,
|
|
2101
|
+
this._resolver
|
|
2102
|
+
);
|
|
1548
2103
|
this._cleanups.push(destroy);
|
|
1549
2104
|
return instance;
|
|
1550
2105
|
}
|
|
1551
2106
|
defineI18nMessages(def) {
|
|
1552
|
-
const result = createI18nMessagesCollection(def, this._client, this._watcher.updates
|
|
2107
|
+
const result = createI18nMessagesCollection(def, this._client, this._watcher.updates$, this._refreshByTtl);
|
|
1553
2108
|
this._cleanups.push(result.destroy);
|
|
1554
2109
|
return result;
|
|
1555
2110
|
}
|
|
@@ -1560,7 +2115,7 @@ function createStore(options) {
|
|
|
1560
2115
|
}
|
|
1561
2116
|
|
|
1562
2117
|
// src/index.ts
|
|
1563
|
-
var
|
|
2118
|
+
var import_projected9 = require("projected");
|
|
1564
2119
|
// Annotate the CommonJS export names for ESM import in node:
|
|
1565
2120
|
0 && (module.exports = {
|
|
1566
2121
|
Store,
|