@raf-query/core 0.11.0 → 0.12.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.
Files changed (71) hide show
  1. package/CHANGELOG.md +18 -2
  2. package/README.md +28 -18
  3. package/dist/client.d.mts +132 -119
  4. package/dist/client.mjs +59 -162
  5. package/dist/entry.d.mts +67 -148
  6. package/dist/entry.mjs +303 -474
  7. package/dist/features/focus-refetch/focus-refetch.d.mts +15 -0
  8. package/dist/features/focus-refetch/focus-refetch.mjs +18 -0
  9. package/dist/features/polling/polling.d.mts +12 -0
  10. package/dist/features/polling/polling.mjs +35 -0
  11. package/dist/features/reconnect-refetch/reconnect-refetch.d.mts +15 -0
  12. package/dist/features/reconnect-refetch/reconnect-refetch.mjs +14 -0
  13. package/dist/features/shared.mjs +13 -0
  14. package/dist/guards.mjs +23 -0
  15. package/dist/hydration/hydration.d.mts +73 -0
  16. package/dist/hydration/hydration.mjs +128 -0
  17. package/dist/index.d.mts +18 -13
  18. package/dist/index.mjs +15 -13
  19. package/dist/infinite/infinite.d.mts +71 -0
  20. package/dist/infinite/infinite.mjs +88 -0
  21. package/dist/internal.d.mts +13 -0
  22. package/dist/internal.mjs +7 -0
  23. package/dist/keys/keys.d.mts +46 -0
  24. package/dist/keys/keys.mjs +53 -0
  25. package/dist/keys/targets.d.mts +56 -0
  26. package/dist/keys/targets.mjs +85 -0
  27. package/dist/links/retry/retry.d.mts +26 -0
  28. package/dist/links/retry/retry.mjs +51 -0
  29. package/dist/links/timeout/timeout.d.mts +9 -0
  30. package/dist/links/timeout/timeout.mjs +27 -0
  31. package/dist/load.d.mts +124 -0
  32. package/dist/load.mjs +165 -0
  33. package/dist/options.d.mts +112 -338
  34. package/dist/options.mjs +25 -36
  35. package/dist/presets.d.mts +29 -0
  36. package/dist/presets.mjs +25 -0
  37. package/dist/root.d.mts +82 -0
  38. package/dist/structural-sharing/structural-sharing.mjs +32 -0
  39. package/dist/subscribe.d.mts +15 -0
  40. package/dist/subscribe.mjs +29 -0
  41. package/dist/thenable.d.mts +13 -17
  42. package/dist/thenable.mjs +25 -6
  43. package/dist/verbs.d.mts +109 -0
  44. package/dist/verbs.mjs +87 -0
  45. package/package.json +13 -9
  46. package/dist/_virtual/_rolldown/runtime.mjs +0 -13
  47. package/dist/all-keyed.d.mts +0 -18
  48. package/dist/all-keyed.mjs +0 -19
  49. package/dist/binding.d.mts +0 -92
  50. package/dist/binding.mjs +0 -186
  51. package/dist/cache.d.mts +0 -164
  52. package/dist/cache.mjs +0 -184
  53. package/dist/controls.d.mts +0 -93
  54. package/dist/controls.mjs +0 -69
  55. package/dist/hydration.d.mts +0 -84
  56. package/dist/hydration.mjs +0 -148
  57. package/dist/infinite.d.mts +0 -5
  58. package/dist/infinite.mjs +0 -103
  59. package/dist/key.d.mts +0 -18
  60. package/dist/key.mjs +0 -36
  61. package/dist/listen.mjs +0 -19
  62. package/dist/notify.mjs +0 -21
  63. package/dist/observer.d.mts +0 -110
  64. package/dist/observer.mjs +0 -300
  65. package/dist/platform.d.mts +0 -27
  66. package/dist/platform.mjs +0 -44
  67. package/dist/query.d.mts +0 -137
  68. package/dist/query.mjs +0 -147
  69. package/dist/request.mjs +0 -47
  70. package/dist/retry.mjs +0 -25
  71. package/dist/share.mjs +0 -40
