@contello/store 2.0.1 → 2.1.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 +827 -292
- package/dist/index.d.cts +122 -26
- package/dist/index.d.ts +122 -26
- package/dist/index.js +858 -307
- package/package.json +4 -4
package/dist/index.js
CHANGED
|
@@ -3,9 +3,12 @@ import { createContelloClient } from "@contello/client";
|
|
|
3
3
|
|
|
4
4
|
// src/assets.ts
|
|
5
5
|
import {
|
|
6
|
-
|
|
6
|
+
collectAsync,
|
|
7
|
+
createSourceSubscription,
|
|
8
|
+
mapAsync,
|
|
9
|
+
runWithBackoff as runWithBackoff2
|
|
7
10
|
} from "@contello/client";
|
|
8
|
-
import {
|
|
11
|
+
import { ProjectedMap } from "projected";
|
|
9
12
|
|
|
10
13
|
// src/diagnostics.ts
|
|
11
14
|
import dc from "diagnostics_channel";
|
|
@@ -133,20 +136,6 @@ var StoreRouteFragmentSchema = `fragment StoreRoute on ContelloRoute {
|
|
|
133
136
|
value
|
|
134
137
|
}
|
|
135
138
|
}`;
|
|
136
|
-
var storeGetAssetsDocument = `${MediaFileFragmentSchema}
|
|
137
|
-
${MediaAssetFragmentSchema}
|
|
138
|
-
${StoreAssetFragmentSchema}
|
|
139
|
-
query StoreGetAssets($filter: ContelloAssetsFilterInput!) {
|
|
140
|
-
contelloAssets(filter: $filter) {
|
|
141
|
-
...StoreAsset
|
|
142
|
-
}
|
|
143
|
-
}`;
|
|
144
|
-
var storeGetI18nMessagesDocument = `${StoreI18nMessageFragmentSchema}
|
|
145
|
-
subscription StoreGetI18nMessages($collection: String!) {
|
|
146
|
-
contelloI18nMessagesBatch(collectionReferenceName: $collection) {
|
|
147
|
-
...StoreI18nMessage
|
|
148
|
-
}
|
|
149
|
-
}`;
|
|
150
139
|
var storeRegisterI18nMessagesDocument = `mutation StoreRegisterI18nMessages($collection: String!, $messages: [ContelloI18nMessageInput!]!) {
|
|
151
140
|
registerContelloI18nMessages(
|
|
152
141
|
collectionReferenceName: $collection
|
|
@@ -156,12 +145,6 @@ var storeRegisterI18nMessagesDocument = `mutation StoreRegisterI18nMessages($col
|
|
|
156
145
|
ids
|
|
157
146
|
}
|
|
158
147
|
}`;
|
|
159
|
-
var storeGetRoutesDocument = `${StoreRouteFragmentSchema}
|
|
160
|
-
query StoreGetRoutes($request: ContelloRoutesRequestInput!) {
|
|
161
|
-
contelloRoutes(request: $request) {
|
|
162
|
-
...StoreRoute
|
|
163
|
-
}
|
|
164
|
-
}`;
|
|
165
148
|
var storeWatchUpdatesDocument = `${StoreRouteFragmentSchema}
|
|
166
149
|
subscription StoreWatchUpdates {
|
|
167
150
|
contelloUpdatesBatch {
|
|
@@ -194,6 +177,10 @@ subscription StoreWatchUpdates {
|
|
|
194
177
|
}
|
|
195
178
|
}
|
|
196
179
|
}`;
|
|
180
|
+
var operations = {
|
|
181
|
+
storeRegisterI18nMessages: { document: storeRegisterI18nMessagesDocument, kind: "mutation" },
|
|
182
|
+
storeWatchUpdates: { document: storeWatchUpdatesDocument, kind: "subscription" }
|
|
183
|
+
};
|
|
197
184
|
var sources = {
|
|
198
185
|
storeAsset: {
|
|
199
186
|
document: `${MediaFileFragmentSchema}
|
|
@@ -219,55 +206,100 @@ ${StoreAssetFragmentSchema}`,
|
|
|
219
206
|
__cardinality: "route"
|
|
220
207
|
}
|
|
221
208
|
};
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
const { max, ttl, onEvict: dispose } = options;
|
|
227
|
-
const lru = new LRUCache({
|
|
228
|
-
max,
|
|
229
|
-
...ttl !== void 0 && { ttl, ttlAutopurge: false },
|
|
230
|
-
...dispose !== void 0 && { dispose }
|
|
231
|
-
});
|
|
232
|
-
return {
|
|
233
|
-
has: (key) => lru.has(key),
|
|
234
|
-
get: (key) => lru.get(key),
|
|
235
|
-
set: (key, value) => {
|
|
236
|
-
lru.set(key, value);
|
|
237
|
-
},
|
|
238
|
-
delete: (key) => {
|
|
239
|
-
lru.delete(key);
|
|
240
|
-
},
|
|
241
|
-
clear: () => {
|
|
242
|
-
lru.clear();
|
|
243
|
-
},
|
|
244
|
-
keys: () => [...lru.keys()]
|
|
245
|
-
};
|
|
246
|
-
}
|
|
209
|
+
var models = {
|
|
210
|
+
dummy: "DummyEntity"
|
|
211
|
+
};
|
|
212
|
+
var schema = { operations, sources, models };
|
|
247
213
|
|
|
248
214
|
// src/utils.ts
|
|
249
|
-
import { runWithBackoff } from "@contello/client";
|
|
215
|
+
import { createAsyncIterableSubject, runWithBackoff } from "@contello/client";
|
|
216
|
+
var DEFAULT_TTL_MS = 3 * 60 * 60 * 1e3;
|
|
217
|
+
function resolveTtl(ttl) {
|
|
218
|
+
if (ttl === void 0) return DEFAULT_TTL_MS;
|
|
219
|
+
if (ttl === false || ttl <= 0) return void 0;
|
|
220
|
+
return ttl;
|
|
221
|
+
}
|
|
250
222
|
function createRefresher(fn, onRefreshed, onStart2) {
|
|
251
223
|
let refreshing = false;
|
|
252
|
-
let
|
|
253
|
-
|
|
224
|
+
let inFlightKind;
|
|
225
|
+
let pendingKind;
|
|
226
|
+
function scheduleRefresh(kind) {
|
|
254
227
|
if (refreshing) {
|
|
255
|
-
|
|
228
|
+
pendingKind = { kind };
|
|
256
229
|
return;
|
|
257
230
|
}
|
|
258
231
|
refreshing = true;
|
|
232
|
+
inFlightKind = kind;
|
|
259
233
|
onStart2?.();
|
|
260
234
|
void runWithBackoff(fn).then(() => {
|
|
235
|
+
const finished = inFlightKind;
|
|
261
236
|
refreshing = false;
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
237
|
+
inFlightKind = void 0;
|
|
238
|
+
onRefreshed(finished);
|
|
239
|
+
if (pendingKind) {
|
|
240
|
+
const { kind: next } = pendingKind;
|
|
241
|
+
pendingKind = void 0;
|
|
242
|
+
scheduleRefresh(next);
|
|
266
243
|
}
|
|
267
244
|
});
|
|
268
245
|
}
|
|
269
246
|
return scheduleRefresh;
|
|
270
247
|
}
|
|
248
|
+
function createRefreshChannel(onRefresh) {
|
|
249
|
+
const stream$ = createAsyncIterableSubject();
|
|
250
|
+
return {
|
|
251
|
+
stream$,
|
|
252
|
+
emit(event) {
|
|
253
|
+
stream$.next(event);
|
|
254
|
+
onRefresh?.(event);
|
|
255
|
+
},
|
|
256
|
+
complete() {
|
|
257
|
+
stream$.complete();
|
|
258
|
+
}
|
|
259
|
+
};
|
|
260
|
+
}
|
|
261
|
+
function createTtlOrchestrator(options) {
|
|
262
|
+
let timer;
|
|
263
|
+
return {
|
|
264
|
+
mark() {
|
|
265
|
+
if (timer !== void 0) {
|
|
266
|
+
clearTimeout(timer);
|
|
267
|
+
timer = void 0;
|
|
268
|
+
}
|
|
269
|
+
if (options.ttl === void 0) {
|
|
270
|
+
return;
|
|
271
|
+
}
|
|
272
|
+
timer = setTimeout(options.run, options.ttl);
|
|
273
|
+
},
|
|
274
|
+
clear() {
|
|
275
|
+
if (timer !== void 0) {
|
|
276
|
+
clearTimeout(timer);
|
|
277
|
+
timer = void 0;
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
};
|
|
281
|
+
}
|
|
282
|
+
function createRefreshByTtlQueue() {
|
|
283
|
+
const waiting = [];
|
|
284
|
+
let running = false;
|
|
285
|
+
function next() {
|
|
286
|
+
if (running) return;
|
|
287
|
+
const task = waiting.shift();
|
|
288
|
+
if (!task) return;
|
|
289
|
+
running = true;
|
|
290
|
+
Promise.resolve().then(task).catch(() => {
|
|
291
|
+
}).finally(() => {
|
|
292
|
+
running = false;
|
|
293
|
+
next();
|
|
294
|
+
});
|
|
295
|
+
}
|
|
296
|
+
return {
|
|
297
|
+
enqueue(task) {
|
|
298
|
+
waiting.push(task);
|
|
299
|
+
next();
|
|
300
|
+
}
|
|
301
|
+
};
|
|
302
|
+
}
|
|
271
303
|
|
|
272
304
|
// src/assets.ts
|
|
273
305
|
function mapFile(raw) {
|
|
@@ -285,92 +317,139 @@ function mapAsset(raw) {
|
|
|
285
317
|
optimized: raw.optimized.map(mapFile)
|
|
286
318
|
};
|
|
287
319
|
}
|
|
288
|
-
function createAssetsCollection(
|
|
289
|
-
const
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
}
|
|
320
|
+
function createAssetsCollection(options, client, updates$, refreshByTtl) {
|
|
321
|
+
const opts = options ?? {};
|
|
322
|
+
const cache = {
|
|
323
|
+
ttl: resolveTtl(opts.cache?.ttl),
|
|
324
|
+
eviction: opts.cache?.eviction ?? "refresh"
|
|
294
325
|
};
|
|
295
|
-
const
|
|
296
|
-
const
|
|
326
|
+
const channel = createRefreshChannel(opts.onRefresh);
|
|
327
|
+
const ttl = createTtlOrchestrator({ ttl: cache.ttl, run: () => runFullRefresh("ttl") });
|
|
328
|
+
let loaded = false;
|
|
329
|
+
const assetsSourceDoc = createSourceSubscription(schema.sources.storeAsset);
|
|
330
|
+
const projected = new ProjectedMap({
|
|
297
331
|
key: (asset) => asset.id,
|
|
298
332
|
values: (ids) => wrap(
|
|
299
333
|
"assets",
|
|
300
|
-
() =>
|
|
301
|
-
(
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
334
|
+
() => collectAsync(
|
|
335
|
+
mapAsync(client.subscribe(assetsSourceDoc, { ids }), (data) => data.source)
|
|
336
|
+
).then((rawItems) => {
|
|
337
|
+
const items = rawItems.map(mapAsset);
|
|
338
|
+
if (ids === void 0 && !loaded) {
|
|
339
|
+
loaded = true;
|
|
340
|
+
ttl.mark();
|
|
341
|
+
opts.onLoad?.(items.map((a) => a.id));
|
|
342
|
+
}
|
|
343
|
+
return items;
|
|
344
|
+
})
|
|
345
|
+
)
|
|
346
|
+
});
|
|
347
|
+
function emit(ids, kind) {
|
|
348
|
+
channel.emit({ ids, kind });
|
|
349
|
+
}
|
|
350
|
+
function runFullRefresh(kind) {
|
|
351
|
+
refreshByTtl.enqueue(
|
|
352
|
+
() => runWithBackoff2(
|
|
353
|
+
() => projected.refresh().then((map) => {
|
|
354
|
+
emit([...map.keys()], kind);
|
|
355
|
+
ttl.mark();
|
|
356
|
+
})
|
|
307
357
|
)
|
|
308
|
-
)
|
|
309
|
-
|
|
358
|
+
);
|
|
359
|
+
}
|
|
360
|
+
function runPartialRefresh(refreshIds, deletedIds) {
|
|
361
|
+
const changedIds = [.../* @__PURE__ */ new Set([...refreshIds, ...deletedIds])];
|
|
362
|
+
if (refreshIds.length === 0) {
|
|
363
|
+
emit(changedIds, "upstream-update");
|
|
364
|
+
return;
|
|
365
|
+
}
|
|
366
|
+
void runWithBackoff2(
|
|
367
|
+
() => projected.refresh(refreshIds).then(() => {
|
|
368
|
+
emit(changedIds, "upstream-update");
|
|
369
|
+
})
|
|
370
|
+
);
|
|
371
|
+
}
|
|
372
|
+
const unsubUpdates = updates$.subscribe((batch) => {
|
|
373
|
+
if (!loaded || batch.asset.length === 0) {
|
|
374
|
+
return;
|
|
375
|
+
}
|
|
376
|
+
const deleted = [];
|
|
377
|
+
const upserted = /* @__PURE__ */ new Set();
|
|
378
|
+
for (const event of batch.asset) {
|
|
379
|
+
if (event.mutation === "delete") {
|
|
380
|
+
deleted.push(event.id);
|
|
381
|
+
} else {
|
|
382
|
+
upserted.add(event.id);
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
if (cache.eviction === "clear") {
|
|
386
|
+
projected.clear();
|
|
387
|
+
return;
|
|
388
|
+
}
|
|
389
|
+
if (deleted.length > 0) {
|
|
390
|
+
projected.delete(deleted);
|
|
391
|
+
}
|
|
392
|
+
runPartialRefresh([...upserted], deleted);
|
|
310
393
|
});
|
|
311
|
-
const
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
if (lastRefreshKeys.length === 0) {
|
|
317
|
-
return;
|
|
394
|
+
const instance = {
|
|
395
|
+
refresh$: channel.stream$,
|
|
396
|
+
async load() {
|
|
397
|
+
if (!loaded) {
|
|
398
|
+
await projected.getAllAsMap();
|
|
318
399
|
}
|
|
319
|
-
await projected.refresh(lastRefreshKeys);
|
|
320
400
|
},
|
|
321
|
-
()
|
|
322
|
-
|
|
323
|
-
refresh$.next(lastRefreshKeys);
|
|
324
|
-
}
|
|
401
|
+
get(idOrIds) {
|
|
402
|
+
return projected.get(idOrIds);
|
|
325
403
|
},
|
|
326
|
-
()
|
|
404
|
+
upload(data, meta, options2) {
|
|
405
|
+
return client.upload(data, meta, options2);
|
|
406
|
+
},
|
|
407
|
+
download(fileId) {
|
|
408
|
+
return client.download(fileId);
|
|
409
|
+
},
|
|
410
|
+
proxyHls(path, signal) {
|
|
411
|
+
return client.proxyHls(path, signal);
|
|
412
|
+
},
|
|
413
|
+
refresh() {
|
|
414
|
+
runFullRefresh("on-demand");
|
|
327
415
|
}
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
416
|
+
};
|
|
417
|
+
return {
|
|
418
|
+
instance,
|
|
419
|
+
destroy() {
|
|
420
|
+
unsubUpdates();
|
|
421
|
+
ttl.clear();
|
|
422
|
+
channel.complete();
|
|
333
423
|
}
|
|
334
|
-
|
|
335
|
-
|
|
424
|
+
};
|
|
425
|
+
}
|
|
426
|
+
function createAssetsSyncCollection(options, client, updates$, refreshByTtl) {
|
|
427
|
+
const { instance: base, destroy } = createAssetsCollection(options, client, updates$, refreshByTtl);
|
|
428
|
+
function assertSync(value, method) {
|
|
429
|
+
if (value instanceof Promise) {
|
|
430
|
+
throw new Error(`assets.${method}() is not initialized yet \u2014 call assets.load() first`);
|
|
431
|
+
}
|
|
432
|
+
return value;
|
|
433
|
+
}
|
|
336
434
|
return {
|
|
337
435
|
instance: {
|
|
338
|
-
|
|
436
|
+
...base,
|
|
339
437
|
get(idOrIds) {
|
|
340
|
-
return
|
|
341
|
-
},
|
|
342
|
-
upload(data, meta, options) {
|
|
343
|
-
return client.upload(data, meta, options);
|
|
344
|
-
},
|
|
345
|
-
download(fileId) {
|
|
346
|
-
return client.download(fileId);
|
|
347
|
-
},
|
|
348
|
-
proxyHls(path, signal) {
|
|
349
|
-
return client.proxyHls(path, signal);
|
|
350
|
-
},
|
|
351
|
-
refresh() {
|
|
352
|
-
scheduleRefresh();
|
|
353
|
-
},
|
|
354
|
-
clear() {
|
|
355
|
-
projected.clear();
|
|
438
|
+
return assertSync(base.get(idOrIds), "get");
|
|
356
439
|
}
|
|
357
440
|
},
|
|
358
|
-
destroy
|
|
359
|
-
unsubUpdates();
|
|
360
|
-
refresh$.complete();
|
|
361
|
-
}
|
|
441
|
+
destroy
|
|
362
442
|
};
|
|
363
443
|
}
|
|
364
444
|
|
|
365
445
|
// src/collection.ts
|
|
366
446
|
import {
|
|
367
|
-
collectAsync,
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
runWithBackoff as runWithBackoff2
|
|
447
|
+
collectAsync as collectAsync2,
|
|
448
|
+
createSourceSubscription as createSourceSubscription2,
|
|
449
|
+
mapAsync as mapAsync2,
|
|
450
|
+
runWithBackoff as runWithBackoff3
|
|
372
451
|
} from "@contello/client";
|
|
373
|
-
import { ProjectedMap, maybeThen } from "projected";
|
|
452
|
+
import { ProjectedMap as ProjectedMap2, maybeThen } from "projected";
|
|
374
453
|
|
|
375
454
|
// src/dependency-collector.ts
|
|
376
455
|
function createDependencyKey(target, id) {
|
|
@@ -526,27 +605,29 @@ var DependencyCollector = class {
|
|
|
526
605
|
|
|
527
606
|
// src/collection.ts
|
|
528
607
|
function fetchCollection(source, client, ids) {
|
|
529
|
-
return
|
|
530
|
-
|
|
531
|
-
client.subscribe(
|
|
608
|
+
return collectAsync2(
|
|
609
|
+
mapAsync2(
|
|
610
|
+
client.subscribe(createSourceSubscription2(source), { ids }),
|
|
532
611
|
(r) => r.source
|
|
533
612
|
)
|
|
534
613
|
);
|
|
535
614
|
}
|
|
536
|
-
function createCollection(source, options, client, updates$, resolver) {
|
|
615
|
+
function createCollection(source, options, client, updates$, resolver, refreshByTtl) {
|
|
537
616
|
const opts = options ?? {};
|
|
538
617
|
const mapFn = opts.map ?? ((item) => item);
|
|
539
618
|
const _def = {
|
|
540
619
|
name: opts.name ?? source.__model,
|
|
541
620
|
model: source.__model,
|
|
542
621
|
cache: {
|
|
543
|
-
ttl: opts.cache?.ttl,
|
|
622
|
+
ttl: resolveTtl(opts.cache?.ttl),
|
|
544
623
|
eviction: opts.cache?.eviction ?? "refresh"
|
|
545
624
|
}
|
|
546
625
|
};
|
|
547
626
|
const dependencyCollector = new DependencyCollector(_def.model, resolver);
|
|
548
|
-
|
|
549
|
-
const
|
|
627
|
+
const channel = createRefreshChannel(opts.onRefresh);
|
|
628
|
+
const ttl = createTtlOrchestrator({ ttl: _def.cache.ttl, run: () => runFullRefresh("ttl") });
|
|
629
|
+
let loaded = false;
|
|
630
|
+
const projected = new ProjectedMap2({
|
|
550
631
|
key: (item) => item.id,
|
|
551
632
|
values: (ids) => wrap(
|
|
552
633
|
`collection:${_def.name}`,
|
|
@@ -564,6 +645,11 @@ function createCollection(source, options, client, updates$, resolver) {
|
|
|
564
645
|
).then((items) => {
|
|
565
646
|
if (ids === void 0) {
|
|
566
647
|
dependencyCollector.retainOnly(new Set(items.map((item) => item.id)));
|
|
648
|
+
if (!loaded) {
|
|
649
|
+
loaded = true;
|
|
650
|
+
ttl.mark();
|
|
651
|
+
opts.onLoad?.(items.map((item) => item.id));
|
|
652
|
+
}
|
|
567
653
|
} else {
|
|
568
654
|
const returnedIds = new Set(items.map((item) => item.id));
|
|
569
655
|
for (const id of ids) {
|
|
@@ -578,38 +664,28 @@ function createCollection(source, options, client, updates$, resolver) {
|
|
|
578
664
|
),
|
|
579
665
|
sort: opts.sort
|
|
580
666
|
});
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
function scheduleTtl() {
|
|
584
|
-
if (_def.cache.ttl === void 0) {
|
|
585
|
-
return;
|
|
586
|
-
}
|
|
587
|
-
clearTimeout(ttlTimer);
|
|
588
|
-
ttlTimer = setTimeout(() => {
|
|
589
|
-
runFullRefresh();
|
|
590
|
-
}, _def.cache.ttl);
|
|
667
|
+
function emit(ids, kind) {
|
|
668
|
+
channel.emit({ ids, kind });
|
|
591
669
|
}
|
|
592
|
-
function runFullRefresh() {
|
|
593
|
-
|
|
594
|
-
() =>
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
670
|
+
function runFullRefresh(kind) {
|
|
671
|
+
refreshByTtl.enqueue(
|
|
672
|
+
() => runWithBackoff3(
|
|
673
|
+
() => projected.refresh().then((map) => {
|
|
674
|
+
emit([...map.keys()], kind);
|
|
675
|
+
ttl.mark();
|
|
676
|
+
})
|
|
677
|
+
)
|
|
600
678
|
);
|
|
601
679
|
}
|
|
602
680
|
function runPartialRefresh(refreshIds, deletedIds) {
|
|
603
681
|
const changedIds = [.../* @__PURE__ */ new Set([...refreshIds, ...deletedIds])];
|
|
604
682
|
if (refreshIds.length === 0) {
|
|
605
|
-
|
|
606
|
-
opts.onRefresh?.(changedIds);
|
|
683
|
+
emit(changedIds, "upstream-update");
|
|
607
684
|
return;
|
|
608
685
|
}
|
|
609
|
-
void
|
|
686
|
+
void runWithBackoff3(
|
|
610
687
|
() => projected.refresh(refreshIds).then(() => {
|
|
611
|
-
|
|
612
|
-
opts.onRefresh?.(changedIds);
|
|
688
|
+
emit(changedIds, "upstream-update");
|
|
613
689
|
})
|
|
614
690
|
);
|
|
615
691
|
}
|
|
@@ -655,7 +731,7 @@ function createCollection(source, options, client, updates$, resolver) {
|
|
|
655
731
|
});
|
|
656
732
|
const instance = {
|
|
657
733
|
name: _def.name,
|
|
658
|
-
refresh$,
|
|
734
|
+
refresh$: channel.stream$,
|
|
659
735
|
get(idOrIds) {
|
|
660
736
|
return projected.get(idOrIds);
|
|
661
737
|
},
|
|
@@ -663,31 +739,31 @@ function createCollection(source, options, client, updates$, resolver) {
|
|
|
663
739
|
return projected.getAll();
|
|
664
740
|
},
|
|
665
741
|
refresh() {
|
|
666
|
-
runFullRefresh();
|
|
742
|
+
runFullRefresh("on-demand");
|
|
667
743
|
},
|
|
668
744
|
async load() {
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
opts.onLoad?.([...map.keys()]);
|
|
745
|
+
if (!loaded) {
|
|
746
|
+
await projected.getAllAsMap();
|
|
747
|
+
}
|
|
673
748
|
}
|
|
674
749
|
};
|
|
675
750
|
return {
|
|
676
751
|
instance,
|
|
677
752
|
destroy() {
|
|
678
753
|
unsubUpdates();
|
|
679
|
-
|
|
680
|
-
|
|
754
|
+
ttl.clear();
|
|
755
|
+
channel.complete();
|
|
681
756
|
}
|
|
682
757
|
};
|
|
683
758
|
}
|
|
684
|
-
function createCollectionSync(source, options, client, updates$, resolver) {
|
|
759
|
+
function createCollectionSync(source, options, client, updates$, resolver, refreshByTtl) {
|
|
685
760
|
const { instance: base, destroy } = createCollection(
|
|
686
761
|
source,
|
|
687
762
|
options,
|
|
688
763
|
client,
|
|
689
764
|
updates$,
|
|
690
|
-
resolver
|
|
765
|
+
resolver,
|
|
766
|
+
refreshByTtl
|
|
691
767
|
);
|
|
692
768
|
function assertSync(value, method) {
|
|
693
769
|
if (value instanceof Promise) {
|
|
@@ -711,11 +787,12 @@ function createCollectionSync(source, options, client, updates$, resolver) {
|
|
|
711
787
|
|
|
712
788
|
// src/i18n.ts
|
|
713
789
|
import {
|
|
714
|
-
collectAsync as
|
|
715
|
-
|
|
716
|
-
mapAsync as
|
|
790
|
+
collectAsync as collectAsync3,
|
|
791
|
+
createSourceSubscription as createSourceSubscription3,
|
|
792
|
+
mapAsync as mapAsync3,
|
|
793
|
+
runWithBackoff as runWithBackoff4
|
|
717
794
|
} from "@contello/client";
|
|
718
|
-
import { ProjectedMap as
|
|
795
|
+
import { ProjectedMap as ProjectedMap3, maybeThen as maybeThen2 } from "projected";
|
|
719
796
|
function toGqlMessageInput(msg) {
|
|
720
797
|
return {
|
|
721
798
|
token: msg.token,
|
|
@@ -726,45 +803,103 @@ function toGqlMessageInput(msg) {
|
|
|
726
803
|
initialTranslations: msg.initialTranslations?.map((t) => ({ language: t.language, message: t.message }))
|
|
727
804
|
};
|
|
728
805
|
}
|
|
729
|
-
function createI18nMessagesCollection(def, client, updates
|
|
730
|
-
const
|
|
806
|
+
function createI18nMessagesCollection(def, client, updates$, refreshByTtl) {
|
|
807
|
+
const channel = createRefreshChannel(def.onRefresh);
|
|
808
|
+
const ttl = createTtlOrchestrator({ ttl: resolveTtl(def.cache?.ttl), run: () => runTtlRefresh() });
|
|
809
|
+
let loaded = false;
|
|
810
|
+
const i18nSourceDoc = createSourceSubscription3(schema.sources.storeI18nMessage);
|
|
811
|
+
const projected = new ProjectedMap3({
|
|
731
812
|
key: (msg) => msg.id,
|
|
732
|
-
values: () => wrap(
|
|
813
|
+
values: (ids) => wrap(
|
|
733
814
|
`i18n:${def.collection}`,
|
|
734
|
-
() =>
|
|
735
|
-
|
|
736
|
-
client.subscribe(
|
|
737
|
-
collection: def.collection
|
|
815
|
+
() => collectAsync3(
|
|
816
|
+
mapAsync3(
|
|
817
|
+
client.subscribe(i18nSourceDoc, {
|
|
818
|
+
collection: def.collection,
|
|
819
|
+
ids
|
|
738
820
|
}),
|
|
739
|
-
(data) => data.
|
|
821
|
+
(data) => data.source
|
|
740
822
|
)
|
|
741
|
-
).then(
|
|
742
|
-
|
|
823
|
+
).then((msgs) => {
|
|
824
|
+
const items = msgs.map((msg) => ({
|
|
743
825
|
id: msg.id,
|
|
744
826
|
token: msg.token,
|
|
745
827
|
translations: new Map(msg.translations.map((t) => [t.language, t.value]))
|
|
746
|
-
}))
|
|
747
|
-
|
|
828
|
+
}));
|
|
829
|
+
if (ids === void 0 && !loaded) {
|
|
830
|
+
loaded = true;
|
|
831
|
+
ttl.mark();
|
|
832
|
+
}
|
|
833
|
+
return items;
|
|
834
|
+
})
|
|
748
835
|
)
|
|
749
836
|
});
|
|
750
|
-
|
|
837
|
+
function emit(ids, kind) {
|
|
838
|
+
channel.emit({ ids, kind });
|
|
839
|
+
}
|
|
840
|
+
function emitWithCurrentIds(kind) {
|
|
841
|
+
maybeThen2(projected.getAll(), (msgs) => {
|
|
842
|
+
emit(
|
|
843
|
+
msgs.map((m) => m.id),
|
|
844
|
+
kind
|
|
845
|
+
);
|
|
846
|
+
});
|
|
847
|
+
}
|
|
848
|
+
function runTtlRefresh() {
|
|
849
|
+
refreshByTtl.enqueue(
|
|
850
|
+
() => runWithBackoff4(() => projected.refresh()).then(() => {
|
|
851
|
+
emitWithCurrentIds("ttl");
|
|
852
|
+
ttl.mark();
|
|
853
|
+
})
|
|
854
|
+
);
|
|
855
|
+
}
|
|
751
856
|
const scheduleRefresh = createRefresher(
|
|
752
857
|
() => projected.refresh(),
|
|
753
|
-
() => {
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
});
|
|
858
|
+
(kind) => {
|
|
859
|
+
emitWithCurrentIds(kind);
|
|
860
|
+
ttl.mark();
|
|
757
861
|
},
|
|
758
862
|
() => {
|
|
759
863
|
}
|
|
760
864
|
);
|
|
865
|
+
function runPartialRefresh(upsertedIds, deletedIds) {
|
|
866
|
+
const changedIds = [.../* @__PURE__ */ new Set([...upsertedIds, ...deletedIds])];
|
|
867
|
+
if (changedIds.length === 0) {
|
|
868
|
+
return;
|
|
869
|
+
}
|
|
870
|
+
if (upsertedIds.length === 0) {
|
|
871
|
+
emit(changedIds, "upstream-update");
|
|
872
|
+
return;
|
|
873
|
+
}
|
|
874
|
+
void runWithBackoff4(
|
|
875
|
+
() => projected.refresh(upsertedIds).then(() => {
|
|
876
|
+
emit(changedIds, "upstream-update");
|
|
877
|
+
})
|
|
878
|
+
);
|
|
879
|
+
}
|
|
761
880
|
const unsubUpdates = updates$.subscribe((batch) => {
|
|
762
|
-
if (
|
|
763
|
-
|
|
881
|
+
if (!loaded) {
|
|
882
|
+
return;
|
|
883
|
+
}
|
|
884
|
+
if (batch.i18nMessage.length === 0) {
|
|
885
|
+
return;
|
|
764
886
|
}
|
|
887
|
+
const deleted = [];
|
|
888
|
+
const upserted = /* @__PURE__ */ new Set();
|
|
889
|
+
for (const event of batch.i18nMessage) {
|
|
890
|
+
if (event.mutation === "delete") {
|
|
891
|
+
deleted.push(event.id);
|
|
892
|
+
} else {
|
|
893
|
+
upserted.add(event.id);
|
|
894
|
+
}
|
|
895
|
+
}
|
|
896
|
+
if (deleted.length > 0) {
|
|
897
|
+
projected.delete(deleted);
|
|
898
|
+
}
|
|
899
|
+
runPartialRefresh([...upserted], deleted);
|
|
765
900
|
});
|
|
766
901
|
return {
|
|
767
|
-
refresh$,
|
|
902
|
+
refresh$: channel.stream$,
|
|
768
903
|
get(idOrIds) {
|
|
769
904
|
return projected.get(idOrIds);
|
|
770
905
|
},
|
|
@@ -781,19 +916,151 @@ function createI18nMessagesCollection(def, client, updates$) {
|
|
|
781
916
|
})
|
|
782
917
|
);
|
|
783
918
|
},
|
|
919
|
+
refresh() {
|
|
920
|
+
scheduleRefresh("on-demand");
|
|
921
|
+
},
|
|
922
|
+
destroy() {
|
|
923
|
+
unsubUpdates();
|
|
924
|
+
ttl.clear();
|
|
925
|
+
channel.complete();
|
|
926
|
+
}
|
|
927
|
+
};
|
|
928
|
+
}
|
|
929
|
+
|
|
930
|
+
// src/lazy-assets.ts
|
|
931
|
+
import {
|
|
932
|
+
collectAsync as collectAsync4,
|
|
933
|
+
createSourceSubscription as createSourceSubscription4,
|
|
934
|
+
mapAsync as mapAsync4
|
|
935
|
+
} from "@contello/client";
|
|
936
|
+
import { ProjectedLazyMap } from "projected";
|
|
937
|
+
|
|
938
|
+
// src/lru.ts
|
|
939
|
+
import { LRUCache } from "lru-cache";
|
|
940
|
+
function createLruCache(options) {
|
|
941
|
+
const { max, ttl, onEvict: dispose } = options;
|
|
942
|
+
const lru = new LRUCache({
|
|
943
|
+
max,
|
|
944
|
+
...ttl !== void 0 && { ttl, ttlAutopurge: false },
|
|
945
|
+
...dispose !== void 0 && { dispose }
|
|
946
|
+
});
|
|
947
|
+
return {
|
|
948
|
+
has: (key) => lru.has(key),
|
|
949
|
+
get: (key) => lru.get(key),
|
|
950
|
+
set: (key, value) => {
|
|
951
|
+
lru.set(key, value);
|
|
952
|
+
},
|
|
953
|
+
delete: (key) => {
|
|
954
|
+
lru.delete(key);
|
|
955
|
+
},
|
|
956
|
+
clear: () => {
|
|
957
|
+
lru.clear();
|
|
958
|
+
},
|
|
959
|
+
keys: () => [...lru.keys()]
|
|
960
|
+
};
|
|
961
|
+
}
|
|
962
|
+
|
|
963
|
+
// src/lazy-assets.ts
|
|
964
|
+
function mapFile2(raw) {
|
|
965
|
+
return {
|
|
966
|
+
uid: raw.uid,
|
|
967
|
+
mimeType: raw.mimeType,
|
|
968
|
+
metadata: raw.metadata ? { width: raw.metadata.width, height: raw.metadata.height } : void 0
|
|
969
|
+
};
|
|
970
|
+
}
|
|
971
|
+
function mapAsset2(raw) {
|
|
972
|
+
return {
|
|
973
|
+
id: raw.id,
|
|
974
|
+
original: mapFile2(raw.original),
|
|
975
|
+
preview: raw.preview ? mapFile2(raw.preview) : void 0,
|
|
976
|
+
optimized: raw.optimized.map(mapFile2)
|
|
977
|
+
};
|
|
978
|
+
}
|
|
979
|
+
function createLazyAssetsCollection(def, client, updates$) {
|
|
980
|
+
const _def = {
|
|
981
|
+
cache: {
|
|
982
|
+
max: def?.cache?.max ?? 1e3,
|
|
983
|
+
ttl: resolveTtl(def?.cache?.ttl)
|
|
984
|
+
}
|
|
985
|
+
};
|
|
986
|
+
const cache = createLruCache({ max: _def.cache.max, ttl: _def.cache.ttl, onEvict: void 0 });
|
|
987
|
+
const assetsSourceDoc = createSourceSubscription4(schema.sources.storeAsset);
|
|
988
|
+
const projected = new ProjectedLazyMap({
|
|
989
|
+
key: (asset) => asset.id,
|
|
990
|
+
values: (ids) => wrap(
|
|
991
|
+
"assets",
|
|
992
|
+
() => collectAsync4(
|
|
993
|
+
mapAsync4(client.subscribe(assetsSourceDoc, { ids }), (data) => data.source)
|
|
994
|
+
).then((rawItems) => rawItems.map(mapAsset2))
|
|
995
|
+
),
|
|
996
|
+
cache
|
|
997
|
+
});
|
|
998
|
+
const channel = createRefreshChannel(def?.onRefresh);
|
|
999
|
+
function emit(ids, kind) {
|
|
1000
|
+
channel.emit({ ids, kind });
|
|
1001
|
+
}
|
|
1002
|
+
let lastRefreshKeys = [];
|
|
1003
|
+
const scheduleRefresh = createRefresher(
|
|
1004
|
+
async () => {
|
|
1005
|
+
lastRefreshKeys = cache.keys();
|
|
1006
|
+
if (lastRefreshKeys.length === 0) {
|
|
1007
|
+
return;
|
|
1008
|
+
}
|
|
1009
|
+
await projected.refresh(lastRefreshKeys);
|
|
1010
|
+
},
|
|
1011
|
+
(kind) => {
|
|
1012
|
+
if (lastRefreshKeys.length > 0) {
|
|
1013
|
+
emit(lastRefreshKeys, kind);
|
|
1014
|
+
}
|
|
1015
|
+
},
|
|
1016
|
+
() => {
|
|
1017
|
+
}
|
|
1018
|
+
);
|
|
1019
|
+
const unsubUpdates = updates$.subscribe((batch) => {
|
|
1020
|
+
if (batch.asset.length === 0) return;
|
|
1021
|
+
for (const event of batch.asset) {
|
|
1022
|
+
projected.delete(event.id);
|
|
1023
|
+
}
|
|
1024
|
+
emit(
|
|
1025
|
+
batch.asset.map((e) => e.id),
|
|
1026
|
+
"upstream-update"
|
|
1027
|
+
);
|
|
1028
|
+
});
|
|
1029
|
+
return {
|
|
1030
|
+
instance: {
|
|
1031
|
+
refresh$: channel.stream$,
|
|
1032
|
+
get(idOrIds) {
|
|
1033
|
+
return projected.get(idOrIds);
|
|
1034
|
+
},
|
|
1035
|
+
upload(data, meta, options) {
|
|
1036
|
+
return client.upload(data, meta, options);
|
|
1037
|
+
},
|
|
1038
|
+
download(fileId) {
|
|
1039
|
+
return client.download(fileId);
|
|
1040
|
+
},
|
|
1041
|
+
proxyHls(path, signal) {
|
|
1042
|
+
return client.proxyHls(path, signal);
|
|
1043
|
+
},
|
|
1044
|
+
refresh() {
|
|
1045
|
+
scheduleRefresh("on-demand");
|
|
1046
|
+
},
|
|
1047
|
+
clear() {
|
|
1048
|
+
projected.clear();
|
|
1049
|
+
}
|
|
1050
|
+
},
|
|
784
1051
|
destroy() {
|
|
785
1052
|
unsubUpdates();
|
|
786
|
-
|
|
1053
|
+
channel.complete();
|
|
787
1054
|
}
|
|
788
1055
|
};
|
|
789
1056
|
}
|
|
790
1057
|
|
|
791
1058
|
// src/lazy-collection.ts
|
|
792
1059
|
import {
|
|
793
|
-
collectAsync as
|
|
794
|
-
createAsyncIterableSubject as
|
|
795
|
-
createSourceSubscription as
|
|
796
|
-
mapAsync as
|
|
1060
|
+
collectAsync as collectAsync5,
|
|
1061
|
+
createAsyncIterableSubject as createAsyncIterableSubject2,
|
|
1062
|
+
createSourceSubscription as createSourceSubscription5,
|
|
1063
|
+
mapAsync as mapAsync5
|
|
797
1064
|
} from "@contello/client";
|
|
798
1065
|
import { ProjectedLazyMap as ProjectedLazyMap2, maybeThen as maybeThen3 } from "projected";
|
|
799
1066
|
function createLazyCollection(source, options, client, updates$, resolver) {
|
|
@@ -804,7 +1071,7 @@ function createLazyCollection(source, options, client, updates$, resolver) {
|
|
|
804
1071
|
model: source.__model,
|
|
805
1072
|
cache: {
|
|
806
1073
|
max: opts.cache?.max ?? 1e3,
|
|
807
|
-
ttl: opts.cache?.ttl
|
|
1074
|
+
ttl: resolveTtl(opts.cache?.ttl)
|
|
808
1075
|
}
|
|
809
1076
|
};
|
|
810
1077
|
const dependencyCollector = new DependencyCollector(_def.model, resolver);
|
|
@@ -818,9 +1085,9 @@ function createLazyCollection(source, options, client, updates$, resolver) {
|
|
|
818
1085
|
values: (keys) => wrap(
|
|
819
1086
|
`lazy-collection:${_def.name}`,
|
|
820
1087
|
() => maybeThen3(
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
client.subscribe(
|
|
1088
|
+
collectAsync5(
|
|
1089
|
+
mapAsync5(
|
|
1090
|
+
client.subscribe(createSourceSubscription5(source), {
|
|
824
1091
|
ids: keys
|
|
825
1092
|
}),
|
|
826
1093
|
(r) => r.source
|
|
@@ -840,7 +1107,12 @@ function createLazyCollection(source, options, client, updates$, resolver) {
|
|
|
840
1107
|
),
|
|
841
1108
|
cache
|
|
842
1109
|
});
|
|
843
|
-
const refresh$ =
|
|
1110
|
+
const refresh$ = createAsyncIterableSubject2();
|
|
1111
|
+
function emit(ids, kind) {
|
|
1112
|
+
const event = { ids, kind };
|
|
1113
|
+
refresh$.next(event);
|
|
1114
|
+
opts.onRefresh?.(event);
|
|
1115
|
+
}
|
|
844
1116
|
let lastRefreshKeys = [];
|
|
845
1117
|
const scheduleRefresh = createRefresher(
|
|
846
1118
|
async () => {
|
|
@@ -850,10 +1122,9 @@ function createLazyCollection(source, options, client, updates$, resolver) {
|
|
|
850
1122
|
}
|
|
851
1123
|
await projected.refresh(lastRefreshKeys);
|
|
852
1124
|
},
|
|
853
|
-
() => {
|
|
1125
|
+
(kind) => {
|
|
854
1126
|
if (lastRefreshKeys.length > 0) {
|
|
855
|
-
|
|
856
|
-
opts.onRefresh?.(lastRefreshKeys);
|
|
1127
|
+
emit(lastRefreshKeys, kind);
|
|
857
1128
|
}
|
|
858
1129
|
},
|
|
859
1130
|
() => {
|
|
@@ -877,12 +1148,9 @@ function createLazyCollection(source, options, client, updates$, resolver) {
|
|
|
877
1148
|
dependencyCollector.removeItem(key);
|
|
878
1149
|
projected.delete(key);
|
|
879
1150
|
}
|
|
880
|
-
|
|
881
|
-
refresh$.next(ids);
|
|
882
|
-
opts.onRefresh?.(ids);
|
|
1151
|
+
emit([...evicted], "upstream-update");
|
|
883
1152
|
}
|
|
884
1153
|
});
|
|
885
|
-
void scheduleRefresh;
|
|
886
1154
|
return {
|
|
887
1155
|
instance: {
|
|
888
1156
|
name: _def.name,
|
|
@@ -891,7 +1159,7 @@ function createLazyCollection(source, options, client, updates$, resolver) {
|
|
|
891
1159
|
return projected.get(idOrIds);
|
|
892
1160
|
},
|
|
893
1161
|
refresh() {
|
|
894
|
-
scheduleRefresh();
|
|
1162
|
+
scheduleRefresh("on-demand");
|
|
895
1163
|
},
|
|
896
1164
|
clear() {
|
|
897
1165
|
dependencyCollector.clear();
|
|
@@ -905,46 +1173,12 @@ function createLazyCollection(source, options, client, updates$, resolver) {
|
|
|
905
1173
|
};
|
|
906
1174
|
}
|
|
907
1175
|
|
|
908
|
-
// src/
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
const toModel = /* @__PURE__ */ new Map();
|
|
915
|
-
const toTypeName = /* @__PURE__ */ new Map();
|
|
916
|
-
let typeNames;
|
|
917
|
-
if (models) {
|
|
918
|
-
typeNames = /* @__PURE__ */ new Set();
|
|
919
|
-
for (const [model, typeName] of Object.entries(models)) {
|
|
920
|
-
toModel.set(typeName, model);
|
|
921
|
-
toTypeName.set(model, typeName);
|
|
922
|
-
typeNames.add(typeName);
|
|
923
|
-
}
|
|
924
|
-
}
|
|
925
|
-
this._typeNames = typeNames;
|
|
926
|
-
this._toModel = toModel;
|
|
927
|
-
this._toTypeName = toTypeName;
|
|
928
|
-
}
|
|
929
|
-
hasTypeName(typeName) {
|
|
930
|
-
return !this._typeNames || this._typeNames.has(typeName);
|
|
931
|
-
}
|
|
932
|
-
getModel(typeName) {
|
|
933
|
-
return this._toModel.get(typeName);
|
|
934
|
-
}
|
|
935
|
-
getTypeName(model) {
|
|
936
|
-
return this._toTypeName.get(model) ?? model;
|
|
937
|
-
}
|
|
938
|
-
hasModel(model) {
|
|
939
|
-
return !this._toTypeName.size || this._toTypeName.has(model);
|
|
940
|
-
}
|
|
941
|
-
resolveModel(typeName) {
|
|
942
|
-
return this._toModel.get(typeName) ?? typeName;
|
|
943
|
-
}
|
|
944
|
-
};
|
|
945
|
-
|
|
946
|
-
// src/routes.ts
|
|
947
|
-
import { createAsyncIterableSubject as createAsyncIterableSubject5 } from "@contello/client";
|
|
1176
|
+
// src/lazy-routes.ts
|
|
1177
|
+
import {
|
|
1178
|
+
collectAsync as collectAsync6,
|
|
1179
|
+
createSourceSubscription as createSourceSubscription6,
|
|
1180
|
+
mapAsync as mapAsync6
|
|
1181
|
+
} from "@contello/client";
|
|
948
1182
|
import { ProjectedLazyMap as ProjectedLazyMap3 } from "projected";
|
|
949
1183
|
|
|
950
1184
|
// src/routes-mapping.ts
|
|
@@ -996,15 +1230,12 @@ function mapRoute(raw, resolver) {
|
|
|
996
1230
|
}
|
|
997
1231
|
}
|
|
998
1232
|
|
|
999
|
-
// src/routes.ts
|
|
1233
|
+
// src/lazy-routes.ts
|
|
1000
1234
|
var ID_PREFIX = "1\0";
|
|
1001
1235
|
var PATH_PREFIX = "2\0";
|
|
1002
1236
|
var CACHE_KEY = /* @__PURE__ */ Symbol("cacheKey");
|
|
1003
|
-
function collectRoutes(
|
|
1004
|
-
return
|
|
1005
|
-
if (!raw) {
|
|
1006
|
-
return acc;
|
|
1007
|
-
}
|
|
1237
|
+
function collectRoutes(rawItems, keyFn, resolver) {
|
|
1238
|
+
return rawItems.reduce((acc, raw) => {
|
|
1008
1239
|
const mapped = mapRoute(raw, resolver);
|
|
1009
1240
|
if (mapped) {
|
|
1010
1241
|
mapped[CACHE_KEY] = keyFn(mapped);
|
|
@@ -1054,14 +1285,20 @@ function createRoutesCache(max, ttl) {
|
|
|
1054
1285
|
keys: () => lru.keys()
|
|
1055
1286
|
};
|
|
1056
1287
|
}
|
|
1057
|
-
function
|
|
1288
|
+
function createLazyRoutesCollection(def, client, updates$, resolver) {
|
|
1058
1289
|
const _def = {
|
|
1059
1290
|
cache: {
|
|
1060
1291
|
max: def?.cache?.max ?? 1e3,
|
|
1061
|
-
ttl: def?.cache?.ttl
|
|
1292
|
+
ttl: resolveTtl(def?.cache?.ttl)
|
|
1062
1293
|
}
|
|
1063
1294
|
};
|
|
1064
1295
|
const cache = createRoutesCache(_def.cache.max, _def.cache.ttl);
|
|
1296
|
+
const routesSourceDoc = createSourceSubscription6(schema.sources.storeRoute);
|
|
1297
|
+
function fetchRoutes(vars) {
|
|
1298
|
+
return collectAsync6(
|
|
1299
|
+
mapAsync6(client.subscribe(routesSourceDoc, vars), (data) => data.source)
|
|
1300
|
+
);
|
|
1301
|
+
}
|
|
1065
1302
|
const projected = new ProjectedLazyMap3({
|
|
1066
1303
|
key: (route) => route[CACHE_KEY] ?? ID_PREFIX + route.id,
|
|
1067
1304
|
values: (prefixedKeys) => wrap("routes", () => {
|
|
@@ -1076,13 +1313,16 @@ function createRoutesCollection(def, client, updates$, resolver) {
|
|
|
1076
1313
|
}
|
|
1077
1314
|
}
|
|
1078
1315
|
return Promise.all([
|
|
1079
|
-
ids.length > 0 ?
|
|
1080
|
-
paths.length > 0 ?
|
|
1316
|
+
ids.length > 0 ? fetchRoutes({ ids }).then((raw) => collectRoutes(raw, (r) => ID_PREFIX + r.id, resolver)) : Promise.resolve([]),
|
|
1317
|
+
paths.length > 0 ? fetchRoutes({ paths }).then((raw) => collectRoutes(raw, (r) => PATH_PREFIX + r.path, resolver)) : Promise.resolve([])
|
|
1081
1318
|
]).then(([byIds, byPaths]) => [...byIds, ...byPaths]);
|
|
1082
1319
|
}),
|
|
1083
1320
|
cache
|
|
1084
1321
|
});
|
|
1085
|
-
const
|
|
1322
|
+
const channel = createRefreshChannel(def?.onRefresh);
|
|
1323
|
+
function emit(ids, kind) {
|
|
1324
|
+
channel.emit({ ids, kind });
|
|
1325
|
+
}
|
|
1086
1326
|
let lastRefreshKeys = [];
|
|
1087
1327
|
const scheduleRefresh = createRefresher(
|
|
1088
1328
|
async () => {
|
|
@@ -1092,9 +1332,12 @@ function createRoutesCollection(def, client, updates$, resolver) {
|
|
|
1092
1332
|
}
|
|
1093
1333
|
await projected.refresh(lastRefreshKeys);
|
|
1094
1334
|
},
|
|
1095
|
-
() => {
|
|
1335
|
+
(kind) => {
|
|
1096
1336
|
if (lastRefreshKeys.length > 0) {
|
|
1097
|
-
|
|
1337
|
+
emit(
|
|
1338
|
+
lastRefreshKeys.map((key) => key.slice(2)),
|
|
1339
|
+
kind
|
|
1340
|
+
);
|
|
1098
1341
|
}
|
|
1099
1342
|
},
|
|
1100
1343
|
() => {
|
|
@@ -1115,12 +1358,12 @@ function createRoutesCollection(def, client, updates$, resolver) {
|
|
|
1115
1358
|
}
|
|
1116
1359
|
}
|
|
1117
1360
|
if (evicted.length > 0) {
|
|
1118
|
-
|
|
1361
|
+
emit(evicted, "upstream-update");
|
|
1119
1362
|
}
|
|
1120
1363
|
});
|
|
1121
1364
|
return {
|
|
1122
1365
|
instance: {
|
|
1123
|
-
refresh$,
|
|
1366
|
+
refresh$: channel.stream$,
|
|
1124
1367
|
get(idOrIds) {
|
|
1125
1368
|
if (Array.isArray(idOrIds)) {
|
|
1126
1369
|
return projected.get(idOrIds.map((id) => ID_PREFIX + id));
|
|
@@ -1134,7 +1377,7 @@ function createRoutesCollection(def, client, updates$, resolver) {
|
|
|
1134
1377
|
return projected.get(PATH_PREFIX + pathOrPaths);
|
|
1135
1378
|
},
|
|
1136
1379
|
refresh() {
|
|
1137
|
-
scheduleRefresh();
|
|
1380
|
+
scheduleRefresh("on-demand");
|
|
1138
1381
|
},
|
|
1139
1382
|
clear() {
|
|
1140
1383
|
projected.clear();
|
|
@@ -1142,65 +1385,321 @@ function createRoutesCollection(def, client, updates$, resolver) {
|
|
|
1142
1385
|
},
|
|
1143
1386
|
destroy() {
|
|
1144
1387
|
unsubUpdates();
|
|
1145
|
-
|
|
1388
|
+
channel.complete();
|
|
1389
|
+
}
|
|
1390
|
+
};
|
|
1391
|
+
}
|
|
1392
|
+
|
|
1393
|
+
// src/model-resolver.ts
|
|
1394
|
+
var ModelResolver = class {
|
|
1395
|
+
_typeNames;
|
|
1396
|
+
_toModel;
|
|
1397
|
+
_toTypeName;
|
|
1398
|
+
constructor(models2) {
|
|
1399
|
+
const toModel = /* @__PURE__ */ new Map();
|
|
1400
|
+
const toTypeName = /* @__PURE__ */ new Map();
|
|
1401
|
+
let typeNames;
|
|
1402
|
+
if (models2) {
|
|
1403
|
+
typeNames = /* @__PURE__ */ new Set();
|
|
1404
|
+
for (const [model, typeName] of Object.entries(models2)) {
|
|
1405
|
+
toModel.set(typeName, model);
|
|
1406
|
+
toTypeName.set(model, typeName);
|
|
1407
|
+
typeNames.add(typeName);
|
|
1408
|
+
}
|
|
1409
|
+
}
|
|
1410
|
+
this._typeNames = typeNames;
|
|
1411
|
+
this._toModel = toModel;
|
|
1412
|
+
this._toTypeName = toTypeName;
|
|
1413
|
+
}
|
|
1414
|
+
hasTypeName(typeName) {
|
|
1415
|
+
return !this._typeNames || this._typeNames.has(typeName);
|
|
1416
|
+
}
|
|
1417
|
+
getModel(typeName) {
|
|
1418
|
+
return this._toModel.get(typeName);
|
|
1419
|
+
}
|
|
1420
|
+
getTypeName(model) {
|
|
1421
|
+
return this._toTypeName.get(model) ?? model;
|
|
1422
|
+
}
|
|
1423
|
+
hasModel(model) {
|
|
1424
|
+
return !this._toTypeName.size || this._toTypeName.has(model);
|
|
1425
|
+
}
|
|
1426
|
+
resolveModel(typeName) {
|
|
1427
|
+
return this._toModel.get(typeName) ?? typeName;
|
|
1428
|
+
}
|
|
1429
|
+
};
|
|
1430
|
+
|
|
1431
|
+
// src/routes.ts
|
|
1432
|
+
import {
|
|
1433
|
+
collectAsync as collectAsync7,
|
|
1434
|
+
createSourceSubscription as createSourceSubscription7,
|
|
1435
|
+
mapAsync as mapAsync7,
|
|
1436
|
+
runWithBackoff as runWithBackoff5
|
|
1437
|
+
} from "@contello/client";
|
|
1438
|
+
import { ProjectedMap as ProjectedMap4, maybeThen as maybeThen4 } from "projected";
|
|
1439
|
+
function createRoutesCollection(options, client, updates$, resolver, refreshByTtl) {
|
|
1440
|
+
const opts = options ?? {};
|
|
1441
|
+
const cache = {
|
|
1442
|
+
ttl: resolveTtl(opts.cache?.ttl),
|
|
1443
|
+
eviction: opts.cache?.eviction ?? "refresh"
|
|
1444
|
+
};
|
|
1445
|
+
const channel = createRefreshChannel(opts.onRefresh);
|
|
1446
|
+
const ttl = createTtlOrchestrator({ ttl: cache.ttl, run: () => runFullRefresh("ttl") });
|
|
1447
|
+
const pathById = /* @__PURE__ */ new Map();
|
|
1448
|
+
const idByPath = /* @__PURE__ */ new Map();
|
|
1449
|
+
let loaded = false;
|
|
1450
|
+
const routesSourceDoc = createSourceSubscription7(schema.sources.storeRoute);
|
|
1451
|
+
const projected = new ProjectedMap4({
|
|
1452
|
+
key: (route) => route.id,
|
|
1453
|
+
values: (ids) => wrap(
|
|
1454
|
+
"routes",
|
|
1455
|
+
() => collectAsync7(
|
|
1456
|
+
mapAsync7(client.subscribe(routesSourceDoc, { ids }), (data) => data.source)
|
|
1457
|
+
).then((rawItems) => {
|
|
1458
|
+
const items = rawItems.reduce((acc, raw) => {
|
|
1459
|
+
const mapped = mapRoute(raw, resolver);
|
|
1460
|
+
if (mapped) {
|
|
1461
|
+
acc.push(mapped);
|
|
1462
|
+
}
|
|
1463
|
+
return acc;
|
|
1464
|
+
}, []);
|
|
1465
|
+
if (ids === void 0) {
|
|
1466
|
+
pathById.clear();
|
|
1467
|
+
idByPath.clear();
|
|
1468
|
+
for (const r of items) {
|
|
1469
|
+
pathById.set(r.id, r.path);
|
|
1470
|
+
idByPath.set(r.path, r.id);
|
|
1471
|
+
}
|
|
1472
|
+
if (!loaded) {
|
|
1473
|
+
loaded = true;
|
|
1474
|
+
ttl.mark();
|
|
1475
|
+
opts.onLoad?.(items.map((r) => r.id));
|
|
1476
|
+
}
|
|
1477
|
+
} else {
|
|
1478
|
+
const returnedIds = new Set(items.map((r) => r.id));
|
|
1479
|
+
for (const id of ids) {
|
|
1480
|
+
if (!returnedIds.has(id)) {
|
|
1481
|
+
const oldPath = pathById.get(id);
|
|
1482
|
+
if (oldPath !== void 0) {
|
|
1483
|
+
idByPath.delete(oldPath);
|
|
1484
|
+
pathById.delete(id);
|
|
1485
|
+
}
|
|
1486
|
+
}
|
|
1487
|
+
}
|
|
1488
|
+
for (const r of items) {
|
|
1489
|
+
const oldPath = pathById.get(r.id);
|
|
1490
|
+
if (oldPath !== void 0 && oldPath !== r.path) {
|
|
1491
|
+
idByPath.delete(oldPath);
|
|
1492
|
+
}
|
|
1493
|
+
pathById.set(r.id, r.path);
|
|
1494
|
+
idByPath.set(r.path, r.id);
|
|
1495
|
+
}
|
|
1496
|
+
}
|
|
1497
|
+
return items;
|
|
1498
|
+
})
|
|
1499
|
+
)
|
|
1500
|
+
});
|
|
1501
|
+
function emit(ids, kind) {
|
|
1502
|
+
channel.emit({ ids, kind });
|
|
1503
|
+
}
|
|
1504
|
+
function runFullRefresh(kind) {
|
|
1505
|
+
refreshByTtl.enqueue(
|
|
1506
|
+
() => runWithBackoff5(
|
|
1507
|
+
() => projected.refresh().then((map) => {
|
|
1508
|
+
emit([...map.keys()], kind);
|
|
1509
|
+
ttl.mark();
|
|
1510
|
+
})
|
|
1511
|
+
)
|
|
1512
|
+
);
|
|
1513
|
+
}
|
|
1514
|
+
function runPartialRefresh(refreshIds, deletedIds) {
|
|
1515
|
+
const changedIds = [.../* @__PURE__ */ new Set([...refreshIds, ...deletedIds])];
|
|
1516
|
+
if (refreshIds.length === 0) {
|
|
1517
|
+
emit(changedIds, "upstream-update");
|
|
1518
|
+
return;
|
|
1519
|
+
}
|
|
1520
|
+
void runWithBackoff5(
|
|
1521
|
+
() => projected.refresh(refreshIds).then(() => {
|
|
1522
|
+
emit(changedIds, "upstream-update");
|
|
1523
|
+
})
|
|
1524
|
+
);
|
|
1525
|
+
}
|
|
1526
|
+
const unsubUpdates = updates$.subscribe((batch) => {
|
|
1527
|
+
if (!loaded || batch.route.length === 0) {
|
|
1528
|
+
return;
|
|
1529
|
+
}
|
|
1530
|
+
const deleted = [];
|
|
1531
|
+
const upserted = /* @__PURE__ */ new Set();
|
|
1532
|
+
for (const event of batch.route) {
|
|
1533
|
+
if (event.mutation === "delete") {
|
|
1534
|
+
deleted.push(event.id);
|
|
1535
|
+
} else {
|
|
1536
|
+
upserted.add(event.id);
|
|
1537
|
+
}
|
|
1538
|
+
}
|
|
1539
|
+
if (cache.eviction === "clear") {
|
|
1540
|
+
pathById.clear();
|
|
1541
|
+
idByPath.clear();
|
|
1542
|
+
projected.clear();
|
|
1543
|
+
return;
|
|
1544
|
+
}
|
|
1545
|
+
for (const id of deleted) {
|
|
1546
|
+
const oldPath = pathById.get(id);
|
|
1547
|
+
if (oldPath !== void 0) {
|
|
1548
|
+
idByPath.delete(oldPath);
|
|
1549
|
+
pathById.delete(id);
|
|
1550
|
+
}
|
|
1551
|
+
}
|
|
1552
|
+
if (deleted.length > 0) {
|
|
1553
|
+
projected.delete(deleted);
|
|
1554
|
+
}
|
|
1555
|
+
runPartialRefresh([...upserted], deleted);
|
|
1556
|
+
});
|
|
1557
|
+
function getByIdSingle(id) {
|
|
1558
|
+
return projected.get(id);
|
|
1559
|
+
}
|
|
1560
|
+
function getByIds(ids) {
|
|
1561
|
+
return projected.get(ids);
|
|
1562
|
+
}
|
|
1563
|
+
function lookupByPathSingle(path) {
|
|
1564
|
+
return maybeThen4(ensureLoaded(), () => {
|
|
1565
|
+
const id = idByPath.get(path);
|
|
1566
|
+
return id === void 0 ? void 0 : projected.get(id);
|
|
1567
|
+
});
|
|
1568
|
+
}
|
|
1569
|
+
function lookupByPaths(paths) {
|
|
1570
|
+
return maybeThen4(ensureLoaded(), () => {
|
|
1571
|
+
const ids = [];
|
|
1572
|
+
for (const p of paths) {
|
|
1573
|
+
const id = idByPath.get(p);
|
|
1574
|
+
if (id !== void 0) {
|
|
1575
|
+
ids.push(id);
|
|
1576
|
+
}
|
|
1577
|
+
}
|
|
1578
|
+
return projected.get(ids);
|
|
1579
|
+
});
|
|
1580
|
+
}
|
|
1581
|
+
function ensureLoaded() {
|
|
1582
|
+
if (loaded) {
|
|
1583
|
+
return void 0;
|
|
1584
|
+
}
|
|
1585
|
+
return maybeThen4(projected.getAllAsMap(), () => void 0);
|
|
1586
|
+
}
|
|
1587
|
+
const instance = {
|
|
1588
|
+
refresh$: channel.stream$,
|
|
1589
|
+
async load() {
|
|
1590
|
+
if (!loaded) {
|
|
1591
|
+
await projected.getAllAsMap();
|
|
1592
|
+
}
|
|
1593
|
+
},
|
|
1594
|
+
get(idOrIds) {
|
|
1595
|
+
return Array.isArray(idOrIds) ? getByIds(idOrIds) : getByIdSingle(idOrIds);
|
|
1596
|
+
},
|
|
1597
|
+
getByPath(pathOrPaths) {
|
|
1598
|
+
return Array.isArray(pathOrPaths) ? lookupByPaths(pathOrPaths) : lookupByPathSingle(pathOrPaths);
|
|
1599
|
+
},
|
|
1600
|
+
refresh() {
|
|
1601
|
+
runFullRefresh("on-demand");
|
|
1602
|
+
}
|
|
1603
|
+
};
|
|
1604
|
+
return {
|
|
1605
|
+
instance,
|
|
1606
|
+
destroy() {
|
|
1607
|
+
unsubUpdates();
|
|
1608
|
+
ttl.clear();
|
|
1609
|
+
channel.complete();
|
|
1146
1610
|
}
|
|
1147
1611
|
};
|
|
1148
1612
|
}
|
|
1613
|
+
function createRoutesSyncCollection(options, client, updates$, resolver, refreshByTtl) {
|
|
1614
|
+
const { instance: base, destroy } = createRoutesCollection(options, client, updates$, resolver, refreshByTtl);
|
|
1615
|
+
function assertSync(value, method) {
|
|
1616
|
+
if (value instanceof Promise) {
|
|
1617
|
+
throw new Error(`routes.${method}() is not initialized yet \u2014 call routes.load() first`);
|
|
1618
|
+
}
|
|
1619
|
+
return value;
|
|
1620
|
+
}
|
|
1621
|
+
return {
|
|
1622
|
+
instance: {
|
|
1623
|
+
...base,
|
|
1624
|
+
get(idOrIds) {
|
|
1625
|
+
return assertSync(base.get(idOrIds), "get");
|
|
1626
|
+
},
|
|
1627
|
+
getByPath(pathOrPaths) {
|
|
1628
|
+
return assertSync(base.getByPath(pathOrPaths), "getByPath");
|
|
1629
|
+
}
|
|
1630
|
+
},
|
|
1631
|
+
destroy
|
|
1632
|
+
};
|
|
1633
|
+
}
|
|
1149
1634
|
|
|
1150
1635
|
// src/singleton.ts
|
|
1151
1636
|
import {
|
|
1152
|
-
|
|
1153
|
-
createSourceSubscription as createSourceSubscription3,
|
|
1637
|
+
createSourceSubscription as createSourceSubscription8,
|
|
1154
1638
|
firstAsync,
|
|
1155
|
-
mapAsync as
|
|
1639
|
+
mapAsync as mapAsync8,
|
|
1640
|
+
runWithBackoff as runWithBackoff6
|
|
1156
1641
|
} from "@contello/client";
|
|
1157
|
-
import { ProjectedValue, maybeThen as
|
|
1158
|
-
function createSingleton(source, options, client, updates$, resolver) {
|
|
1642
|
+
import { ProjectedValue, maybeThen as maybeThen5 } from "projected";
|
|
1643
|
+
function createSingleton(source, options, client, updates$, resolver, refreshByTtl) {
|
|
1159
1644
|
const opts = options ?? {};
|
|
1160
1645
|
const mapFn = opts.map ?? ((item) => item);
|
|
1161
1646
|
const _def = {
|
|
1162
1647
|
name: opts.name ?? source.__model,
|
|
1163
1648
|
model: source.__model,
|
|
1164
1649
|
cache: {
|
|
1165
|
-
ttl: opts.cache?.ttl,
|
|
1650
|
+
ttl: resolveTtl(opts.cache?.ttl),
|
|
1166
1651
|
eviction: opts.cache?.eviction ?? "refresh"
|
|
1167
1652
|
}
|
|
1168
1653
|
};
|
|
1169
1654
|
const dependencyCollector = new DependencyCollector(_def.model, resolver);
|
|
1170
1655
|
const itemKey = `singleton:${_def.name}`;
|
|
1171
|
-
|
|
1656
|
+
const channel = createRefreshChannel(opts.onRefresh);
|
|
1657
|
+
const ttl = createTtlOrchestrator({ ttl: _def.cache.ttl, run: () => runTtlRefresh() });
|
|
1658
|
+
let loaded = false;
|
|
1172
1659
|
const projected = new ProjectedValue({
|
|
1173
1660
|
value: () => wrap(
|
|
1174
1661
|
`singleton:${_def.name}`,
|
|
1175
|
-
() =>
|
|
1662
|
+
() => maybeThen5(
|
|
1176
1663
|
firstAsync(
|
|
1177
|
-
|
|
1178
|
-
client.subscribe(
|
|
1664
|
+
mapAsync8(
|
|
1665
|
+
client.subscribe(createSourceSubscription8(source)),
|
|
1179
1666
|
(r) => r.source
|
|
1180
1667
|
)
|
|
1181
1668
|
),
|
|
1182
1669
|
(raw) => dependencyCollector.createContext(
|
|
1183
|
-
(ref, register) =>
|
|
1670
|
+
(ref, register) => maybeThen5(mapFn(raw, ref), (mapped) => {
|
|
1184
1671
|
register(itemKey);
|
|
1672
|
+
if (!loaded) {
|
|
1673
|
+
loaded = true;
|
|
1674
|
+
ttl.mark();
|
|
1675
|
+
opts.onLoad?.();
|
|
1676
|
+
}
|
|
1185
1677
|
return mapped;
|
|
1186
1678
|
})
|
|
1187
1679
|
)
|
|
1188
1680
|
)
|
|
1189
1681
|
)
|
|
1190
1682
|
});
|
|
1191
|
-
|
|
1683
|
+
function emit(kind) {
|
|
1684
|
+
channel.emit({ kind });
|
|
1685
|
+
}
|
|
1686
|
+
function runTtlRefresh() {
|
|
1687
|
+
refreshByTtl.enqueue(
|
|
1688
|
+
() => runWithBackoff6(() => projected.refresh()).then(() => {
|
|
1689
|
+
emit("ttl");
|
|
1690
|
+
ttl.mark();
|
|
1691
|
+
})
|
|
1692
|
+
);
|
|
1693
|
+
}
|
|
1192
1694
|
const scheduleRefresh = createRefresher(
|
|
1193
1695
|
() => projected.refresh(),
|
|
1194
|
-
() => {
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
if (_def.cache.ttl !== void 0) {
|
|
1198
|
-
timer = setTimeout(scheduleRefresh, _def.cache.ttl);
|
|
1199
|
-
}
|
|
1696
|
+
(kind) => {
|
|
1697
|
+
emit(kind);
|
|
1698
|
+
ttl.mark();
|
|
1200
1699
|
},
|
|
1201
|
-
() =>
|
|
1700
|
+
() => {
|
|
1701
|
+
}
|
|
1202
1702
|
);
|
|
1203
|
-
let loaded = false;
|
|
1204
1703
|
const unsubUpdates = updates$.subscribe((batch) => {
|
|
1205
1704
|
if (!loaded) {
|
|
1206
1705
|
return;
|
|
@@ -1215,41 +1714,39 @@ function createSingleton(source, options, client, updates$, resolver) {
|
|
|
1215
1714
|
projected.clear();
|
|
1216
1715
|
return;
|
|
1217
1716
|
}
|
|
1218
|
-
scheduleRefresh();
|
|
1717
|
+
scheduleRefresh("upstream-update");
|
|
1219
1718
|
});
|
|
1220
1719
|
return {
|
|
1221
1720
|
instance: {
|
|
1222
1721
|
name: _def.name,
|
|
1223
|
-
refresh$,
|
|
1722
|
+
refresh$: channel.stream$,
|
|
1224
1723
|
get() {
|
|
1225
1724
|
return projected.get();
|
|
1226
1725
|
},
|
|
1227
1726
|
refresh() {
|
|
1228
|
-
scheduleRefresh();
|
|
1727
|
+
scheduleRefresh("on-demand");
|
|
1229
1728
|
},
|
|
1230
1729
|
async load() {
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
if (_def.cache.ttl !== void 0) {
|
|
1234
|
-
timer = setTimeout(scheduleRefresh, _def.cache.ttl);
|
|
1730
|
+
if (!loaded) {
|
|
1731
|
+
await projected.get();
|
|
1235
1732
|
}
|
|
1236
|
-
opts.onLoad?.();
|
|
1237
1733
|
}
|
|
1238
1734
|
},
|
|
1239
1735
|
destroy() {
|
|
1240
1736
|
unsubUpdates();
|
|
1241
|
-
|
|
1242
|
-
|
|
1737
|
+
ttl.clear();
|
|
1738
|
+
channel.complete();
|
|
1243
1739
|
}
|
|
1244
1740
|
};
|
|
1245
1741
|
}
|
|
1246
|
-
function createSingletonSync(source, options, client, updates$, resolver) {
|
|
1742
|
+
function createSingletonSync(source, options, client, updates$, resolver, refreshByTtl) {
|
|
1247
1743
|
const { instance: base, destroy } = createSingleton(
|
|
1248
1744
|
source,
|
|
1249
1745
|
options,
|
|
1250
1746
|
client,
|
|
1251
1747
|
updates$,
|
|
1252
|
-
resolver
|
|
1748
|
+
resolver,
|
|
1749
|
+
refreshByTtl
|
|
1253
1750
|
);
|
|
1254
1751
|
return {
|
|
1255
1752
|
instance: {
|
|
@@ -1269,8 +1766,8 @@ function createSingletonSync(source, options, client, updates$, resolver) {
|
|
|
1269
1766
|
// src/watcher.ts
|
|
1270
1767
|
import {
|
|
1271
1768
|
asyncKeepalive,
|
|
1272
|
-
createAsyncIterableSubject as
|
|
1273
|
-
mapAsync as
|
|
1769
|
+
createAsyncIterableSubject as createAsyncIterableSubject3,
|
|
1770
|
+
mapAsync as mapAsync9
|
|
1274
1771
|
} from "@contello/client";
|
|
1275
1772
|
function addRouteByModel(map, entityType, event) {
|
|
1276
1773
|
let list = map.get(entityType);
|
|
@@ -1361,7 +1858,7 @@ function mapEvent(raw, resolver) {
|
|
|
1361
1858
|
}
|
|
1362
1859
|
}
|
|
1363
1860
|
function createInternalWatcher(client, resolver) {
|
|
1364
|
-
const updates$ =
|
|
1861
|
+
const updates$ = createAsyncIterableSubject3();
|
|
1365
1862
|
let controller;
|
|
1366
1863
|
return {
|
|
1367
1864
|
updates$,
|
|
@@ -1377,7 +1874,7 @@ function createInternalWatcher(client, resolver) {
|
|
|
1377
1874
|
() => client.subscribe(storeWatchUpdatesDocument),
|
|
1378
1875
|
signal
|
|
1379
1876
|
);
|
|
1380
|
-
const events =
|
|
1877
|
+
const events = mapAsync9(
|
|
1381
1878
|
source,
|
|
1382
1879
|
(data) => (data.contelloUpdatesBatch?.events ?? []).map((e) => mapEvent(e, resolver)).filter((e) => e !== void 0)
|
|
1383
1880
|
);
|
|
@@ -1408,26 +1905,27 @@ var Store = class {
|
|
|
1408
1905
|
_watcher;
|
|
1409
1906
|
_schema;
|
|
1410
1907
|
_cleanups = [];
|
|
1908
|
+
_refreshByTtl = createRefreshByTtlQueue();
|
|
1411
1909
|
/**
|
|
1412
1910
|
* Multicast update-batch stream from the watcher. Consume with `for await (const batch of updates$)`;
|
|
1413
1911
|
* each iteration starts an independent fan-out from the shared source.
|
|
1414
1912
|
*/
|
|
1415
1913
|
updates$;
|
|
1416
1914
|
constructor(options) {
|
|
1417
|
-
const { url, project, token, schema } = options;
|
|
1418
|
-
this._schema =
|
|
1915
|
+
const { url, project, token, schema: schema2 } = options;
|
|
1916
|
+
this._schema = schema2;
|
|
1419
1917
|
this._client = createContelloClient({
|
|
1420
1918
|
url,
|
|
1421
1919
|
project,
|
|
1422
1920
|
token,
|
|
1423
|
-
schema,
|
|
1921
|
+
schema: schema2,
|
|
1424
1922
|
connections: options.connections,
|
|
1425
1923
|
onConnected: options.onConnected,
|
|
1426
1924
|
onReconnecting: options.onReconnecting,
|
|
1427
1925
|
onError: options.onError,
|
|
1428
1926
|
connectionEvents: options.connectionEvents
|
|
1429
1927
|
});
|
|
1430
|
-
this._resolver = new ModelResolver(
|
|
1928
|
+
this._resolver = new ModelResolver(schema2?.models);
|
|
1431
1929
|
this._watcher = createInternalWatcher(this._client, this._resolver);
|
|
1432
1930
|
this.updates$ = this._watcher.updates$;
|
|
1433
1931
|
this.ping = () => this._client.ping();
|
|
@@ -1469,7 +1967,8 @@ var Store = class {
|
|
|
1469
1967
|
options,
|
|
1470
1968
|
this._client,
|
|
1471
1969
|
this._watcher.updates$,
|
|
1472
|
-
this._resolver
|
|
1970
|
+
this._resolver,
|
|
1971
|
+
this._refreshByTtl
|
|
1473
1972
|
);
|
|
1474
1973
|
this._cleanups.push(destroy);
|
|
1475
1974
|
return instance;
|
|
@@ -1481,7 +1980,8 @@ var Store = class {
|
|
|
1481
1980
|
options,
|
|
1482
1981
|
this._client,
|
|
1483
1982
|
this._watcher.updates$,
|
|
1484
|
-
this._resolver
|
|
1983
|
+
this._resolver,
|
|
1984
|
+
this._refreshByTtl
|
|
1485
1985
|
);
|
|
1486
1986
|
this._cleanups.push(destroy);
|
|
1487
1987
|
return instance;
|
|
@@ -1494,7 +1994,8 @@ var Store = class {
|
|
|
1494
1994
|
options,
|
|
1495
1995
|
this._client,
|
|
1496
1996
|
this._watcher.updates$,
|
|
1497
|
-
this._resolver
|
|
1997
|
+
this._resolver,
|
|
1998
|
+
this._refreshByTtl
|
|
1498
1999
|
);
|
|
1499
2000
|
this._cleanups.push(destroy);
|
|
1500
2001
|
return instance;
|
|
@@ -1506,7 +2007,8 @@ var Store = class {
|
|
|
1506
2007
|
options,
|
|
1507
2008
|
this._client,
|
|
1508
2009
|
this._watcher.updates$,
|
|
1509
|
-
this._resolver
|
|
2010
|
+
this._resolver,
|
|
2011
|
+
this._refreshByTtl
|
|
1510
2012
|
);
|
|
1511
2013
|
this._cleanups.push(destroy);
|
|
1512
2014
|
return instance;
|
|
@@ -1525,18 +2027,67 @@ var Store = class {
|
|
|
1525
2027
|
return instance;
|
|
1526
2028
|
}
|
|
1527
2029
|
// --- assets / routes / i18n (no source — built-ins) ---
|
|
2030
|
+
// --- assets ---
|
|
1528
2031
|
defineAssets(options) {
|
|
1529
|
-
const { instance, destroy } = createAssetsCollection(
|
|
2032
|
+
const { instance, destroy } = createAssetsCollection(
|
|
2033
|
+
options,
|
|
2034
|
+
this._client,
|
|
2035
|
+
this._watcher.updates$,
|
|
2036
|
+
this._refreshByTtl
|
|
2037
|
+
);
|
|
2038
|
+
this._cleanups.push(destroy);
|
|
2039
|
+
return instance;
|
|
2040
|
+
}
|
|
2041
|
+
defineAssetsSync(options) {
|
|
2042
|
+
const { instance, destroy } = createAssetsSyncCollection(
|
|
2043
|
+
options,
|
|
2044
|
+
this._client,
|
|
2045
|
+
this._watcher.updates$,
|
|
2046
|
+
this._refreshByTtl
|
|
2047
|
+
);
|
|
2048
|
+
this._cleanups.push(destroy);
|
|
2049
|
+
return instance;
|
|
2050
|
+
}
|
|
2051
|
+
defineLazyAssets(options) {
|
|
2052
|
+
const { instance, destroy } = createLazyAssetsCollection(options, this._client, this._watcher.updates$);
|
|
1530
2053
|
this._cleanups.push(destroy);
|
|
1531
2054
|
return instance;
|
|
1532
2055
|
}
|
|
2056
|
+
// --- routes ---
|
|
1533
2057
|
defineRoutes(options) {
|
|
1534
|
-
const { instance, destroy } = createRoutesCollection(
|
|
2058
|
+
const { instance, destroy } = createRoutesCollection(
|
|
2059
|
+
options,
|
|
2060
|
+
this._client,
|
|
2061
|
+
this._watcher.updates$,
|
|
2062
|
+
this._resolver,
|
|
2063
|
+
this._refreshByTtl
|
|
2064
|
+
);
|
|
2065
|
+
this._cleanups.push(destroy);
|
|
2066
|
+
return instance;
|
|
2067
|
+
}
|
|
2068
|
+
defineRoutesSync(options) {
|
|
2069
|
+
const { instance, destroy } = createRoutesSyncCollection(
|
|
2070
|
+
options,
|
|
2071
|
+
this._client,
|
|
2072
|
+
this._watcher.updates$,
|
|
2073
|
+
this._resolver,
|
|
2074
|
+
this._refreshByTtl
|
|
2075
|
+
);
|
|
2076
|
+
this._cleanups.push(destroy);
|
|
2077
|
+
return instance;
|
|
2078
|
+
}
|
|
2079
|
+
defineLazyRoutes(options) {
|
|
2080
|
+
const { instance, destroy } = createLazyRoutesCollection(
|
|
2081
|
+
options,
|
|
2082
|
+
this._client,
|
|
2083
|
+
this._watcher.updates$,
|
|
2084
|
+
this._resolver
|
|
2085
|
+
);
|
|
1535
2086
|
this._cleanups.push(destroy);
|
|
1536
2087
|
return instance;
|
|
1537
2088
|
}
|
|
1538
2089
|
defineI18nMessages(def) {
|
|
1539
|
-
const result = createI18nMessagesCollection(def, this._client, this._watcher.updates
|
|
2090
|
+
const result = createI18nMessagesCollection(def, this._client, this._watcher.updates$, this._refreshByTtl);
|
|
1540
2091
|
this._cleanups.push(result.destroy);
|
|
1541
2092
|
return result;
|
|
1542
2093
|
}
|
|
@@ -1547,10 +2098,10 @@ function createStore(options) {
|
|
|
1547
2098
|
}
|
|
1548
2099
|
|
|
1549
2100
|
// src/index.ts
|
|
1550
|
-
import { maybeThen as
|
|
2101
|
+
import { maybeThen as maybeThen6, maybeCatch } from "projected";
|
|
1551
2102
|
export {
|
|
1552
2103
|
Store,
|
|
1553
2104
|
createStore,
|
|
1554
2105
|
maybeCatch,
|
|
1555
|
-
|
|
2106
|
+
maybeThen6 as maybeThen
|
|
1556
2107
|
};
|