@contello/store 1.5.1 → 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.d.cts CHANGED
@@ -1,6 +1,6 @@
1
- import { OperationMap, ContelloClient, ConnectionEvents, UploadData, UploadMetadata, UploadOptions, DownloadResult, ProxyResult } from '@contello/client';
2
- import { Observable } from 'rxjs';
3
- import { MaybePromise } from 'projected';
1
+ import { Schema, ConnectionEvents, SourceDef, SourceCardinality, UploadData, UploadMetadata, UploadOptions, DownloadResult, ProxyResult, ContelloClient } from '@contello/client';
2
+ import { MaybePromise, ReadonlyDeep } from 'projected';
3
+ export { MaybePromise, ReadonlyDeep, maybeCatch, maybeThen } from 'projected';
4
4
 
5
5
  type StoreRouteCustomHeader = {
6
6
  name: string;
@@ -100,13 +100,28 @@ type MapperContext<TModels extends string = string> = {
100
100
  trackRoute(id: string): void;
101
101
  };
102
102
 
103
- type Fetchable<T> = MaybePromise<T> | Observable<T>;
104
- type CreateStoreOptions<TOps extends OperationMap | undefined = undefined, TModels extends string = string> = {
103
+ /** Extracts the typed fragment result from a SourceDef. */
104
+ type ExtractSourceResult<S extends SourceDef<string, 'entity' | 'singleton'>> = NonNullable<S['__result']>;
105
+ /** Keys of `schema.sources` whose entries match the given cardinality. */
106
+ type SourceKeysOf<TSchema, TCardinality extends SourceCardinality> = TSchema extends {
107
+ sources: infer TSources;
108
+ } ? {
109
+ [K in keyof TSources]: TSources[K] extends SourceDef<any, TCardinality, any> ? K : never;
110
+ }[keyof TSources] : never;
111
+ /** The SourceDef at `schema.sources[TKey]`. */
112
+ type SourceAt<TSchema, TKey> = TSchema extends {
113
+ sources: infer TSources;
114
+ } ? TKey extends keyof TSources ? TSources[TKey] : never : never;
115
+ /**
116
+ * For `define*(arg, options?)` — accepts either a string key (looked up in `schema.sources`) or a SourceDef directly.
117
+ * `ResolveSource` projects the argument to the underlying SourceDef so `ExtractSourceResult` can extract the fragment type.
118
+ */
119
+ type ResolveSource<TSchema, T> = T extends string ? SourceAt<TSchema, T> : T extends SourceDef<any, any, any> ? T : never;
120
+ type CreateStoreOptions<TSchema extends Schema | undefined = undefined> = {
105
121
  url: string;
106
122
  project: string;
107
123
  token: string;
108
- models?: Record<TModels, string> | undefined;
109
- operations?: TOps | undefined;
124
+ schema?: TSchema | undefined;
110
125
  connections?: number | undefined;
111
126
  onConnected?: (() => void) | undefined;
112
127
  onReconnecting?: (() => void) | undefined;
@@ -119,8 +134,10 @@ type CacheOptions = {
119
134
  ttl?: number | undefined;
120
135
  /**
121
136
  * Controls how the cache responds to Contello update events.
122
- * `'refresh'` (default) serves stale data while a new fetch runs in the background (SWR).
137
+ * `'refresh'` (default) serves stale data while a partial fetch runs in the background (SWR);
138
+ * only the ids touched by the update are re-fetched, not the whole collection.
123
139
  * `'clear'` wipes the cache immediately so the next `get()` awaits the fresh result.
140
+ * Note: the partial-refresh path is bypassed in `'clear'` mode.
124
141
  */
125
142
  eviction?: 'refresh' | 'clear' | undefined;
126
143
  };
@@ -136,20 +153,16 @@ type LazyCacheOptions = {
136
153
  /** Items are evicted from the LRU cache after this many ms. */
137
154
  ttl?: number | undefined;
138
155
  };
139
- type SingletonDef<TOps extends OperationMap | undefined, TModel extends string, TRaw, TMapped, TModels extends string = string> = {
156
+ type SingletonOptions<TRaw, TMapped, TModels extends string = string> = {
140
157
  name?: string | undefined;
141
- model: TModel;
142
- fetch: (client: ContelloClient<TOps>) => MaybePromise<TRaw>;
143
- map: (item: TRaw, ref: MapperContext<TModels>) => MaybePromise<TMapped>;
158
+ map?: ((item: TRaw, ref: MapperContext<TModels>) => MaybePromise<TMapped>) | undefined;
144
159
  cache?: CacheOptions | undefined;
145
160
  onLoad?: (() => void) | undefined;
146
161
  onRefresh?: (() => void) | undefined;
147
162
  };
148
- type SingletonSyncDef<TOps extends OperationMap | undefined, TModel extends string, TRaw, TMapped, TModels extends string = string> = {
163
+ type SingletonSyncOptions<TRaw, TMapped, TModels extends string = string> = {
149
164
  name?: string | undefined;
150
- model: TModel;
151
- fetch: (client: ContelloClient<TOps>) => MaybePromise<TRaw>;
152
- map: (item: TRaw, ref: MapperContext<TModels>) => MaybePromise<TMapped>;
165
+ map?: ((item: TRaw, ref: MapperContext<TModels>) => MaybePromise<TMapped>) | undefined;
153
166
  cache?: SyncCacheOptions | undefined;
154
167
  onLoad?: (() => void) | undefined;
155
168
  onRefresh?: (() => void) | undefined;
@@ -159,73 +172,69 @@ type Loadable = {
159
172
  };
160
173
  type Singleton<T> = {
161
174
  readonly name: string;
162
- readonly refresh$: Observable<void>;
175
+ readonly refresh$: AsyncIterable<void>;
163
176
  load(): Promise<void>;
164
- get(): MaybePromise<T>;
177
+ get(): MaybePromise<ReadonlyDeep<T>>;
165
178
  refresh(): void;
166
179
  };
167
180
  type SingletonSync<T> = {
168
181
  readonly name: string;
169
- readonly refresh$: Observable<void>;
182
+ readonly refresh$: AsyncIterable<void>;
170
183
  load(): Promise<void>;
171
- get(): T;
184
+ get(): ReadonlyDeep<T>;
172
185
  refresh(): void;
173
186
  };
174
- type CollectionDef<TOps extends OperationMap | undefined, TModel extends string, TRaw, TMapped extends {
187
+ type CollectionOptions<TRaw, TMapped extends {
175
188
  id: string;
176
189
  }, TModels extends string = string> = {
177
190
  name?: string | undefined;
178
- model: TModel;
179
- fetch: (client: ContelloClient<TOps>) => Fetchable<TRaw[]>;
180
- map: (item: TRaw, ref: MapperContext<TModels>) => MaybePromise<TMapped>;
191
+ map?: ((item: TRaw, ref: MapperContext<TModels>) => MaybePromise<TMapped>) | undefined;
192
+ sort?: ((a: TMapped, b: TMapped) => number) | undefined;
181
193
  cache?: CacheOptions | undefined;
182
194
  onLoad?: ((ids: string[]) => void) | undefined;
183
195
  onRefresh?: ((ids: string[]) => void) | undefined;
184
196
  };
185
- type CollectionSyncDef<TOps extends OperationMap | undefined, TModel extends string, TRaw, TMapped extends {
197
+ type CollectionSyncOptions<TRaw, TMapped extends {
186
198
  id: string;
187
199
  }, TModels extends string = string> = {
188
200
  name?: string | undefined;
189
- model: TModel;
190
- fetch: (client: ContelloClient<TOps>) => Fetchable<TRaw[]>;
191
- map: (item: TRaw, ref: MapperContext<TModels>) => MaybePromise<TMapped>;
201
+ map?: ((item: TRaw, ref: MapperContext<TModels>) => MaybePromise<TMapped>) | undefined;
202
+ sort?: ((a: TMapped, b: TMapped) => number) | undefined;
192
203
  cache?: SyncCacheOptions | undefined;
193
204
  onLoad?: ((ids: string[]) => void) | undefined;
194
205
  onRefresh?: ((ids: string[]) => void) | undefined;
195
206
  };
196
207
  type Collection<T> = {
197
208
  readonly name: string;
198
- readonly refresh$: Observable<string[]>;
209
+ readonly refresh$: AsyncIterable<string[]>;
199
210
  load(): Promise<void>;
200
- get(id: string): MaybePromise<T | undefined>;
201
- get(ids: string[]): MaybePromise<T[]>;
202
- getAll(): MaybePromise<T[]>;
211
+ get(id: string): MaybePromise<ReadonlyDeep<T> | undefined>;
212
+ get(ids: string[]): MaybePromise<ReadonlyArray<ReadonlyDeep<T>>>;
213
+ getAll(): MaybePromise<ReadonlyArray<ReadonlyDeep<T>>>;
203
214
  refresh(): void;
204
215
  };
205
216
  type CollectionSync<T> = {
206
217
  readonly name: string;
207
- readonly refresh$: Observable<string[]>;
218
+ readonly refresh$: AsyncIterable<string[]>;
208
219
  load(): Promise<void>;
209
- get(id: string): T | undefined;
210
- get(ids: string[]): T[];
211
- getAll(): T[];
220
+ get(id: string): ReadonlyDeep<T> | undefined;
221
+ get(ids: string[]): ReadonlyArray<ReadonlyDeep<T>>;
222
+ getAll(): ReadonlyArray<ReadonlyDeep<T>>;
212
223
  refresh(): void;
213
224
  };
214
- type LazyCollectionDef<TOps extends OperationMap | undefined, TModel extends string, TRaw, TMapped extends {
225
+ type LazyCollectionOptions<TRaw, TMapped extends {
215
226
  id: string;
216
227
  }, TModels extends string = string> = {
217
228
  name?: string | undefined;
218
- model: TModel;
219
229
  cache?: LazyCacheOptions | undefined;
220
- fetch: (ids: string[], client: ContelloClient<TOps>) => Fetchable<TRaw[]>;
221
- map: (item: TRaw, ref: MapperContext<TModels>) => MaybePromise<TMapped>;
230
+ map?: ((item: TRaw, ref: MapperContext<TModels>) => MaybePromise<TMapped>) | undefined;
222
231
  onRefresh?: ((ids: string[]) => void) | undefined;
223
232
  };
224
233
  type LazyCollection<T> = {
225
234
  readonly name: string;
226
- readonly refresh$: Observable<string[]>;
227
- get(id: string): MaybePromise<T | undefined>;
228
- get(ids: string[]): MaybePromise<T[]>;
235
+ readonly refresh$: AsyncIterable<string[]>;
236
+ get(id: string): MaybePromise<ReadonlyDeep<T> | undefined>;
237
+ get(ids: string[]): MaybePromise<ReadonlyArray<ReadonlyDeep<T>>>;
229
238
  refresh(): void;
230
239
  clear(): void;
231
240
  };
@@ -249,9 +258,9 @@ type AssetCollectionOptions = {
249
258
  cache?: LazyCacheOptions | undefined;
250
259
  };
251
260
  type Assets = {
252
- readonly refresh$: Observable<string[]>;
253
- get(id: string): Promise<StoreAsset | undefined>;
254
- get(ids: string[]): Promise<StoreAsset[]>;
261
+ readonly refresh$: AsyncIterable<string[]>;
262
+ get(id: string): Promise<ReadonlyDeep<StoreAsset> | undefined>;
263
+ get(ids: string[]): Promise<ReadonlyDeep<StoreAsset[]>>;
255
264
  upload(data: UploadData, meta: UploadMetadata, options?: UploadOptions | undefined): Promise<string>;
256
265
  download(fileId: string): Promise<DownloadResult>;
257
266
  proxyHls(path: string, signal?: AbortSignal | undefined): Promise<ProxyResult>;
@@ -291,50 +300,75 @@ type I18nMessage = {
291
300
  translations: Map<string, string>;
292
301
  };
293
302
  type I18nMessages = {
294
- readonly refresh$: Observable<string[]>;
295
- get(id: string): MaybePromise<I18nMessage | undefined>;
296
- get(ids: string[]): MaybePromise<I18nMessage[]>;
297
- getAll(): MaybePromise<I18nMessage[]>;
303
+ readonly refresh$: AsyncIterable<string[]>;
304
+ get(id: string): MaybePromise<ReadonlyDeep<I18nMessage> | undefined>;
305
+ get(ids: string[]): MaybePromise<ReadonlyDeep<I18nMessage[]>>;
306
+ getAll(): MaybePromise<ReadonlyDeep<I18nMessage[]>>;
298
307
  register(messages: I18nMessageRegistrationDefinition[]): Promise<void>;
308
+ /** Completes refresh$ and detaches from the watcher — called by `Store.destroy()`. */
309
+ destroy(): void;
299
310
  };
300
311
 
301
312
  type RouteCollectionOptions = {
302
313
  cache?: LazyCacheOptions | undefined;
303
314
  };
304
315
  type Routes = {
305
- readonly refresh$: Observable<string[]>;
306
- get(id: string): Promise<StoreRoute | undefined>;
307
- get(ids: string[]): Promise<StoreRoute[]>;
308
- getByPath(path: string): Promise<StoreRoute | undefined>;
309
- getByPath(paths: string[]): Promise<StoreRoute[]>;
316
+ readonly refresh$: AsyncIterable<string[]>;
317
+ get(id: string): Promise<ReadonlyDeep<StoreRoute> | undefined>;
318
+ get(ids: string[]): Promise<ReadonlyDeep<StoreRoute[]>>;
319
+ getByPath(path: string): Promise<ReadonlyDeep<StoreRoute> | undefined>;
320
+ getByPath(paths: string[]): Promise<ReadonlyDeep<StoreRoute[]>>;
310
321
  refresh(): void;
311
322
  clear(): void;
312
323
  };
313
324
 
314
- declare class Store<TOps extends OperationMap | undefined = undefined, TModels extends string = string> {
325
+ /** Project the model-reference-name union out of a Schema generic, falling back to `string`. */
326
+ type ModelsOf<TSchema> = TSchema extends {
327
+ models: infer M;
328
+ } ? keyof M & string : string;
329
+ type CollectionArg<TSchema> = SourceKeysOf<TSchema, 'entity'> | SourceDef<ModelsOf<TSchema>, 'entity'>;
330
+ type SingletonArg<TSchema> = SourceKeysOf<TSchema, 'singleton'> | SourceDef<ModelsOf<TSchema>, 'singleton'>;
331
+ type CollectionRaw<TSchema, TArg> = ExtractSourceResult<Extract<ResolveSource<TSchema, TArg>, SourceDef<string, 'entity'>>>;
332
+ type SingletonRaw<TSchema, TArg> = ExtractSourceResult<Extract<ResolveSource<TSchema, TArg>, SourceDef<string, 'singleton'>>>;
333
+ declare class Store<TSchema extends Schema | undefined = undefined> {
315
334
  private _client;
316
335
  private _resolver;
317
336
  private _watcher;
318
- readonly updates$: Observable<UpdateBatch>;
319
- constructor(options: CreateStoreOptions<TOps, TModels>);
337
+ private _schema;
338
+ private _cleanups;
339
+ /**
340
+ * Multicast update-batch stream from the watcher. Consume with `for await (const batch of updates$)`;
341
+ * each iteration starts an independent fan-out from the shared source.
342
+ */
343
+ readonly updates$: AsyncIterable<UpdateBatch>;
344
+ constructor(options: CreateStoreOptions<TSchema>);
345
+ get client(): ContelloClient<TSchema>;
320
346
  init(): Promise<void>;
321
347
  destroy(): Promise<void>;
322
- defineSingleton<TModel extends TModels, TRaw, TMapped>(def: SingletonDef<TOps, TModel, TRaw, TMapped, TModels>): Singleton<TMapped>;
323
- defineSingletonSync<TModel extends TModels, TRaw, TMapped>(def: SingletonSyncDef<TOps, TModel, TRaw, TMapped, TModels>): SingletonSync<TMapped>;
324
- defineCollection<TModel extends TModels, TRaw, TMapped extends {
348
+ /** Resolve a string key (looked up via `schema.sources`) or a SourceDef directly to a SourceDef. */
349
+ private _resolveSource;
350
+ defineSingleton<TArg extends SingletonArg<TSchema>, TMapped = SingletonRaw<TSchema, TArg>>(sourceOrKey: TArg, options?: SingletonOptions<SingletonRaw<TSchema, TArg>, TMapped, ModelsOf<TSchema>>): Singleton<TMapped>;
351
+ defineSingletonSync<TArg extends SingletonArg<TSchema>, TMapped = SingletonRaw<TSchema, TArg>>(sourceOrKey: TArg, options?: SingletonSyncOptions<SingletonRaw<TSchema, TArg>, TMapped, ModelsOf<TSchema>>): SingletonSync<TMapped>;
352
+ defineCollection<TArg extends CollectionArg<TSchema>, TMapped extends {
353
+ id: string;
354
+ } = CollectionRaw<TSchema, TArg> & {
355
+ id: string;
356
+ }>(sourceOrKey: TArg, options?: CollectionOptions<CollectionRaw<TSchema, TArg>, TMapped, ModelsOf<TSchema>>): Collection<TMapped>;
357
+ defineCollectionSync<TArg extends CollectionArg<TSchema>, TMapped extends {
358
+ id: string;
359
+ } = CollectionRaw<TSchema, TArg> & {
325
360
  id: string;
326
- }>(def: CollectionDef<TOps, TModel, TRaw, TMapped, TModels>): Collection<TMapped>;
327
- defineCollectionSync<TModel extends TModels, TRaw, TMapped extends {
361
+ }>(sourceOrKey: TArg, options?: CollectionSyncOptions<CollectionRaw<TSchema, TArg>, TMapped, ModelsOf<TSchema>>): CollectionSync<TMapped>;
362
+ defineLazyCollection<TArg extends CollectionArg<TSchema>, TMapped extends {
328
363
  id: string;
329
- }>(def: CollectionSyncDef<TOps, TModel, TRaw, TMapped, TModels>): CollectionSync<TMapped>;
330
- defineLazyCollection<TModel extends TModels, TRaw, TMapped extends {
364
+ } = CollectionRaw<TSchema, TArg> & {
331
365
  id: string;
332
- }>(def: LazyCollectionDef<TOps, TModel, TRaw, TMapped, TModels>): LazyCollection<TMapped>;
366
+ }>(sourceOrKey: TArg, options?: LazyCollectionOptions<CollectionRaw<TSchema, TArg>, TMapped, ModelsOf<TSchema>>): LazyCollection<TMapped>;
333
367
  defineAssets(options?: AssetCollectionOptions | undefined): Assets;
334
368
  defineRoutes(options?: RouteCollectionOptions | undefined): Routes;
335
369
  defineI18nMessages(def: I18nMessageDef): I18nMessages;
336
370
  ping: () => Promise<void>;
337
371
  }
338
- declare function createStore<TOps extends OperationMap | undefined = undefined, TModels extends string = string>(options: CreateStoreOptions<TOps, TModels>): Store<TOps, TModels>;
372
+ declare function createStore<TSchema extends Schema | undefined = undefined>(options: CreateStoreOptions<TSchema>): Store<TSchema>;
339
373
 
340
- export { type AssetCollectionOptions, type Assets, type CacheOptions, type Collection, type CollectionDef, type CollectionSync, type CollectionSyncDef, type CreateStoreOptions, type Fetchable, type I18nInitialTranslation, type I18nMessage, type I18nMessageDef, type I18nMessageRegistrationDefinition, type I18nMessages, type I18nTranslation, type I18nVariableInput, type LazyCacheOptions, type LazyCollection, type LazyCollectionDef, type Loadable, type MapperContext, type RouteCollectionOptions, type Routes, type Singleton, type SingletonDef, type SingletonSync, type SingletonSyncDef, Store, type StoreAsset, type StoreFile, type StoreFileMetadata, type StoreRoute, type StoreRouteCustomHeader, type SyncCacheOptions, type UpdateBatch, type UpdateEvent, type UpdateEventFor, type UpdateMutationType, createStore };
374
+ export { type AssetCollectionOptions, type Assets, type CacheOptions, type Collection, type CollectionOptions, type CollectionSync, type CollectionSyncOptions, type CreateStoreOptions, type ExtractSourceResult, type I18nInitialTranslation, type I18nMessage, type I18nMessageDef, type I18nMessageRegistrationDefinition, type I18nMessages, type I18nTranslation, type I18nVariableInput, type LazyCacheOptions, type LazyCollection, type LazyCollectionOptions, type Loadable, type MapperContext, type ResolveSource, type RouteCollectionOptions, type Routes, type Singleton, type SingletonOptions, type SingletonSync, type SingletonSyncOptions, type SourceAt, type SourceKeysOf, Store, type StoreAsset, type StoreFile, type StoreFileMetadata, type StoreRoute, type StoreRouteCustomHeader, type SyncCacheOptions, type UpdateBatch, type UpdateEvent, type UpdateEventFor, type UpdateMutationType, createStore };
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
- import { OperationMap, ContelloClient, ConnectionEvents, UploadData, UploadMetadata, UploadOptions, DownloadResult, ProxyResult } from '@contello/client';
2
- import { Observable } from 'rxjs';
3
- import { MaybePromise } from 'projected';
1
+ import { Schema, ConnectionEvents, SourceDef, SourceCardinality, UploadData, UploadMetadata, UploadOptions, DownloadResult, ProxyResult, ContelloClient } from '@contello/client';
2
+ import { MaybePromise, ReadonlyDeep } from 'projected';
3
+ export { MaybePromise, ReadonlyDeep, maybeCatch, maybeThen } from 'projected';
4
4
 
5
5
  type StoreRouteCustomHeader = {
6
6
  name: string;
@@ -100,13 +100,28 @@ type MapperContext<TModels extends string = string> = {
100
100
  trackRoute(id: string): void;
101
101
  };
102
102
 
103
- type Fetchable<T> = MaybePromise<T> | Observable<T>;
104
- type CreateStoreOptions<TOps extends OperationMap | undefined = undefined, TModels extends string = string> = {
103
+ /** Extracts the typed fragment result from a SourceDef. */
104
+ type ExtractSourceResult<S extends SourceDef<string, 'entity' | 'singleton'>> = NonNullable<S['__result']>;
105
+ /** Keys of `schema.sources` whose entries match the given cardinality. */
106
+ type SourceKeysOf<TSchema, TCardinality extends SourceCardinality> = TSchema extends {
107
+ sources: infer TSources;
108
+ } ? {
109
+ [K in keyof TSources]: TSources[K] extends SourceDef<any, TCardinality, any> ? K : never;
110
+ }[keyof TSources] : never;
111
+ /** The SourceDef at `schema.sources[TKey]`. */
112
+ type SourceAt<TSchema, TKey> = TSchema extends {
113
+ sources: infer TSources;
114
+ } ? TKey extends keyof TSources ? TSources[TKey] : never : never;
115
+ /**
116
+ * For `define*(arg, options?)` — accepts either a string key (looked up in `schema.sources`) or a SourceDef directly.
117
+ * `ResolveSource` projects the argument to the underlying SourceDef so `ExtractSourceResult` can extract the fragment type.
118
+ */
119
+ type ResolveSource<TSchema, T> = T extends string ? SourceAt<TSchema, T> : T extends SourceDef<any, any, any> ? T : never;
120
+ type CreateStoreOptions<TSchema extends Schema | undefined = undefined> = {
105
121
  url: string;
106
122
  project: string;
107
123
  token: string;
108
- models?: Record<TModels, string> | undefined;
109
- operations?: TOps | undefined;
124
+ schema?: TSchema | undefined;
110
125
  connections?: number | undefined;
111
126
  onConnected?: (() => void) | undefined;
112
127
  onReconnecting?: (() => void) | undefined;
@@ -119,8 +134,10 @@ type CacheOptions = {
119
134
  ttl?: number | undefined;
120
135
  /**
121
136
  * Controls how the cache responds to Contello update events.
122
- * `'refresh'` (default) serves stale data while a new fetch runs in the background (SWR).
137
+ * `'refresh'` (default) serves stale data while a partial fetch runs in the background (SWR);
138
+ * only the ids touched by the update are re-fetched, not the whole collection.
123
139
  * `'clear'` wipes the cache immediately so the next `get()` awaits the fresh result.
140
+ * Note: the partial-refresh path is bypassed in `'clear'` mode.
124
141
  */
125
142
  eviction?: 'refresh' | 'clear' | undefined;
126
143
  };
@@ -136,20 +153,16 @@ type LazyCacheOptions = {
136
153
  /** Items are evicted from the LRU cache after this many ms. */
137
154
  ttl?: number | undefined;
138
155
  };
139
- type SingletonDef<TOps extends OperationMap | undefined, TModel extends string, TRaw, TMapped, TModels extends string = string> = {
156
+ type SingletonOptions<TRaw, TMapped, TModels extends string = string> = {
140
157
  name?: string | undefined;
141
- model: TModel;
142
- fetch: (client: ContelloClient<TOps>) => MaybePromise<TRaw>;
143
- map: (item: TRaw, ref: MapperContext<TModels>) => MaybePromise<TMapped>;
158
+ map?: ((item: TRaw, ref: MapperContext<TModels>) => MaybePromise<TMapped>) | undefined;
144
159
  cache?: CacheOptions | undefined;
145
160
  onLoad?: (() => void) | undefined;
146
161
  onRefresh?: (() => void) | undefined;
147
162
  };
148
- type SingletonSyncDef<TOps extends OperationMap | undefined, TModel extends string, TRaw, TMapped, TModels extends string = string> = {
163
+ type SingletonSyncOptions<TRaw, TMapped, TModels extends string = string> = {
149
164
  name?: string | undefined;
150
- model: TModel;
151
- fetch: (client: ContelloClient<TOps>) => MaybePromise<TRaw>;
152
- map: (item: TRaw, ref: MapperContext<TModels>) => MaybePromise<TMapped>;
165
+ map?: ((item: TRaw, ref: MapperContext<TModels>) => MaybePromise<TMapped>) | undefined;
153
166
  cache?: SyncCacheOptions | undefined;
154
167
  onLoad?: (() => void) | undefined;
155
168
  onRefresh?: (() => void) | undefined;
@@ -159,73 +172,69 @@ type Loadable = {
159
172
  };
160
173
  type Singleton<T> = {
161
174
  readonly name: string;
162
- readonly refresh$: Observable<void>;
175
+ readonly refresh$: AsyncIterable<void>;
163
176
  load(): Promise<void>;
164
- get(): MaybePromise<T>;
177
+ get(): MaybePromise<ReadonlyDeep<T>>;
165
178
  refresh(): void;
166
179
  };
167
180
  type SingletonSync<T> = {
168
181
  readonly name: string;
169
- readonly refresh$: Observable<void>;
182
+ readonly refresh$: AsyncIterable<void>;
170
183
  load(): Promise<void>;
171
- get(): T;
184
+ get(): ReadonlyDeep<T>;
172
185
  refresh(): void;
173
186
  };
174
- type CollectionDef<TOps extends OperationMap | undefined, TModel extends string, TRaw, TMapped extends {
187
+ type CollectionOptions<TRaw, TMapped extends {
175
188
  id: string;
176
189
  }, TModels extends string = string> = {
177
190
  name?: string | undefined;
178
- model: TModel;
179
- fetch: (client: ContelloClient<TOps>) => Fetchable<TRaw[]>;
180
- map: (item: TRaw, ref: MapperContext<TModels>) => MaybePromise<TMapped>;
191
+ map?: ((item: TRaw, ref: MapperContext<TModels>) => MaybePromise<TMapped>) | undefined;
192
+ sort?: ((a: TMapped, b: TMapped) => number) | undefined;
181
193
  cache?: CacheOptions | undefined;
182
194
  onLoad?: ((ids: string[]) => void) | undefined;
183
195
  onRefresh?: ((ids: string[]) => void) | undefined;
184
196
  };
185
- type CollectionSyncDef<TOps extends OperationMap | undefined, TModel extends string, TRaw, TMapped extends {
197
+ type CollectionSyncOptions<TRaw, TMapped extends {
186
198
  id: string;
187
199
  }, TModels extends string = string> = {
188
200
  name?: string | undefined;
189
- model: TModel;
190
- fetch: (client: ContelloClient<TOps>) => Fetchable<TRaw[]>;
191
- map: (item: TRaw, ref: MapperContext<TModels>) => MaybePromise<TMapped>;
201
+ map?: ((item: TRaw, ref: MapperContext<TModels>) => MaybePromise<TMapped>) | undefined;
202
+ sort?: ((a: TMapped, b: TMapped) => number) | undefined;
192
203
  cache?: SyncCacheOptions | undefined;
193
204
  onLoad?: ((ids: string[]) => void) | undefined;
194
205
  onRefresh?: ((ids: string[]) => void) | undefined;
195
206
  };
196
207
  type Collection<T> = {
197
208
  readonly name: string;
198
- readonly refresh$: Observable<string[]>;
209
+ readonly refresh$: AsyncIterable<string[]>;
199
210
  load(): Promise<void>;
200
- get(id: string): MaybePromise<T | undefined>;
201
- get(ids: string[]): MaybePromise<T[]>;
202
- getAll(): MaybePromise<T[]>;
211
+ get(id: string): MaybePromise<ReadonlyDeep<T> | undefined>;
212
+ get(ids: string[]): MaybePromise<ReadonlyArray<ReadonlyDeep<T>>>;
213
+ getAll(): MaybePromise<ReadonlyArray<ReadonlyDeep<T>>>;
203
214
  refresh(): void;
204
215
  };
205
216
  type CollectionSync<T> = {
206
217
  readonly name: string;
207
- readonly refresh$: Observable<string[]>;
218
+ readonly refresh$: AsyncIterable<string[]>;
208
219
  load(): Promise<void>;
209
- get(id: string): T | undefined;
210
- get(ids: string[]): T[];
211
- getAll(): T[];
220
+ get(id: string): ReadonlyDeep<T> | undefined;
221
+ get(ids: string[]): ReadonlyArray<ReadonlyDeep<T>>;
222
+ getAll(): ReadonlyArray<ReadonlyDeep<T>>;
212
223
  refresh(): void;
213
224
  };
214
- type LazyCollectionDef<TOps extends OperationMap | undefined, TModel extends string, TRaw, TMapped extends {
225
+ type LazyCollectionOptions<TRaw, TMapped extends {
215
226
  id: string;
216
227
  }, TModels extends string = string> = {
217
228
  name?: string | undefined;
218
- model: TModel;
219
229
  cache?: LazyCacheOptions | undefined;
220
- fetch: (ids: string[], client: ContelloClient<TOps>) => Fetchable<TRaw[]>;
221
- map: (item: TRaw, ref: MapperContext<TModels>) => MaybePromise<TMapped>;
230
+ map?: ((item: TRaw, ref: MapperContext<TModels>) => MaybePromise<TMapped>) | undefined;
222
231
  onRefresh?: ((ids: string[]) => void) | undefined;
223
232
  };
224
233
  type LazyCollection<T> = {
225
234
  readonly name: string;
226
- readonly refresh$: Observable<string[]>;
227
- get(id: string): MaybePromise<T | undefined>;
228
- get(ids: string[]): MaybePromise<T[]>;
235
+ readonly refresh$: AsyncIterable<string[]>;
236
+ get(id: string): MaybePromise<ReadonlyDeep<T> | undefined>;
237
+ get(ids: string[]): MaybePromise<ReadonlyArray<ReadonlyDeep<T>>>;
229
238
  refresh(): void;
230
239
  clear(): void;
231
240
  };
@@ -249,9 +258,9 @@ type AssetCollectionOptions = {
249
258
  cache?: LazyCacheOptions | undefined;
250
259
  };
251
260
  type Assets = {
252
- readonly refresh$: Observable<string[]>;
253
- get(id: string): Promise<StoreAsset | undefined>;
254
- get(ids: string[]): Promise<StoreAsset[]>;
261
+ readonly refresh$: AsyncIterable<string[]>;
262
+ get(id: string): Promise<ReadonlyDeep<StoreAsset> | undefined>;
263
+ get(ids: string[]): Promise<ReadonlyDeep<StoreAsset[]>>;
255
264
  upload(data: UploadData, meta: UploadMetadata, options?: UploadOptions | undefined): Promise<string>;
256
265
  download(fileId: string): Promise<DownloadResult>;
257
266
  proxyHls(path: string, signal?: AbortSignal | undefined): Promise<ProxyResult>;
@@ -291,50 +300,75 @@ type I18nMessage = {
291
300
  translations: Map<string, string>;
292
301
  };
293
302
  type I18nMessages = {
294
- readonly refresh$: Observable<string[]>;
295
- get(id: string): MaybePromise<I18nMessage | undefined>;
296
- get(ids: string[]): MaybePromise<I18nMessage[]>;
297
- getAll(): MaybePromise<I18nMessage[]>;
303
+ readonly refresh$: AsyncIterable<string[]>;
304
+ get(id: string): MaybePromise<ReadonlyDeep<I18nMessage> | undefined>;
305
+ get(ids: string[]): MaybePromise<ReadonlyDeep<I18nMessage[]>>;
306
+ getAll(): MaybePromise<ReadonlyDeep<I18nMessage[]>>;
298
307
  register(messages: I18nMessageRegistrationDefinition[]): Promise<void>;
308
+ /** Completes refresh$ and detaches from the watcher — called by `Store.destroy()`. */
309
+ destroy(): void;
299
310
  };
300
311
 
301
312
  type RouteCollectionOptions = {
302
313
  cache?: LazyCacheOptions | undefined;
303
314
  };
304
315
  type Routes = {
305
- readonly refresh$: Observable<string[]>;
306
- get(id: string): Promise<StoreRoute | undefined>;
307
- get(ids: string[]): Promise<StoreRoute[]>;
308
- getByPath(path: string): Promise<StoreRoute | undefined>;
309
- getByPath(paths: string[]): Promise<StoreRoute[]>;
316
+ readonly refresh$: AsyncIterable<string[]>;
317
+ get(id: string): Promise<ReadonlyDeep<StoreRoute> | undefined>;
318
+ get(ids: string[]): Promise<ReadonlyDeep<StoreRoute[]>>;
319
+ getByPath(path: string): Promise<ReadonlyDeep<StoreRoute> | undefined>;
320
+ getByPath(paths: string[]): Promise<ReadonlyDeep<StoreRoute[]>>;
310
321
  refresh(): void;
311
322
  clear(): void;
312
323
  };
313
324
 
314
- declare class Store<TOps extends OperationMap | undefined = undefined, TModels extends string = string> {
325
+ /** Project the model-reference-name union out of a Schema generic, falling back to `string`. */
326
+ type ModelsOf<TSchema> = TSchema extends {
327
+ models: infer M;
328
+ } ? keyof M & string : string;
329
+ type CollectionArg<TSchema> = SourceKeysOf<TSchema, 'entity'> | SourceDef<ModelsOf<TSchema>, 'entity'>;
330
+ type SingletonArg<TSchema> = SourceKeysOf<TSchema, 'singleton'> | SourceDef<ModelsOf<TSchema>, 'singleton'>;
331
+ type CollectionRaw<TSchema, TArg> = ExtractSourceResult<Extract<ResolveSource<TSchema, TArg>, SourceDef<string, 'entity'>>>;
332
+ type SingletonRaw<TSchema, TArg> = ExtractSourceResult<Extract<ResolveSource<TSchema, TArg>, SourceDef<string, 'singleton'>>>;
333
+ declare class Store<TSchema extends Schema | undefined = undefined> {
315
334
  private _client;
316
335
  private _resolver;
317
336
  private _watcher;
318
- readonly updates$: Observable<UpdateBatch>;
319
- constructor(options: CreateStoreOptions<TOps, TModels>);
337
+ private _schema;
338
+ private _cleanups;
339
+ /**
340
+ * Multicast update-batch stream from the watcher. Consume with `for await (const batch of updates$)`;
341
+ * each iteration starts an independent fan-out from the shared source.
342
+ */
343
+ readonly updates$: AsyncIterable<UpdateBatch>;
344
+ constructor(options: CreateStoreOptions<TSchema>);
345
+ get client(): ContelloClient<TSchema>;
320
346
  init(): Promise<void>;
321
347
  destroy(): Promise<void>;
322
- defineSingleton<TModel extends TModels, TRaw, TMapped>(def: SingletonDef<TOps, TModel, TRaw, TMapped, TModels>): Singleton<TMapped>;
323
- defineSingletonSync<TModel extends TModels, TRaw, TMapped>(def: SingletonSyncDef<TOps, TModel, TRaw, TMapped, TModels>): SingletonSync<TMapped>;
324
- defineCollection<TModel extends TModels, TRaw, TMapped extends {
348
+ /** Resolve a string key (looked up via `schema.sources`) or a SourceDef directly to a SourceDef. */
349
+ private _resolveSource;
350
+ defineSingleton<TArg extends SingletonArg<TSchema>, TMapped = SingletonRaw<TSchema, TArg>>(sourceOrKey: TArg, options?: SingletonOptions<SingletonRaw<TSchema, TArg>, TMapped, ModelsOf<TSchema>>): Singleton<TMapped>;
351
+ defineSingletonSync<TArg extends SingletonArg<TSchema>, TMapped = SingletonRaw<TSchema, TArg>>(sourceOrKey: TArg, options?: SingletonSyncOptions<SingletonRaw<TSchema, TArg>, TMapped, ModelsOf<TSchema>>): SingletonSync<TMapped>;
352
+ defineCollection<TArg extends CollectionArg<TSchema>, TMapped extends {
353
+ id: string;
354
+ } = CollectionRaw<TSchema, TArg> & {
355
+ id: string;
356
+ }>(sourceOrKey: TArg, options?: CollectionOptions<CollectionRaw<TSchema, TArg>, TMapped, ModelsOf<TSchema>>): Collection<TMapped>;
357
+ defineCollectionSync<TArg extends CollectionArg<TSchema>, TMapped extends {
358
+ id: string;
359
+ } = CollectionRaw<TSchema, TArg> & {
325
360
  id: string;
326
- }>(def: CollectionDef<TOps, TModel, TRaw, TMapped, TModels>): Collection<TMapped>;
327
- defineCollectionSync<TModel extends TModels, TRaw, TMapped extends {
361
+ }>(sourceOrKey: TArg, options?: CollectionSyncOptions<CollectionRaw<TSchema, TArg>, TMapped, ModelsOf<TSchema>>): CollectionSync<TMapped>;
362
+ defineLazyCollection<TArg extends CollectionArg<TSchema>, TMapped extends {
328
363
  id: string;
329
- }>(def: CollectionSyncDef<TOps, TModel, TRaw, TMapped, TModels>): CollectionSync<TMapped>;
330
- defineLazyCollection<TModel extends TModels, TRaw, TMapped extends {
364
+ } = CollectionRaw<TSchema, TArg> & {
331
365
  id: string;
332
- }>(def: LazyCollectionDef<TOps, TModel, TRaw, TMapped, TModels>): LazyCollection<TMapped>;
366
+ }>(sourceOrKey: TArg, options?: LazyCollectionOptions<CollectionRaw<TSchema, TArg>, TMapped, ModelsOf<TSchema>>): LazyCollection<TMapped>;
333
367
  defineAssets(options?: AssetCollectionOptions | undefined): Assets;
334
368
  defineRoutes(options?: RouteCollectionOptions | undefined): Routes;
335
369
  defineI18nMessages(def: I18nMessageDef): I18nMessages;
336
370
  ping: () => Promise<void>;
337
371
  }
338
- declare function createStore<TOps extends OperationMap | undefined = undefined, TModels extends string = string>(options: CreateStoreOptions<TOps, TModels>): Store<TOps, TModels>;
372
+ declare function createStore<TSchema extends Schema | undefined = undefined>(options: CreateStoreOptions<TSchema>): Store<TSchema>;
339
373
 
340
- export { type AssetCollectionOptions, type Assets, type CacheOptions, type Collection, type CollectionDef, type CollectionSync, type CollectionSyncDef, type CreateStoreOptions, type Fetchable, type I18nInitialTranslation, type I18nMessage, type I18nMessageDef, type I18nMessageRegistrationDefinition, type I18nMessages, type I18nTranslation, type I18nVariableInput, type LazyCacheOptions, type LazyCollection, type LazyCollectionDef, type Loadable, type MapperContext, type RouteCollectionOptions, type Routes, type Singleton, type SingletonDef, type SingletonSync, type SingletonSyncDef, Store, type StoreAsset, type StoreFile, type StoreFileMetadata, type StoreRoute, type StoreRouteCustomHeader, type SyncCacheOptions, type UpdateBatch, type UpdateEvent, type UpdateEventFor, type UpdateMutationType, createStore };
374
+ export { type AssetCollectionOptions, type Assets, type CacheOptions, type Collection, type CollectionOptions, type CollectionSync, type CollectionSyncOptions, type CreateStoreOptions, type ExtractSourceResult, type I18nInitialTranslation, type I18nMessage, type I18nMessageDef, type I18nMessageRegistrationDefinition, type I18nMessages, type I18nTranslation, type I18nVariableInput, type LazyCacheOptions, type LazyCollection, type LazyCollectionOptions, type Loadable, type MapperContext, type ResolveSource, type RouteCollectionOptions, type Routes, type Singleton, type SingletonOptions, type SingletonSync, type SingletonSyncOptions, type SourceAt, type SourceKeysOf, Store, type StoreAsset, type StoreFile, type StoreFileMetadata, type StoreRoute, type StoreRouteCustomHeader, type SyncCacheOptions, type UpdateBatch, type UpdateEvent, type UpdateEventFor, type UpdateMutationType, createStore };