@contello/store 1.5.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +531 -322
- package/dist/index.d.cts +102 -68
- package/dist/index.d.ts +102 -68
- package/dist/index.js +552 -319
- package/fragments/i18n.gql +10 -0
- package/package.json +7 -11
package/dist/index.js
CHANGED
|
@@ -2,8 +2,10 @@
|
|
|
2
2
|
import { createContelloClient } from "@contello/client";
|
|
3
3
|
|
|
4
4
|
// src/assets.ts
|
|
5
|
+
import {
|
|
6
|
+
createAsyncIterableSubject
|
|
7
|
+
} from "@contello/client";
|
|
5
8
|
import { ProjectedLazyMap } from "projected";
|
|
6
|
-
import { Subject } from "rxjs";
|
|
7
9
|
|
|
8
10
|
// src/diagnostics.ts
|
|
9
11
|
import dc from "diagnostics_channel";
|
|
@@ -48,7 +50,26 @@ function wrap(name, fn) {
|
|
|
48
50
|
}
|
|
49
51
|
|
|
50
52
|
// src/generated/graphql.ts
|
|
51
|
-
var
|
|
53
|
+
var MediaAssetFragmentSchema = `fragment MediaAsset on ContelloAsset {
|
|
54
|
+
id
|
|
55
|
+
original {
|
|
56
|
+
...MediaFile
|
|
57
|
+
}
|
|
58
|
+
preview {
|
|
59
|
+
...MediaFile
|
|
60
|
+
}
|
|
61
|
+
optimized {
|
|
62
|
+
...MediaFile
|
|
63
|
+
... on ContelloOptimizedFile {
|
|
64
|
+
optimizationConfig {
|
|
65
|
+
... on ContelloImageOptimizationConfig {
|
|
66
|
+
name
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}`;
|
|
72
|
+
var MediaFileFragmentSchema = `fragment MediaFile on ContelloFile {
|
|
52
73
|
uid
|
|
53
74
|
mimeType
|
|
54
75
|
metadata {
|
|
@@ -61,44 +82,21 @@ var storeGetAssetsDocument = `fragment StoreFile on ContelloFile {
|
|
|
61
82
|
height
|
|
62
83
|
}
|
|
63
84
|
}
|
|
64
|
-
}
|
|
65
|
-
fragment StoreAsset on ContelloAsset {
|
|
66
|
-
id
|
|
67
|
-
original {
|
|
68
|
-
...StoreFile
|
|
69
|
-
}
|
|
70
|
-
preview {
|
|
71
|
-
...StoreFile
|
|
72
|
-
}
|
|
73
|
-
optimized {
|
|
74
|
-
...StoreFile
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
query StoreGetAssets($filter: ContelloAssetsFilterInput!) {
|
|
78
|
-
contelloAssets(filter: $filter) {
|
|
79
|
-
...StoreAsset
|
|
80
|
-
}
|
|
81
85
|
}`;
|
|
82
|
-
var
|
|
83
|
-
|
|
84
|
-
id
|
|
85
|
-
token
|
|
86
|
-
translations {
|
|
87
|
-
language
|
|
88
|
-
value
|
|
89
|
-
}
|
|
90
|
-
}
|
|
86
|
+
var StoreAssetFragmentSchema = `fragment StoreAsset on ContelloAsset {
|
|
87
|
+
...MediaAsset
|
|
91
88
|
}`;
|
|
92
|
-
var
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
89
|
+
var StoreI18nMessageFragmentSchema = `fragment StoreI18nMessage on ContelloI18nMessage {
|
|
90
|
+
id
|
|
91
|
+
token
|
|
92
|
+
description
|
|
93
|
+
example
|
|
94
|
+
translations {
|
|
95
|
+
language
|
|
96
|
+
value
|
|
99
97
|
}
|
|
100
98
|
}`;
|
|
101
|
-
var
|
|
99
|
+
var StoreRouteFragmentSchema = `fragment StoreRoute on ContelloRoute {
|
|
102
100
|
id
|
|
103
101
|
path
|
|
104
102
|
target {
|
|
@@ -134,49 +132,37 @@ var storeGetRoutesDocument = `fragment StoreRoute on ContelloRoute {
|
|
|
134
132
|
name
|
|
135
133
|
value
|
|
136
134
|
}
|
|
137
|
-
}
|
|
135
|
+
}`;
|
|
136
|
+
var storeGetAssetsDocument = `${MediaFileFragmentSchema}
|
|
137
|
+
${MediaAssetFragmentSchema}
|
|
138
|
+
${StoreAssetFragmentSchema}
|
|
139
|
+
query StoreGetAssets($filter: ContelloAssetsFilterInput!) {
|
|
140
|
+
contelloAssets(filter: $filter) {
|
|
141
|
+
...StoreAsset
|
|
142
|
+
}
|
|
143
|
+
}`;
|
|
144
|
+
var storeGetI18nMessagesDocument = `${StoreI18nMessageFragmentSchema}
|
|
145
|
+
subscription StoreGetI18nMessages($collection: String!) {
|
|
146
|
+
contelloI18nMessagesBatch(collectionReferenceName: $collection) {
|
|
147
|
+
...StoreI18nMessage
|
|
148
|
+
}
|
|
149
|
+
}`;
|
|
150
|
+
var storeRegisterI18nMessagesDocument = `mutation StoreRegisterI18nMessages($collection: String!, $messages: [ContelloI18nMessageInput!]!) {
|
|
151
|
+
registerContelloI18nMessages(
|
|
152
|
+
collectionReferenceName: $collection
|
|
153
|
+
messages: $messages
|
|
154
|
+
deprecateUnmentioned: true
|
|
155
|
+
) {
|
|
156
|
+
ids
|
|
157
|
+
}
|
|
158
|
+
}`;
|
|
159
|
+
var storeGetRoutesDocument = `${StoreRouteFragmentSchema}
|
|
138
160
|
query StoreGetRoutes($request: ContelloRoutesRequestInput!) {
|
|
139
161
|
contelloRoutes(request: $request) {
|
|
140
162
|
...StoreRoute
|
|
141
163
|
}
|
|
142
164
|
}`;
|
|
143
|
-
var storeWatchUpdatesDocument =
|
|
144
|
-
id
|
|
145
|
-
path
|
|
146
|
-
target {
|
|
147
|
-
__typename
|
|
148
|
-
... on ContelloRouteTargetEntity {
|
|
149
|
-
entity {
|
|
150
|
-
__typename
|
|
151
|
-
... on ContelloEntityBase {
|
|
152
|
-
id
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
... on ContelloRouteTargetAsset {
|
|
157
|
-
asset {
|
|
158
|
-
id
|
|
159
|
-
original {
|
|
160
|
-
uid
|
|
161
|
-
mimeType
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
contentDisposition
|
|
165
|
-
}
|
|
166
|
-
... on ContelloRouteTargetRedirect {
|
|
167
|
-
location
|
|
168
|
-
responseCode
|
|
169
|
-
}
|
|
170
|
-
... on ContelloRouteTargetText {
|
|
171
|
-
content
|
|
172
|
-
mimeType
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
customHeaders {
|
|
176
|
-
name
|
|
177
|
-
value
|
|
178
|
-
}
|
|
179
|
-
}
|
|
165
|
+
var storeWatchUpdatesDocument = `${StoreRouteFragmentSchema}
|
|
180
166
|
subscription StoreWatchUpdates {
|
|
181
167
|
contelloUpdatesBatch {
|
|
182
168
|
events {
|
|
@@ -208,6 +194,31 @@ subscription StoreWatchUpdates {
|
|
|
208
194
|
}
|
|
209
195
|
}
|
|
210
196
|
}`;
|
|
197
|
+
var sources = {
|
|
198
|
+
storeAsset: {
|
|
199
|
+
document: `${MediaFileFragmentSchema}
|
|
200
|
+
${MediaAssetFragmentSchema}
|
|
201
|
+
${StoreAssetFragmentSchema}`,
|
|
202
|
+
fragment: "StoreAsset",
|
|
203
|
+
subscription: "contelloAssetsBatch",
|
|
204
|
+
__model: "storeAsset",
|
|
205
|
+
__cardinality: "asset"
|
|
206
|
+
},
|
|
207
|
+
storeI18nMessage: {
|
|
208
|
+
document: StoreI18nMessageFragmentSchema,
|
|
209
|
+
fragment: "StoreI18nMessage",
|
|
210
|
+
subscription: "contelloI18nMessagesBatch",
|
|
211
|
+
__model: "storeI18nMessage",
|
|
212
|
+
__cardinality: "i18nMessage"
|
|
213
|
+
},
|
|
214
|
+
storeRoute: {
|
|
215
|
+
document: StoreRouteFragmentSchema,
|
|
216
|
+
fragment: "StoreRoute",
|
|
217
|
+
subscription: "contelloRoutesBatch",
|
|
218
|
+
__model: "storeRoute",
|
|
219
|
+
__cardinality: "route"
|
|
220
|
+
}
|
|
221
|
+
};
|
|
211
222
|
|
|
212
223
|
// src/lru.ts
|
|
213
224
|
import { LRUCache } from "lru-cache";
|
|
@@ -235,22 +246,7 @@ function createLruCache(options) {
|
|
|
235
246
|
}
|
|
236
247
|
|
|
237
248
|
// src/utils.ts
|
|
238
|
-
import {
|
|
239
|
-
import { Observable, catchError, defer, repeat } from "rxjs";
|
|
240
|
-
var keepalive = (params) => (source) => source.pipe(
|
|
241
|
-
catchError((error) => {
|
|
242
|
-
params?.onError?.(error);
|
|
243
|
-
throw error;
|
|
244
|
-
}),
|
|
245
|
-
retryBackoff({ initialInterval: 100, maxInterval: 5e3 }),
|
|
246
|
-
repeat({ delay: 0 })
|
|
247
|
-
);
|
|
248
|
-
function resolveFetchable(value) {
|
|
249
|
-
if (value instanceof Observable) {
|
|
250
|
-
return collect(value);
|
|
251
|
-
}
|
|
252
|
-
return value;
|
|
253
|
-
}
|
|
249
|
+
import { runWithBackoff } from "@contello/client";
|
|
254
250
|
function createRefresher(fn, onRefreshed, onStart2) {
|
|
255
251
|
let refreshing = false;
|
|
256
252
|
let pending = false;
|
|
@@ -261,29 +257,17 @@ function createRefresher(fn, onRefreshed, onStart2) {
|
|
|
261
257
|
}
|
|
262
258
|
refreshing = true;
|
|
263
259
|
onStart2?.();
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
scheduleRefresh();
|
|
271
|
-
}
|
|
260
|
+
void runWithBackoff(fn).then(() => {
|
|
261
|
+
refreshing = false;
|
|
262
|
+
onRefreshed();
|
|
263
|
+
if (pending) {
|
|
264
|
+
pending = false;
|
|
265
|
+
scheduleRefresh();
|
|
272
266
|
}
|
|
273
267
|
});
|
|
274
268
|
}
|
|
275
269
|
return scheduleRefresh;
|
|
276
270
|
}
|
|
277
|
-
function collect(observable) {
|
|
278
|
-
return new Promise((resolve, reject) => {
|
|
279
|
-
const collected = [];
|
|
280
|
-
observable.subscribe({
|
|
281
|
-
error: reject,
|
|
282
|
-
next: (chunk) => collected.push(...chunk),
|
|
283
|
-
complete: () => resolve(collected)
|
|
284
|
-
});
|
|
285
|
-
});
|
|
286
|
-
}
|
|
287
271
|
|
|
288
272
|
// src/assets.ts
|
|
289
273
|
function mapFile(raw) {
|
|
@@ -322,10 +306,9 @@ function createAssetsCollection(def, client, updates$) {
|
|
|
322
306
|
}, [])
|
|
323
307
|
)
|
|
324
308
|
),
|
|
325
|
-
cache
|
|
326
|
-
protection: "freeze"
|
|
309
|
+
cache
|
|
327
310
|
});
|
|
328
|
-
const refresh$ =
|
|
311
|
+
const refresh$ = createAsyncIterableSubject();
|
|
329
312
|
let lastRefreshKeys = [];
|
|
330
313
|
const scheduleRefresh = createRefresher(
|
|
331
314
|
async () => {
|
|
@@ -343,7 +326,7 @@ function createAssetsCollection(def, client, updates$) {
|
|
|
343
326
|
() => {
|
|
344
327
|
}
|
|
345
328
|
);
|
|
346
|
-
updates$.subscribe((batch) => {
|
|
329
|
+
const unsubUpdates = updates$.subscribe((batch) => {
|
|
347
330
|
if (batch.asset.length === 0) return;
|
|
348
331
|
for (const event of batch.asset) {
|
|
349
332
|
projected.delete(event.id);
|
|
@@ -351,31 +334,43 @@ function createAssetsCollection(def, client, updates$) {
|
|
|
351
334
|
refresh$.next(batch.asset.map((e) => e.id));
|
|
352
335
|
});
|
|
353
336
|
return {
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
337
|
+
instance: {
|
|
338
|
+
refresh$,
|
|
339
|
+
get(idOrIds) {
|
|
340
|
+
return projected.get(idOrIds);
|
|
341
|
+
},
|
|
342
|
+
upload(data, meta, options) {
|
|
343
|
+
return client.upload(data, meta, options);
|
|
344
|
+
},
|
|
345
|
+
download(fileId) {
|
|
346
|
+
return client.download(fileId);
|
|
347
|
+
},
|
|
348
|
+
proxyHls(path, signal) {
|
|
349
|
+
return client.proxyHls(path, signal);
|
|
350
|
+
},
|
|
351
|
+
refresh() {
|
|
352
|
+
scheduleRefresh();
|
|
353
|
+
},
|
|
354
|
+
clear() {
|
|
355
|
+
projected.clear();
|
|
356
|
+
}
|
|
369
357
|
},
|
|
370
|
-
|
|
371
|
-
|
|
358
|
+
destroy() {
|
|
359
|
+
unsubUpdates();
|
|
360
|
+
refresh$.complete();
|
|
372
361
|
}
|
|
373
362
|
};
|
|
374
363
|
}
|
|
375
364
|
|
|
376
365
|
// src/collection.ts
|
|
366
|
+
import {
|
|
367
|
+
createAsyncIterableSubject as createAsyncIterableSubject2,
|
|
368
|
+
createSourceSubscription,
|
|
369
|
+
firstAsync,
|
|
370
|
+
mapAsync,
|
|
371
|
+
runWithBackoff as runWithBackoff2
|
|
372
|
+
} from "@contello/client";
|
|
377
373
|
import { ProjectedMap, maybeThen } from "projected";
|
|
378
|
-
import { Subject as Subject2 } from "rxjs";
|
|
379
374
|
|
|
380
375
|
// src/dependency-collector.ts
|
|
381
376
|
function createDependencyKey(target, id) {
|
|
@@ -530,66 +525,115 @@ var DependencyCollector = class {
|
|
|
530
525
|
};
|
|
531
526
|
|
|
532
527
|
// src/collection.ts
|
|
533
|
-
function
|
|
528
|
+
function fetchCollection(source, client, ids) {
|
|
529
|
+
return firstAsync(
|
|
530
|
+
mapAsync(
|
|
531
|
+
client.subscribe(createSourceSubscription(source), { ids }),
|
|
532
|
+
(r) => r.source
|
|
533
|
+
)
|
|
534
|
+
);
|
|
535
|
+
}
|
|
536
|
+
function createCollection(source, options, client, updates$, resolver) {
|
|
537
|
+
const opts = options ?? {};
|
|
538
|
+
const mapFn = opts.map ?? ((item) => item);
|
|
534
539
|
const _def = {
|
|
535
|
-
name:
|
|
536
|
-
model:
|
|
540
|
+
name: opts.name ?? source.__model,
|
|
541
|
+
model: source.__model,
|
|
537
542
|
cache: {
|
|
538
|
-
ttl:
|
|
539
|
-
eviction:
|
|
543
|
+
ttl: opts.cache?.ttl,
|
|
544
|
+
eviction: opts.cache?.eviction ?? "refresh"
|
|
540
545
|
}
|
|
541
546
|
};
|
|
542
547
|
const dependencyCollector = new DependencyCollector(_def.model, resolver);
|
|
543
|
-
let
|
|
548
|
+
let ttlTimer;
|
|
544
549
|
const projected = new ProjectedMap({
|
|
545
550
|
key: (item) => item.id,
|
|
546
|
-
values: () => wrap(
|
|
551
|
+
values: (ids) => wrap(
|
|
547
552
|
`collection:${_def.name}`,
|
|
548
553
|
() => maybeThen(
|
|
549
|
-
|
|
550
|
-
(rawItems) => (
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
return mapped;
|
|
558
|
-
})
|
|
559
|
-
)
|
|
554
|
+
fetchCollection(source, client, ids),
|
|
555
|
+
(rawItems) => Promise.all(
|
|
556
|
+
rawItems.map(
|
|
557
|
+
(item) => dependencyCollector.createContext(
|
|
558
|
+
(ref, register) => maybeThen(mapFn(item, ref), (mapped) => {
|
|
559
|
+
register(mapped.id);
|
|
560
|
+
return mapped;
|
|
561
|
+
})
|
|
560
562
|
)
|
|
561
|
-
)
|
|
563
|
+
)
|
|
564
|
+
).then((items) => {
|
|
565
|
+
if (ids === void 0) {
|
|
562
566
|
dependencyCollector.retainOnly(new Set(items.map((item) => item.id)));
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
567
|
+
} else {
|
|
568
|
+
const returnedIds = new Set(items.map((item) => item.id));
|
|
569
|
+
for (const id of ids) {
|
|
570
|
+
if (!returnedIds.has(id)) {
|
|
571
|
+
dependencyCollector.removeItem(id);
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
return items;
|
|
576
|
+
})
|
|
566
577
|
)
|
|
567
578
|
),
|
|
568
|
-
|
|
579
|
+
sort: opts.sort
|
|
569
580
|
});
|
|
570
|
-
const refresh$ =
|
|
571
|
-
const scheduleRefresh = createRefresher(
|
|
572
|
-
() => projected.refresh(),
|
|
573
|
-
() => {
|
|
574
|
-
maybeThen(projected.getAll(), (items) => {
|
|
575
|
-
const ids = items.map((item) => item.id);
|
|
576
|
-
refresh$.next(ids);
|
|
577
|
-
def.onRefresh?.(ids);
|
|
578
|
-
if (_def.cache.ttl !== void 0) {
|
|
579
|
-
timer = setTimeout(scheduleRefresh, _def.cache.ttl);
|
|
580
|
-
}
|
|
581
|
-
});
|
|
582
|
-
},
|
|
583
|
-
() => clearTimeout(timer)
|
|
584
|
-
);
|
|
581
|
+
const refresh$ = createAsyncIterableSubject2();
|
|
585
582
|
let loaded = false;
|
|
586
|
-
|
|
583
|
+
function scheduleTtl() {
|
|
584
|
+
if (_def.cache.ttl === void 0) {
|
|
585
|
+
return;
|
|
586
|
+
}
|
|
587
|
+
clearTimeout(ttlTimer);
|
|
588
|
+
ttlTimer = setTimeout(() => {
|
|
589
|
+
runFullRefresh();
|
|
590
|
+
}, _def.cache.ttl);
|
|
591
|
+
}
|
|
592
|
+
function runFullRefresh() {
|
|
593
|
+
void runWithBackoff2(
|
|
594
|
+
() => projected.refresh().then((map) => {
|
|
595
|
+
const ids = [...map.keys()];
|
|
596
|
+
refresh$.next(ids);
|
|
597
|
+
opts.onRefresh?.(ids);
|
|
598
|
+
scheduleTtl();
|
|
599
|
+
})
|
|
600
|
+
);
|
|
601
|
+
}
|
|
602
|
+
function runPartialRefresh(refreshIds, deletedIds) {
|
|
603
|
+
const changedIds = [.../* @__PURE__ */ new Set([...refreshIds, ...deletedIds])];
|
|
604
|
+
if (refreshIds.length === 0) {
|
|
605
|
+
refresh$.next(changedIds);
|
|
606
|
+
opts.onRefresh?.(changedIds);
|
|
607
|
+
return;
|
|
608
|
+
}
|
|
609
|
+
void runWithBackoff2(
|
|
610
|
+
() => projected.refresh(refreshIds).then(() => {
|
|
611
|
+
refresh$.next(changedIds);
|
|
612
|
+
opts.onRefresh?.(changedIds);
|
|
613
|
+
})
|
|
614
|
+
);
|
|
615
|
+
}
|
|
616
|
+
const unsubUpdates = updates$.subscribe((batch) => {
|
|
587
617
|
if (!loaded) {
|
|
588
618
|
return;
|
|
589
619
|
}
|
|
590
|
-
const
|
|
591
|
-
const
|
|
592
|
-
|
|
620
|
+
const ownEvents = batch.entity.get(_def.model) ?? [];
|
|
621
|
+
const deleted = [];
|
|
622
|
+
const upserted = /* @__PURE__ */ new Set();
|
|
623
|
+
for (const event of ownEvents) {
|
|
624
|
+
if (event.mutation === "delete") {
|
|
625
|
+
deleted.push(event.id);
|
|
626
|
+
} else {
|
|
627
|
+
upserted.add(event.id);
|
|
628
|
+
}
|
|
629
|
+
}
|
|
630
|
+
const depAffected = /* @__PURE__ */ new Set();
|
|
631
|
+
for (const event of batch.events) {
|
|
632
|
+
for (const id of dependencyCollector.getAffectedKeys(event)) {
|
|
633
|
+
depAffected.add(id);
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
if (deleted.length === 0 && upserted.size === 0 && depAffected.size === 0) {
|
|
593
637
|
return;
|
|
594
638
|
}
|
|
595
639
|
if (_def.cache.eviction === "clear") {
|
|
@@ -597,11 +641,21 @@ function createCollection(def, client, updates$, resolver) {
|
|
|
597
641
|
projected.clear();
|
|
598
642
|
return;
|
|
599
643
|
}
|
|
600
|
-
|
|
644
|
+
for (const id of deleted) {
|
|
645
|
+
dependencyCollector.removeItem(id);
|
|
646
|
+
}
|
|
647
|
+
if (deleted.length > 0) {
|
|
648
|
+
projected.delete(deleted);
|
|
649
|
+
}
|
|
650
|
+
const refreshIds = /* @__PURE__ */ new Set([...upserted, ...depAffected]);
|
|
651
|
+
for (const id of deleted) {
|
|
652
|
+
refreshIds.delete(id);
|
|
653
|
+
}
|
|
654
|
+
runPartialRefresh([...refreshIds], deleted);
|
|
601
655
|
});
|
|
602
656
|
const instance = {
|
|
603
657
|
name: _def.name,
|
|
604
|
-
refresh
|
|
658
|
+
refresh$,
|
|
605
659
|
get(idOrIds) {
|
|
606
660
|
return projected.get(idOrIds);
|
|
607
661
|
},
|
|
@@ -609,41 +663,59 @@ function createCollection(def, client, updates$, resolver) {
|
|
|
609
663
|
return projected.getAll();
|
|
610
664
|
},
|
|
611
665
|
refresh() {
|
|
612
|
-
|
|
666
|
+
runFullRefresh();
|
|
613
667
|
},
|
|
614
668
|
async load() {
|
|
615
|
-
const
|
|
669
|
+
const map = await projected.refresh();
|
|
616
670
|
loaded = true;
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
671
|
+
scheduleTtl();
|
|
672
|
+
opts.onLoad?.([...map.keys()]);
|
|
673
|
+
}
|
|
674
|
+
};
|
|
675
|
+
return {
|
|
676
|
+
instance,
|
|
677
|
+
destroy() {
|
|
678
|
+
unsubUpdates();
|
|
679
|
+
clearTimeout(ttlTimer);
|
|
680
|
+
refresh$.complete();
|
|
621
681
|
}
|
|
622
682
|
};
|
|
623
|
-
return instance;
|
|
624
683
|
}
|
|
625
|
-
function createCollectionSync(
|
|
626
|
-
const base = createCollection(
|
|
684
|
+
function createCollectionSync(source, options, client, updates$, resolver) {
|
|
685
|
+
const { instance: base, destroy } = createCollection(
|
|
686
|
+
source,
|
|
687
|
+
options,
|
|
688
|
+
client,
|
|
689
|
+
updates$,
|
|
690
|
+
resolver
|
|
691
|
+
);
|
|
627
692
|
function assertSync(value, method) {
|
|
628
693
|
if (value instanceof Promise) {
|
|
629
|
-
throw new Error(`collection "${
|
|
694
|
+
throw new Error(`collection "${base.name}".${method}() is not initialized yet \u2014 call collection.load() first`);
|
|
630
695
|
}
|
|
631
696
|
return value;
|
|
632
697
|
}
|
|
633
698
|
return {
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
699
|
+
instance: {
|
|
700
|
+
...base,
|
|
701
|
+
get(idOrIds) {
|
|
702
|
+
return assertSync(base.get(idOrIds), "get");
|
|
703
|
+
},
|
|
704
|
+
getAll() {
|
|
705
|
+
return assertSync(base.getAll(), "getAll");
|
|
706
|
+
}
|
|
637
707
|
},
|
|
638
|
-
|
|
639
|
-
return assertSync(base.getAll(), "getAll");
|
|
640
|
-
}
|
|
708
|
+
destroy
|
|
641
709
|
};
|
|
642
710
|
}
|
|
643
711
|
|
|
644
712
|
// src/i18n.ts
|
|
713
|
+
import {
|
|
714
|
+
collectAsync,
|
|
715
|
+
createAsyncIterableSubject as createAsyncIterableSubject3,
|
|
716
|
+
mapAsync as mapAsync2
|
|
717
|
+
} from "@contello/client";
|
|
645
718
|
import { ProjectedMap as ProjectedMap2, maybeThen as maybeThen2 } from "projected";
|
|
646
|
-
import { Subject as Subject3, map } from "rxjs";
|
|
647
719
|
function toGqlMessageInput(msg) {
|
|
648
720
|
return {
|
|
649
721
|
token: msg.token,
|
|
@@ -659,8 +731,13 @@ function createI18nMessagesCollection(def, client, updates$) {
|
|
|
659
731
|
key: (msg) => msg.id,
|
|
660
732
|
values: () => wrap(
|
|
661
733
|
`i18n:${def.collection}`,
|
|
662
|
-
() =>
|
|
663
|
-
|
|
734
|
+
() => collectAsync(
|
|
735
|
+
mapAsync2(
|
|
736
|
+
client.subscribe(storeGetI18nMessagesDocument, {
|
|
737
|
+
collection: def.collection
|
|
738
|
+
}),
|
|
739
|
+
(data) => data.contelloI18nMessagesBatch
|
|
740
|
+
)
|
|
664
741
|
).then(
|
|
665
742
|
(msgs) => msgs.map((msg) => ({
|
|
666
743
|
id: msg.id,
|
|
@@ -668,10 +745,9 @@ function createI18nMessagesCollection(def, client, updates$) {
|
|
|
668
745
|
translations: new Map(msg.translations.map((t) => [t.language, t.value]))
|
|
669
746
|
}))
|
|
670
747
|
)
|
|
671
|
-
)
|
|
672
|
-
protection: "freeze"
|
|
748
|
+
)
|
|
673
749
|
});
|
|
674
|
-
const refresh$ =
|
|
750
|
+
const refresh$ = createAsyncIterableSubject3();
|
|
675
751
|
const scheduleRefresh = createRefresher(
|
|
676
752
|
() => projected.refresh(),
|
|
677
753
|
() => {
|
|
@@ -682,13 +758,13 @@ function createI18nMessagesCollection(def, client, updates$) {
|
|
|
682
758
|
() => {
|
|
683
759
|
}
|
|
684
760
|
);
|
|
685
|
-
updates$.subscribe((batch) => {
|
|
761
|
+
const unsubUpdates = updates$.subscribe((batch) => {
|
|
686
762
|
if (batch.i18nMessage.length > 0) {
|
|
687
763
|
scheduleRefresh();
|
|
688
764
|
}
|
|
689
765
|
});
|
|
690
766
|
return {
|
|
691
|
-
refresh
|
|
767
|
+
refresh$,
|
|
692
768
|
get(idOrIds) {
|
|
693
769
|
return projected.get(idOrIds);
|
|
694
770
|
},
|
|
@@ -704,20 +780,31 @@ function createI18nMessagesCollection(def, client, updates$) {
|
|
|
704
780
|
}).then(() => {
|
|
705
781
|
})
|
|
706
782
|
);
|
|
783
|
+
},
|
|
784
|
+
destroy() {
|
|
785
|
+
unsubUpdates();
|
|
786
|
+
refresh$.complete();
|
|
707
787
|
}
|
|
708
788
|
};
|
|
709
789
|
}
|
|
710
790
|
|
|
711
791
|
// src/lazy-collection.ts
|
|
792
|
+
import {
|
|
793
|
+
createAsyncIterableSubject as createAsyncIterableSubject4,
|
|
794
|
+
createSourceSubscription as createSourceSubscription2,
|
|
795
|
+
firstAsync as firstAsync2,
|
|
796
|
+
mapAsync as mapAsync3
|
|
797
|
+
} from "@contello/client";
|
|
712
798
|
import { ProjectedLazyMap as ProjectedLazyMap2, maybeThen as maybeThen3 } from "projected";
|
|
713
|
-
|
|
714
|
-
|
|
799
|
+
function createLazyCollection(source, options, client, updates$, resolver) {
|
|
800
|
+
const opts = options ?? {};
|
|
801
|
+
const mapFn = opts.map ?? ((item) => item);
|
|
715
802
|
const _def = {
|
|
716
|
-
name:
|
|
717
|
-
model:
|
|
803
|
+
name: opts.name ?? source.__model,
|
|
804
|
+
model: source.__model,
|
|
718
805
|
cache: {
|
|
719
|
-
max:
|
|
720
|
-
ttl:
|
|
806
|
+
max: opts.cache?.max ?? 1e3,
|
|
807
|
+
ttl: opts.cache?.ttl
|
|
721
808
|
}
|
|
722
809
|
};
|
|
723
810
|
const dependencyCollector = new DependencyCollector(_def.model, resolver);
|
|
@@ -731,11 +818,18 @@ function createLazyCollection(def, client, updates$, resolver) {
|
|
|
731
818
|
values: (keys) => wrap(
|
|
732
819
|
`lazy-collection:${_def.name}`,
|
|
733
820
|
() => maybeThen3(
|
|
734
|
-
|
|
821
|
+
firstAsync2(
|
|
822
|
+
mapAsync3(
|
|
823
|
+
client.subscribe(createSourceSubscription2(source), {
|
|
824
|
+
ids: keys
|
|
825
|
+
}),
|
|
826
|
+
(r) => r.source
|
|
827
|
+
)
|
|
828
|
+
),
|
|
735
829
|
(rawItems) => Promise.all(
|
|
736
830
|
rawItems.map(
|
|
737
831
|
(item) => dependencyCollector.createContext(
|
|
738
|
-
(ref, register) => maybeThen3(
|
|
832
|
+
(ref, register) => maybeThen3(mapFn(item, ref), (mapped) => {
|
|
739
833
|
register(mapped.id);
|
|
740
834
|
return mapped;
|
|
741
835
|
})
|
|
@@ -744,10 +838,9 @@ function createLazyCollection(def, client, updates$, resolver) {
|
|
|
744
838
|
)
|
|
745
839
|
)
|
|
746
840
|
),
|
|
747
|
-
cache
|
|
748
|
-
protection: "freeze"
|
|
841
|
+
cache
|
|
749
842
|
});
|
|
750
|
-
const refresh$ =
|
|
843
|
+
const refresh$ = createAsyncIterableSubject4();
|
|
751
844
|
let lastRefreshKeys = [];
|
|
752
845
|
const scheduleRefresh = createRefresher(
|
|
753
846
|
async () => {
|
|
@@ -760,13 +853,13 @@ function createLazyCollection(def, client, updates$, resolver) {
|
|
|
760
853
|
() => {
|
|
761
854
|
if (lastRefreshKeys.length > 0) {
|
|
762
855
|
refresh$.next(lastRefreshKeys);
|
|
763
|
-
|
|
856
|
+
opts.onRefresh?.(lastRefreshKeys);
|
|
764
857
|
}
|
|
765
858
|
},
|
|
766
859
|
() => {
|
|
767
860
|
}
|
|
768
861
|
);
|
|
769
|
-
updates$.subscribe((batch) => {
|
|
862
|
+
const unsubUpdates = updates$.subscribe((batch) => {
|
|
770
863
|
const evicted = /* @__PURE__ */ new Set();
|
|
771
864
|
const ownModelEvents = batch.entity.get(_def.model);
|
|
772
865
|
if (ownModelEvents) {
|
|
@@ -786,21 +879,28 @@ function createLazyCollection(def, client, updates$, resolver) {
|
|
|
786
879
|
}
|
|
787
880
|
const ids = [...evicted];
|
|
788
881
|
refresh$.next(ids);
|
|
789
|
-
|
|
882
|
+
opts.onRefresh?.(ids);
|
|
790
883
|
}
|
|
791
884
|
});
|
|
885
|
+
void scheduleRefresh;
|
|
792
886
|
return {
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
887
|
+
instance: {
|
|
888
|
+
name: _def.name,
|
|
889
|
+
refresh$,
|
|
890
|
+
get(idOrIds) {
|
|
891
|
+
return projected.get(idOrIds);
|
|
892
|
+
},
|
|
893
|
+
refresh() {
|
|
894
|
+
scheduleRefresh();
|
|
895
|
+
},
|
|
896
|
+
clear() {
|
|
897
|
+
dependencyCollector.clear();
|
|
898
|
+
projected.clear();
|
|
899
|
+
}
|
|
800
900
|
},
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
901
|
+
destroy() {
|
|
902
|
+
unsubUpdates();
|
|
903
|
+
refresh$.complete();
|
|
804
904
|
}
|
|
805
905
|
};
|
|
806
906
|
}
|
|
@@ -844,8 +944,8 @@ var ModelResolver = class {
|
|
|
844
944
|
};
|
|
845
945
|
|
|
846
946
|
// src/routes.ts
|
|
947
|
+
import { createAsyncIterableSubject as createAsyncIterableSubject5 } from "@contello/client";
|
|
847
948
|
import { ProjectedLazyMap as ProjectedLazyMap3 } from "projected";
|
|
848
|
-
import { Subject as Subject5 } from "rxjs";
|
|
849
949
|
|
|
850
950
|
// src/routes-mapping.ts
|
|
851
951
|
function mapRoute(raw, resolver) {
|
|
@@ -980,10 +1080,9 @@ function createRoutesCollection(def, client, updates$, resolver) {
|
|
|
980
1080
|
paths.length > 0 ? client.execute(storeGetRoutesDocument, { request: { paths } }).then((data) => collectRoutes(data, (r) => PATH_PREFIX + r.path, resolver)) : Promise.resolve([])
|
|
981
1081
|
]).then(([byIds, byPaths]) => [...byIds, ...byPaths]);
|
|
982
1082
|
}),
|
|
983
|
-
cache
|
|
984
|
-
protection: "freeze"
|
|
1083
|
+
cache
|
|
985
1084
|
});
|
|
986
|
-
const refresh$ =
|
|
1085
|
+
const refresh$ = createAsyncIterableSubject5();
|
|
987
1086
|
let lastRefreshKeys = [];
|
|
988
1087
|
const scheduleRefresh = createRefresher(
|
|
989
1088
|
async () => {
|
|
@@ -1001,7 +1100,7 @@ function createRoutesCollection(def, client, updates$, resolver) {
|
|
|
1001
1100
|
() => {
|
|
1002
1101
|
}
|
|
1003
1102
|
);
|
|
1004
|
-
updates$.subscribe((batch) => {
|
|
1103
|
+
const unsubUpdates = updates$.subscribe((batch) => {
|
|
1005
1104
|
const evicted = [];
|
|
1006
1105
|
for (const event of batch.route) {
|
|
1007
1106
|
const idKey = ID_PREFIX + event.id;
|
|
@@ -1010,7 +1109,7 @@ function createRoutesCollection(def, client, updates$, resolver) {
|
|
|
1010
1109
|
evicted.push(event.id);
|
|
1011
1110
|
} else {
|
|
1012
1111
|
if (cache.has(idKey)) {
|
|
1013
|
-
cache.set(idKey,
|
|
1112
|
+
cache.set(idKey, event.after);
|
|
1014
1113
|
}
|
|
1015
1114
|
evicted.push(event.after.path);
|
|
1016
1115
|
}
|
|
@@ -1020,38 +1119,51 @@ function createRoutesCollection(def, client, updates$, resolver) {
|
|
|
1020
1119
|
}
|
|
1021
1120
|
});
|
|
1022
1121
|
return {
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1122
|
+
instance: {
|
|
1123
|
+
refresh$,
|
|
1124
|
+
get(idOrIds) {
|
|
1125
|
+
if (Array.isArray(idOrIds)) {
|
|
1126
|
+
return projected.get(idOrIds.map((id) => ID_PREFIX + id));
|
|
1127
|
+
}
|
|
1128
|
+
return projected.get(ID_PREFIX + idOrIds);
|
|
1129
|
+
},
|
|
1130
|
+
getByPath(pathOrPaths) {
|
|
1131
|
+
if (Array.isArray(pathOrPaths)) {
|
|
1132
|
+
return projected.get(pathOrPaths.map((p) => PATH_PREFIX + p));
|
|
1133
|
+
}
|
|
1134
|
+
return projected.get(PATH_PREFIX + pathOrPaths);
|
|
1135
|
+
},
|
|
1136
|
+
refresh() {
|
|
1137
|
+
scheduleRefresh();
|
|
1138
|
+
},
|
|
1139
|
+
clear() {
|
|
1140
|
+
projected.clear();
|
|
1033
1141
|
}
|
|
1034
|
-
return projected.get(PATH_PREFIX + pathOrPaths);
|
|
1035
1142
|
},
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
clear() {
|
|
1040
|
-
projected.clear();
|
|
1143
|
+
destroy() {
|
|
1144
|
+
unsubUpdates();
|
|
1145
|
+
refresh$.complete();
|
|
1041
1146
|
}
|
|
1042
1147
|
};
|
|
1043
1148
|
}
|
|
1044
1149
|
|
|
1045
1150
|
// src/singleton.ts
|
|
1151
|
+
import {
|
|
1152
|
+
createAsyncIterableSubject as createAsyncIterableSubject6,
|
|
1153
|
+
createSourceSubscription as createSourceSubscription3,
|
|
1154
|
+
firstAsync as firstAsync3,
|
|
1155
|
+
mapAsync as mapAsync4
|
|
1156
|
+
} from "@contello/client";
|
|
1046
1157
|
import { ProjectedValue, maybeThen as maybeThen4 } from "projected";
|
|
1047
|
-
|
|
1048
|
-
|
|
1158
|
+
function createSingleton(source, options, client, updates$, resolver) {
|
|
1159
|
+
const opts = options ?? {};
|
|
1160
|
+
const mapFn = opts.map ?? ((item) => item);
|
|
1049
1161
|
const _def = {
|
|
1050
|
-
name:
|
|
1051
|
-
model:
|
|
1162
|
+
name: opts.name ?? source.__model,
|
|
1163
|
+
model: source.__model,
|
|
1052
1164
|
cache: {
|
|
1053
|
-
ttl:
|
|
1054
|
-
eviction:
|
|
1165
|
+
ttl: opts.cache?.ttl,
|
|
1166
|
+
eviction: opts.cache?.eviction ?? "refresh"
|
|
1055
1167
|
}
|
|
1056
1168
|
};
|
|
1057
1169
|
const dependencyCollector = new DependencyCollector(_def.model, resolver);
|
|
@@ -1061,23 +1173,27 @@ function createSingleton(def, client, updates$, resolver) {
|
|
|
1061
1173
|
value: () => wrap(
|
|
1062
1174
|
`singleton:${_def.name}`,
|
|
1063
1175
|
() => maybeThen4(
|
|
1064
|
-
|
|
1176
|
+
firstAsync3(
|
|
1177
|
+
mapAsync4(
|
|
1178
|
+
client.subscribe(createSourceSubscription3(source)),
|
|
1179
|
+
(r) => r.source
|
|
1180
|
+
)
|
|
1181
|
+
),
|
|
1065
1182
|
(raw) => dependencyCollector.createContext(
|
|
1066
|
-
(ref, register) => maybeThen4(
|
|
1183
|
+
(ref, register) => maybeThen4(mapFn(raw, ref), (mapped) => {
|
|
1067
1184
|
register(itemKey);
|
|
1068
1185
|
return mapped;
|
|
1069
1186
|
})
|
|
1070
1187
|
)
|
|
1071
1188
|
)
|
|
1072
|
-
)
|
|
1073
|
-
protection: "freeze"
|
|
1189
|
+
)
|
|
1074
1190
|
});
|
|
1075
|
-
const refresh$ =
|
|
1191
|
+
const refresh$ = createAsyncIterableSubject6();
|
|
1076
1192
|
const scheduleRefresh = createRefresher(
|
|
1077
1193
|
() => projected.refresh(),
|
|
1078
1194
|
() => {
|
|
1079
1195
|
refresh$.next();
|
|
1080
|
-
|
|
1196
|
+
opts.onRefresh?.();
|
|
1081
1197
|
if (_def.cache.ttl !== void 0) {
|
|
1082
1198
|
timer = setTimeout(scheduleRefresh, _def.cache.ttl);
|
|
1083
1199
|
}
|
|
@@ -1085,7 +1201,7 @@ function createSingleton(def, client, updates$, resolver) {
|
|
|
1085
1201
|
() => clearTimeout(timer)
|
|
1086
1202
|
);
|
|
1087
1203
|
let loaded = false;
|
|
1088
|
-
updates$.subscribe((batch) => {
|
|
1204
|
+
const unsubUpdates = updates$.subscribe((batch) => {
|
|
1089
1205
|
if (!loaded) {
|
|
1090
1206
|
return;
|
|
1091
1207
|
}
|
|
@@ -1102,45 +1218,65 @@ function createSingleton(def, client, updates$, resolver) {
|
|
|
1102
1218
|
scheduleRefresh();
|
|
1103
1219
|
});
|
|
1104
1220
|
return {
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1221
|
+
instance: {
|
|
1222
|
+
name: _def.name,
|
|
1223
|
+
refresh$,
|
|
1224
|
+
get() {
|
|
1225
|
+
return projected.get();
|
|
1226
|
+
},
|
|
1227
|
+
refresh() {
|
|
1228
|
+
scheduleRefresh();
|
|
1229
|
+
},
|
|
1230
|
+
async load() {
|
|
1231
|
+
await projected.get();
|
|
1232
|
+
loaded = true;
|
|
1233
|
+
if (_def.cache.ttl !== void 0) {
|
|
1234
|
+
timer = setTimeout(scheduleRefresh, _def.cache.ttl);
|
|
1235
|
+
}
|
|
1236
|
+
opts.onLoad?.();
|
|
1118
1237
|
}
|
|
1119
|
-
|
|
1238
|
+
},
|
|
1239
|
+
destroy() {
|
|
1240
|
+
unsubUpdates();
|
|
1241
|
+
clearTimeout(timer);
|
|
1242
|
+
refresh$.complete();
|
|
1120
1243
|
}
|
|
1121
1244
|
};
|
|
1122
1245
|
}
|
|
1123
|
-
function createSingletonSync(
|
|
1124
|
-
const base = createSingleton(
|
|
1246
|
+
function createSingletonSync(source, options, client, updates$, resolver) {
|
|
1247
|
+
const { instance: base, destroy } = createSingleton(
|
|
1248
|
+
source,
|
|
1249
|
+
options,
|
|
1250
|
+
client,
|
|
1251
|
+
updates$,
|
|
1252
|
+
resolver
|
|
1253
|
+
);
|
|
1125
1254
|
return {
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1255
|
+
instance: {
|
|
1256
|
+
...base,
|
|
1257
|
+
get() {
|
|
1258
|
+
const result = base.get();
|
|
1259
|
+
if (result instanceof Promise) {
|
|
1260
|
+
throw new Error(`singleton "${base.name}" is not initialized yet \u2014 call singleton.load() first`);
|
|
1261
|
+
}
|
|
1262
|
+
return result;
|
|
1131
1263
|
}
|
|
1132
|
-
|
|
1133
|
-
|
|
1264
|
+
},
|
|
1265
|
+
destroy
|
|
1134
1266
|
};
|
|
1135
1267
|
}
|
|
1136
1268
|
|
|
1137
1269
|
// src/watcher.ts
|
|
1138
|
-
import {
|
|
1139
|
-
|
|
1140
|
-
|
|
1270
|
+
import {
|
|
1271
|
+
asyncKeepalive,
|
|
1272
|
+
createAsyncIterableSubject as createAsyncIterableSubject7,
|
|
1273
|
+
mapAsync as mapAsync5
|
|
1274
|
+
} from "@contello/client";
|
|
1275
|
+
function addRouteByModel(map, entityType, event) {
|
|
1276
|
+
let list = map.get(entityType);
|
|
1141
1277
|
if (!list) {
|
|
1142
1278
|
list = [];
|
|
1143
|
-
|
|
1279
|
+
map.set(entityType, list);
|
|
1144
1280
|
}
|
|
1145
1281
|
list.push(event);
|
|
1146
1282
|
}
|
|
@@ -1225,33 +1361,42 @@ function mapEvent(raw, resolver) {
|
|
|
1225
1361
|
}
|
|
1226
1362
|
}
|
|
1227
1363
|
function createInternalWatcher(client, resolver) {
|
|
1228
|
-
const
|
|
1229
|
-
let
|
|
1230
|
-
const updates$ = subject.asObservable().pipe(share());
|
|
1364
|
+
const updates$ = createAsyncIterableSubject7();
|
|
1365
|
+
let controller;
|
|
1231
1366
|
return {
|
|
1232
1367
|
updates$,
|
|
1233
1368
|
start() {
|
|
1234
|
-
if (
|
|
1369
|
+
if (controller) {
|
|
1235
1370
|
return;
|
|
1236
1371
|
}
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
map2(
|
|
1240
|
-
(data) => (data.contelloUpdatesBatch?.events ?? []).map((e) => mapEvent(e, resolver)).filter((e) => e !== void 0)
|
|
1241
|
-
),
|
|
1242
|
-
filter((events) => events.length > 0),
|
|
1243
|
-
map2(createUpdateBatch)
|
|
1244
|
-
);
|
|
1372
|
+
controller = new AbortController();
|
|
1373
|
+
const signal = controller.signal;
|
|
1245
1374
|
wrap("watcher:start", () => {
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1375
|
+
void (async () => {
|
|
1376
|
+
const source = asyncKeepalive(
|
|
1377
|
+
() => client.subscribe(storeWatchUpdatesDocument),
|
|
1378
|
+
signal
|
|
1379
|
+
);
|
|
1380
|
+
const events = mapAsync5(
|
|
1381
|
+
source,
|
|
1382
|
+
(data) => (data.contelloUpdatesBatch?.events ?? []).map((e) => mapEvent(e, resolver)).filter((e) => e !== void 0)
|
|
1383
|
+
);
|
|
1384
|
+
for await (const list of events) {
|
|
1385
|
+
if (signal.aborted) {
|
|
1386
|
+
return;
|
|
1387
|
+
}
|
|
1388
|
+
if (list.length === 0) {
|
|
1389
|
+
continue;
|
|
1390
|
+
}
|
|
1391
|
+
updates$.next(createUpdateBatch(list));
|
|
1392
|
+
}
|
|
1393
|
+
})();
|
|
1250
1394
|
});
|
|
1251
1395
|
},
|
|
1252
1396
|
stop() {
|
|
1253
|
-
|
|
1254
|
-
|
|
1397
|
+
controller?.abort();
|
|
1398
|
+
controller = void 0;
|
|
1399
|
+
updates$.complete();
|
|
1255
1400
|
}
|
|
1256
1401
|
};
|
|
1257
1402
|
}
|
|
@@ -1261,63 +1406,151 @@ var Store = class {
|
|
|
1261
1406
|
_client;
|
|
1262
1407
|
_resolver;
|
|
1263
1408
|
_watcher;
|
|
1409
|
+
_schema;
|
|
1410
|
+
_cleanups = [];
|
|
1411
|
+
/**
|
|
1412
|
+
* Multicast update-batch stream from the watcher. Consume with `for await (const batch of updates$)`;
|
|
1413
|
+
* each iteration starts an independent fan-out from the shared source.
|
|
1414
|
+
*/
|
|
1264
1415
|
updates$;
|
|
1265
1416
|
constructor(options) {
|
|
1266
|
-
const { url, project, token,
|
|
1417
|
+
const { url, project, token, schema } = options;
|
|
1418
|
+
this._schema = schema;
|
|
1267
1419
|
this._client = createContelloClient({
|
|
1268
1420
|
url,
|
|
1269
1421
|
project,
|
|
1270
1422
|
token,
|
|
1271
|
-
|
|
1423
|
+
schema,
|
|
1272
1424
|
connections: options.connections,
|
|
1273
1425
|
onConnected: options.onConnected,
|
|
1274
1426
|
onReconnecting: options.onReconnecting,
|
|
1275
1427
|
onError: options.onError,
|
|
1276
1428
|
connectionEvents: options.connectionEvents
|
|
1277
1429
|
});
|
|
1278
|
-
this._resolver = new ModelResolver(
|
|
1430
|
+
this._resolver = new ModelResolver(schema?.models);
|
|
1279
1431
|
this._watcher = createInternalWatcher(this._client, this._resolver);
|
|
1280
1432
|
this.updates$ = this._watcher.updates$;
|
|
1281
1433
|
this.ping = () => this._client.ping();
|
|
1282
1434
|
}
|
|
1435
|
+
get client() {
|
|
1436
|
+
return this._client;
|
|
1437
|
+
}
|
|
1283
1438
|
async init() {
|
|
1284
1439
|
await wrap("store:init", () => this._client.init());
|
|
1285
1440
|
this._watcher.start();
|
|
1286
1441
|
}
|
|
1287
1442
|
async destroy() {
|
|
1443
|
+
for (const fn of this._cleanups.splice(0)) {
|
|
1444
|
+
try {
|
|
1445
|
+
fn();
|
|
1446
|
+
} catch {
|
|
1447
|
+
}
|
|
1448
|
+
}
|
|
1288
1449
|
this._watcher.stop();
|
|
1289
1450
|
await wrap("store:destroy", () => this._client.destroy());
|
|
1290
1451
|
}
|
|
1291
|
-
|
|
1292
|
-
|
|
1452
|
+
/** Resolve a string key (looked up via `schema.sources`) or a SourceDef directly to a SourceDef. */
|
|
1453
|
+
_resolveSource(sourceOrKey) {
|
|
1454
|
+
if (typeof sourceOrKey !== "string") {
|
|
1455
|
+
return sourceOrKey;
|
|
1456
|
+
}
|
|
1457
|
+
const sources2 = this._schema?.sources;
|
|
1458
|
+
const source = sources2?.[sourceOrKey];
|
|
1459
|
+
if (!source) {
|
|
1460
|
+
throw new Error(`@contello/store: no source named "${sourceOrKey}" in schema.sources`);
|
|
1461
|
+
}
|
|
1462
|
+
return source;
|
|
1293
1463
|
}
|
|
1294
|
-
|
|
1295
|
-
|
|
1464
|
+
// --- singleton ---
|
|
1465
|
+
defineSingleton(sourceOrKey, options) {
|
|
1466
|
+
const source = this._resolveSource(sourceOrKey);
|
|
1467
|
+
const { instance, destroy } = createSingleton(
|
|
1468
|
+
source,
|
|
1469
|
+
options,
|
|
1470
|
+
this._client,
|
|
1471
|
+
this._watcher.updates$,
|
|
1472
|
+
this._resolver
|
|
1473
|
+
);
|
|
1474
|
+
this._cleanups.push(destroy);
|
|
1475
|
+
return instance;
|
|
1296
1476
|
}
|
|
1297
|
-
|
|
1298
|
-
|
|
1477
|
+
defineSingletonSync(sourceOrKey, options) {
|
|
1478
|
+
const source = this._resolveSource(sourceOrKey);
|
|
1479
|
+
const { instance, destroy } = createSingletonSync(
|
|
1480
|
+
source,
|
|
1481
|
+
options,
|
|
1482
|
+
this._client,
|
|
1483
|
+
this._watcher.updates$,
|
|
1484
|
+
this._resolver
|
|
1485
|
+
);
|
|
1486
|
+
this._cleanups.push(destroy);
|
|
1487
|
+
return instance;
|
|
1299
1488
|
}
|
|
1300
|
-
|
|
1301
|
-
|
|
1489
|
+
// --- collection ---
|
|
1490
|
+
defineCollection(sourceOrKey, options) {
|
|
1491
|
+
const source = this._resolveSource(sourceOrKey);
|
|
1492
|
+
const { instance, destroy } = createCollection(
|
|
1493
|
+
source,
|
|
1494
|
+
options,
|
|
1495
|
+
this._client,
|
|
1496
|
+
this._watcher.updates$,
|
|
1497
|
+
this._resolver
|
|
1498
|
+
);
|
|
1499
|
+
this._cleanups.push(destroy);
|
|
1500
|
+
return instance;
|
|
1302
1501
|
}
|
|
1303
|
-
|
|
1304
|
-
|
|
1502
|
+
defineCollectionSync(sourceOrKey, options) {
|
|
1503
|
+
const source = this._resolveSource(sourceOrKey);
|
|
1504
|
+
const { instance, destroy } = createCollectionSync(
|
|
1505
|
+
source,
|
|
1506
|
+
options,
|
|
1507
|
+
this._client,
|
|
1508
|
+
this._watcher.updates$,
|
|
1509
|
+
this._resolver
|
|
1510
|
+
);
|
|
1511
|
+
this._cleanups.push(destroy);
|
|
1512
|
+
return instance;
|
|
1305
1513
|
}
|
|
1514
|
+
// --- lazy collection ---
|
|
1515
|
+
defineLazyCollection(sourceOrKey, options) {
|
|
1516
|
+
const source = this._resolveSource(sourceOrKey);
|
|
1517
|
+
const { instance, destroy } = createLazyCollection(
|
|
1518
|
+
source,
|
|
1519
|
+
options,
|
|
1520
|
+
this._client,
|
|
1521
|
+
this._watcher.updates$,
|
|
1522
|
+
this._resolver
|
|
1523
|
+
);
|
|
1524
|
+
this._cleanups.push(destroy);
|
|
1525
|
+
return instance;
|
|
1526
|
+
}
|
|
1527
|
+
// --- assets / routes / i18n (no source — built-ins) ---
|
|
1306
1528
|
defineAssets(options) {
|
|
1307
|
-
|
|
1529
|
+
const { instance, destroy } = createAssetsCollection(options, this._client, this._watcher.updates$);
|
|
1530
|
+
this._cleanups.push(destroy);
|
|
1531
|
+
return instance;
|
|
1308
1532
|
}
|
|
1309
1533
|
defineRoutes(options) {
|
|
1310
|
-
|
|
1534
|
+
const { instance, destroy } = createRoutesCollection(options, this._client, this._watcher.updates$, this._resolver);
|
|
1535
|
+
this._cleanups.push(destroy);
|
|
1536
|
+
return instance;
|
|
1311
1537
|
}
|
|
1312
1538
|
defineI18nMessages(def) {
|
|
1313
|
-
|
|
1539
|
+
const result = createI18nMessagesCollection(def, this._client, this._watcher.updates$);
|
|
1540
|
+
this._cleanups.push(result.destroy);
|
|
1541
|
+
return result;
|
|
1314
1542
|
}
|
|
1315
1543
|
ping;
|
|
1316
1544
|
};
|
|
1317
1545
|
function createStore(options) {
|
|
1318
1546
|
return new Store(options);
|
|
1319
1547
|
}
|
|
1548
|
+
|
|
1549
|
+
// src/index.ts
|
|
1550
|
+
import { maybeThen as maybeThen5, maybeCatch } from "projected";
|
|
1320
1551
|
export {
|
|
1321
1552
|
Store,
|
|
1322
|
-
createStore
|
|
1553
|
+
createStore,
|
|
1554
|
+
maybeCatch,
|
|
1555
|
+
maybeThen5 as maybeThen
|
|
1323
1556
|
};
|