forked from angular/angular
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): experimental impl of
rxResource()
Implementations of two rxjs-interop APIs which produce `Resource`s from RxJS Observables. `rxResource()` is a flavor of `resource()` which uses a projection to an `Observable` as its loader (like `switchMap`).
- Loading branch information
Showing
4 changed files
with
118 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/** | ||
* @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 { | ||
assertInInjectionContext, | ||
ResourceOptions, | ||
resource, | ||
ResourceLoaderParams, | ||
ResourceRef, | ||
} from '@angular/core'; | ||
import {firstValueFrom, Observable, Subject} from 'rxjs'; | ||
import {takeUntil} from 'rxjs/operators'; | ||
|
||
/** | ||
* Like `ResourceOptions` but uses an RxJS-based `loader`. | ||
* | ||
* @experimental | ||
*/ | ||
export interface RxResourceOptions<T, R> extends Omit<ResourceOptions<T, R>, 'loader'> { | ||
loader: (params: ResourceLoaderParams<R>) => Observable<T>; | ||
} | ||
|
||
/** | ||
* Like `resource` but uses an RxJS based `loader` which maps the request to an `Observable` of the | ||
* resource's value. Like `firstValueFrom`, only the first emission of the Observable is considered. | ||
* | ||
* @experimental | ||
*/ | ||
export function rxResource<T, R>(opts: RxResourceOptions<T, R>): ResourceRef<T> { | ||
opts?.injector || assertInInjectionContext(rxResource); | ||
return resource<T, R>({ | ||
...opts, | ||
loader: (params) => { | ||
const cancelled = new Subject<void>(); | ||
params.abortSignal.addEventListener('abort', () => cancelled.next()); | ||
return firstValueFrom(opts.loader(params).pipe(takeUntil(cancelled))); | ||
}, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/** | ||
* @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 {of, Observable} from 'rxjs'; | ||
import {TestBed} from '@angular/core/testing'; | ||
import {ApplicationRef, Injector, signal} from '@angular/core'; | ||
import {rxResource} from '@angular/core/rxjs-interop'; | ||
|
||
describe('rxResource()', () => { | ||
it('should fetch data using an observable loader', async () => { | ||
const injector = TestBed.inject(Injector); | ||
const appRef = TestBed.inject(ApplicationRef); | ||
const res = rxResource({ | ||
loader: () => of(1), | ||
injector, | ||
}); | ||
await appRef.whenStable(); | ||
expect(res.value()).toBe(1); | ||
}); | ||
|
||
it('should cancel the fetch when a new request comes in', async () => { | ||
const injector = TestBed.inject(Injector); | ||
const appRef = TestBed.inject(ApplicationRef); | ||
let unsub = false; | ||
const request = signal(1); | ||
const res = rxResource({ | ||
request, | ||
loader: ({request}) => | ||
new Observable((sub) => { | ||
if (request === 2) { | ||
sub.next(true); | ||
} | ||
return () => { | ||
if (request === 1) { | ||
unsub = true; | ||
} | ||
}; | ||
}), | ||
injector, | ||
}); | ||
|
||
// Wait for the resource to reach loading state. | ||
await waitFor(() => res.isLoading()); | ||
|
||
// Setting request = 2 should cancel request = 1 | ||
request.set(2); | ||
await appRef.whenStable(); | ||
expect(unsub).toBe(true); | ||
}); | ||
}); | ||
|
||
async function waitFor(fn: () => boolean): Promise<void> { | ||
while (!fn()) { | ||
await new Promise((resolve) => setTimeout(resolve, 1)); | ||
} | ||
} |