ngx-signal-query 1.8.0-dev.1 → 1.8.0-dev.3

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/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { Observable } from 'rxjs';
2
2
  import * as i0 from '@angular/core';
3
- import { Signal, Provider, EnvironmentProviders, Injector } from '@angular/core';
3
+ import { Signal, PendingTasks, Provider, EnvironmentProviders, Injector } from '@angular/core';
4
4
 
5
5
  /** @internal */
6
6
  declare abstract class Cache<TEntry> {
@@ -112,7 +112,9 @@ type QueryOptions<TData, TError = Error> = {
112
112
  * for no data: `undefined` fails the fetch, with no retry. Once no
113
113
  * component shows the query, an `Observable` fetch in flight is
114
114
  * unsubscribed, which aborts an `HttpClient` request; a `Promise` one runs
115
- * to the end, and its response is kept.
115
+ * to the end, and its response is kept. Either way, Angular counts the app
116
+ * as busy until the fetch is done: a server render and `whenStable()` wait
117
+ * for it.
116
118
  */
117
119
  queryFn: () => Observable<TData> | Promise<TData>;
118
120
  /** How long fetched data stays fresh before refetching, in ms. Default `0`. */
@@ -218,9 +220,10 @@ type QueryResult<TData, TError = Error> = {
218
220
  /**
219
221
  * When the data was last updated, as a timestamp in ms: when the last
220
222
  * response landed or `setQueryData` wrote it; for `initialData`, its
221
- * `initialDataUpdatedAt`, or when it was seeded. A refetch that brings the
222
- * same data moves it too; one that fails or is cancelled doesn't. `0` while
223
- * there is no data, placeholder data included.
223
+ * `initialDataUpdatedAt`, or when it was seeded; for data a server render
224
+ * brought over (see {@link withHydration}), when the server fetched it. A
225
+ * refetch that brings the same data moves it too; one that fails or is
226
+ * cancelled doesn't. `0` while there is no data, placeholder data included.
224
227
  */
225
228
  dataUpdatedAt: Signal<number>;
226
229
  /** Current {@link QueryStatus}. */
@@ -340,7 +343,8 @@ interface QuerySubscriber<TData, TError = Error> {
340
343
  }
341
344
  /**
342
345
  * What a query is created with: its key and the key's hash, the cache it
343
- * lives in, and how long it stays there once nothing uses it.
346
+ * lives in, how long it stays there once nothing uses it, and the app's
347
+ * pending tasks, one of which each of its fetches holds while in flight.
344
348
  *
345
349
  * @internal
346
350
  */
@@ -349,6 +353,7 @@ interface QueryConfig {
349
353
  queryHash: string;
350
354
  cache: QueryCache;
351
355
  gcTime: number;
356
+ pendingTasks: Pick<PendingTasks, 'add'>;
352
357
  }
353
358
  /** @internal */
354
359
  declare class Query<TData, TError = Error> {
@@ -356,7 +361,7 @@ declare class Query<TData, TError = Error> {
356
361
  readonly key: QueryKey;
357
362
  readonly queryHash: string;
358
363
  readonly state: i0.Signal<QueryState<TData, TError>>;
359
- constructor({ key, queryHash, cache, gcTime }: QueryConfig);
364
+ constructor({ key, queryHash, cache, gcTime, pendingTasks }: QueryConfig);
360
365
  get observerCount(): number;
361
366
  updateGcTime(ms: number): void;
362
367
  addObserver(observer: QuerySubscriber<TData, TError>): void;
@@ -380,7 +385,9 @@ declare class Query<TData, TError = Error> {
380
385
 
381
386
  /** @internal */
382
387
  declare class QueryCache extends Cache<Query<unknown, unknown>> {
383
- getOrCreate<TData, TError = Error>(key: QueryKey, gcTime: number): Query<TData, TError>;
388
+ #private;
389
+ constructor();
390
+ getOrCreate<TData, TError = Error>(key: QueryKey, gcTime: number, queryHash?: string): Query<TData, TError>;
384
391
  get<TData, TError = Error>(key: QueryKey): Query<TData, TError> | undefined;
385
392
  /**
386
393
  * Like get(), but reactive, as findAll() is: a computed or an effect that
@@ -499,7 +506,7 @@ declare class Mutation<TData = unknown, TError = Error, TVariables = void, TCont
499
506
  #private;
500
507
  readonly mutationId: number;
501
508
  readonly state: Signal<MutationState<TData, TError, TVariables, TContext>>;
502
- constructor(mutationId: number, options: MutationOptions<TData, TError, TVariables, TContext>, cache: MutationCache);
509
+ constructor(mutationId: number, options: MutationOptions<TData, TError, TVariables, TContext>, cache: MutationCache, pendingTasks: Pick<PendingTasks, 'add'>);
503
510
  get observerCount(): number;
504
511
  addObserver(): void;
505
512
  /**
@@ -583,7 +590,9 @@ declare class QueryClient {
583
590
  * if the fetch fails. A cancelled fetch resolves with the data the cache
584
591
  * holds, or rejects with a {@link CancelledError} if it holds none; a
585
592
  * removed query always rejects with one. Nothing has to wait for the
586
- * promise: a prefetch that fails only leaves the query in `'error'`.
593
+ * promise: a prefetch that fails only leaves the query in `'error'`. A
594
+ * server render and `whenStable()` wait for the fetch all the same, as for
595
+ * a component's query, so its data is in the cache by then.
587
596
  *
588
597
  * Unlike a component's query in the browser, it doesn't retry a failure
589
598
  * unless `retry` is set here or in {@link withDefaultOptions}, so whoever
@@ -718,7 +727,8 @@ declare class QueryClient {
718
727
  * `refetch()`. A mutation in flight isn't stopped: its write
719
728
  * has most likely reached the server, so it finishes and runs its hooks, as
720
729
  * when its component goes away, and a component showing it sees how it
721
- * went. A hook that writes into the cache puts that key back.
730
+ * went. The app counts as busy until it's done. A hook that writes into the
731
+ * cache puts that key back.
722
732
  *
723
733
  * @example
724
734
  * ```ts
@@ -753,7 +763,8 @@ declare class QueryClient {
753
763
  * @internal
754
764
  */
755
765
  declare enum QueryClientFeatureKind {
756
- DefaultOptions = 0
766
+ DefaultOptions = 0,
767
+ Hydration = 1
757
768
  }
758
769
  /**
759
770
  * An opaque feature passed to {@link provideQueryClient}. Create one with a
@@ -769,11 +780,15 @@ interface QueryClientFeature<K extends QueryClientFeatureKind = QueryClientFeatu
769
780
  * Registers the {@link QueryClient} and its caches for the application.
770
781
  *
771
782
  * Call once at the app root (in `ApplicationConfig.providers` or a root
772
- * `bootstrapApplication`). Pass features such as {@link withDefaultOptions} to
773
- * configure defaults. Registering the same feature twice throws.
783
+ * `bootstrapApplication`). A client provided by another environment injector
784
+ * goes with it: once that injector is destroyed, its queries stop, and none
785
+ * of their fetches keeps the app busy. Pass features such as
786
+ * {@link withDefaultOptions} to configure defaults, or {@link withHydration}
787
+ * to bring the data of a server render over to the browser. Registering the
788
+ * same feature twice throws.
774
789
  *
775
790
  * @param features - Optional {@link QueryClientFeature}s (e.g.
776
- * {@link withDefaultOptions}).
791
+ * {@link withDefaultOptions}, {@link withHydration}).
777
792
  * @returns Environment providers to add to the app's provider list.
778
793
  *
779
794
  * @example
@@ -936,7 +951,9 @@ declare function injectQueries<TData, TError = Error>(optionsFn: () => InjectQue
936
951
  * context. Destroying it doesn't cancel a run in flight: the write has most
937
952
  * likely reached the server, so the run finishes and fires its hooks. A
938
953
  * `mutate()` that comes after that, say from a timer, runs and fires its
939
- * hooks too.
954
+ * hooks too. From `mutate()` until its last hook is done, a run keeps the app
955
+ * busy, whether its component is still there or not: a server render and
956
+ * `whenStable()` wait for it.
940
957
  *
941
958
  * Must run in an injection context, or be given an explicit `injector`.
942
959
  *
@@ -1125,5 +1142,42 @@ declare class CancelledError extends Error {
1125
1142
  */
1126
1143
  declare function withDefaultOptions(options: DefaultOptions): QueryClientFeature<QueryClientFeatureKind.DefaultOptions>;
1127
1144
 
1128
- export { CancelledError, QueryClient, QueryClientFeatureKind, injectIsFetching, injectIsMutating, injectMutation, injectQueries, injectQuery, injectQueryClient, keepPreviousData, mutationOptions, provideQueryClient, queryOptions, withDefaultOptions };
1145
+ /**
1146
+ * Feature for {@link provideQueryClient} that brings the data a server render
1147
+ * fetched over to the browser, so the page shows it at once rather than a
1148
+ * loading state. Add it to the providers the server and the browser share. It
1149
+ * works with Angular's `provideClientHydration()` or without it.
1150
+ *
1151
+ * Only queries that succeeded go over, and only data JSON carries as is:
1152
+ * strings, numbers, booleans, `null`, plain objects and arrays. A query whose
1153
+ * data holds a `Date`, a `Map` or a class instance stays on the server, and
1154
+ * the browser fetches it again; in development the server logs a warning
1155
+ * naming it.
1156
+ *
1157
+ * In the browser the data counts as fetched when the server fetched it, and
1158
+ * `staleTime` decides as for any data in the cache: with the default of `0`,
1159
+ * a component refetches its query in the background as it appears, showing
1160
+ * the server's data meanwhile. Set a `staleTime` to skip that request.
1161
+ *
1162
+ * Give it to one client per app: clients provided by other environment
1163
+ * injectors would share one place in the page, and only one of them would get
1164
+ * its data over.
1165
+ *
1166
+ * @returns A {@link QueryClientFeature} to pass to {@link provideQueryClient}.
1167
+ *
1168
+ * @example
1169
+ * ```ts
1170
+ * export const appConfig: ApplicationConfig = {
1171
+ * providers: [
1172
+ * provideQueryClient(
1173
+ * withDefaultOptions({ queries: { staleTime: 30_000 } }),
1174
+ * withHydration(),
1175
+ * ),
1176
+ * ],
1177
+ * }
1178
+ * ```
1179
+ */
1180
+ declare function withHydration(): QueryClientFeature<QueryClientFeatureKind.Hydration>;
1181
+
1182
+ export { CancelledError, QueryClient, QueryClientFeatureKind, injectIsFetching, injectIsMutating, injectMutation, injectQueries, injectQuery, injectQueryClient, keepPreviousData, mutationOptions, provideQueryClient, queryOptions, withDefaultOptions, withHydration };
1129
1183
  export type { DefaultMutationOptions, DefaultOptions, DefaultQueryOptions, DefaultedQueryOptions, InjectQueriesOptions, InvalidateQueryFilters, MutationFilters, MutationOptions, MutationResult, MutationState, MutationStatus, PlaceholderDataFunction, QueryClientFeature, QueryFilters, QueryKey, QueryOptions, QueryResult, QueryState, QueryStatus, RefetchIntervalValue, RetryDelayValue, RetryValue, StructuralSharingValue, Updater };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ngx-signal-query",
3
- "version": "1.8.0-dev.1",
3
+ "version": "1.8.0-dev.3",
4
4
  "description": "Data fetching, caching and mutations for Angular, built on signals and RxJS. Observables end to end, TanStack Query concepts.",
5
5
  "keywords": [
6
6
  "angular",