@contello/store 2.1.1 → 3.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/fragments.d.ts +3 -1
- package/dist/fragments.d.ts.map +1 -0
- package/dist/fragments.js +18 -11
- package/dist/fragments.js.map +1 -0
- package/dist/index.cjs +1633 -1918
- package/dist/index.d.cts +326 -324
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.ts +326 -324
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1606 -1928
- package/dist/index.js.map +1 -0
- package/package.json +6 -6
package/dist/index.js
CHANGED
|
@@ -1,59 +1,62 @@
|
|
|
1
|
-
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
} from "@contello/client";
|
|
11
|
-
import { ProjectedMap } from "projected";
|
|
12
|
-
|
|
13
|
-
// src/diagnostics.ts
|
|
14
|
-
import dc from "diagnostics_channel";
|
|
15
|
-
var channels = {
|
|
16
|
-
start: "@contello/store:start",
|
|
17
|
-
end: "@contello/store:end",
|
|
18
|
-
error: "@contello/store:error"
|
|
1
|
+
import { asyncKeepalive, collectAsync, createAsyncIterableSubject, createContelloClient, createSourceSubscription, firstAsync, mapAsync, runWithBackoff } from "@contello/client";
|
|
2
|
+
import { ProjectedLazyMap, ProjectedMap, ProjectedValue, maybeCatch, maybeThen, maybeThen as maybeThen$1 } from "projected";
|
|
3
|
+
import dc from "node:diagnostics_channel";
|
|
4
|
+
import { LRUCache } from "lru-cache";
|
|
5
|
+
//#region src/diagnostics.ts
|
|
6
|
+
const channels = {
|
|
7
|
+
start: "@contello/store:start",
|
|
8
|
+
end: "@contello/store:end",
|
|
9
|
+
error: "@contello/store:error"
|
|
19
10
|
};
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
11
|
+
const onStart = dc.channel(channels.start);
|
|
12
|
+
const onEnd = dc.channel(channels.end);
|
|
13
|
+
const onError = dc.channel(channels.error);
|
|
23
14
|
function hasSubscribers() {
|
|
24
|
-
|
|
15
|
+
return onStart.hasSubscribers || onEnd.hasSubscribers || onError.hasSubscribers;
|
|
25
16
|
}
|
|
26
17
|
function wrap(name, fn) {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
18
|
+
if (!hasSubscribers()) return fn();
|
|
19
|
+
const start = performance.now();
|
|
20
|
+
onStart.publish({ name });
|
|
21
|
+
let result;
|
|
22
|
+
try {
|
|
23
|
+
result = fn();
|
|
24
|
+
} catch (error) {
|
|
25
|
+
onError.publish({
|
|
26
|
+
name,
|
|
27
|
+
error,
|
|
28
|
+
durationMs: performance.now() - start
|
|
29
|
+
});
|
|
30
|
+
throw error;
|
|
31
|
+
}
|
|
32
|
+
if (result instanceof Promise) return (async () => {
|
|
33
|
+
try {
|
|
34
|
+
const r = await result;
|
|
35
|
+
onEnd.publish({
|
|
36
|
+
name,
|
|
37
|
+
cached: false,
|
|
38
|
+
durationMs: performance.now() - start
|
|
39
|
+
});
|
|
40
|
+
return r;
|
|
41
|
+
} catch (error) {
|
|
42
|
+
onError.publish({
|
|
43
|
+
name,
|
|
44
|
+
error,
|
|
45
|
+
durationMs: performance.now() - start
|
|
46
|
+
});
|
|
47
|
+
throw error;
|
|
48
|
+
}
|
|
49
|
+
})();
|
|
50
|
+
onEnd.publish({
|
|
51
|
+
name,
|
|
52
|
+
cached: true,
|
|
53
|
+
durationMs: performance.now() - start
|
|
54
|
+
});
|
|
55
|
+
return result;
|
|
53
56
|
}
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
+
//#endregion
|
|
58
|
+
//#region src/generated/graphql.ts
|
|
59
|
+
const MediaAssetFragmentSchema = `fragment MediaAsset on ContelloAsset {
|
|
57
60
|
id
|
|
58
61
|
original {
|
|
59
62
|
...MediaFile
|
|
@@ -72,7 +75,7 @@ var MediaAssetFragmentSchema = `fragment MediaAsset on ContelloAsset {
|
|
|
72
75
|
}
|
|
73
76
|
}
|
|
74
77
|
}`;
|
|
75
|
-
|
|
78
|
+
const MediaFileFragmentSchema = `fragment MediaFile on ContelloFile {
|
|
76
79
|
uid
|
|
77
80
|
mimeType
|
|
78
81
|
metadata {
|
|
@@ -86,10 +89,10 @@ var MediaFileFragmentSchema = `fragment MediaFile on ContelloFile {
|
|
|
86
89
|
}
|
|
87
90
|
}
|
|
88
91
|
}`;
|
|
89
|
-
|
|
92
|
+
const StoreAssetFragmentSchema = `fragment StoreAsset on ContelloAsset {
|
|
90
93
|
...MediaAsset
|
|
91
94
|
}`;
|
|
92
|
-
|
|
95
|
+
const StoreI18nMessageFragmentSchema = `fragment StoreI18nMessage on ContelloI18nMessage {
|
|
93
96
|
id
|
|
94
97
|
token
|
|
95
98
|
description
|
|
@@ -99,7 +102,7 @@ var StoreI18nMessageFragmentSchema = `fragment StoreI18nMessage on ContelloI18nM
|
|
|
99
102
|
value
|
|
100
103
|
}
|
|
101
104
|
}`;
|
|
102
|
-
|
|
105
|
+
const StoreRouteFragmentSchema = `fragment StoreRoute on ContelloRoute {
|
|
103
106
|
id
|
|
104
107
|
path
|
|
105
108
|
target {
|
|
@@ -136,7 +139,7 @@ var StoreRouteFragmentSchema = `fragment StoreRoute on ContelloRoute {
|
|
|
136
139
|
value
|
|
137
140
|
}
|
|
138
141
|
}`;
|
|
139
|
-
|
|
142
|
+
const storeRegisterI18nMessagesDocument = `mutation StoreRegisterI18nMessages($collection: String!, $messages: [ContelloI18nMessageInput!]!) {
|
|
140
143
|
registerContelloI18nMessages(
|
|
141
144
|
collectionReferenceName: $collection
|
|
142
145
|
messages: $messages
|
|
@@ -145,7 +148,7 @@ var storeRegisterI18nMessagesDocument = `mutation StoreRegisterI18nMessages($col
|
|
|
145
148
|
ids
|
|
146
149
|
}
|
|
147
150
|
}`;
|
|
148
|
-
|
|
151
|
+
const storeWatchUpdatesDocument = `${StoreRouteFragmentSchema}
|
|
149
152
|
subscription StoreWatchUpdates {
|
|
150
153
|
contelloUpdatesBatch {
|
|
151
154
|
events {
|
|
@@ -177,1951 +180,1626 @@ subscription StoreWatchUpdates {
|
|
|
177
180
|
}
|
|
178
181
|
}
|
|
179
182
|
}`;
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
183
|
+
const schema = {
|
|
184
|
+
operations: {
|
|
185
|
+
storeRegisterI18nMessages: {
|
|
186
|
+
document: storeRegisterI18nMessagesDocument,
|
|
187
|
+
kind: "mutation"
|
|
188
|
+
},
|
|
189
|
+
storeWatchUpdates: {
|
|
190
|
+
document: storeWatchUpdatesDocument,
|
|
191
|
+
kind: "subscription"
|
|
192
|
+
}
|
|
193
|
+
},
|
|
194
|
+
sources: {
|
|
195
|
+
storeAsset: {
|
|
196
|
+
document: `${MediaFileFragmentSchema}
|
|
187
197
|
${MediaAssetFragmentSchema}
|
|
188
198
|
${StoreAssetFragmentSchema}`,
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
dummy: "DummyEntity"
|
|
199
|
+
fragment: "StoreAsset",
|
|
200
|
+
subscription: "contelloAssetsBatch",
|
|
201
|
+
__model: "storeAsset",
|
|
202
|
+
__cardinality: "asset"
|
|
203
|
+
},
|
|
204
|
+
storeI18nMessage: {
|
|
205
|
+
document: StoreI18nMessageFragmentSchema,
|
|
206
|
+
fragment: "StoreI18nMessage",
|
|
207
|
+
subscription: "contelloI18nMessagesBatch",
|
|
208
|
+
__model: "storeI18nMessage",
|
|
209
|
+
__cardinality: "i18nMessage"
|
|
210
|
+
},
|
|
211
|
+
storeRoute: {
|
|
212
|
+
document: StoreRouteFragmentSchema,
|
|
213
|
+
fragment: "StoreRoute",
|
|
214
|
+
subscription: "contelloRoutesBatch",
|
|
215
|
+
__model: "storeRoute",
|
|
216
|
+
__cardinality: "route"
|
|
217
|
+
}
|
|
218
|
+
},
|
|
219
|
+
models: { dummy: "DummyEntity" }
|
|
211
220
|
};
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
221
|
+
//#endregion
|
|
222
|
+
//#region src/utils.ts
|
|
223
|
+
/**
|
|
224
|
+
* default `cache.ttl` applied across all store kinds. eager stores (collections, singletons)
|
|
225
|
+
* use it as the interval for a periodic full refresh; lazy stores use it as the per-item
|
|
226
|
+
* LRU eviction window. 3 hours.
|
|
227
|
+
*/
|
|
228
|
+
const DEFAULT_TTL_MS = 10800 * 1e3;
|
|
229
|
+
/**
|
|
230
|
+
* resolves the consumer-supplied `ttl` (number | false | undefined) into a runtime
|
|
231
|
+
* value: undefined → default, false or 0 → disabled (undefined), positive → as-is.
|
|
232
|
+
*/
|
|
218
233
|
function resolveTtl(ttl) {
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
234
|
+
if (ttl === void 0) return DEFAULT_TTL_MS;
|
|
235
|
+
if (ttl === false || ttl <= 0) return void 0;
|
|
236
|
+
return ttl;
|
|
222
237
|
}
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
238
|
+
/**
|
|
239
|
+
* creates a coalescing refresh scheduler with exponential-backoff retry on failure.
|
|
240
|
+
*
|
|
241
|
+
* at most one refresh runs at a time. if a new refresh is requested while one is in-flight,
|
|
242
|
+
* it is queued as pending — the queued slot keeps the latest `kind` passed via
|
|
243
|
+
* scheduleRefresh. once the in-flight refresh completes, at most one queued refresh
|
|
244
|
+
* starts, with that latest kind — collapsing any number of intermediate requests into one.
|
|
245
|
+
*
|
|
246
|
+
* on error, retries indefinitely with exponential backoff. the projected value keeps serving
|
|
247
|
+
* the stale cached value throughout (SWR). the `onRefreshed` callback receives the kind that
|
|
248
|
+
* triggered the just-completed refresh.
|
|
249
|
+
*/
|
|
250
|
+
function createRefresher(fn, onRefreshed, onStart) {
|
|
251
|
+
let refreshing = false;
|
|
252
|
+
let inFlightKind;
|
|
253
|
+
let pendingKind;
|
|
254
|
+
function scheduleRefresh(kind) {
|
|
255
|
+
if (refreshing) {
|
|
256
|
+
pendingKind = { kind };
|
|
257
|
+
return;
|
|
258
|
+
}
|
|
259
|
+
refreshing = true;
|
|
260
|
+
inFlightKind = kind;
|
|
261
|
+
onStart?.();
|
|
262
|
+
runWithBackoff(fn).then(() => {
|
|
263
|
+
const finished = inFlightKind;
|
|
264
|
+
refreshing = false;
|
|
265
|
+
inFlightKind = void 0;
|
|
266
|
+
onRefreshed(finished);
|
|
267
|
+
if (pendingKind) {
|
|
268
|
+
const { kind: next } = pendingKind;
|
|
269
|
+
pendingKind = void 0;
|
|
270
|
+
scheduleRefresh(next);
|
|
271
|
+
}
|
|
272
|
+
});
|
|
273
|
+
}
|
|
274
|
+
return scheduleRefresh;
|
|
248
275
|
}
|
|
249
276
|
function createRefreshChannel(onRefresh) {
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
277
|
+
const stream$ = createAsyncIterableSubject();
|
|
278
|
+
return {
|
|
279
|
+
stream$,
|
|
280
|
+
emit(event) {
|
|
281
|
+
stream$.next(event);
|
|
282
|
+
onRefresh?.(event);
|
|
283
|
+
},
|
|
284
|
+
complete() {
|
|
285
|
+
stream$.complete();
|
|
286
|
+
}
|
|
287
|
+
};
|
|
261
288
|
}
|
|
262
289
|
function createTtlOrchestrator(options) {
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
}
|
|
280
|
-
}
|
|
281
|
-
};
|
|
290
|
+
let timer;
|
|
291
|
+
return {
|
|
292
|
+
mark() {
|
|
293
|
+
if (timer !== void 0) {
|
|
294
|
+
clearTimeout(timer);
|
|
295
|
+
timer = void 0;
|
|
296
|
+
}
|
|
297
|
+
if (options.ttl === void 0) return;
|
|
298
|
+
timer = setTimeout(options.run, options.ttl);
|
|
299
|
+
},
|
|
300
|
+
clear() {
|
|
301
|
+
if (timer === void 0) return;
|
|
302
|
+
clearTimeout(timer);
|
|
303
|
+
timer = void 0;
|
|
304
|
+
}
|
|
305
|
+
};
|
|
282
306
|
}
|
|
283
307
|
function createRefreshByTtlQueue() {
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
308
|
+
const waiting = [];
|
|
309
|
+
let running = false;
|
|
310
|
+
function next() {
|
|
311
|
+
if (running) return;
|
|
312
|
+
const task = waiting.shift();
|
|
313
|
+
if (!task) return;
|
|
314
|
+
running = true;
|
|
315
|
+
(async () => {
|
|
316
|
+
try {
|
|
317
|
+
await task();
|
|
318
|
+
} catch {} finally {
|
|
319
|
+
running = false;
|
|
320
|
+
next();
|
|
321
|
+
}
|
|
322
|
+
})();
|
|
323
|
+
}
|
|
324
|
+
return { enqueue(task) {
|
|
325
|
+
waiting.push(task);
|
|
326
|
+
next();
|
|
327
|
+
} };
|
|
303
328
|
}
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
function mapFile(raw) {
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
329
|
+
//#endregion
|
|
330
|
+
//#region src/assets.ts
|
|
331
|
+
function mapFile$1(raw) {
|
|
332
|
+
return {
|
|
333
|
+
uid: raw.uid,
|
|
334
|
+
mimeType: raw.mimeType,
|
|
335
|
+
metadata: raw.metadata ? {
|
|
336
|
+
width: raw.metadata.width,
|
|
337
|
+
height: raw.metadata.height
|
|
338
|
+
} : void 0
|
|
339
|
+
};
|
|
312
340
|
}
|
|
313
|
-
function mapAsset(raw) {
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
341
|
+
function mapAsset$1(raw) {
|
|
342
|
+
return {
|
|
343
|
+
id: raw.id,
|
|
344
|
+
original: mapFile$1(raw.original),
|
|
345
|
+
preview: raw.preview ? mapFile$1(raw.preview) : void 0,
|
|
346
|
+
optimized: raw.optimized.map((file) => mapFile$1(file))
|
|
347
|
+
};
|
|
320
348
|
}
|
|
321
349
|
function createAssetsCollection(options, client, updates$, refreshByTtl) {
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
},
|
|
411
|
-
proxyHls(path, signal) {
|
|
412
|
-
return client.proxyHls(path, signal);
|
|
413
|
-
},
|
|
414
|
-
refresh() {
|
|
415
|
-
runFullRefresh("on-demand");
|
|
416
|
-
}
|
|
417
|
-
};
|
|
418
|
-
return {
|
|
419
|
-
instance,
|
|
420
|
-
destroy() {
|
|
421
|
-
unsubUpdates();
|
|
422
|
-
ttl.clear();
|
|
423
|
-
channel.complete();
|
|
424
|
-
}
|
|
425
|
-
};
|
|
350
|
+
const opts = options ?? {};
|
|
351
|
+
const cache = {
|
|
352
|
+
ttl: resolveTtl(opts.cache?.ttl),
|
|
353
|
+
eviction: opts.cache?.eviction ?? "refresh"
|
|
354
|
+
};
|
|
355
|
+
const channel = createRefreshChannel(opts.onRefresh);
|
|
356
|
+
const ttl = createTtlOrchestrator({
|
|
357
|
+
ttl: cache.ttl,
|
|
358
|
+
run: () => runFullRefresh("ttl")
|
|
359
|
+
});
|
|
360
|
+
let loaded = false;
|
|
361
|
+
const assetsSourceDoc = createSourceSubscription(schema.sources.storeAsset);
|
|
362
|
+
const projected = new ProjectedMap({
|
|
363
|
+
key: (asset) => asset.id,
|
|
364
|
+
values: (ids) => wrap("assets", async () => {
|
|
365
|
+
const items = (await collectAsync(mapAsync(client.subscribe(assetsSourceDoc, { ids }), (data) => data.source))).map((item) => mapAsset$1(item));
|
|
366
|
+
if (ids === void 0 && !loaded) {
|
|
367
|
+
loaded = true;
|
|
368
|
+
ttl.mark();
|
|
369
|
+
opts.onLoad?.(items.map((a) => a.id));
|
|
370
|
+
}
|
|
371
|
+
return items;
|
|
372
|
+
})
|
|
373
|
+
});
|
|
374
|
+
function emit(ids, kind) {
|
|
375
|
+
channel.emit({
|
|
376
|
+
ids,
|
|
377
|
+
kind
|
|
378
|
+
});
|
|
379
|
+
}
|
|
380
|
+
function runFullRefresh(kind) {
|
|
381
|
+
refreshByTtl.enqueue(() => runWithBackoff(async () => {
|
|
382
|
+
emit((await projected.refresh()).keys().toArray(), kind);
|
|
383
|
+
ttl.mark();
|
|
384
|
+
}));
|
|
385
|
+
}
|
|
386
|
+
function runPartialRefresh(refreshIds, deletedIds) {
|
|
387
|
+
const changedIds = [.../* @__PURE__ */ new Set([...refreshIds, ...deletedIds])];
|
|
388
|
+
if (refreshIds.length === 0) {
|
|
389
|
+
emit(changedIds, "upstream-update");
|
|
390
|
+
return;
|
|
391
|
+
}
|
|
392
|
+
runWithBackoff(async () => {
|
|
393
|
+
await projected.refresh(refreshIds);
|
|
394
|
+
emit(changedIds, "upstream-update");
|
|
395
|
+
});
|
|
396
|
+
}
|
|
397
|
+
const unsubUpdates = updates$.subscribe((batch) => {
|
|
398
|
+
if (!loaded || batch.asset.length === 0) return;
|
|
399
|
+
const deleted = [];
|
|
400
|
+
const upserted = /* @__PURE__ */ new Set();
|
|
401
|
+
for (const event of batch.asset) if (event.mutation === "delete") deleted.push(event.id);
|
|
402
|
+
else upserted.add(event.id);
|
|
403
|
+
if (cache.eviction === "clear") {
|
|
404
|
+
projected.clear();
|
|
405
|
+
return;
|
|
406
|
+
}
|
|
407
|
+
if (deleted.length > 0) projected.delete(deleted);
|
|
408
|
+
runPartialRefresh([...upserted], deleted);
|
|
409
|
+
});
|
|
410
|
+
return {
|
|
411
|
+
instance: {
|
|
412
|
+
refresh$: channel.stream$,
|
|
413
|
+
async load() {
|
|
414
|
+
if (!loaded) await projected.getAllAsMap();
|
|
415
|
+
},
|
|
416
|
+
get(idOrIds) {
|
|
417
|
+
return projected.get(idOrIds);
|
|
418
|
+
},
|
|
419
|
+
upload(data, meta, options) {
|
|
420
|
+
return client.upload(data, meta, options);
|
|
421
|
+
},
|
|
422
|
+
download(fileId) {
|
|
423
|
+
return client.download(fileId);
|
|
424
|
+
},
|
|
425
|
+
proxyHls(path, signal) {
|
|
426
|
+
return client.proxyHls(path, signal);
|
|
427
|
+
},
|
|
428
|
+
refresh() {
|
|
429
|
+
runFullRefresh("on-demand");
|
|
430
|
+
}
|
|
431
|
+
},
|
|
432
|
+
destroy() {
|
|
433
|
+
unsubUpdates();
|
|
434
|
+
ttl.clear();
|
|
435
|
+
channel.complete();
|
|
436
|
+
}
|
|
437
|
+
};
|
|
426
438
|
}
|
|
427
439
|
function createAssetsSyncCollection(options, client, updates$, refreshByTtl) {
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
destroy
|
|
443
|
-
};
|
|
440
|
+
const { instance: base, destroy } = createAssetsCollection(options, client, updates$, refreshByTtl);
|
|
441
|
+
function assertSync(value, method) {
|
|
442
|
+
if (value instanceof Promise) throw new TypeError(`assets.${method}() is not initialized yet — call assets.load() first`);
|
|
443
|
+
return value;
|
|
444
|
+
}
|
|
445
|
+
return {
|
|
446
|
+
instance: {
|
|
447
|
+
...base,
|
|
448
|
+
get(idOrIds) {
|
|
449
|
+
return assertSync(base.get(idOrIds), "get");
|
|
450
|
+
}
|
|
451
|
+
},
|
|
452
|
+
destroy
|
|
453
|
+
};
|
|
444
454
|
}
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
collectAsync as collectAsync2,
|
|
449
|
-
createSourceSubscription as createSourceSubscription2,
|
|
450
|
-
mapAsync as mapAsync2,
|
|
451
|
-
runWithBackoff as runWithBackoff3
|
|
452
|
-
} from "@contello/client";
|
|
453
|
-
import { ProjectedMap as ProjectedMap2, maybeThen } from "projected";
|
|
454
|
-
|
|
455
|
-
// src/dependency-collector.ts
|
|
455
|
+
//#endregion
|
|
456
|
+
//#region src/dependency-collector.ts
|
|
457
|
+
const EMPTY_KEYS = /* @__PURE__ */ new Set();
|
|
456
458
|
function createDependencyKey(target, id) {
|
|
457
|
-
|
|
459
|
+
return `${target}\0${id}`;
|
|
458
460
|
}
|
|
459
461
|
function createEntityDependencyKey(typename, id) {
|
|
460
|
-
|
|
462
|
+
return `entity\0${typename}\0${id}`;
|
|
461
463
|
}
|
|
462
464
|
function createRouteTargetKey(typename, id) {
|
|
463
|
-
|
|
465
|
+
return `routeTarget\0${typename}\0${id}`;
|
|
464
466
|
}
|
|
467
|
+
/**
|
|
468
|
+
* bidirectional dependency index for a single collection.
|
|
469
|
+
*
|
|
470
|
+
* during each map cycle, `createContext` is called per item. it creates a `MapperContext` the mapper
|
|
471
|
+
* uses to declare cross-entity dependencies, and a `register(key)` function the caller invokes
|
|
472
|
+
* once the item key is known. `register` flushes the collected deps into two maps:
|
|
473
|
+
*
|
|
474
|
+
* forwardIndex: item key -> set of dep keys it declared
|
|
475
|
+
* reverseIndex: dep key -> set of item keys that declared it
|
|
476
|
+
*
|
|
477
|
+
* the reverse map is the hot path: given an update event (typename + id), `getAffectedKeys()`
|
|
478
|
+
* looks up the dep key and returns all item keys that need to be invalidated.
|
|
479
|
+
*
|
|
480
|
+
* lifecycle per map cycle:
|
|
481
|
+
* 1. createContext(fn) — allocates deps array, creates MapperContext, calls fn(ctx, register)
|
|
482
|
+
* 2. fn calls map(raw, ctx) — mapper runs, calls track methods, fills deps array
|
|
483
|
+
* 3. fn calls register(key) — real item key is known, deps written into both maps
|
|
484
|
+
*/
|
|
465
485
|
var DependencyCollector = class {
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
);
|
|
584
|
-
}
|
|
585
|
-
}
|
|
586
|
-
/**
|
|
587
|
-
* removes all index entries for one item key.
|
|
588
|
-
* cleans up the forward entry and removes the item from every reverse set it appeared in.
|
|
589
|
-
* if a reverse set becomes empty after removal, it is deleted to avoid memory leaks.
|
|
590
|
-
*/
|
|
591
|
-
clearItem(key) {
|
|
592
|
-
const existingDeps = this.forwardIndex.get(key);
|
|
593
|
-
if (!existingDeps) {
|
|
594
|
-
return;
|
|
595
|
-
}
|
|
596
|
-
for (const dk of existingDeps) {
|
|
597
|
-
const set = this.reverseIndex.get(dk);
|
|
598
|
-
if (set) {
|
|
599
|
-
set.delete(key);
|
|
600
|
-
if (set.size === 0) {
|
|
601
|
-
this.reverseIndex.delete(dk);
|
|
602
|
-
}
|
|
603
|
-
}
|
|
604
|
-
}
|
|
605
|
-
this.forwardIndex.delete(key);
|
|
606
|
-
}
|
|
486
|
+
model;
|
|
487
|
+
resolver;
|
|
488
|
+
/** dep key -> item keys that declared a dependency on it */
|
|
489
|
+
reverseIndex = /* @__PURE__ */ new Map();
|
|
490
|
+
/** item key -> dep keys it currently declares */
|
|
491
|
+
forwardIndex = /* @__PURE__ */ new Map();
|
|
492
|
+
constructor(model, resolver) {
|
|
493
|
+
this.model = model;
|
|
494
|
+
this.resolver = resolver;
|
|
495
|
+
}
|
|
496
|
+
mergeRouteTargetKeys(existing, entityType, entityId) {
|
|
497
|
+
const items = this.reverseIndex.get(createRouteTargetKey(entityType, entityId));
|
|
498
|
+
if (!items) return existing;
|
|
499
|
+
return existing ? existing.union(items) : items;
|
|
500
|
+
}
|
|
501
|
+
warnIfUnknownModel(model) {
|
|
502
|
+
if (!this.resolver.hasModel(model)) console.warn(`[contello/store] tracking model "${model}" which is not in models — updates for this model will be ignored`);
|
|
503
|
+
}
|
|
504
|
+
/**
|
|
505
|
+
* removes all index entries for one item key.
|
|
506
|
+
* cleans up the forward entry and removes the item from every reverse set it appeared in.
|
|
507
|
+
* if a reverse set becomes empty after removal, it is deleted to avoid memory leaks.
|
|
508
|
+
*/
|
|
509
|
+
clearItem(key) {
|
|
510
|
+
const existingDeps = this.forwardIndex.get(key);
|
|
511
|
+
if (!existingDeps) return;
|
|
512
|
+
for (const dk of existingDeps) {
|
|
513
|
+
const set = this.reverseIndex.get(dk);
|
|
514
|
+
if (set) {
|
|
515
|
+
set.delete(key);
|
|
516
|
+
if (set.size === 0) this.reverseIndex.delete(dk);
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
this.forwardIndex.delete(key);
|
|
520
|
+
}
|
|
521
|
+
/**
|
|
522
|
+
* creates a scoped MapperContext for one item and runs `fn` with it.
|
|
523
|
+
* `fn` receives the context (for declaring deps during mapping) and a `register(key)` function
|
|
524
|
+
* that must be called once the item key is known — it writes all declared deps into the index.
|
|
525
|
+
* returns whatever `fn` returns, preserving MaybePromise semantics.
|
|
526
|
+
*/
|
|
527
|
+
createContext(fn) {
|
|
528
|
+
const deps = [];
|
|
529
|
+
const ctx = {
|
|
530
|
+
track: (obj) => {
|
|
531
|
+
if (!obj.id) throw new Error(`track(): object with __typename "${obj.__typename}" has no "id" field`);
|
|
532
|
+
switch (obj.__typename) {
|
|
533
|
+
case "ContelloAsset":
|
|
534
|
+
deps.push(createDependencyKey("asset", obj.id));
|
|
535
|
+
break;
|
|
536
|
+
case "ContelloRoute":
|
|
537
|
+
deps.push(createDependencyKey("route", obj.id));
|
|
538
|
+
break;
|
|
539
|
+
default: {
|
|
540
|
+
const model = this.resolver.getModel(obj.__typename);
|
|
541
|
+
if (!model) throw new Error(`track(): unexpected __typename "${obj.__typename}" — expected ContelloAsset, ContelloRoute, or a known entity model`);
|
|
542
|
+
deps.push(createEntityDependencyKey(model, obj.id));
|
|
543
|
+
}
|
|
544
|
+
}
|
|
545
|
+
},
|
|
546
|
+
trackEntity: (model, id) => {
|
|
547
|
+
this.warnIfUnknownModel(model);
|
|
548
|
+
deps.push(createEntityDependencyKey(model, id));
|
|
549
|
+
},
|
|
550
|
+
trackAsset: (id) => {
|
|
551
|
+
deps.push(createDependencyKey("asset", id));
|
|
552
|
+
},
|
|
553
|
+
trackRoute: (id) => {
|
|
554
|
+
deps.push(createDependencyKey("route", id));
|
|
555
|
+
}
|
|
556
|
+
};
|
|
557
|
+
const register = (key) => {
|
|
558
|
+
this.clearItem(key);
|
|
559
|
+
deps.push(createRouteTargetKey(this.model, String(key)));
|
|
560
|
+
const fwd = /* @__PURE__ */ new Set();
|
|
561
|
+
for (const dk of deps) {
|
|
562
|
+
fwd.add(dk);
|
|
563
|
+
let set = this.reverseIndex.get(dk);
|
|
564
|
+
if (!set) {
|
|
565
|
+
set = /* @__PURE__ */ new Set();
|
|
566
|
+
this.reverseIndex.set(dk, set);
|
|
567
|
+
}
|
|
568
|
+
set.add(key);
|
|
569
|
+
}
|
|
570
|
+
if (fwd.size > 0) this.forwardIndex.set(key, fwd);
|
|
571
|
+
};
|
|
572
|
+
return fn(ctx, register);
|
|
573
|
+
}
|
|
574
|
+
/**
|
|
575
|
+
* looks up which item keys are affected by an update event.
|
|
576
|
+
* for route events, checks route-target deps for both the new and previous entity targets.
|
|
577
|
+
*/
|
|
578
|
+
getAffectedKeys(event) {
|
|
579
|
+
if (event.target === "entity") return this.reverseIndex.get(createEntityDependencyKey(event.model, event.id)) ?? EMPTY_KEYS;
|
|
580
|
+
let affected = this.reverseIndex.get(createDependencyKey(event.target, event.id));
|
|
581
|
+
if (event.target === "route") {
|
|
582
|
+
if ("after" in event && event.after.type === "entity") affected = this.mergeRouteTargetKeys(affected, event.after.model, event.after.entityId);
|
|
583
|
+
if ("before" in event && event.before.type === "entity") affected = this.mergeRouteTargetKeys(affected, event.before.model, event.before.entityId);
|
|
584
|
+
}
|
|
585
|
+
return affected ?? EMPTY_KEYS;
|
|
586
|
+
}
|
|
587
|
+
/** removes all recorded deps for a single item (called on eviction) */
|
|
588
|
+
removeItem(key) {
|
|
589
|
+
this.clearItem(key);
|
|
590
|
+
}
|
|
591
|
+
/** drops all recorded deps — used when clearing the entire cache */
|
|
592
|
+
clear() {
|
|
593
|
+
this.reverseIndex.clear();
|
|
594
|
+
this.forwardIndex.clear();
|
|
595
|
+
}
|
|
596
|
+
/**
|
|
597
|
+
* removes index entries for any item key NOT present in `keys`.
|
|
598
|
+
* called after a full re-fetch to prune refs from items that were deleted upstream.
|
|
599
|
+
*/
|
|
600
|
+
retainOnly(keys) {
|
|
601
|
+
for (const key of this.forwardIndex.keys()) if (!keys.has(key)) this.clearItem(key);
|
|
602
|
+
}
|
|
607
603
|
};
|
|
608
|
-
|
|
609
|
-
|
|
604
|
+
//#endregion
|
|
605
|
+
//#region src/collection.ts
|
|
610
606
|
function fetchCollection(source, client, ids) {
|
|
611
|
-
|
|
612
|
-
mapAsync2(
|
|
613
|
-
client.subscribe(createSourceSubscription2(source), { ids }),
|
|
614
|
-
(r) => r.source
|
|
615
|
-
)
|
|
616
|
-
);
|
|
607
|
+
return collectAsync(mapAsync(client.subscribe(createSourceSubscription(source), { ids }), (r) => r.source));
|
|
617
608
|
}
|
|
618
609
|
function createCollection(source, options, client, updates$, resolver, refreshByTtl) {
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
for (const id of deleted) {
|
|
726
|
-
dependencyCollector.removeItem(id);
|
|
727
|
-
}
|
|
728
|
-
if (deleted.length > 0) {
|
|
729
|
-
projected.delete(deleted);
|
|
730
|
-
}
|
|
731
|
-
const refreshIds = /* @__PURE__ */ new Set([...upserted, ...depAffected]);
|
|
732
|
-
for (const id of deleted) {
|
|
733
|
-
refreshIds.delete(id);
|
|
734
|
-
}
|
|
735
|
-
runPartialRefresh([...refreshIds], deleted);
|
|
736
|
-
});
|
|
737
|
-
const instance = {
|
|
738
|
-
name: _def.name,
|
|
739
|
-
refresh$: channel.stream$,
|
|
740
|
-
get(idOrIds) {
|
|
741
|
-
return projected.get(idOrIds);
|
|
742
|
-
},
|
|
743
|
-
getAll() {
|
|
744
|
-
return projected.getAll();
|
|
745
|
-
},
|
|
746
|
-
refresh() {
|
|
747
|
-
runFullRefresh("on-demand");
|
|
748
|
-
},
|
|
749
|
-
async load() {
|
|
750
|
-
if (!loaded) {
|
|
751
|
-
await projected.getAllAsMap();
|
|
752
|
-
}
|
|
753
|
-
}
|
|
754
|
-
};
|
|
755
|
-
return {
|
|
756
|
-
instance,
|
|
757
|
-
destroy() {
|
|
758
|
-
unsubUpdates();
|
|
759
|
-
ttl.clear();
|
|
760
|
-
channel.complete();
|
|
761
|
-
}
|
|
762
|
-
};
|
|
610
|
+
const opts = options ?? {};
|
|
611
|
+
const mapFn = opts.map ?? ((item) => item);
|
|
612
|
+
const _def = {
|
|
613
|
+
name: opts.name ?? source.__model,
|
|
614
|
+
model: source.__model,
|
|
615
|
+
cache: {
|
|
616
|
+
ttl: resolveTtl(opts.cache?.ttl),
|
|
617
|
+
eviction: opts.cache?.eviction ?? "refresh"
|
|
618
|
+
}
|
|
619
|
+
};
|
|
620
|
+
const dependencyCollector = new DependencyCollector(_def.model, resolver);
|
|
621
|
+
const channel = createRefreshChannel(opts.onRefresh);
|
|
622
|
+
const ttl = createTtlOrchestrator({
|
|
623
|
+
ttl: _def.cache.ttl,
|
|
624
|
+
run: () => runFullRefresh("ttl")
|
|
625
|
+
});
|
|
626
|
+
let loaded = false;
|
|
627
|
+
const projected = new ProjectedMap({
|
|
628
|
+
key: (item) => item.id,
|
|
629
|
+
values: (ids) => wrap(`collection:${_def.name}`, () => maybeThen$1(fetchCollection(source, client, ids), async (rawItems) => {
|
|
630
|
+
const items = await Promise.all(rawItems.map((item) => Promise.resolve(dependencyCollector.createContext((ref, register) => maybeThen$1(mapFn(item, ref), (mapped) => {
|
|
631
|
+
register(mapped.id);
|
|
632
|
+
return mapped;
|
|
633
|
+
})))));
|
|
634
|
+
if (ids === void 0) {
|
|
635
|
+
dependencyCollector.retainOnly(new Set(items.map((item) => item.id)));
|
|
636
|
+
if (!loaded) {
|
|
637
|
+
loaded = true;
|
|
638
|
+
ttl.mark();
|
|
639
|
+
opts.onLoad?.(items.map((item) => item.id));
|
|
640
|
+
}
|
|
641
|
+
} else {
|
|
642
|
+
const returnedIds = new Set(items.map((item) => item.id));
|
|
643
|
+
for (const id of ids) if (!returnedIds.has(id)) dependencyCollector.removeItem(id);
|
|
644
|
+
}
|
|
645
|
+
return items;
|
|
646
|
+
})),
|
|
647
|
+
sort: opts.sort
|
|
648
|
+
});
|
|
649
|
+
function emit(ids, kind) {
|
|
650
|
+
channel.emit({
|
|
651
|
+
ids,
|
|
652
|
+
kind
|
|
653
|
+
});
|
|
654
|
+
}
|
|
655
|
+
function runFullRefresh(kind) {
|
|
656
|
+
refreshByTtl.enqueue(() => runWithBackoff(async () => {
|
|
657
|
+
emit((await projected.refresh()).keys().toArray(), kind);
|
|
658
|
+
ttl.mark();
|
|
659
|
+
}));
|
|
660
|
+
}
|
|
661
|
+
function runPartialRefresh(refreshIds, deletedIds) {
|
|
662
|
+
const changedIds = [.../* @__PURE__ */ new Set([...refreshIds, ...deletedIds])];
|
|
663
|
+
if (refreshIds.length === 0) {
|
|
664
|
+
emit(changedIds, "upstream-update");
|
|
665
|
+
return;
|
|
666
|
+
}
|
|
667
|
+
runWithBackoff(async () => {
|
|
668
|
+
await projected.refresh(refreshIds);
|
|
669
|
+
emit(changedIds, "upstream-update");
|
|
670
|
+
});
|
|
671
|
+
}
|
|
672
|
+
const unsubUpdates = updates$.subscribe((batch) => {
|
|
673
|
+
if (!loaded) return;
|
|
674
|
+
const ownEvents = batch.entity.get(_def.model) ?? [];
|
|
675
|
+
const deleted = [];
|
|
676
|
+
const upserted = /* @__PURE__ */ new Set();
|
|
677
|
+
for (const event of ownEvents) if (event.mutation === "delete") deleted.push(event.id);
|
|
678
|
+
else upserted.add(event.id);
|
|
679
|
+
const depAffected = /* @__PURE__ */ new Set();
|
|
680
|
+
for (const event of batch.events) for (const id of dependencyCollector.getAffectedKeys(event)) depAffected.add(id);
|
|
681
|
+
if (deleted.length === 0 && upserted.size === 0 && depAffected.size === 0) return;
|
|
682
|
+
if (_def.cache.eviction === "clear") {
|
|
683
|
+
dependencyCollector.clear();
|
|
684
|
+
projected.clear();
|
|
685
|
+
return;
|
|
686
|
+
}
|
|
687
|
+
for (const id of deleted) dependencyCollector.removeItem(id);
|
|
688
|
+
if (deleted.length > 0) projected.delete(deleted);
|
|
689
|
+
const refreshIds = upserted.union(depAffected);
|
|
690
|
+
for (const id of deleted) refreshIds.delete(id);
|
|
691
|
+
runPartialRefresh([...refreshIds], deleted);
|
|
692
|
+
});
|
|
693
|
+
return {
|
|
694
|
+
instance: {
|
|
695
|
+
name: _def.name,
|
|
696
|
+
refresh$: channel.stream$,
|
|
697
|
+
get(idOrIds) {
|
|
698
|
+
return projected.get(idOrIds);
|
|
699
|
+
},
|
|
700
|
+
getAll() {
|
|
701
|
+
return projected.getAll();
|
|
702
|
+
},
|
|
703
|
+
refresh() {
|
|
704
|
+
runFullRefresh("on-demand");
|
|
705
|
+
},
|
|
706
|
+
async load() {
|
|
707
|
+
if (!loaded) await projected.getAllAsMap();
|
|
708
|
+
}
|
|
709
|
+
},
|
|
710
|
+
destroy() {
|
|
711
|
+
unsubUpdates();
|
|
712
|
+
ttl.clear();
|
|
713
|
+
channel.complete();
|
|
714
|
+
}
|
|
715
|
+
};
|
|
763
716
|
}
|
|
764
717
|
function createCollectionSync(source, options, client, updates$, resolver, refreshByTtl) {
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
get(idOrIds) {
|
|
783
|
-
return assertSync(base.get(idOrIds), "get");
|
|
784
|
-
},
|
|
785
|
-
getAll() {
|
|
786
|
-
return assertSync(base.getAll(), "getAll");
|
|
787
|
-
}
|
|
788
|
-
},
|
|
789
|
-
destroy
|
|
790
|
-
};
|
|
718
|
+
const { instance: base, destroy } = createCollection(source, options, client, updates$, resolver, refreshByTtl);
|
|
719
|
+
function assertSync(value, method) {
|
|
720
|
+
if (value instanceof Promise) throw new TypeError(`collection "${base.name}".${method}() is not initialized yet — call collection.load() first`);
|
|
721
|
+
return value;
|
|
722
|
+
}
|
|
723
|
+
return {
|
|
724
|
+
instance: {
|
|
725
|
+
...base,
|
|
726
|
+
get(idOrIds) {
|
|
727
|
+
return assertSync(base.get(idOrIds), "get");
|
|
728
|
+
},
|
|
729
|
+
getAll() {
|
|
730
|
+
return assertSync(base.getAll(), "getAll");
|
|
731
|
+
}
|
|
732
|
+
},
|
|
733
|
+
destroy
|
|
734
|
+
};
|
|
791
735
|
}
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
import {
|
|
795
|
-
collectAsync as collectAsync3,
|
|
796
|
-
createSourceSubscription as createSourceSubscription3,
|
|
797
|
-
mapAsync as mapAsync3,
|
|
798
|
-
runWithBackoff as runWithBackoff4
|
|
799
|
-
} from "@contello/client";
|
|
800
|
-
import { ProjectedMap as ProjectedMap3, maybeThen as maybeThen2 } from "projected";
|
|
736
|
+
//#endregion
|
|
737
|
+
//#region src/i18n.ts
|
|
801
738
|
function toGqlMessageInput(msg) {
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
739
|
+
return {
|
|
740
|
+
token: msg.token,
|
|
741
|
+
example: msg.example,
|
|
742
|
+
description: msg.description,
|
|
743
|
+
variables: msg.variables?.map((v) => ({
|
|
744
|
+
name: v.name,
|
|
745
|
+
description: v.description,
|
|
746
|
+
example: v.example
|
|
747
|
+
})),
|
|
748
|
+
translations: msg.translations?.map((t) => ({
|
|
749
|
+
language: t.language,
|
|
750
|
+
message: t.message
|
|
751
|
+
})),
|
|
752
|
+
initialTranslations: msg.initialTranslations?.map((t) => ({
|
|
753
|
+
language: t.language,
|
|
754
|
+
message: t.message
|
|
755
|
+
}))
|
|
756
|
+
};
|
|
810
757
|
}
|
|
811
758
|
function createI18nMessagesCollection(def, client, updates$, refreshByTtl) {
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
return {
|
|
907
|
-
refresh$: channel.stream$,
|
|
908
|
-
get(idOrIds) {
|
|
909
|
-
return projected.get(idOrIds);
|
|
910
|
-
},
|
|
911
|
-
getAll() {
|
|
912
|
-
return projected.getAll();
|
|
913
|
-
},
|
|
914
|
-
register(messages) {
|
|
915
|
-
return wrap(
|
|
916
|
-
`i18n-register:${def.collection}`,
|
|
917
|
-
() => client.execute(storeRegisterI18nMessagesDocument, {
|
|
918
|
-
collection: def.collection,
|
|
919
|
-
messages: messages.map((message) => toGqlMessageInput(message))
|
|
920
|
-
}).then(() => {
|
|
921
|
-
})
|
|
922
|
-
);
|
|
923
|
-
},
|
|
924
|
-
refresh() {
|
|
925
|
-
scheduleRefresh("on-demand");
|
|
926
|
-
},
|
|
927
|
-
destroy() {
|
|
928
|
-
unsubUpdates();
|
|
929
|
-
ttl.clear();
|
|
930
|
-
channel.complete();
|
|
931
|
-
}
|
|
932
|
-
};
|
|
759
|
+
const channel = createRefreshChannel(def.onRefresh);
|
|
760
|
+
const ttl = createTtlOrchestrator({
|
|
761
|
+
ttl: resolveTtl(def.cache?.ttl),
|
|
762
|
+
run: () => runTtlRefresh()
|
|
763
|
+
});
|
|
764
|
+
let loaded = false;
|
|
765
|
+
const i18nSourceDoc = createSourceSubscription(schema.sources.storeI18nMessage);
|
|
766
|
+
const projected = new ProjectedMap({
|
|
767
|
+
key: (msg) => msg.id,
|
|
768
|
+
values: (ids) => wrap(`i18n:${def.collection}`, async () => {
|
|
769
|
+
const items = (await collectAsync(mapAsync(client.subscribe(i18nSourceDoc, {
|
|
770
|
+
collection: def.collection,
|
|
771
|
+
ids
|
|
772
|
+
}), (data) => data.source))).map((msg) => ({
|
|
773
|
+
id: msg.id,
|
|
774
|
+
token: msg.token,
|
|
775
|
+
translations: new Map(msg.translations.map((t) => [t.language, t.value]))
|
|
776
|
+
}));
|
|
777
|
+
if (ids === void 0 && !loaded) {
|
|
778
|
+
loaded = true;
|
|
779
|
+
ttl.mark();
|
|
780
|
+
}
|
|
781
|
+
return items;
|
|
782
|
+
})
|
|
783
|
+
});
|
|
784
|
+
function emit(ids, kind) {
|
|
785
|
+
channel.emit({
|
|
786
|
+
ids,
|
|
787
|
+
kind
|
|
788
|
+
});
|
|
789
|
+
}
|
|
790
|
+
function emitWithCurrentIds(kind) {
|
|
791
|
+
maybeThen$1(projected.getAll(), (msgs) => {
|
|
792
|
+
emit(msgs.map((m) => m.id), kind);
|
|
793
|
+
});
|
|
794
|
+
}
|
|
795
|
+
function runTtlRefresh() {
|
|
796
|
+
refreshByTtl.enqueue(async () => {
|
|
797
|
+
await runWithBackoff(() => projected.refresh());
|
|
798
|
+
emitWithCurrentIds("ttl");
|
|
799
|
+
ttl.mark();
|
|
800
|
+
});
|
|
801
|
+
}
|
|
802
|
+
const scheduleRefresh = createRefresher(() => projected.refresh(), (kind) => {
|
|
803
|
+
emitWithCurrentIds(kind);
|
|
804
|
+
ttl.mark();
|
|
805
|
+
}, () => {});
|
|
806
|
+
function runPartialRefresh(upsertedIds, deletedIds) {
|
|
807
|
+
const changedIds = [.../* @__PURE__ */ new Set([...upsertedIds, ...deletedIds])];
|
|
808
|
+
if (changedIds.length === 0) return;
|
|
809
|
+
if (upsertedIds.length === 0) {
|
|
810
|
+
emit(changedIds, "upstream-update");
|
|
811
|
+
return;
|
|
812
|
+
}
|
|
813
|
+
runWithBackoff(async () => {
|
|
814
|
+
await projected.refresh(upsertedIds);
|
|
815
|
+
emit(changedIds, "upstream-update");
|
|
816
|
+
});
|
|
817
|
+
}
|
|
818
|
+
const unsubUpdates = updates$.subscribe((batch) => {
|
|
819
|
+
if (!loaded) return;
|
|
820
|
+
if (batch.i18nMessage.length === 0) return;
|
|
821
|
+
const deleted = [];
|
|
822
|
+
const upserted = /* @__PURE__ */ new Set();
|
|
823
|
+
for (const event of batch.i18nMessage) if (event.mutation === "delete") deleted.push(event.id);
|
|
824
|
+
else upserted.add(event.id);
|
|
825
|
+
if (deleted.length > 0) projected.delete(deleted);
|
|
826
|
+
runPartialRefresh([...upserted], deleted);
|
|
827
|
+
});
|
|
828
|
+
return {
|
|
829
|
+
refresh$: channel.stream$,
|
|
830
|
+
get(idOrIds) {
|
|
831
|
+
return projected.get(idOrIds);
|
|
832
|
+
},
|
|
833
|
+
getAll() {
|
|
834
|
+
return projected.getAll();
|
|
835
|
+
},
|
|
836
|
+
register(messages) {
|
|
837
|
+
return wrap(`i18n-register:${def.collection}`, async () => {
|
|
838
|
+
await client.execute(storeRegisterI18nMessagesDocument, {
|
|
839
|
+
collection: def.collection,
|
|
840
|
+
messages: messages.map((message) => toGqlMessageInput(message))
|
|
841
|
+
});
|
|
842
|
+
});
|
|
843
|
+
},
|
|
844
|
+
refresh() {
|
|
845
|
+
scheduleRefresh("on-demand");
|
|
846
|
+
},
|
|
847
|
+
destroy() {
|
|
848
|
+
unsubUpdates();
|
|
849
|
+
ttl.clear();
|
|
850
|
+
channel.complete();
|
|
851
|
+
}
|
|
852
|
+
};
|
|
933
853
|
}
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
import {
|
|
937
|
-
collectAsync as collectAsync4,
|
|
938
|
-
createSourceSubscription as createSourceSubscription4,
|
|
939
|
-
mapAsync as mapAsync4
|
|
940
|
-
} from "@contello/client";
|
|
941
|
-
import { ProjectedLazyMap } from "projected";
|
|
942
|
-
|
|
943
|
-
// src/lru.ts
|
|
944
|
-
import { LRUCache } from "lru-cache";
|
|
854
|
+
//#endregion
|
|
855
|
+
//#region src/lru.ts
|
|
945
856
|
function createLruCache(options) {
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
857
|
+
const { max, ttl, onEvict: dispose } = options;
|
|
858
|
+
const lru = new LRUCache({
|
|
859
|
+
max,
|
|
860
|
+
...ttl !== void 0 && {
|
|
861
|
+
ttl,
|
|
862
|
+
ttlAutopurge: false
|
|
863
|
+
},
|
|
864
|
+
...dispose !== void 0 && { dispose }
|
|
865
|
+
});
|
|
866
|
+
return {
|
|
867
|
+
has: (key) => lru.has(key),
|
|
868
|
+
get: (key) => lru.get(key),
|
|
869
|
+
set: (key, value) => {
|
|
870
|
+
lru.set(key, value);
|
|
871
|
+
},
|
|
872
|
+
delete: (key) => {
|
|
873
|
+
lru.delete(key);
|
|
874
|
+
},
|
|
875
|
+
clear: () => {
|
|
876
|
+
lru.clear();
|
|
877
|
+
},
|
|
878
|
+
keys: () => lru.keys().toArray()
|
|
879
|
+
};
|
|
966
880
|
}
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
function
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
881
|
+
//#endregion
|
|
882
|
+
//#region src/lazy-assets.ts
|
|
883
|
+
function mapFile(raw) {
|
|
884
|
+
return {
|
|
885
|
+
uid: raw.uid,
|
|
886
|
+
mimeType: raw.mimeType,
|
|
887
|
+
metadata: raw.metadata ? {
|
|
888
|
+
width: raw.metadata.width,
|
|
889
|
+
height: raw.metadata.height
|
|
890
|
+
} : void 0
|
|
891
|
+
};
|
|
975
892
|
}
|
|
976
|
-
function
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
893
|
+
function mapAsset(raw) {
|
|
894
|
+
return {
|
|
895
|
+
id: raw.id,
|
|
896
|
+
original: mapFile(raw.original),
|
|
897
|
+
preview: raw.preview ? mapFile(raw.preview) : void 0,
|
|
898
|
+
optimized: raw.optimized.map((file) => mapFile(file))
|
|
899
|
+
};
|
|
983
900
|
}
|
|
984
901
|
function createLazyAssetsCollection(def, client, updates$) {
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
refresh() {
|
|
1050
|
-
scheduleRefresh("on-demand");
|
|
1051
|
-
},
|
|
1052
|
-
clear() {
|
|
1053
|
-
projected.clear();
|
|
1054
|
-
}
|
|
1055
|
-
},
|
|
1056
|
-
destroy() {
|
|
1057
|
-
unsubUpdates();
|
|
1058
|
-
channel.complete();
|
|
1059
|
-
}
|
|
1060
|
-
};
|
|
902
|
+
const _def = { cache: {
|
|
903
|
+
max: def?.cache?.max ?? 1e3,
|
|
904
|
+
ttl: resolveTtl(def?.cache?.ttl)
|
|
905
|
+
} };
|
|
906
|
+
const cache = createLruCache({
|
|
907
|
+
max: _def.cache.max,
|
|
908
|
+
ttl: _def.cache.ttl,
|
|
909
|
+
onEvict: void 0
|
|
910
|
+
});
|
|
911
|
+
const assetsSourceDoc = createSourceSubscription(schema.sources.storeAsset);
|
|
912
|
+
const projected = new ProjectedLazyMap({
|
|
913
|
+
key: (asset) => asset.id,
|
|
914
|
+
values: (ids) => wrap("assets", async () => {
|
|
915
|
+
return (await collectAsync(mapAsync(client.subscribe(assetsSourceDoc, { ids }), (data) => data.source))).map((item) => mapAsset(item));
|
|
916
|
+
}),
|
|
917
|
+
cache
|
|
918
|
+
});
|
|
919
|
+
const channel = createRefreshChannel(def?.onRefresh);
|
|
920
|
+
function emit(ids, kind) {
|
|
921
|
+
channel.emit({
|
|
922
|
+
ids,
|
|
923
|
+
kind
|
|
924
|
+
});
|
|
925
|
+
}
|
|
926
|
+
let lastRefreshKeys = [];
|
|
927
|
+
const scheduleRefresh = createRefresher(async () => {
|
|
928
|
+
lastRefreshKeys = cache.keys();
|
|
929
|
+
if (lastRefreshKeys.length === 0) return;
|
|
930
|
+
await projected.refresh(lastRefreshKeys);
|
|
931
|
+
}, (kind) => {
|
|
932
|
+
if (lastRefreshKeys.length > 0) emit(lastRefreshKeys, kind);
|
|
933
|
+
}, () => {});
|
|
934
|
+
const unsubUpdates = updates$.subscribe((batch) => {
|
|
935
|
+
if (batch.asset.length === 0) return;
|
|
936
|
+
for (const event of batch.asset) projected.delete(event.id);
|
|
937
|
+
emit(batch.asset.map((e) => e.id), "upstream-update");
|
|
938
|
+
});
|
|
939
|
+
return {
|
|
940
|
+
instance: {
|
|
941
|
+
refresh$: channel.stream$,
|
|
942
|
+
get(idOrIds) {
|
|
943
|
+
return projected.get(idOrIds);
|
|
944
|
+
},
|
|
945
|
+
upload(data, meta, options) {
|
|
946
|
+
return client.upload(data, meta, options);
|
|
947
|
+
},
|
|
948
|
+
download(fileId) {
|
|
949
|
+
return client.download(fileId);
|
|
950
|
+
},
|
|
951
|
+
proxyHls(path, signal) {
|
|
952
|
+
return client.proxyHls(path, signal);
|
|
953
|
+
},
|
|
954
|
+
refresh() {
|
|
955
|
+
scheduleRefresh("on-demand");
|
|
956
|
+
},
|
|
957
|
+
clear() {
|
|
958
|
+
projected.clear();
|
|
959
|
+
}
|
|
960
|
+
},
|
|
961
|
+
destroy() {
|
|
962
|
+
unsubUpdates();
|
|
963
|
+
channel.complete();
|
|
964
|
+
}
|
|
965
|
+
};
|
|
1061
966
|
}
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
import {
|
|
1065
|
-
collectAsync as collectAsync5,
|
|
1066
|
-
createAsyncIterableSubject as createAsyncIterableSubject2,
|
|
1067
|
-
createSourceSubscription as createSourceSubscription5,
|
|
1068
|
-
mapAsync as mapAsync5
|
|
1069
|
-
} from "@contello/client";
|
|
1070
|
-
import { ProjectedLazyMap as ProjectedLazyMap2, maybeThen as maybeThen3 } from "projected";
|
|
967
|
+
//#endregion
|
|
968
|
+
//#region src/lazy-collection.ts
|
|
1071
969
|
function createLazyCollection(source, options, client, updates$, resolver) {
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
}
|
|
1147
|
-
}
|
|
1148
|
-
for (const event of batch.events) {
|
|
1149
|
-
for (const key of dependencyCollector.getAffectedKeys(event)) {
|
|
1150
|
-
evicted.add(key);
|
|
1151
|
-
}
|
|
1152
|
-
}
|
|
1153
|
-
if (evicted.size > 0) {
|
|
1154
|
-
for (const key of evicted) {
|
|
1155
|
-
dependencyCollector.removeItem(key);
|
|
1156
|
-
projected.delete(key);
|
|
1157
|
-
}
|
|
1158
|
-
emit([...evicted], "upstream-update");
|
|
1159
|
-
}
|
|
1160
|
-
});
|
|
1161
|
-
return {
|
|
1162
|
-
instance: {
|
|
1163
|
-
name: _def.name,
|
|
1164
|
-
refresh$,
|
|
1165
|
-
get(idOrIds) {
|
|
1166
|
-
return projected.get(idOrIds);
|
|
1167
|
-
},
|
|
1168
|
-
refresh() {
|
|
1169
|
-
scheduleRefresh("on-demand");
|
|
1170
|
-
},
|
|
1171
|
-
clear() {
|
|
1172
|
-
dependencyCollector.clear();
|
|
1173
|
-
projected.clear();
|
|
1174
|
-
}
|
|
1175
|
-
},
|
|
1176
|
-
destroy() {
|
|
1177
|
-
unsubUpdates();
|
|
1178
|
-
refresh$.complete();
|
|
1179
|
-
}
|
|
1180
|
-
};
|
|
970
|
+
const opts = options ?? {};
|
|
971
|
+
const mapFn = opts.map ?? ((item) => item);
|
|
972
|
+
const _def = {
|
|
973
|
+
name: opts.name ?? source.__model,
|
|
974
|
+
model: source.__model,
|
|
975
|
+
cache: {
|
|
976
|
+
max: opts.cache?.max ?? 1e3,
|
|
977
|
+
ttl: resolveTtl(opts.cache?.ttl)
|
|
978
|
+
}
|
|
979
|
+
};
|
|
980
|
+
const dependencyCollector = new DependencyCollector(_def.model, resolver);
|
|
981
|
+
const cache = createLruCache({
|
|
982
|
+
max: _def.cache.max,
|
|
983
|
+
ttl: _def.cache.ttl,
|
|
984
|
+
onEvict: (_value, key) => dependencyCollector.removeItem(key)
|
|
985
|
+
});
|
|
986
|
+
const projected = new ProjectedLazyMap({
|
|
987
|
+
key: (item) => item.id,
|
|
988
|
+
values: (keys) => wrap(`lazy-collection:${_def.name}`, () => maybeThen$1(collectAsync(mapAsync(client.subscribe(createSourceSubscription(source), { ids: keys }), (r) => r.source)), (rawItems) => Promise.all(rawItems.map((item) => Promise.resolve(dependencyCollector.createContext((ref, register) => maybeThen$1(mapFn(item, ref), (mapped) => {
|
|
989
|
+
register(mapped.id);
|
|
990
|
+
return mapped;
|
|
991
|
+
}))))))),
|
|
992
|
+
cache
|
|
993
|
+
});
|
|
994
|
+
const refresh$ = createAsyncIterableSubject();
|
|
995
|
+
function emit(ids, kind) {
|
|
996
|
+
const event = {
|
|
997
|
+
ids,
|
|
998
|
+
kind
|
|
999
|
+
};
|
|
1000
|
+
refresh$.next(event);
|
|
1001
|
+
opts.onRefresh?.(event);
|
|
1002
|
+
}
|
|
1003
|
+
let lastRefreshKeys = [];
|
|
1004
|
+
const scheduleRefresh = createRefresher(async () => {
|
|
1005
|
+
lastRefreshKeys = cache.keys();
|
|
1006
|
+
if (lastRefreshKeys.length === 0) return;
|
|
1007
|
+
await projected.refresh(lastRefreshKeys);
|
|
1008
|
+
}, (kind) => {
|
|
1009
|
+
if (lastRefreshKeys.length > 0) emit(lastRefreshKeys, kind);
|
|
1010
|
+
}, () => {});
|
|
1011
|
+
const unsubUpdates = updates$.subscribe((batch) => {
|
|
1012
|
+
const evicted = /* @__PURE__ */ new Set();
|
|
1013
|
+
const ownModelEvents = batch.entity.get(_def.model);
|
|
1014
|
+
if (ownModelEvents) for (const event of ownModelEvents) evicted.add(event.id);
|
|
1015
|
+
for (const event of batch.events) for (const key of dependencyCollector.getAffectedKeys(event)) evicted.add(key);
|
|
1016
|
+
if (evicted.size > 0) {
|
|
1017
|
+
for (const key of evicted) {
|
|
1018
|
+
dependencyCollector.removeItem(key);
|
|
1019
|
+
projected.delete(key);
|
|
1020
|
+
}
|
|
1021
|
+
emit([...evicted], "upstream-update");
|
|
1022
|
+
}
|
|
1023
|
+
});
|
|
1024
|
+
return {
|
|
1025
|
+
instance: {
|
|
1026
|
+
name: _def.name,
|
|
1027
|
+
refresh$,
|
|
1028
|
+
get(idOrIds) {
|
|
1029
|
+
return projected.get(idOrIds);
|
|
1030
|
+
},
|
|
1031
|
+
refresh() {
|
|
1032
|
+
scheduleRefresh("on-demand");
|
|
1033
|
+
},
|
|
1034
|
+
clear() {
|
|
1035
|
+
dependencyCollector.clear();
|
|
1036
|
+
projected.clear();
|
|
1037
|
+
}
|
|
1038
|
+
},
|
|
1039
|
+
destroy() {
|
|
1040
|
+
unsubUpdates();
|
|
1041
|
+
refresh$.complete();
|
|
1042
|
+
}
|
|
1043
|
+
};
|
|
1181
1044
|
}
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
import {
|
|
1185
|
-
collectAsync as collectAsync6,
|
|
1186
|
-
createSourceSubscription as createSourceSubscription6,
|
|
1187
|
-
mapAsync as mapAsync6
|
|
1188
|
-
} from "@contello/client";
|
|
1189
|
-
import { ProjectedLazyMap as ProjectedLazyMap3 } from "projected";
|
|
1190
|
-
|
|
1191
|
-
// src/routes-mapping.ts
|
|
1045
|
+
//#endregion
|
|
1046
|
+
//#region src/routes-mapping.ts
|
|
1192
1047
|
function mapRoute(raw, resolver) {
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
entityId: raw.target.entity.id
|
|
1236
|
-
};
|
|
1237
|
-
}
|
|
1238
|
-
default: {
|
|
1239
|
-
return void 0;
|
|
1240
|
-
}
|
|
1241
|
-
}
|
|
1048
|
+
const base = {
|
|
1049
|
+
id: raw.id,
|
|
1050
|
+
path: raw.path,
|
|
1051
|
+
customHeaders: raw.customHeaders.map((h) => ({
|
|
1052
|
+
name: h.name,
|
|
1053
|
+
value: h.value
|
|
1054
|
+
}))
|
|
1055
|
+
};
|
|
1056
|
+
switch (raw.target.__typename) {
|
|
1057
|
+
case "ContelloRouteTargetRedirect": return {
|
|
1058
|
+
...base,
|
|
1059
|
+
type: "redirect",
|
|
1060
|
+
location: raw.target.location,
|
|
1061
|
+
status: raw.target.responseCode
|
|
1062
|
+
};
|
|
1063
|
+
case "ContelloRouteTargetText": return {
|
|
1064
|
+
...base,
|
|
1065
|
+
type: "text",
|
|
1066
|
+
content: raw.target.content,
|
|
1067
|
+
mimeType: raw.target.mimeType
|
|
1068
|
+
};
|
|
1069
|
+
case "ContelloRouteTargetAsset": return {
|
|
1070
|
+
...base,
|
|
1071
|
+
type: "asset",
|
|
1072
|
+
assetId: raw.target.asset.id,
|
|
1073
|
+
fileId: raw.target.asset.original.uid,
|
|
1074
|
+
contentDisposition: raw.target.contentDisposition === "INLINE" ? "inline" : "attachment",
|
|
1075
|
+
mimeType: raw.target.asset.original.mimeType
|
|
1076
|
+
};
|
|
1077
|
+
case "ContelloRouteTargetEntity": {
|
|
1078
|
+
if (!raw.target.entity) return;
|
|
1079
|
+
const entityType = raw.target.entity.__typename;
|
|
1080
|
+
return {
|
|
1081
|
+
...base,
|
|
1082
|
+
type: "entity",
|
|
1083
|
+
model: resolver.resolveModel(entityType),
|
|
1084
|
+
entityType,
|
|
1085
|
+
entityId: raw.target.entity.id
|
|
1086
|
+
};
|
|
1087
|
+
}
|
|
1088
|
+
default: return;
|
|
1089
|
+
}
|
|
1242
1090
|
}
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1091
|
+
//#endregion
|
|
1092
|
+
//#region src/lazy-routes.ts
|
|
1093
|
+
const ID_PREFIX = "1\0";
|
|
1094
|
+
const PATH_PREFIX = "2\0";
|
|
1095
|
+
const CACHE_KEY = Symbol("cacheKey");
|
|
1248
1096
|
function collectRoutes(rawItems, keyFn, resolver) {
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1097
|
+
return rawItems.reduce((acc, raw) => {
|
|
1098
|
+
const mapped = mapRoute(raw, resolver);
|
|
1099
|
+
if (mapped) {
|
|
1100
|
+
mapped[CACHE_KEY] = keyFn(mapped);
|
|
1101
|
+
acc.push(mapped);
|
|
1102
|
+
}
|
|
1103
|
+
return acc;
|
|
1104
|
+
}, []);
|
|
1257
1105
|
}
|
|
1106
|
+
/**
|
|
1107
|
+
* cache wrapper that presents a unified `ID_PREFIX + id` / `PATH_PREFIX + path` key space
|
|
1108
|
+
* to ProjectedLazyMap while storing entries in the underlying LRU under ID_PREFIX keys only.
|
|
1109
|
+
*
|
|
1110
|
+
* path -> id resolution is handled internally via a pathToId map kept in sync with the LRU
|
|
1111
|
+
* (populated on set, cleared on natural LRU eviction via onEvict, and on explicit delete).
|
|
1112
|
+
*/
|
|
1258
1113
|
function createRoutesCache(max, ttl) {
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
|
|
1114
|
+
const pathToId = /* @__PURE__ */ new Map();
|
|
1115
|
+
const idToPath = /* @__PURE__ */ new Map();
|
|
1116
|
+
const lru = createLruCache({
|
|
1117
|
+
max,
|
|
1118
|
+
ttl,
|
|
1119
|
+
onEvict: (route) => {
|
|
1120
|
+
pathToId.delete(route.path);
|
|
1121
|
+
idToPath.delete(route.id);
|
|
1122
|
+
}
|
|
1123
|
+
});
|
|
1124
|
+
function resolveKey(key) {
|
|
1125
|
+
if (key.startsWith(PATH_PREFIX)) {
|
|
1126
|
+
const id = pathToId.get(key.slice(2));
|
|
1127
|
+
if (id) return ID_PREFIX + id;
|
|
1128
|
+
return key;
|
|
1129
|
+
}
|
|
1130
|
+
return key;
|
|
1131
|
+
}
|
|
1132
|
+
return {
|
|
1133
|
+
has: (key) => lru.has(resolveKey(key)),
|
|
1134
|
+
get: (key) => lru.get(resolveKey(key)),
|
|
1135
|
+
set: (_key, value) => {
|
|
1136
|
+
const previousPath = idToPath.get(value.id);
|
|
1137
|
+
if (previousPath !== void 0 && previousPath !== value.path) pathToId.delete(previousPath);
|
|
1138
|
+
pathToId.set(value.path, value.id);
|
|
1139
|
+
idToPath.set(value.id, value.path);
|
|
1140
|
+
lru.set(ID_PREFIX + value.id, value);
|
|
1141
|
+
},
|
|
1142
|
+
delete: (key) => {
|
|
1143
|
+
const resolved = resolveKey(key);
|
|
1144
|
+
const value = lru.get(resolved);
|
|
1145
|
+
if (value) {
|
|
1146
|
+
pathToId.delete(value.path);
|
|
1147
|
+
idToPath.delete(value.id);
|
|
1148
|
+
}
|
|
1149
|
+
lru.delete(resolved);
|
|
1150
|
+
},
|
|
1151
|
+
clear: () => {
|
|
1152
|
+
pathToId.clear();
|
|
1153
|
+
idToPath.clear();
|
|
1154
|
+
lru.clear();
|
|
1155
|
+
},
|
|
1156
|
+
keys: () => lru.keys()
|
|
1157
|
+
};
|
|
1298
1158
|
}
|
|
1299
1159
|
function createLazyRoutesCollection(def, client, updates$, resolver) {
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
get(idOrIds) {
|
|
1379
|
-
if (Array.isArray(idOrIds)) {
|
|
1380
|
-
return projected.get(idOrIds.map((id) => ID_PREFIX + id));
|
|
1381
|
-
}
|
|
1382
|
-
return projected.get(ID_PREFIX + idOrIds);
|
|
1383
|
-
},
|
|
1384
|
-
getByPath(pathOrPaths) {
|
|
1385
|
-
if (Array.isArray(pathOrPaths)) {
|
|
1386
|
-
return projected.get(pathOrPaths.map((p) => PATH_PREFIX + p));
|
|
1387
|
-
}
|
|
1388
|
-
return projected.get(PATH_PREFIX + pathOrPaths);
|
|
1389
|
-
},
|
|
1390
|
-
refresh() {
|
|
1391
|
-
scheduleRefresh("on-demand");
|
|
1392
|
-
},
|
|
1393
|
-
clear() {
|
|
1394
|
-
projected.clear();
|
|
1395
|
-
}
|
|
1396
|
-
},
|
|
1397
|
-
destroy() {
|
|
1398
|
-
unsubUpdates();
|
|
1399
|
-
channel.complete();
|
|
1400
|
-
}
|
|
1401
|
-
};
|
|
1160
|
+
const _def = { cache: {
|
|
1161
|
+
max: def?.cache?.max ?? 1e3,
|
|
1162
|
+
ttl: resolveTtl(def?.cache?.ttl)
|
|
1163
|
+
} };
|
|
1164
|
+
const cache = createRoutesCache(_def.cache.max, _def.cache.ttl);
|
|
1165
|
+
const routesSourceDoc = createSourceSubscription(schema.sources.storeRoute);
|
|
1166
|
+
function fetchRoutes(vars) {
|
|
1167
|
+
return collectAsync(mapAsync(client.subscribe(routesSourceDoc, vars), (data) => data.source));
|
|
1168
|
+
}
|
|
1169
|
+
const projected = new ProjectedLazyMap({
|
|
1170
|
+
key: (route) => route[CACHE_KEY] ?? ID_PREFIX + route.id,
|
|
1171
|
+
values: (prefixedKeys) => wrap("routes", async () => {
|
|
1172
|
+
const ids = [];
|
|
1173
|
+
const paths = [];
|
|
1174
|
+
for (const key of prefixedKeys) {
|
|
1175
|
+
const value = key.slice(2);
|
|
1176
|
+
if (key.startsWith(ID_PREFIX)) ids.push(value);
|
|
1177
|
+
else paths.push(value);
|
|
1178
|
+
}
|
|
1179
|
+
const [rawByIds, rawByPaths] = await Promise.all([ids.length > 0 ? fetchRoutes({ ids }) : Promise.resolve([]), paths.length > 0 ? fetchRoutes({ paths }) : Promise.resolve([])]);
|
|
1180
|
+
const byIds = collectRoutes(rawByIds, (r) => ID_PREFIX + r.id, resolver);
|
|
1181
|
+
const byPaths = collectRoutes(rawByPaths, (r) => PATH_PREFIX + r.path, resolver);
|
|
1182
|
+
return [...byIds, ...byPaths];
|
|
1183
|
+
}),
|
|
1184
|
+
cache
|
|
1185
|
+
});
|
|
1186
|
+
const channel = createRefreshChannel(def?.onRefresh);
|
|
1187
|
+
function emit(ids, kind) {
|
|
1188
|
+
channel.emit({
|
|
1189
|
+
ids,
|
|
1190
|
+
kind
|
|
1191
|
+
});
|
|
1192
|
+
}
|
|
1193
|
+
let lastRefreshKeys = [];
|
|
1194
|
+
const scheduleRefresh = createRefresher(async () => {
|
|
1195
|
+
lastRefreshKeys = cache.keys();
|
|
1196
|
+
if (lastRefreshKeys.length === 0) return;
|
|
1197
|
+
await projected.refresh(lastRefreshKeys);
|
|
1198
|
+
}, (kind) => {
|
|
1199
|
+
if (lastRefreshKeys.length > 0) emit(lastRefreshKeys.map((key) => key.slice(2)), kind);
|
|
1200
|
+
}, () => {});
|
|
1201
|
+
const unsubUpdates = updates$.subscribe((batch) => {
|
|
1202
|
+
const evicted = [];
|
|
1203
|
+
for (const event of batch.route) {
|
|
1204
|
+
const idKey = ID_PREFIX + event.id;
|
|
1205
|
+
if (event.mutation === "delete") {
|
|
1206
|
+
cache.delete(idKey);
|
|
1207
|
+
evicted.push(event.id);
|
|
1208
|
+
} else {
|
|
1209
|
+
if (cache.has(idKey)) cache.set(idKey, event.after);
|
|
1210
|
+
evicted.push(event.after.path);
|
|
1211
|
+
}
|
|
1212
|
+
}
|
|
1213
|
+
if (evicted.length > 0) emit(evicted, "upstream-update");
|
|
1214
|
+
});
|
|
1215
|
+
return {
|
|
1216
|
+
instance: {
|
|
1217
|
+
refresh$: channel.stream$,
|
|
1218
|
+
get(idOrIds) {
|
|
1219
|
+
if (Array.isArray(idOrIds)) return projected.get(idOrIds.map((id) => ID_PREFIX + id));
|
|
1220
|
+
return projected.get(ID_PREFIX + idOrIds);
|
|
1221
|
+
},
|
|
1222
|
+
getByPath(pathOrPaths) {
|
|
1223
|
+
if (Array.isArray(pathOrPaths)) return projected.get(pathOrPaths.map((p) => PATH_PREFIX + p));
|
|
1224
|
+
return projected.get(PATH_PREFIX + pathOrPaths);
|
|
1225
|
+
},
|
|
1226
|
+
refresh() {
|
|
1227
|
+
scheduleRefresh("on-demand");
|
|
1228
|
+
},
|
|
1229
|
+
clear() {
|
|
1230
|
+
projected.clear();
|
|
1231
|
+
}
|
|
1232
|
+
},
|
|
1233
|
+
destroy() {
|
|
1234
|
+
unsubUpdates();
|
|
1235
|
+
channel.complete();
|
|
1236
|
+
}
|
|
1237
|
+
};
|
|
1402
1238
|
}
|
|
1403
|
-
|
|
1404
|
-
|
|
1239
|
+
//#endregion
|
|
1240
|
+
//#region src/model-resolver.ts
|
|
1405
1241
|
var ModelResolver = class {
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
|
|
1420
|
-
|
|
1421
|
-
|
|
1422
|
-
|
|
1423
|
-
|
|
1424
|
-
|
|
1425
|
-
|
|
1426
|
-
|
|
1427
|
-
|
|
1428
|
-
|
|
1429
|
-
|
|
1430
|
-
|
|
1431
|
-
|
|
1432
|
-
|
|
1433
|
-
|
|
1434
|
-
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
|
|
1438
|
-
|
|
1439
|
-
|
|
1242
|
+
_typeNames;
|
|
1243
|
+
_toModel;
|
|
1244
|
+
_toTypeName;
|
|
1245
|
+
constructor(models) {
|
|
1246
|
+
const toModel = /* @__PURE__ */ new Map();
|
|
1247
|
+
const toTypeName = /* @__PURE__ */ new Map();
|
|
1248
|
+
let typeNames;
|
|
1249
|
+
if (models) {
|
|
1250
|
+
typeNames = /* @__PURE__ */ new Set();
|
|
1251
|
+
for (const [model, typeName] of Object.entries(models)) {
|
|
1252
|
+
toModel.set(typeName, model);
|
|
1253
|
+
toTypeName.set(model, typeName);
|
|
1254
|
+
typeNames.add(typeName);
|
|
1255
|
+
}
|
|
1256
|
+
}
|
|
1257
|
+
this._typeNames = typeNames;
|
|
1258
|
+
this._toModel = toModel;
|
|
1259
|
+
this._toTypeName = toTypeName;
|
|
1260
|
+
}
|
|
1261
|
+
hasTypeName(typeName) {
|
|
1262
|
+
return !this._typeNames || this._typeNames.has(typeName);
|
|
1263
|
+
}
|
|
1264
|
+
getModel(typeName) {
|
|
1265
|
+
return this._toModel.get(typeName);
|
|
1266
|
+
}
|
|
1267
|
+
getTypeName(model) {
|
|
1268
|
+
return this._toTypeName.get(model) ?? model;
|
|
1269
|
+
}
|
|
1270
|
+
hasModel(model) {
|
|
1271
|
+
return this._toTypeName.size === 0 || this._toTypeName.has(model);
|
|
1272
|
+
}
|
|
1273
|
+
resolveModel(typeName) {
|
|
1274
|
+
return this._toModel.get(typeName) ?? typeName;
|
|
1275
|
+
}
|
|
1440
1276
|
};
|
|
1441
|
-
|
|
1442
|
-
|
|
1443
|
-
import {
|
|
1444
|
-
collectAsync as collectAsync7,
|
|
1445
|
-
createSourceSubscription as createSourceSubscription7,
|
|
1446
|
-
mapAsync as mapAsync7,
|
|
1447
|
-
runWithBackoff as runWithBackoff5
|
|
1448
|
-
} from "@contello/client";
|
|
1449
|
-
import { ProjectedMap as ProjectedMap4, maybeThen as maybeThen4 } from "projected";
|
|
1277
|
+
//#endregion
|
|
1278
|
+
//#region src/routes.ts
|
|
1450
1279
|
function createRoutesCollection(options, client, updates$, resolver, refreshByTtl) {
|
|
1451
|
-
|
|
1452
|
-
|
|
1453
|
-
|
|
1454
|
-
|
|
1455
|
-
|
|
1456
|
-
|
|
1457
|
-
|
|
1458
|
-
|
|
1459
|
-
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
|
|
1470
|
-
|
|
1471
|
-
|
|
1472
|
-
|
|
1473
|
-
|
|
1474
|
-
|
|
1475
|
-
|
|
1476
|
-
|
|
1477
|
-
|
|
1478
|
-
|
|
1479
|
-
|
|
1480
|
-
|
|
1481
|
-
|
|
1482
|
-
|
|
1483
|
-
|
|
1484
|
-
|
|
1485
|
-
|
|
1486
|
-
|
|
1487
|
-
|
|
1488
|
-
|
|
1489
|
-
|
|
1490
|
-
|
|
1491
|
-
|
|
1492
|
-
|
|
1493
|
-
|
|
1494
|
-
|
|
1495
|
-
|
|
1496
|
-
|
|
1497
|
-
|
|
1498
|
-
|
|
1499
|
-
|
|
1500
|
-
|
|
1501
|
-
|
|
1502
|
-
|
|
1503
|
-
|
|
1504
|
-
|
|
1505
|
-
|
|
1506
|
-
|
|
1507
|
-
|
|
1508
|
-
|
|
1509
|
-
|
|
1510
|
-
|
|
1511
|
-
|
|
1512
|
-
|
|
1513
|
-
|
|
1514
|
-
|
|
1515
|
-
|
|
1516
|
-
|
|
1517
|
-
|
|
1518
|
-
|
|
1519
|
-
|
|
1520
|
-
|
|
1521
|
-
|
|
1522
|
-
|
|
1523
|
-
|
|
1524
|
-
|
|
1525
|
-
|
|
1526
|
-
|
|
1527
|
-
|
|
1528
|
-
|
|
1529
|
-
|
|
1530
|
-
|
|
1531
|
-
|
|
1532
|
-
|
|
1533
|
-
|
|
1534
|
-
|
|
1535
|
-
|
|
1536
|
-
|
|
1537
|
-
|
|
1538
|
-
|
|
1539
|
-
|
|
1540
|
-
|
|
1541
|
-
|
|
1542
|
-
|
|
1543
|
-
|
|
1544
|
-
|
|
1545
|
-
|
|
1546
|
-
|
|
1547
|
-
|
|
1548
|
-
|
|
1549
|
-
|
|
1550
|
-
|
|
1551
|
-
|
|
1552
|
-
|
|
1553
|
-
|
|
1554
|
-
|
|
1555
|
-
|
|
1556
|
-
|
|
1557
|
-
|
|
1558
|
-
|
|
1559
|
-
|
|
1560
|
-
|
|
1561
|
-
|
|
1562
|
-
|
|
1563
|
-
|
|
1564
|
-
|
|
1565
|
-
|
|
1566
|
-
|
|
1567
|
-
|
|
1568
|
-
|
|
1569
|
-
|
|
1570
|
-
|
|
1571
|
-
|
|
1572
|
-
|
|
1573
|
-
|
|
1574
|
-
|
|
1575
|
-
|
|
1576
|
-
|
|
1577
|
-
|
|
1578
|
-
|
|
1579
|
-
|
|
1580
|
-
|
|
1581
|
-
|
|
1582
|
-
|
|
1583
|
-
|
|
1584
|
-
|
|
1585
|
-
|
|
1586
|
-
|
|
1587
|
-
|
|
1588
|
-
|
|
1589
|
-
|
|
1590
|
-
|
|
1591
|
-
|
|
1592
|
-
|
|
1593
|
-
|
|
1594
|
-
|
|
1595
|
-
|
|
1596
|
-
|
|
1597
|
-
|
|
1598
|
-
}
|
|
1599
|
-
const instance = {
|
|
1600
|
-
refresh$: channel.stream$,
|
|
1601
|
-
async load() {
|
|
1602
|
-
if (!loaded) {
|
|
1603
|
-
await projected.getAllAsMap();
|
|
1604
|
-
}
|
|
1605
|
-
},
|
|
1606
|
-
get(idOrIds) {
|
|
1607
|
-
return typeof idOrIds === "string" ? getByIdSingle(idOrIds) : getByIds(idOrIds);
|
|
1608
|
-
},
|
|
1609
|
-
getByPath(pathOrPaths) {
|
|
1610
|
-
return typeof pathOrPaths === "string" ? lookupByPathSingle(pathOrPaths) : lookupByPaths(pathOrPaths);
|
|
1611
|
-
},
|
|
1612
|
-
refresh() {
|
|
1613
|
-
runFullRefresh("on-demand");
|
|
1614
|
-
}
|
|
1615
|
-
};
|
|
1616
|
-
return {
|
|
1617
|
-
instance,
|
|
1618
|
-
destroy() {
|
|
1619
|
-
unsubUpdates();
|
|
1620
|
-
ttl.clear();
|
|
1621
|
-
channel.complete();
|
|
1622
|
-
}
|
|
1623
|
-
};
|
|
1280
|
+
const opts = options ?? {};
|
|
1281
|
+
const cache = {
|
|
1282
|
+
ttl: resolveTtl(opts.cache?.ttl),
|
|
1283
|
+
eviction: opts.cache?.eviction ?? "refresh"
|
|
1284
|
+
};
|
|
1285
|
+
const channel = createRefreshChannel(opts.onRefresh);
|
|
1286
|
+
const ttl = createTtlOrchestrator({
|
|
1287
|
+
ttl: cache.ttl,
|
|
1288
|
+
run: () => runFullRefresh("ttl")
|
|
1289
|
+
});
|
|
1290
|
+
const pathById = /* @__PURE__ */ new Map();
|
|
1291
|
+
const idByPath = /* @__PURE__ */ new Map();
|
|
1292
|
+
let loaded = false;
|
|
1293
|
+
const routesSourceDoc = createSourceSubscription(schema.sources.storeRoute);
|
|
1294
|
+
const projected = new ProjectedMap({
|
|
1295
|
+
key: (route) => route.id,
|
|
1296
|
+
values: (ids) => wrap("routes", async () => {
|
|
1297
|
+
const items = (await collectAsync(mapAsync(client.subscribe(routesSourceDoc, { ids }), (data) => data.source))).reduce((acc, raw) => {
|
|
1298
|
+
const mapped = mapRoute(raw, resolver);
|
|
1299
|
+
if (mapped) acc.push(mapped);
|
|
1300
|
+
return acc;
|
|
1301
|
+
}, []);
|
|
1302
|
+
if (ids === void 0) {
|
|
1303
|
+
pathById.clear();
|
|
1304
|
+
idByPath.clear();
|
|
1305
|
+
for (const r of items) {
|
|
1306
|
+
pathById.set(r.id, r.path);
|
|
1307
|
+
idByPath.set(r.path, r.id);
|
|
1308
|
+
}
|
|
1309
|
+
if (!loaded) {
|
|
1310
|
+
loaded = true;
|
|
1311
|
+
ttl.mark();
|
|
1312
|
+
opts.onLoad?.(items.map((r) => r.id));
|
|
1313
|
+
}
|
|
1314
|
+
} else {
|
|
1315
|
+
const returnedIds = new Set(items.map((r) => r.id));
|
|
1316
|
+
for (const id of ids) {
|
|
1317
|
+
if (returnedIds.has(id)) continue;
|
|
1318
|
+
const oldPath = pathById.get(id);
|
|
1319
|
+
if (oldPath !== void 0) {
|
|
1320
|
+
idByPath.delete(oldPath);
|
|
1321
|
+
pathById.delete(id);
|
|
1322
|
+
}
|
|
1323
|
+
}
|
|
1324
|
+
for (const r of items) {
|
|
1325
|
+
const oldPath = pathById.get(r.id);
|
|
1326
|
+
if (oldPath !== void 0 && oldPath !== r.path) idByPath.delete(oldPath);
|
|
1327
|
+
pathById.set(r.id, r.path);
|
|
1328
|
+
idByPath.set(r.path, r.id);
|
|
1329
|
+
}
|
|
1330
|
+
}
|
|
1331
|
+
return items;
|
|
1332
|
+
})
|
|
1333
|
+
});
|
|
1334
|
+
function emit(ids, kind) {
|
|
1335
|
+
channel.emit({
|
|
1336
|
+
ids,
|
|
1337
|
+
kind
|
|
1338
|
+
});
|
|
1339
|
+
}
|
|
1340
|
+
function runFullRefresh(kind) {
|
|
1341
|
+
refreshByTtl.enqueue(() => runWithBackoff(async () => {
|
|
1342
|
+
emit((await projected.refresh()).keys().toArray(), kind);
|
|
1343
|
+
ttl.mark();
|
|
1344
|
+
}));
|
|
1345
|
+
}
|
|
1346
|
+
function runPartialRefresh(refreshIds, deletedIds) {
|
|
1347
|
+
const changedIds = [.../* @__PURE__ */ new Set([...refreshIds, ...deletedIds])];
|
|
1348
|
+
if (refreshIds.length === 0) {
|
|
1349
|
+
emit(changedIds, "upstream-update");
|
|
1350
|
+
return;
|
|
1351
|
+
}
|
|
1352
|
+
runWithBackoff(async () => {
|
|
1353
|
+
await projected.refresh(refreshIds);
|
|
1354
|
+
emit(changedIds, "upstream-update");
|
|
1355
|
+
});
|
|
1356
|
+
}
|
|
1357
|
+
const unsubUpdates = updates$.subscribe((batch) => {
|
|
1358
|
+
if (!loaded || batch.route.length === 0) return;
|
|
1359
|
+
const deleted = [];
|
|
1360
|
+
const upserted = /* @__PURE__ */ new Set();
|
|
1361
|
+
for (const event of batch.route) if (event.mutation === "delete") deleted.push(event.id);
|
|
1362
|
+
else upserted.add(event.id);
|
|
1363
|
+
if (cache.eviction === "clear") {
|
|
1364
|
+
pathById.clear();
|
|
1365
|
+
idByPath.clear();
|
|
1366
|
+
projected.clear();
|
|
1367
|
+
return;
|
|
1368
|
+
}
|
|
1369
|
+
for (const id of deleted) {
|
|
1370
|
+
const oldPath = pathById.get(id);
|
|
1371
|
+
if (oldPath !== void 0) {
|
|
1372
|
+
idByPath.delete(oldPath);
|
|
1373
|
+
pathById.delete(id);
|
|
1374
|
+
}
|
|
1375
|
+
}
|
|
1376
|
+
if (deleted.length > 0) projected.delete(deleted);
|
|
1377
|
+
runPartialRefresh([...upserted], deleted);
|
|
1378
|
+
});
|
|
1379
|
+
function getByIdSingle(id) {
|
|
1380
|
+
return projected.get(id);
|
|
1381
|
+
}
|
|
1382
|
+
function getByIds(ids) {
|
|
1383
|
+
return projected.get(ids);
|
|
1384
|
+
}
|
|
1385
|
+
function lookupByPathSingle(path) {
|
|
1386
|
+
return maybeThen$1(ensureLoaded(), () => {
|
|
1387
|
+
const id = idByPath.get(path);
|
|
1388
|
+
return id === void 0 ? void 0 : projected.get(id);
|
|
1389
|
+
});
|
|
1390
|
+
}
|
|
1391
|
+
function lookupByPaths(paths) {
|
|
1392
|
+
return maybeThen$1(ensureLoaded(), () => {
|
|
1393
|
+
const ids = [];
|
|
1394
|
+
for (const p of paths) {
|
|
1395
|
+
const id = idByPath.get(p);
|
|
1396
|
+
if (id !== void 0) ids.push(id);
|
|
1397
|
+
}
|
|
1398
|
+
return projected.get(ids);
|
|
1399
|
+
});
|
|
1400
|
+
}
|
|
1401
|
+
function ensureLoaded() {
|
|
1402
|
+
if (loaded) return;
|
|
1403
|
+
return maybeThen$1(projected.getAllAsMap(), () => {});
|
|
1404
|
+
}
|
|
1405
|
+
return {
|
|
1406
|
+
instance: {
|
|
1407
|
+
refresh$: channel.stream$,
|
|
1408
|
+
async load() {
|
|
1409
|
+
if (!loaded) await projected.getAllAsMap();
|
|
1410
|
+
},
|
|
1411
|
+
get(idOrIds) {
|
|
1412
|
+
return typeof idOrIds === "string" ? getByIdSingle(idOrIds) : getByIds(idOrIds);
|
|
1413
|
+
},
|
|
1414
|
+
getByPath(pathOrPaths) {
|
|
1415
|
+
return typeof pathOrPaths === "string" ? lookupByPathSingle(pathOrPaths) : lookupByPaths(pathOrPaths);
|
|
1416
|
+
},
|
|
1417
|
+
refresh() {
|
|
1418
|
+
runFullRefresh("on-demand");
|
|
1419
|
+
}
|
|
1420
|
+
},
|
|
1421
|
+
destroy() {
|
|
1422
|
+
unsubUpdates();
|
|
1423
|
+
ttl.clear();
|
|
1424
|
+
channel.complete();
|
|
1425
|
+
}
|
|
1426
|
+
};
|
|
1624
1427
|
}
|
|
1625
1428
|
function createRoutesSyncCollection(options, client, updates$, resolver, refreshByTtl) {
|
|
1626
|
-
|
|
1627
|
-
|
|
1628
|
-
|
|
1629
|
-
|
|
1630
|
-
|
|
1631
|
-
|
|
1632
|
-
|
|
1633
|
-
|
|
1634
|
-
|
|
1635
|
-
|
|
1636
|
-
|
|
1637
|
-
|
|
1638
|
-
|
|
1639
|
-
|
|
1640
|
-
|
|
1641
|
-
|
|
1642
|
-
|
|
1643
|
-
destroy
|
|
1644
|
-
};
|
|
1429
|
+
const { instance: base, destroy } = createRoutesCollection(options, client, updates$, resolver, refreshByTtl);
|
|
1430
|
+
function assertSync(value, method) {
|
|
1431
|
+
if (value instanceof Promise) throw new TypeError(`routes.${method}() is not initialized yet — call routes.load() first`);
|
|
1432
|
+
return value;
|
|
1433
|
+
}
|
|
1434
|
+
return {
|
|
1435
|
+
instance: {
|
|
1436
|
+
...base,
|
|
1437
|
+
get(idOrIds) {
|
|
1438
|
+
return assertSync(base.get(idOrIds), "get");
|
|
1439
|
+
},
|
|
1440
|
+
getByPath(pathOrPaths) {
|
|
1441
|
+
return assertSync(base.getByPath(pathOrPaths), "getByPath");
|
|
1442
|
+
}
|
|
1443
|
+
},
|
|
1444
|
+
destroy
|
|
1445
|
+
};
|
|
1645
1446
|
}
|
|
1646
|
-
|
|
1647
|
-
|
|
1648
|
-
import {
|
|
1649
|
-
createSourceSubscription as createSourceSubscription8,
|
|
1650
|
-
firstAsync,
|
|
1651
|
-
mapAsync as mapAsync8,
|
|
1652
|
-
runWithBackoff as runWithBackoff6
|
|
1653
|
-
} from "@contello/client";
|
|
1654
|
-
import { ProjectedValue, maybeThen as maybeThen5 } from "projected";
|
|
1447
|
+
//#endregion
|
|
1448
|
+
//#region src/singleton.ts
|
|
1655
1449
|
function createSingleton(source, options, client, updates$, resolver, refreshByTtl) {
|
|
1656
|
-
|
|
1657
|
-
|
|
1658
|
-
|
|
1659
|
-
|
|
1660
|
-
|
|
1661
|
-
|
|
1662
|
-
|
|
1663
|
-
|
|
1664
|
-
|
|
1665
|
-
|
|
1666
|
-
|
|
1667
|
-
|
|
1668
|
-
|
|
1669
|
-
|
|
1670
|
-
|
|
1671
|
-
|
|
1672
|
-
|
|
1673
|
-
|
|
1674
|
-
|
|
1675
|
-
|
|
1676
|
-
|
|
1677
|
-
|
|
1678
|
-
|
|
1679
|
-
|
|
1680
|
-
|
|
1681
|
-
|
|
1682
|
-
|
|
1683
|
-
|
|
1684
|
-
|
|
1685
|
-
|
|
1686
|
-
|
|
1687
|
-
|
|
1688
|
-
|
|
1689
|
-
|
|
1690
|
-
|
|
1691
|
-
|
|
1692
|
-
|
|
1693
|
-
|
|
1694
|
-
|
|
1695
|
-
|
|
1696
|
-
|
|
1697
|
-
|
|
1698
|
-
|
|
1699
|
-
|
|
1700
|
-
|
|
1701
|
-
|
|
1702
|
-
|
|
1703
|
-
|
|
1704
|
-
|
|
1705
|
-
|
|
1706
|
-
|
|
1707
|
-
|
|
1708
|
-
|
|
1709
|
-
|
|
1710
|
-
|
|
1711
|
-
|
|
1712
|
-
|
|
1713
|
-
|
|
1714
|
-
|
|
1715
|
-
|
|
1716
|
-
|
|
1717
|
-
|
|
1718
|
-
|
|
1719
|
-
|
|
1720
|
-
|
|
1721
|
-
|
|
1722
|
-
|
|
1723
|
-
|
|
1724
|
-
|
|
1725
|
-
|
|
1726
|
-
|
|
1727
|
-
|
|
1728
|
-
|
|
1729
|
-
scheduleRefresh("upstream-update");
|
|
1730
|
-
});
|
|
1731
|
-
return {
|
|
1732
|
-
instance: {
|
|
1733
|
-
name: _def.name,
|
|
1734
|
-
refresh$: channel.stream$,
|
|
1735
|
-
get() {
|
|
1736
|
-
return projected.get();
|
|
1737
|
-
},
|
|
1738
|
-
refresh() {
|
|
1739
|
-
scheduleRefresh("on-demand");
|
|
1740
|
-
},
|
|
1741
|
-
async load() {
|
|
1742
|
-
if (!loaded) {
|
|
1743
|
-
await projected.get();
|
|
1744
|
-
}
|
|
1745
|
-
}
|
|
1746
|
-
},
|
|
1747
|
-
destroy() {
|
|
1748
|
-
unsubUpdates();
|
|
1749
|
-
ttl.clear();
|
|
1750
|
-
channel.complete();
|
|
1751
|
-
}
|
|
1752
|
-
};
|
|
1450
|
+
const opts = options ?? {};
|
|
1451
|
+
const mapFn = opts.map ?? ((item) => item);
|
|
1452
|
+
const _def = {
|
|
1453
|
+
name: opts.name ?? source.__model,
|
|
1454
|
+
model: source.__model,
|
|
1455
|
+
cache: {
|
|
1456
|
+
ttl: resolveTtl(opts.cache?.ttl),
|
|
1457
|
+
eviction: opts.cache?.eviction ?? "refresh"
|
|
1458
|
+
}
|
|
1459
|
+
};
|
|
1460
|
+
const dependencyCollector = new DependencyCollector(_def.model, resolver);
|
|
1461
|
+
const itemKey = `singleton:${_def.name}`;
|
|
1462
|
+
const channel = createRefreshChannel(opts.onRefresh);
|
|
1463
|
+
const ttl = createTtlOrchestrator({
|
|
1464
|
+
ttl: _def.cache.ttl,
|
|
1465
|
+
run: () => runTtlRefresh()
|
|
1466
|
+
});
|
|
1467
|
+
let loaded = false;
|
|
1468
|
+
const projected = new ProjectedValue({ value: () => wrap(`singleton:${_def.name}`, () => maybeThen$1(firstAsync(mapAsync(client.subscribe(createSourceSubscription(source)), (r) => r.source)), (raw) => dependencyCollector.createContext((ref, register) => maybeThen$1(mapFn(raw, ref), (mapped) => {
|
|
1469
|
+
register(itemKey);
|
|
1470
|
+
if (!loaded) {
|
|
1471
|
+
loaded = true;
|
|
1472
|
+
ttl.mark();
|
|
1473
|
+
opts.onLoad?.();
|
|
1474
|
+
}
|
|
1475
|
+
return mapped;
|
|
1476
|
+
})))) });
|
|
1477
|
+
function emit(kind) {
|
|
1478
|
+
channel.emit({ kind });
|
|
1479
|
+
}
|
|
1480
|
+
function runTtlRefresh() {
|
|
1481
|
+
refreshByTtl.enqueue(async () => {
|
|
1482
|
+
await runWithBackoff(() => projected.refresh());
|
|
1483
|
+
emit("ttl");
|
|
1484
|
+
ttl.mark();
|
|
1485
|
+
});
|
|
1486
|
+
}
|
|
1487
|
+
const scheduleRefresh = createRefresher(() => projected.refresh(), (kind) => {
|
|
1488
|
+
emit(kind);
|
|
1489
|
+
ttl.mark();
|
|
1490
|
+
}, () => {});
|
|
1491
|
+
const unsubUpdates = updates$.subscribe((batch) => {
|
|
1492
|
+
if (!loaded) return;
|
|
1493
|
+
const hasOwnModel = batch.entity.has(_def.model);
|
|
1494
|
+
const hasAffectedRefs = batch.events.some((event) => dependencyCollector.getAffectedKeys(event).size > 0);
|
|
1495
|
+
if (!hasOwnModel && !hasAffectedRefs) return;
|
|
1496
|
+
if (_def.cache.eviction === "clear") {
|
|
1497
|
+
dependencyCollector.clear();
|
|
1498
|
+
projected.clear();
|
|
1499
|
+
return;
|
|
1500
|
+
}
|
|
1501
|
+
scheduleRefresh("upstream-update");
|
|
1502
|
+
});
|
|
1503
|
+
return {
|
|
1504
|
+
instance: {
|
|
1505
|
+
name: _def.name,
|
|
1506
|
+
refresh$: channel.stream$,
|
|
1507
|
+
get() {
|
|
1508
|
+
return projected.get();
|
|
1509
|
+
},
|
|
1510
|
+
refresh() {
|
|
1511
|
+
scheduleRefresh("on-demand");
|
|
1512
|
+
},
|
|
1513
|
+
async load() {
|
|
1514
|
+
if (!loaded) await projected.get();
|
|
1515
|
+
}
|
|
1516
|
+
},
|
|
1517
|
+
destroy() {
|
|
1518
|
+
unsubUpdates();
|
|
1519
|
+
ttl.clear();
|
|
1520
|
+
channel.complete();
|
|
1521
|
+
}
|
|
1522
|
+
};
|
|
1753
1523
|
}
|
|
1754
1524
|
function createSingletonSync(source, options, client, updates$, resolver, refreshByTtl) {
|
|
1755
|
-
|
|
1756
|
-
|
|
1757
|
-
|
|
1758
|
-
|
|
1759
|
-
|
|
1760
|
-
|
|
1761
|
-
|
|
1762
|
-
|
|
1763
|
-
|
|
1764
|
-
|
|
1765
|
-
|
|
1766
|
-
|
|
1767
|
-
const result = base.get();
|
|
1768
|
-
if (result instanceof Promise) {
|
|
1769
|
-
throw new TypeError(`singleton "${base.name}" is not initialized yet \u2014 call singleton.load() first`);
|
|
1770
|
-
}
|
|
1771
|
-
return result;
|
|
1772
|
-
}
|
|
1773
|
-
},
|
|
1774
|
-
destroy
|
|
1775
|
-
};
|
|
1525
|
+
const { instance: base, destroy } = createSingleton(source, options, client, updates$, resolver, refreshByTtl);
|
|
1526
|
+
return {
|
|
1527
|
+
instance: {
|
|
1528
|
+
...base,
|
|
1529
|
+
get() {
|
|
1530
|
+
const result = base.get();
|
|
1531
|
+
if (result instanceof Promise) throw new TypeError(`singleton "${base.name}" is not initialized yet — call singleton.load() first`);
|
|
1532
|
+
return result;
|
|
1533
|
+
}
|
|
1534
|
+
},
|
|
1535
|
+
destroy
|
|
1536
|
+
};
|
|
1776
1537
|
}
|
|
1777
|
-
|
|
1778
|
-
|
|
1779
|
-
import {
|
|
1780
|
-
asyncKeepalive,
|
|
1781
|
-
createAsyncIterableSubject as createAsyncIterableSubject3,
|
|
1782
|
-
mapAsync as mapAsync9
|
|
1783
|
-
} from "@contello/client";
|
|
1538
|
+
//#endregion
|
|
1539
|
+
//#region src/watcher.ts
|
|
1784
1540
|
function addRouteByModel(map, entityType, event) {
|
|
1785
|
-
|
|
1786
|
-
|
|
1787
|
-
|
|
1788
|
-
|
|
1789
|
-
|
|
1790
|
-
|
|
1541
|
+
let list = map.get(entityType);
|
|
1542
|
+
if (!list) {
|
|
1543
|
+
list = [];
|
|
1544
|
+
map.set(entityType, list);
|
|
1545
|
+
}
|
|
1546
|
+
list.push(event);
|
|
1547
|
+
}
|
|
1548
|
+
function addEventToBatch(batch, event) {
|
|
1549
|
+
switch (event.target) {
|
|
1550
|
+
case "route":
|
|
1551
|
+
batch.route.push(event);
|
|
1552
|
+
if ("after" in event && event.after.type === "entity") addRouteByModel(batch.routeByEntityModel, event.after.model, event);
|
|
1553
|
+
if ("before" in event && event.before.type === "entity") addRouteByModel(batch.routeByEntityModel, event.before.model, event);
|
|
1554
|
+
break;
|
|
1555
|
+
case "asset":
|
|
1556
|
+
batch.asset.push(event);
|
|
1557
|
+
break;
|
|
1558
|
+
case "i18nMessage":
|
|
1559
|
+
batch.i18nMessage.push(event);
|
|
1560
|
+
break;
|
|
1561
|
+
case "entity": {
|
|
1562
|
+
let list = batch.entity.get(event.model);
|
|
1563
|
+
if (!list) {
|
|
1564
|
+
list = [];
|
|
1565
|
+
batch.entity.set(event.model, list);
|
|
1566
|
+
}
|
|
1567
|
+
list.push(event);
|
|
1568
|
+
break;
|
|
1569
|
+
}
|
|
1570
|
+
}
|
|
1791
1571
|
}
|
|
1792
1572
|
function createUpdateBatch(events) {
|
|
1793
|
-
|
|
1794
|
-
|
|
1795
|
-
|
|
1796
|
-
|
|
1797
|
-
|
|
1798
|
-
|
|
1799
|
-
|
|
1800
|
-
|
|
1801
|
-
|
|
1802
|
-
|
|
1803
|
-
|
|
1804
|
-
|
|
1805
|
-
|
|
1806
|
-
|
|
1807
|
-
|
|
1808
|
-
|
|
1809
|
-
}
|
|
1810
|
-
case "asset": {
|
|
1811
|
-
asset.push(event);
|
|
1812
|
-
break;
|
|
1813
|
-
}
|
|
1814
|
-
case "i18nMessage": {
|
|
1815
|
-
i18nMessage.push(event);
|
|
1816
|
-
break;
|
|
1817
|
-
}
|
|
1818
|
-
case "entity": {
|
|
1819
|
-
let list = entity.get(event.model);
|
|
1820
|
-
if (!list) {
|
|
1821
|
-
list = [];
|
|
1822
|
-
entity.set(event.model, list);
|
|
1823
|
-
}
|
|
1824
|
-
list.push(event);
|
|
1825
|
-
break;
|
|
1826
|
-
}
|
|
1827
|
-
}
|
|
1828
|
-
}
|
|
1829
|
-
return { events, route, asset, i18nMessage, entity, routeByEntityModel };
|
|
1573
|
+
const batch = {
|
|
1574
|
+
route: [],
|
|
1575
|
+
asset: [],
|
|
1576
|
+
i18nMessage: [],
|
|
1577
|
+
entity: /* @__PURE__ */ new Map(),
|
|
1578
|
+
routeByEntityModel: /* @__PURE__ */ new Map()
|
|
1579
|
+
};
|
|
1580
|
+
for (const event of events) addEventToBatch(batch, event);
|
|
1581
|
+
return {
|
|
1582
|
+
events,
|
|
1583
|
+
route: batch.route,
|
|
1584
|
+
asset: batch.asset,
|
|
1585
|
+
i18nMessage: batch.i18nMessage,
|
|
1586
|
+
entity: batch.entity,
|
|
1587
|
+
routeByEntityModel: batch.routeByEntityModel
|
|
1588
|
+
};
|
|
1830
1589
|
}
|
|
1831
1590
|
function castMutationType(type) {
|
|
1832
|
-
|
|
1833
|
-
|
|
1834
|
-
|
|
1835
|
-
|
|
1836
|
-
|
|
1837
|
-
return "update";
|
|
1838
|
-
}
|
|
1839
|
-
case "DELETE": {
|
|
1840
|
-
return "delete";
|
|
1841
|
-
}
|
|
1842
|
-
}
|
|
1591
|
+
switch (type) {
|
|
1592
|
+
case "CREATE": return "create";
|
|
1593
|
+
case "UPDATE": return "update";
|
|
1594
|
+
case "DELETE": return "delete";
|
|
1595
|
+
}
|
|
1843
1596
|
}
|
|
1844
1597
|
function mapEvent(raw, resolver) {
|
|
1845
|
-
|
|
1846
|
-
|
|
1847
|
-
|
|
1848
|
-
|
|
1849
|
-
|
|
1850
|
-
|
|
1851
|
-
|
|
1852
|
-
|
|
1853
|
-
|
|
1854
|
-
|
|
1855
|
-
|
|
1856
|
-
|
|
1857
|
-
|
|
1858
|
-
|
|
1859
|
-
|
|
1860
|
-
|
|
1861
|
-
|
|
1862
|
-
|
|
1863
|
-
|
|
1864
|
-
|
|
1865
|
-
|
|
1866
|
-
|
|
1867
|
-
|
|
1868
|
-
|
|
1869
|
-
|
|
1870
|
-
|
|
1871
|
-
|
|
1872
|
-
|
|
1873
|
-
|
|
1874
|
-
|
|
1875
|
-
|
|
1876
|
-
|
|
1877
|
-
|
|
1878
|
-
|
|
1598
|
+
const mutation = castMutationType(raw.mutation.type);
|
|
1599
|
+
const target = raw.target;
|
|
1600
|
+
const { id, __typename } = target;
|
|
1601
|
+
switch (__typename) {
|
|
1602
|
+
case "ContelloRoute": {
|
|
1603
|
+
if (mutation === "delete") return {
|
|
1604
|
+
id,
|
|
1605
|
+
mutation: "delete",
|
|
1606
|
+
target: "route"
|
|
1607
|
+
};
|
|
1608
|
+
const after = mapRoute(target, resolver);
|
|
1609
|
+
if (!after) return;
|
|
1610
|
+
if (mutation === "create") return {
|
|
1611
|
+
id,
|
|
1612
|
+
mutation,
|
|
1613
|
+
target: "route",
|
|
1614
|
+
after
|
|
1615
|
+
};
|
|
1616
|
+
const before = raw.prev?.__typename === "ContelloRoute" ? mapRoute(raw.prev, resolver) ?? void 0 : void 0;
|
|
1617
|
+
if (!before) return;
|
|
1618
|
+
return {
|
|
1619
|
+
id,
|
|
1620
|
+
mutation,
|
|
1621
|
+
target: "route",
|
|
1622
|
+
after,
|
|
1623
|
+
before
|
|
1624
|
+
};
|
|
1625
|
+
}
|
|
1626
|
+
case "ContelloAsset": return {
|
|
1627
|
+
id,
|
|
1628
|
+
mutation,
|
|
1629
|
+
target: "asset"
|
|
1630
|
+
};
|
|
1631
|
+
case "ContelloI18nMessage": return {
|
|
1632
|
+
id,
|
|
1633
|
+
mutation,
|
|
1634
|
+
target: "i18nMessage",
|
|
1635
|
+
token: target.token
|
|
1636
|
+
};
|
|
1637
|
+
default:
|
|
1638
|
+
if (!__typename || !resolver.hasTypeName(__typename)) return;
|
|
1639
|
+
return {
|
|
1640
|
+
id,
|
|
1641
|
+
mutation,
|
|
1642
|
+
target: "entity",
|
|
1643
|
+
model: resolver.resolveModel(__typename)
|
|
1644
|
+
};
|
|
1645
|
+
}
|
|
1646
|
+
}
|
|
1647
|
+
function safeMapEvent(raw, resolver) {
|
|
1648
|
+
try {
|
|
1649
|
+
return mapEvent(raw, resolver);
|
|
1650
|
+
} catch {
|
|
1651
|
+
return;
|
|
1652
|
+
}
|
|
1879
1653
|
}
|
|
1880
1654
|
function createInternalWatcher(client, resolver) {
|
|
1881
|
-
|
|
1882
|
-
|
|
1883
|
-
|
|
1884
|
-
|
|
1885
|
-
|
|
1886
|
-
|
|
1887
|
-
|
|
1888
|
-
|
|
1889
|
-
|
|
1890
|
-
|
|
1891
|
-
|
|
1892
|
-
|
|
1893
|
-
|
|
1894
|
-
|
|
1895
|
-
|
|
1896
|
-
|
|
1897
|
-
|
|
1898
|
-
|
|
1899
|
-
|
|
1900
|
-
|
|
1901
|
-
|
|
1902
|
-
|
|
1903
|
-
|
|
1904
|
-
|
|
1905
|
-
|
|
1906
|
-
|
|
1907
|
-
|
|
1908
|
-
updates$.next(createUpdateBatch(list));
|
|
1909
|
-
}
|
|
1910
|
-
})();
|
|
1911
|
-
});
|
|
1912
|
-
},
|
|
1913
|
-
stop() {
|
|
1914
|
-
controller?.abort();
|
|
1915
|
-
controller = void 0;
|
|
1916
|
-
updates$.complete();
|
|
1917
|
-
}
|
|
1918
|
-
};
|
|
1655
|
+
const updates$ = createAsyncIterableSubject();
|
|
1656
|
+
let controller;
|
|
1657
|
+
return {
|
|
1658
|
+
updates$,
|
|
1659
|
+
start() {
|
|
1660
|
+
if (controller) return;
|
|
1661
|
+
controller = new AbortController();
|
|
1662
|
+
const signal = controller.signal;
|
|
1663
|
+
wrap("watcher:start", () => {
|
|
1664
|
+
(async () => {
|
|
1665
|
+
const events = mapAsync(asyncKeepalive(() => client.subscribe(storeWatchUpdatesDocument), signal), (data) => (data.contelloUpdatesBatch?.events ?? []).map((e) => safeMapEvent(e, resolver)).filter((e) => e !== void 0));
|
|
1666
|
+
for await (const list of events) {
|
|
1667
|
+
if (signal.aborted) return;
|
|
1668
|
+
if (list.length === 0) continue;
|
|
1669
|
+
try {
|
|
1670
|
+
updates$.next(createUpdateBatch(list));
|
|
1671
|
+
} catch {}
|
|
1672
|
+
}
|
|
1673
|
+
})().catch(() => {});
|
|
1674
|
+
});
|
|
1675
|
+
},
|
|
1676
|
+
stop() {
|
|
1677
|
+
controller?.abort();
|
|
1678
|
+
controller = void 0;
|
|
1679
|
+
updates$.complete();
|
|
1680
|
+
}
|
|
1681
|
+
};
|
|
1919
1682
|
}
|
|
1920
|
-
|
|
1921
|
-
|
|
1683
|
+
//#endregion
|
|
1684
|
+
//#region src/store.ts
|
|
1922
1685
|
var Store = class {
|
|
1923
|
-
|
|
1924
|
-
|
|
1925
|
-
|
|
1926
|
-
|
|
1927
|
-
|
|
1928
|
-
|
|
1929
|
-
|
|
1930
|
-
|
|
1931
|
-
|
|
1932
|
-
|
|
1933
|
-
|
|
1934
|
-
|
|
1935
|
-
|
|
1936
|
-
|
|
1937
|
-
|
|
1938
|
-
|
|
1939
|
-
|
|
1940
|
-
|
|
1941
|
-
|
|
1942
|
-
|
|
1943
|
-
|
|
1944
|
-
|
|
1945
|
-
|
|
1946
|
-
|
|
1947
|
-
|
|
1948
|
-
|
|
1949
|
-
|
|
1950
|
-
|
|
1951
|
-
|
|
1952
|
-
|
|
1953
|
-
|
|
1954
|
-
|
|
1955
|
-
|
|
1956
|
-
|
|
1957
|
-
|
|
1958
|
-
|
|
1959
|
-
|
|
1960
|
-
|
|
1961
|
-
|
|
1962
|
-
|
|
1963
|
-
|
|
1964
|
-
|
|
1965
|
-
|
|
1966
|
-
|
|
1967
|
-
|
|
1968
|
-
|
|
1969
|
-
|
|
1970
|
-
|
|
1971
|
-
|
|
1972
|
-
|
|
1973
|
-
|
|
1974
|
-
|
|
1975
|
-
|
|
1976
|
-
|
|
1977
|
-
|
|
1978
|
-
|
|
1979
|
-
|
|
1980
|
-
|
|
1981
|
-
|
|
1982
|
-
|
|
1983
|
-
|
|
1984
|
-
|
|
1985
|
-
|
|
1986
|
-
|
|
1987
|
-
|
|
1988
|
-
|
|
1989
|
-
|
|
1990
|
-
|
|
1991
|
-
|
|
1992
|
-
|
|
1993
|
-
|
|
1994
|
-
|
|
1995
|
-
|
|
1996
|
-
|
|
1997
|
-
|
|
1998
|
-
|
|
1999
|
-
|
|
2000
|
-
|
|
2001
|
-
|
|
2002
|
-
|
|
2003
|
-
|
|
2004
|
-
|
|
2005
|
-
|
|
2006
|
-
|
|
2007
|
-
|
|
2008
|
-
|
|
2009
|
-
|
|
2010
|
-
|
|
2011
|
-
|
|
2012
|
-
|
|
2013
|
-
|
|
2014
|
-
|
|
2015
|
-
|
|
2016
|
-
|
|
2017
|
-
|
|
2018
|
-
|
|
2019
|
-
|
|
2020
|
-
|
|
2021
|
-
|
|
2022
|
-
|
|
2023
|
-
|
|
2024
|
-
|
|
2025
|
-
|
|
2026
|
-
|
|
2027
|
-
|
|
2028
|
-
|
|
2029
|
-
|
|
2030
|
-
|
|
2031
|
-
|
|
2032
|
-
|
|
2033
|
-
|
|
2034
|
-
|
|
2035
|
-
}
|
|
2036
|
-
// --- lazy collection ---
|
|
2037
|
-
defineLazyCollection(sourceOrKey, options) {
|
|
2038
|
-
const source = this._resolveSource(sourceOrKey);
|
|
2039
|
-
const { instance, destroy } = createLazyCollection(
|
|
2040
|
-
source,
|
|
2041
|
-
options,
|
|
2042
|
-
this._client,
|
|
2043
|
-
this._watcher.updates$,
|
|
2044
|
-
this._resolver
|
|
2045
|
-
);
|
|
2046
|
-
this._cleanups.push(destroy);
|
|
2047
|
-
return instance;
|
|
2048
|
-
}
|
|
2049
|
-
// --- assets / routes / i18n (no source — built-ins) ---
|
|
2050
|
-
// --- assets ---
|
|
2051
|
-
defineAssets(options) {
|
|
2052
|
-
const { instance, destroy } = createAssetsCollection(
|
|
2053
|
-
options,
|
|
2054
|
-
this._client,
|
|
2055
|
-
this._watcher.updates$,
|
|
2056
|
-
this._refreshByTtl
|
|
2057
|
-
);
|
|
2058
|
-
this._cleanups.push(destroy);
|
|
2059
|
-
return instance;
|
|
2060
|
-
}
|
|
2061
|
-
defineAssetsSync(options) {
|
|
2062
|
-
const { instance, destroy } = createAssetsSyncCollection(
|
|
2063
|
-
options,
|
|
2064
|
-
this._client,
|
|
2065
|
-
this._watcher.updates$,
|
|
2066
|
-
this._refreshByTtl
|
|
2067
|
-
);
|
|
2068
|
-
this._cleanups.push(destroy);
|
|
2069
|
-
return instance;
|
|
2070
|
-
}
|
|
2071
|
-
defineLazyAssets(options) {
|
|
2072
|
-
const { instance, destroy } = createLazyAssetsCollection(options, this._client, this._watcher.updates$);
|
|
2073
|
-
this._cleanups.push(destroy);
|
|
2074
|
-
return instance;
|
|
2075
|
-
}
|
|
2076
|
-
// --- routes ---
|
|
2077
|
-
defineRoutes(options) {
|
|
2078
|
-
const { instance, destroy } = createRoutesCollection(
|
|
2079
|
-
options,
|
|
2080
|
-
this._client,
|
|
2081
|
-
this._watcher.updates$,
|
|
2082
|
-
this._resolver,
|
|
2083
|
-
this._refreshByTtl
|
|
2084
|
-
);
|
|
2085
|
-
this._cleanups.push(destroy);
|
|
2086
|
-
return instance;
|
|
2087
|
-
}
|
|
2088
|
-
defineRoutesSync(options) {
|
|
2089
|
-
const { instance, destroy } = createRoutesSyncCollection(
|
|
2090
|
-
options,
|
|
2091
|
-
this._client,
|
|
2092
|
-
this._watcher.updates$,
|
|
2093
|
-
this._resolver,
|
|
2094
|
-
this._refreshByTtl
|
|
2095
|
-
);
|
|
2096
|
-
this._cleanups.push(destroy);
|
|
2097
|
-
return instance;
|
|
2098
|
-
}
|
|
2099
|
-
defineLazyRoutes(options) {
|
|
2100
|
-
const { instance, destroy } = createLazyRoutesCollection(
|
|
2101
|
-
options,
|
|
2102
|
-
this._client,
|
|
2103
|
-
this._watcher.updates$,
|
|
2104
|
-
this._resolver
|
|
2105
|
-
);
|
|
2106
|
-
this._cleanups.push(destroy);
|
|
2107
|
-
return instance;
|
|
2108
|
-
}
|
|
2109
|
-
defineI18nMessages(def) {
|
|
2110
|
-
const result = createI18nMessagesCollection(def, this._client, this._watcher.updates$, this._refreshByTtl);
|
|
2111
|
-
this._cleanups.push(result.destroy);
|
|
2112
|
-
return result;
|
|
2113
|
-
}
|
|
2114
|
-
ping;
|
|
1686
|
+
_client;
|
|
1687
|
+
_resolver;
|
|
1688
|
+
_watcher;
|
|
1689
|
+
_schema;
|
|
1690
|
+
_cleanups = [];
|
|
1691
|
+
_refreshByTtl = createRefreshByTtlQueue();
|
|
1692
|
+
/**
|
|
1693
|
+
* Multicast update-batch stream from the watcher. Consume with `for await (const batch of updates$)`;
|
|
1694
|
+
* each iteration starts an independent fan-out from the shared source.
|
|
1695
|
+
*/
|
|
1696
|
+
updates$;
|
|
1697
|
+
ping;
|
|
1698
|
+
constructor(options) {
|
|
1699
|
+
const { url, project, token, schema } = options;
|
|
1700
|
+
this._schema = schema;
|
|
1701
|
+
this._client = createContelloClient({
|
|
1702
|
+
url,
|
|
1703
|
+
project,
|
|
1704
|
+
token,
|
|
1705
|
+
schema,
|
|
1706
|
+
connections: options.connections,
|
|
1707
|
+
onConnected: options.onConnected,
|
|
1708
|
+
onReconnecting: options.onReconnecting,
|
|
1709
|
+
onError: options.onError,
|
|
1710
|
+
connectionEvents: options.connectionEvents
|
|
1711
|
+
});
|
|
1712
|
+
this._resolver = new ModelResolver(schema?.models);
|
|
1713
|
+
this._watcher = createInternalWatcher(this._client, this._resolver);
|
|
1714
|
+
this.updates$ = this._watcher.updates$;
|
|
1715
|
+
this.ping = () => this._client.ping();
|
|
1716
|
+
}
|
|
1717
|
+
/** Resolve a string key (looked up via `schema.sources`) or a SourceDef directly to a SourceDef. */
|
|
1718
|
+
_resolveSource(sourceOrKey) {
|
|
1719
|
+
if (typeof sourceOrKey !== "string") return sourceOrKey;
|
|
1720
|
+
const source = (this._schema?.sources)?.[sourceOrKey];
|
|
1721
|
+
if (!source) throw new Error(`@contello/store: no source named "${sourceOrKey}" in schema.sources`);
|
|
1722
|
+
return source;
|
|
1723
|
+
}
|
|
1724
|
+
get client() {
|
|
1725
|
+
return this._client;
|
|
1726
|
+
}
|
|
1727
|
+
async init() {
|
|
1728
|
+
await wrap("store:init", () => this._client.init());
|
|
1729
|
+
this._watcher.start();
|
|
1730
|
+
}
|
|
1731
|
+
async destroy() {
|
|
1732
|
+
for (const fn of this._cleanups.splice(0)) try {
|
|
1733
|
+
fn();
|
|
1734
|
+
} catch {}
|
|
1735
|
+
this._watcher.stop();
|
|
1736
|
+
await wrap("store:destroy", () => this._client.destroy());
|
|
1737
|
+
}
|
|
1738
|
+
defineSingleton(sourceOrKey, options) {
|
|
1739
|
+
const { instance, destroy } = createSingleton(this._resolveSource(sourceOrKey), options, this._client, this._watcher.updates$, this._resolver, this._refreshByTtl);
|
|
1740
|
+
this._cleanups.push(destroy);
|
|
1741
|
+
return instance;
|
|
1742
|
+
}
|
|
1743
|
+
defineSingletonSync(sourceOrKey, options) {
|
|
1744
|
+
const { instance, destroy } = createSingletonSync(this._resolveSource(sourceOrKey), options, this._client, this._watcher.updates$, this._resolver, this._refreshByTtl);
|
|
1745
|
+
this._cleanups.push(destroy);
|
|
1746
|
+
return instance;
|
|
1747
|
+
}
|
|
1748
|
+
defineCollection(sourceOrKey, options) {
|
|
1749
|
+
const { instance, destroy } = createCollection(this._resolveSource(sourceOrKey), options, this._client, this._watcher.updates$, this._resolver, this._refreshByTtl);
|
|
1750
|
+
this._cleanups.push(destroy);
|
|
1751
|
+
return instance;
|
|
1752
|
+
}
|
|
1753
|
+
defineCollectionSync(sourceOrKey, options) {
|
|
1754
|
+
const { instance, destroy } = createCollectionSync(this._resolveSource(sourceOrKey), options, this._client, this._watcher.updates$, this._resolver, this._refreshByTtl);
|
|
1755
|
+
this._cleanups.push(destroy);
|
|
1756
|
+
return instance;
|
|
1757
|
+
}
|
|
1758
|
+
defineLazyCollection(sourceOrKey, options) {
|
|
1759
|
+
const { instance, destroy } = createLazyCollection(this._resolveSource(sourceOrKey), options, this._client, this._watcher.updates$, this._resolver);
|
|
1760
|
+
this._cleanups.push(destroy);
|
|
1761
|
+
return instance;
|
|
1762
|
+
}
|
|
1763
|
+
defineAssets(options) {
|
|
1764
|
+
const { instance, destroy } = createAssetsCollection(options, this._client, this._watcher.updates$, this._refreshByTtl);
|
|
1765
|
+
this._cleanups.push(destroy);
|
|
1766
|
+
return instance;
|
|
1767
|
+
}
|
|
1768
|
+
defineAssetsSync(options) {
|
|
1769
|
+
const { instance, destroy } = createAssetsSyncCollection(options, this._client, this._watcher.updates$, this._refreshByTtl);
|
|
1770
|
+
this._cleanups.push(destroy);
|
|
1771
|
+
return instance;
|
|
1772
|
+
}
|
|
1773
|
+
defineLazyAssets(options) {
|
|
1774
|
+
const { instance, destroy } = createLazyAssetsCollection(options, this._client, this._watcher.updates$);
|
|
1775
|
+
this._cleanups.push(destroy);
|
|
1776
|
+
return instance;
|
|
1777
|
+
}
|
|
1778
|
+
defineRoutes(options) {
|
|
1779
|
+
const { instance, destroy } = createRoutesCollection(options, this._client, this._watcher.updates$, this._resolver, this._refreshByTtl);
|
|
1780
|
+
this._cleanups.push(destroy);
|
|
1781
|
+
return instance;
|
|
1782
|
+
}
|
|
1783
|
+
defineRoutesSync(options) {
|
|
1784
|
+
const { instance, destroy } = createRoutesSyncCollection(options, this._client, this._watcher.updates$, this._resolver, this._refreshByTtl);
|
|
1785
|
+
this._cleanups.push(destroy);
|
|
1786
|
+
return instance;
|
|
1787
|
+
}
|
|
1788
|
+
defineLazyRoutes(options) {
|
|
1789
|
+
const { instance, destroy } = createLazyRoutesCollection(options, this._client, this._watcher.updates$, this._resolver);
|
|
1790
|
+
this._cleanups.push(destroy);
|
|
1791
|
+
return instance;
|
|
1792
|
+
}
|
|
1793
|
+
defineI18nMessages(def) {
|
|
1794
|
+
const result = createI18nMessagesCollection(def, this._client, this._watcher.updates$, this._refreshByTtl);
|
|
1795
|
+
this._cleanups.push(result.destroy);
|
|
1796
|
+
return result;
|
|
1797
|
+
}
|
|
2115
1798
|
};
|
|
2116
1799
|
function createStore(options) {
|
|
2117
|
-
|
|
1800
|
+
return new Store(options);
|
|
2118
1801
|
}
|
|
1802
|
+
//#endregion
|
|
1803
|
+
export { Store, createStore, maybeCatch, maybeThen };
|
|
2119
1804
|
|
|
2120
|
-
|
|
2121
|
-
import { maybeThen as maybeThen6, maybeCatch } from "projected";
|
|
2122
|
-
export {
|
|
2123
|
-
Store,
|
|
2124
|
-
createStore,
|
|
2125
|
-
maybeCatch,
|
|
2126
|
-
maybeThen6 as maybeThen
|
|
2127
|
-
};
|
|
1805
|
+
//# sourceMappingURL=index.js.map
|