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