Content-Length: 464841 | pFad | http://github.com/Rohit-byt/angular-open/commit/98a584c1e83b3ad429a514a33b2e725c4dd001f4

FB refactor(core): use stream in rxResource instead of loader (#59910) · Rohit-byt/angular-open@98a584c · GitHub
Skip to content

Commit 98a584c

Browse files
cexbrayatatscott
authored andcommitted
refactor(core): use stream in rxResource instead of loader (angular#59910)
With the changes in angular#59573, `resource` can now define a `stream` rather than a `loader`. In the same PR, `rxResource` was updated to leverage this new functionality to handle multiple responses from the underlying observable, rather than just the first one as it was previously. This commit renames the `loader` option of `rxResource` into `stream` to be better aligned with its new behavior. The previous version is temporarily kept and marked as deprecated to help migrating the current usage. Before ``` usersResource = rxResource({ request: () => ..., loader: ({ request }) => ... }); ``` After ``` usersResource = rxResource({ request: () => ..., stream: ({ request }) => ... }); ``` PR Close angular#59910
1 parent c0f41bc commit 98a584c

File tree

3 files changed

+30
-11
lines changed

3 files changed

+30
-11
lines changed

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,7 @@ export function rxResource<T, R>(opts: RxResourceOptions<T, R> & {
2626
export function rxResource<T, R>(opts: RxResourceOptions<T, R>): ResourceRef<T | undefined>;
2727

2828
// @public
29-
export interface RxResourceOptions<T, R> extends BaseResourceOptions<T, R> {
30-
// (undocumented)
31-
loader: (params: ResourceLoaderParams<R>) => Observable<T>;
32-
}
29+
export type RxResourceOptions<T, R> = RxResourceStreamOptions<T, R> | RxResourceLoaderOptions<T, R>;
3330

3431
// @public
3532
export function takeUntilDestroyed<T>(destroyRef?: DestroyRef): MonoTypeOperatorFunction<T>;

packages/core/rxjs-interop/src/rx_resource.ts

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,35 @@ import {
1717
} from '../../src/core';
1818
import {Observable, Subscription} from 'rxjs';
1919

20+
/**
21+
* Like `ResourceOptions` but uses an RxJS-based `stream`.
22+
*
23+
* @experimental
24+
*/
25+
interface RxResourceStreamOptions<T, R> extends BaseResourceOptions<T, R> {
26+
stream: (params: ResourceLoaderParams<R>) => Observable<T>;
27+
}
28+
2029
/**
2130
* Like `ResourceOptions` but uses an RxJS-based `loader`.
2231
*
2332
* @experimental
33+
* @deprecated Use `stream` instead of `loader`.
2434
*/
25-
export interface RxResourceOptions<T, R> extends BaseResourceOptions<T, R> {
35+
interface RxResourceLoaderOptions<T, R> extends BaseResourceOptions<T, R> {
36+
/** @deprecated Use `stream` instead of `loader`. */
2637
loader: (params: ResourceLoaderParams<R>) => Observable<T>;
2738
}
2839

2940
/**
30-
* Like `resource` but uses an RxJS based `loader` which maps the request to an `Observable` of the
41+
* Like `ResourceOptions` but uses an RxJS-based `stream`.
42+
*
43+
* @experimental
44+
*/
45+
export type RxResourceOptions<T, R> = RxResourceStreamOptions<T, R> | RxResourceLoaderOptions<T, R>;
46+
47+
/**
48+
* Like `resource` but uses an RxJS based `stream` which maps the request to an `Observable` of the
3149
* resource's value.
3250
*
3351
* @experimental
@@ -37,7 +55,7 @@ export function rxResource<T, R>(
3755
): ResourceRef<T>;
3856

3957
/**
40-
* Like `resource` but uses an RxJS based `loader` which maps the request to an `Observable` of the
58+
* Like `resource` but uses an RxJS based `stream` which maps the request to an `Observable` of the
4159
* resource's value.
4260
*
4361
* @experimental
@@ -67,7 +85,11 @@ export function rxResource<T, R>(opts: RxResourceOptions<T, R>): ResourceRef<T |
6785
resolve = undefined;
6886
}
6987

70-
sub = opts.loader(params).subscribe({
88+
// loader is kept for backwards compatibility
89+
const streamOrLoader =
90+
(opts as RxResourceStreamOptions<T, R>).stream ??
91+
(opts as RxResourceLoaderOptions<T, R>).loader;
92+
sub = streamOrLoader(params).subscribe({
7193
next: (value) => send({value}),
7294
error: (error) => send({error}),
7395
complete: () => {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ describe('rxResource()', () => {
1616
const injector = TestBed.inject(Injector);
1717
const appRef = TestBed.inject(ApplicationRef);
1818
const res = rxResource({
19-
loader: () => of(1),
19+
stream: () => of(1),
2020
injector,
2121
});
2222
await appRef.whenStable();
@@ -31,7 +31,7 @@ describe('rxResource()', () => {
3131
let lastSeenRequest: number = 0;
3232
rxResource({
3333
request,
34-
loader: ({request}) => {
34+
stream: ({request}) => {
3535
lastSeenRequest = request;
3636
return new Observable((sub) => {
3737
if (request === 2) {
@@ -61,7 +61,7 @@ describe('rxResource()', () => {
6161
const appRef = TestBed.inject(ApplicationRef);
6262
const response = new BehaviorSubject(1);
6363
const res = rxResource({
64-
loader: () => response,
64+
stream: () => response,
6565
injector,
6666
});
6767
await appRef.whenStable();

0 commit comments

Comments
 (0)








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/Rohit-byt/angular-open/commit/98a584c1e83b3ad429a514a33b2e725c4dd001f4

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy