-
Notifications
You must be signed in to change notification settings - Fork 27.2k
Expand file tree
/
Copy pathapi.ts
More file actions
321 lines (283 loc) Β· 9.11 KB
/
api.ts
File metadata and controls
321 lines (283 loc) Β· 9.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
import {Injector} from '../di/injector';
import {Signal, ValueEqualityFn} from '../render3/reactivity/api';
import {WritableSignal} from '../render3/reactivity/signal';
/** Error thrown when a `Resource` dependency of another resource errors. */
export class ResourceDependencyError extends Error {
/** The dependency that errored. */
readonly dependency: Resource<unknown>;
constructor(dependency: Resource<unknown>) {
super('Dependency error', {cause: dependency.error()});
this.name = 'ResourceDependencyError';
this.dependency = dependency;
}
}
/**
* Special status codes that can be thrown from a resource's `params` or `request` function to
* indicate that the resource should transition to that status.
*/
export class ResourceParamsStatus extends Error {
private readonly _brand: undefined;
private constructor(msg: string) {
super(msg);
}
/** Status code that transitions the resource to `idle` status. */
static readonly IDLE = new ResourceParamsStatus('IDLE');
/** Status code that transitions the resource to `loading` status. */
static readonly LOADING = new ResourceParamsStatus('LOADING');
}
/** Context received by a resource's `params` or `request` function. */
export interface ResourceParamsContext {
/**
* Chains the current params off of the value of another resource, returning the value
* of the other resource if it is available, or propagating the status to the current resource by
* throwing the appropriate status code if the value is not available.
*/
readonly chain: <T>(resource: Resource<T>) => T;
}
/**
* String value capturing the status of a `Resource`.
*
* Possible statuses are:
*
* `idle` - The resource has no valid request and will not perform any loading. `value()` will be
* `undefined`.
*
* `loading` - The resource is currently loading a new value as a result of a change in its reactive
* dependencies. `value()` will be `undefined`.
*
* `reloading` - The resource is currently reloading a fresh value for the same reactive
* dependencies. `value()` will continue to return the previously fetched value during the reloading
* operation.
*
* `error` - Loading failed with an error. `value()` will be `undefined`.
*
* `resolved` - Loading has completed and the resource has the value returned from the loader.
*
* `local` - The resource's value was set locally via `.set()` or `.update()`.
*
* @experimental
*/
export type ResourceStatus = 'idle' | 'error' | 'loading' | 'reloading' | 'resolved' | 'local';
/**
* A Resource is an asynchronous dependency (for example, the results of an API call) that is
* managed and delivered through signals.
*
* The usual way of creating a `Resource` is through the `resource` function, but various other APIs
* may present `Resource` instances to describe their own concepts.
*
* @experimental
*/
export interface Resource<T> {
/**
* The current value of the `Resource`, or throws an error if the resource is in an error state.
*/
readonly value: Signal<T>;
/**
* The current status of the `Resource`, which describes what the resource is currently doing and
* what can be expected of its `value`.
*/
readonly status: Signal<ResourceStatus>;
/**
* When in the `error` state, this returns the last known error from the `Resource`.
*/
readonly error: Signal<Error | undefined>;
/**
* Whether this resource is loading a new value (or reloading the existing one).
*/
readonly isLoading: Signal<boolean>;
/**
* The current state of this resource, represented as a `ResourceSnapshot`.
*/
readonly snapshot: Signal<ResourceSnapshot<T>>;
/**
* Whether this resource has a valid current value.
*
* This function is reactive.
*/
hasValue(this: T extends undefined ? this : never): this is Resource<Exclude<T, undefined>>;
hasValue(): boolean;
}
/**
* A `Resource` with a mutable value.
*
* Overwriting the value of a resource sets it to the 'local' state.
*
* @experimental
*/
export interface WritableResource<T> extends Resource<T> {
readonly value: WritableSignal<T>;
hasValue(
this: T extends undefined ? this : never,
): this is WritableResource<Exclude<T, undefined>>;
hasValue(): boolean;
/**
* Convenience wrapper for `value.set`.
*/
set(value: T): void;
/**
* Convenience wrapper for `value.update`.
*/
update(updater: (value: T) => T): void;
asReadonly(): Resource<T>;
/**
* Instructs the resource to re-load any asynchronous dependency it may have.
*
* Note that the resource will not enter its reloading state until the actual backend request is
* made.
*
* @returns true if a reload was initiated, false if a reload was unnecessary or unsupported
*/
reload(): boolean;
}
/**
* A `WritableResource` created through the `resource` function.
*
* @experimental
*/
export interface ResourceRef<T> extends WritableResource<T> {
hasValue(this: T extends undefined ? this : never): this is ResourceRef<Exclude<T, undefined>>;
hasValue(): boolean;
/**
* Manually destroy the resource, which cancels pending requests and returns it to `idle` state.
*/
destroy(): void;
}
/**
* Parameter to a `ResourceLoader` which gives the request and other options for the current loading
* operation.
*
* @experimental
*/
export interface ResourceLoaderParams<R> {
params: NoInfer<Exclude<R, undefined>>;
abortSignal: AbortSignal;
previous: {
status: ResourceStatus;
};
}
/**
* Loading function for a `Resource`.
*
* @experimental
*/
export type ResourceLoader<T, R> = (param: ResourceLoaderParams<R>) => PromiseLike<T>;
/**
* Streaming loader for a `Resource`.
*
* @experimental
*/
export type ResourceStreamingLoader<T, R> = (
param: ResourceLoaderParams<R>,
) => Signal<ResourceStreamItem<T>> | PromiseLike<Signal<ResourceStreamItem<T>>> | undefined;
/**
* Options to the `resource` function, for creating a resource.
*
* @experimental
*/
export interface BaseResourceOptions<T, R> {
/**
* A reactive function which determines the request to be made. Whenever the request changes, the
* loader will be triggered to fetch a new value for the resource.
*
* If a params function isn't provided, the loader won't rerun unless the resource is reloaded.
*/
params?: (ctx: ResourceParamsContext) => R;
/**
* The value which will be returned from the resource when a server value is unavailable, such as
* when the resource is still loading.
*/
defaultValue?: NoInfer<T>;
/**
* Equality function used to compare the return value of the loader.
*/
equal?: ValueEqualityFn<T>;
/**
* Overrides the `Injector` used by `resource`.
*/
injector?: Injector;
}
/**
* Options to the `resource` function, for creating a resource.
*
* @experimental
*/
export interface PromiseResourceOptions<T, R> extends BaseResourceOptions<T, R> {
/**
* Loading function which returns a `Promise` of the resource's value for a given request.
*/
loader: ResourceLoader<T, R>;
/**
* Cannot specify `stream` and `loader` at the same time.
*/
stream?: never;
}
/**
* Options to the `resource` function, for creating a resource.
*
* @experimental
*/
export interface StreamingResourceOptions<T, R> extends BaseResourceOptions<T, R> {
/**
* Loading function which returns a `Promise` of a signal of the resource's value for a given
* request, which can change over time as new values are received from a stream.
*/
stream: ResourceStreamingLoader<T, R>;
/**
* Cannot specify `stream` and `loader` at the same time.
*/
loader?: never;
}
/**
* @experimental
*/
export type ResourceOptions<T, R> = (
| PromiseResourceOptions<T, R>
| StreamingResourceOptions<T, R>
) & {
/**
* A debug name for the reactive node. Used in Angular DevTools to identify the node.
*/
debugName?: string;
};
/**
* @experimental
*/
export type ResourceStreamItem<T> = {value: T} | {error: Error};
/**
* An explicit representation of a resource's state.
*
* @experimental
* @see [Resource composition with snapshots](guide/signals/resource#resource-composition-with-snapshots)
*/
export type ResourceSnapshot<T> =
| {readonly status: 'idle'; readonly value: T}
| {readonly status: 'loading' | 'reloading'; readonly value: T}
| {readonly status: 'resolved' | 'local'; readonly value: T}
| {readonly status: 'error'; readonly error: Error};
/**
* Options for `debounced`.
*
* @see [Debouncing signals with `debounced`](guide/signals/debounced)
*/
export interface DebouncedOptions<T> {
/** The `Injector` to use for the debounced resource. */
injector?: Injector;
/** The equality function to use for comparing values. */
equal?: ValueEqualityFn<T>;
}
/**
* Represents the wait condition for item debouncing.
* Can be a number of milliseconds or a function that returns a Promise.
*
* @see [Debouncing signals with `debounced`](guide/signals/debounced)
*/
export type DebounceTimer<T> =
| number
| ((value: T, lastValue: ResourceSnapshot<T>) => Promise<void> | void);