package/dist/entry.d.mts CHANGED
@@ -1,159 +1,78 @@
1
- import { QueryKey } from "./key.mjs";
2
- import { AnyQueryOptions, QueryBehavior, QueryFactory, QueryHandle, QueryMeta } from "./options.mjs";
3
- import { Platform, PlatformEvent } from "./platform.mjs";
1
+ import { AnyFactory, QueryKey } from "./keys/keys.mjs";
2
+ import { QueryOptions } from "./options.mjs";
3
+ import { Listeners, SubscribeOptions } from "./subscribe.mjs";
4
4
  import { Pending, Thenable } from "./thenable.mjs";
5
- import { FetchRequest, Query, State, SubscribeOptions } from "./query.mjs";
6
- import { Event } from "./cache.mjs";
7
- import { Cause, Clock, Context, Deferred, Effect, Scope, Stream } from "effect";
8
- declare namespace entry_d_exports {
9
- export { CacheState, CancelledError, DisabledError, Entry, TypeId, changes, emit, entryOf, fetch, hasNextPageUnsafe, hasPreviousPageUnsafe, invalidate, isDisabled, isObserved, isQuery, isStaleUnsafe, make, metaOf, needsRefetch, onPlatformEvent, provide, readUnsafe, remove, reset, resume, revalidate, setData, setOptions, start, subscribeUnsafe };
5
+ import { QueryClient } from "./client.mjs";
6
+ //#region src/entry.d.ts
7
+ /** An entry's state. A new object on every change, so it works as a snapshot. */
8
+ export interface EntryState {
9
+ readonly data: unknown;
10
+ /** The last request's error, while it's the latest outcome. */
11
+ readonly error: unknown;
12
+ readonly fetchingDirection: "next" | "previous" | undefined;
13
+ readonly hasData: boolean;
14
+ readonly isFetching: boolean;
15
+ readonly status: "error" | "pending" | "success";
16
+ /** When the data was written (ms since epoch); `0` without data. */
17
+ readonly updatedAt: number;
10
18
  }
11
- export declare const TypeId = "~@raf-query/core/Query";
12
- declare const CancelledError_base: new <A extends Record<string, any> = {}>(args: import("effect/Types").VoidIfEmpty<{ readonly [P in keyof A as P extends "_tag" ? never : P]: A[P]; }>) => Cause.YieldableError & {
13
- readonly _tag: "CancelledError";
14
- } & Readonly<A>;
15
- /**
16
- * The failure of a request that was interrupted rather than settled: its
17
- * entry was removed or cleared, or an Effect `queryFn` was interrupted.
18
- */
19
- export declare class CancelledError extends CancelledError_base {
20
- readonly message = "The query was cancelled";
19
+ /** How a request ended, for whoever waited on it. */
20
+ export type Outcome = {
21
+ readonly error: unknown;
22
+ readonly ok: false;
23
+ } | {
24
+ readonly ok: true;
25
+ };
26
+ interface Run {
27
+ readonly controller: AbortController;
28
+ readonly done: PromiseWithResolvers<Outcome>;
29
+ /** Resolve `done` at the first chunk of a stream, not its end. */
30
+ settled: boolean;
31
+ readonly startedAt: number;
21
32
  }
22
- declare const DisabledError_base: new <A extends Record<string, any> = {}>(args: import("effect/Types").VoidIfEmpty<{ readonly [P in keyof A as P extends "_tag" ? never : P]: A[P]; }>) => Cause.YieldableError & {
23
- readonly _tag: "DisabledError";
24
- } & Readonly<A>;
25
- /**
26
- * Why a query with `enabled: false` has no data: `ensure` / `fetch` die
27
- * with it, and on a server its promise rejects with it.
28
- */
29
- export declare class DisabledError extends DisabledError_base<{
33
+ export interface Entry {
34
+ readonly client: QueryClient;
35
+ factory: AnyFactory | undefined;
36
+ gcTimer: unknown;
30
37
  readonly hash: string;
31
- }> {
32
- get message(): string;
38
+ input: unknown;
39
+ readonly key: QueryKey;
40
+ options: QueryOptions | undefined;
41
+ pendingThenable: Pending<unknown> | undefined;
42
+ readonly readers: Listeners<EntryState>;
43
+ /** `~seq` when the request behind the data started; invalidations after it make it stale. */
44
+ requestedAt: number;
45
+ run: Run | undefined;
46
+ state: EntryState;
47
+ thenable: Thenable<unknown>;
33
48
  }
49
+ export declare function abortError(): DOMException;
34
50
  /**
35
- * The state a cache shares with its entries. Entries are created
36
- * synchronously (a render reads one), so they fork their fibers with the
37
- * services and into the scope the cache was made with.
51
+ * The entry for a factory's input, created if missing, with the factory's
52
+ * current options. Every read goes through here, which links an entry that
53
+ * was only hydrated to its factory.
38
54
  */
39
- export interface CacheState {
40
- readonly clock: Clock.Clock;
41
- readonly defaults: QueryBehavior;
42
- readonly entries: Map<string, Entry>;
43
- readonly isServer: boolean;
44
- readonly listeners: Set<(event: Event) => void>;
45
- readonly platform: Platform;
46
- readonly scope: Scope.Scope;
47
- readonly services: Context.Context<never>;
48
- }
49
- /** The background work of an entry, one fiber per kind. */
50
- type Task = "errorShown" | "gc" | "request" | "stale";
51
- interface Tasks {
52
- cancel(task: Task): void;
53
- fork(task: Task, effect: Effect.Effect<void>, options?: {
54
- readonly onlyIfMissing?: boolean;
55
- }): void;
56
- }
57
- interface Commands {
58
- readonly fetchNextPage: Effect.Effect<void>;
59
- readonly fetchPreviousPage: Effect.Effect<void>;
60
- readonly refetch: Effect.Effect<void>;
61
- }
62
- /** A cache entry: a {@link Query} and the state behind it. */
63
- export interface Entry extends Query {
64
- readonly cache: CacheState;
65
- /** One Effect per command, so readers can compare them by identity. */
66
- readonly commands: Commands;
67
- /** A failed first load was shown; the next read retries it. */
68
- errorShown: boolean;
69
- /** `failureCount` before the request in flight, restored if it's cancelled. */
70
- failureCountBefore: number;
71
- handle: QueryHandle;
72
- /** `meta` sent with dehydrated state, for an entry no factory call has reached. */
73
- hydratedMeta: QueryMeta | undefined;
74
- /** Awaited by every caller of the request in flight; outlives restarts. */
75
- inFlight: Deferred.Deferred<unknown, unknown> | undefined;
76
- readonly listeners: Map<() => void, SubscribeOptions>;
77
- options: AnyQueryOptions | undefined;
78
- /** The first load's promise, until it settles. */
79
- pending: Pending<unknown> | undefined;
80
- /** The last reader left during the request in flight: it retries no more. */
81
- retriesStopped: boolean;
82
- /**
83
- * The last `queryFn` call returned an Effect (`true`) or a promise or
84
- * plain value (`false`), here or on the cache it was hydrated from.
85
- * `undefined` until one has run.
86
- */
87
- returnsEffect: boolean | undefined;
88
- /** Open while the entry is cached; owns its fibers. */
89
- readonly scope: Scope.Closeable;
90
- /**
91
- * What its requests run with: the cache's services, under those of the
92
- * latest verb that called for this entry ({@link provide}).
93
- */
94
- services: Context.Context<never>;
95
- /**
96
- * The request in flight read its `signal`, or returned an Effect, so
97
- * interrupting it cancels its work (TanStack's `abortSignalConsumed`).
98
- */
99
- signalRead: boolean;
100
- state: State<unknown>;
101
- readonly tasks: Tasks;
55
+ export declare function entryOf(client: QueryClient, factory: AnyFactory, input: unknown): Entry;
56
+ /** Whether an invalidation newer than the entry's data covers it. */
57
+ export declare function isInvalidated(entry: Entry): boolean;
58
+ /** Whether the entry's data should be refetched before it's trusted. */
59
+ export declare function isStale(entry: Entry): boolean;
60
+ export interface FetchRequest {
61
+ readonly direction?: "next" | "previous";
62
+ readonly pages?: number;
63
+ /** Abort a request in flight and start over (an invalidation). */
64
+ readonly restart?: boolean;
102
65
  }
103
- export declare function isQuery(value: unknown): value is Query;
104
- export declare function entryOf<TFactory extends QueryFactory>(query: Query<TFactory>): Entry;
105
- export declare function emit(cache: CacheState, event: Event): void;
106
- export declare function make(cache: CacheState, key: QueryKey, hash: string): Entry;
107
- /** Readers are subscribed; `invalidate` and focus refetch only what is read. */
108
- export declare function isObserved(entry: Entry): boolean;
109
- /** The entry's latest factory call returned `enabled: false`: it doesn't run. */
110
- export declare function isDisabled(entry: Entry): boolean;
111
- /** Until a factory call attaches options, `meta` comes from hydration. */
112
- export declare function metaOf(entry: Entry): QueryMeta | undefined;
113
- export declare function isStaleUnsafe(entry: Entry): boolean;
114
- export declare function hasNextPageUnsafe(entry: Entry): boolean;
115
- export declare function hasPreviousPageUnsafe(entry: Entry): boolean;
116
- export declare function subscribeUnsafe(entry: Entry, listener: () => void, options?: SubscribeOptions): () => void;
117
- export declare function changes(entry: Entry): Stream.Stream<State<unknown>>;
118
66
  /**
119
- * Run this entry's requests with the services of `caller`, a verb's fiber
120
- * context, over the cache's: every request from now on, including ones a
121
- * render, a refetch or `invalidate` starts.
67
+ * Run the entry's request, or join the one in flight. Resolves with how it
68
+ * ended, never rejects: a removed entry's request ends with an `AbortError`.
69
+ * Whoever waited on a restarted request gets the new one's outcome.
122
70
  */
123
- export declare function provide(entry: Entry, caller: Context.Context<never>): void;
124
- /** Attach the options and input of the latest factory call. */
125
- export declare function setOptions(entry: Entry, options: AnyQueryOptions, input: unknown): void;
126
- /**
127
- * The promise a render should unwrap. Starts the first request if nothing
128
- * has, and retries a failed first load once its error has been shown.
129
- */
130
- export declare function readUnsafe(entry: Entry): Thenable<unknown>;
131
- /** Start a request unless one is in flight, without waiting for it. */
132
- export declare function start(entry: Entry): void;
133
- /**
134
- * Whether cached data that was asked for again should be refetched. A server
135
- * cache renders once, so a refetch there could only duplicate a request whose
136
- * result is never shown; it refetches only what `invalidate` marked.
137
- */
138
- export declare function needsRefetch(entry: Entry): boolean;
139
- /** Background refetch of cached data that was asked for again. */
140
- export declare function revalidate(entry: Entry): void;
141
- export declare function fetch(entry: Entry, request?: FetchRequest): Effect.Effect<unknown, unknown>;
142
- export declare function invalidate(entry: Entry): Effect.Effect<void>;
143
- export declare function reset(entry: Entry): Effect.Effect<void>;
144
- /** Write data directly, as if a request had returned it. */
145
- export declare function setData(entry: Entry, data: unknown, updatedAt?: number): void;
146
- /**
147
- * Adopt a request started elsewhere (the server's, during hydration). Its
148
- * data counts as fetched when the sender got it, not when it arrived.
149
- */
150
- export declare function resume(entry: Entry, promise: PromiseLike<{
151
- readonly data?: unknown;
152
- readonly updatedAt: number;
153
- }>): void;
154
- /** Refetch a read, stale entry whose readers want it on this event. */
155
- export declare function onPlatformEvent(entry: Entry, event: PlatformEvent): void;
156
- /** Take `entry` out of its cache: interrupt its fibers and fail its awaiters. */
71
+ export declare function fetchEntry(entry: Entry, request?: FetchRequest): Promise<Outcome>;
72
+ /** Subscribe to an entry's state. Subscribers are its readers: an entry with none can be gc'd. */
73
+ export declare function subscribeEntry(entry: Entry, listener: (state: EntryState) => void, options?: SubscribeOptions): () => void;
74
+ /** Drop the entry, aborting its request. Waiters get an `AbortError`. */
157
75
  export declare function remove(entry: Entry): void;
158
- //#endregion
159
- export { entry_d_exports };
76
+ /** Drop data and errors. Read entries (or ones loading) refetch; resolves when they settle. */
77
+ export declare function reset(entry: Entry): Promise<Outcome>;
78
+ //#endregion