Skip to content

Commit d0c9a64

Browse files
alxhubmmalerba
authored andcommitted
refactor(core): rename resource's request to params (angular#60919)
As decided in the resource RFC, this commit renames the `request` option of a resource to `params`, including the subsequent argument passed to the loader. It also corrects the type in the process to properly allow narrowing of the `undefined` value. Fixes angular#58871 PR Close angular#60919
1 parent d8ca560 commit d0c9a64

File tree

8 files changed

+83
-77
lines changed

8 files changed

+83
-77
lines changed

adev/shared-docs/services/search.service.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ export class Search {
5555
private readonly client = inject(ALGOLIA_CLIENT);
5656

5757
searchResults = resource({
58-
request: () => this.searchQuery() || undefined, // coerces empty string to undefined
59-
loader: async ({request: query, abortSignal}) => {
58+
params: () => this.searchQuery() || undefined, // coerces empty string to undefined
59+
loader: async ({params: query, abortSignal}) => {
6060
// Until we have a better alternative we debounce by awaiting for a short delay.
6161
await wait(SEARCH_DELAY, abortSignal);
6262

adev/src/content/guide/signals/resource.md

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,52 +14,52 @@ import {resource, Signal} from '@angular/core';
1414
const userId: Signal<string> = getUserId();
1515

1616
const userResource = resource({
17-
// Define a reactive request computation.
18-
// The request value recomputes whenever any read signals change.
19-
request: () => ({id: userId()}),
17+
// Define a reactive computation.
18+
// The params value recomputes whenever any read signals change.
19+
params: () => ({id: userId()}),
2020

2121
// Define an async loader that retrieves data.
22-
// The resource calls this function every time the `request` value changes.
23-
loader: ({request}) => fetchUser(request),
22+
// The resource calls this function every time the `params` value changes.
23+
loader: ({params}) => fetchUser(params),
2424
});
2525

2626
// Create a computed signal based on the result of the resource's loader function.
2727
const firstName = computed(() => userResource.value().firstName);
2828
```
2929

30-
The `resource` function accepts a `ResourceOptions` object with two main properties: `request` and `loader`.
30+
The `resource` function accepts a `ResourceOptions` object with two main properties: `params` and `loader`.
3131

32-
The `request` property defines a reactive computation that produce a request value. Whenever signals read in this computation change, the resource produces a new request value, similar to `computed`.
32+
The `params` property defines a reactive computation that produces a parameter value. Whenever signals read in this computation change, the resource produces a new parameter value, similar to `computed`.
3333

34-
The `loader` property defines a `ResourceLoader`— an async function that retrieves some state. The resource calls the loader every time the `request` computation produces a new value, passing that value to the loader. See [Resource loaders](#resource-loaders) below for more details.
34+
The `loader` property defines a `ResourceLoader`— an async function that retrieves some state. The resource calls the loader every time the `params` computation produces a new value, passing that value to the loader. See [Resource loaders](#resource-loaders) below for more details.
3535

3636
`Resource` has a `value` signal that contains the results of the loader.
3737

3838
## Resource loaders
3939

4040
When creating a resource, you specify a `ResourceLoader`. This loader is an async function that accepts a single parameter— a `ResourceLoaderParams` object— and returns a value.
4141

42-
The `ResourceLoaderParams` object contains three properties: `request`, `previous`, and `abortSignal`.
42+
The `ResourceLoaderParams` object contains three properties: `params`, `previous`, and `abortSignal`.
4343

4444
| Property | Description |
4545
| ------------- | ------------------------------------------------------------------------------------------------------------------------------------------------ |
46-
| `request` | The value of the resource's `request` computation. |
46+
| `params` | The value of the resource's `params` computation. |
4747
| `previous` | An object with a `status` property, containing the previous `ResourceStatus`. |
4848
| `abortSignal` | An [`AbortSignal`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal). See [Aborting requests](#aborting-requests) below for details. |
4949

50-
If the `request` computation returns `undefined`, the loader function does not run and the resource status becomes `'idle'`.
50+
If the `params` computation returns `undefined`, the loader function does not run and the resource status becomes `'idle'`.
5151

5252
### Aborting requests
5353

54-
A resource aborts an outstanding request if the `request` computation changes while the resource is loading.
54+
A resource aborts an outstanding loading operation if the `params` computation changes while the resource is loading.
5555

5656
You can use the `abortSignal` in `ResourceLoaderParams` to respond to aborted requests. For example, the native `fetch` function accepts an `AbortSignal`:
5757

5858
```typescript
5959
const userId: Signal<string> = getUserId();
6060

6161
const userResource = resource({
62-
request: () => ({id: userId()}),
62+
params: () => ({id: userId()}),
6363
loader: ({request, abortSignal}): Promise<User> => {
6464
// fetch cancels any outstanding HTTP requests when the given `AbortSignal`
6565
// indicates that the request has been aborted.
@@ -78,8 +78,8 @@ You can programmatically trigger a resource's `loader` by calling the `reload` m
7878
const userId: Signal<string> = getUserId();
7979

8080
const userResource = resource({
81-
request: () => ({id: userId()}),
82-
loader: ({request}) => fetchUser(request),
81+
params: () => ({id: userId()}),
82+
loader: ({params}) => fetchUser(params),
8383
});
8484

8585
// ...

goldens/public-api/core/index.api.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ export interface BaseResourceOptions<T, R> {
179179
defaultValue?: NoInfer<T>;
180180
equal?: ValueEqualityFn<T>;
181181
injector?: Injector;
182-
request?: () => R;
182+
params?: () => R;
183183
}
184184

185185
// @public
@@ -1620,11 +1620,11 @@ export interface ResourceLoaderParams<R> {
16201620
// (undocumented)
16211621
abortSignal: AbortSignal;
16221622
// (undocumented)
1623+
params: NoInfer<Exclude<R, undefined>>;
1624+
// (undocumented)
16231625
previous: {
16241626
status: ResourceStatus;
16251627
};
1626-
// (undocumented)
1627-
request: Exclude<NoInfer<R>, undefined>;
16281628
}
16291629

16301630
// @public (undocumented)

packages/common/http/src/resource.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ class HttpResourceImpl<T>
315315
) {
316316
super(
317317
request,
318-
({request, abortSignal}) => {
318+
({params: request, abortSignal}) => {
319319
let sub: Subscription;
320320

321321
// Track the abort listener so it can be removed if the Observable completes (as a memory

packages/core/rxjs-interop/test/rx_resource_spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ describe('rxResource()', () => {
3030
let unsub = false;
3131
let lastSeenRequest: number = 0;
3232
rxResource({
33-
request,
34-
loader: ({request}) => {
33+
params: request,
34+
loader: ({params: request}) => {
3535
lastSeenRequest = request;
3636
return new Observable((sub) => {
3737
if (request === 2) {

packages/core/src/resource/api.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ export interface ResourceRef<T> extends WritableResource<T> {
128128
* @experimental
129129
*/
130130
export interface ResourceLoaderParams<R> {
131-
request: Exclude<NoInfer<R>, undefined>;
131+
params: NoInfer<Exclude<R, undefined>>;
132132
abortSignal: AbortSignal;
133133
previous: {
134134
status: ResourceStatus;
@@ -163,7 +163,7 @@ export interface BaseResourceOptions<T, R> {
163163
*
164164
* If a request function isn't provided, the loader won't rerun unless the resource is reloaded.
165165
*/
166-
request?: () => R;
166+
params?: () => R;
167167

168168
/**
169169
* The value which will be returned from the resource when a server value is unavailable, such as

packages/core/src/resource/resource.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
ResourceStreamingLoader,
2121
StreamingResourceOptions,
2222
ResourceStreamItem,
23+
ResourceLoaderParams,
2324
} from './api';
2425

2526
import {ValueEqualityFn} from '../../primitives/signals';
@@ -58,9 +59,12 @@ export function resource<T, R>(
5859
export function resource<T, R>(options: ResourceOptions<T, R>): ResourceRef<T | undefined>;
5960
export function resource<T, R>(options: ResourceOptions<T, R>): ResourceRef<T | undefined> {
6061
options?.injector || assertInInjectionContext(resource);
61-
const request = (options.request ?? (() => null)) as () => R;
62+
const oldNameForParams = (
63+
options as ResourceOptions<T, R> & {request: ResourceOptions<T, R>['params']}
64+
).request;
65+
const params = (options.params ?? oldNameForParams ?? (() => null)) as () => R;
6266
return new ResourceImpl<T | undefined, R>(
63-
request,
67+
params,
6468
getLoader(options),
6569
options.defaultValue,
6670
options.equal ? wrapEqualityFn(options.equal) : undefined,
@@ -308,12 +312,14 @@ export class ResourceImpl<T, R> extends BaseWritableResource<T> implements Resou
308312
// which side of the `await` they are.
309313
const stream = await untracked(() => {
310314
return this.loaderFn({
315+
params: extRequest.request as Exclude<R, undefined>,
316+
// TODO(alxhub): cleanup after g3 removal of `request` alias.
311317
request: extRequest.request as Exclude<R, undefined>,
312318
abortSignal,
313319
previous: {
314320
status: previousStatus,
315321
},
316-
});
322+
} as ResourceLoaderParams<R>);
317323
});
318324

319325
// If this request has been aborted, or the current request no longer

0 commit comments

Comments
 (0)