fix(core): move reload method from Resource to WritableResource (#61441)

Now only mutable resources can be reloaded.

PR Close #61441
This commit is contained in:
Maciej Sawicki 2025-03-07 15:32:20 +01:00 committed by Andrew Scott
parent 5762c23a77
commit 07811ddd7d
3 changed files with 25 additions and 11 deletions

View file

@ -1610,7 +1610,6 @@ export interface Resource<T> {
readonly error: Signal<unknown>;
hasValue(): this is Resource<Exclude<T, undefined>>;
readonly isLoading: Signal<boolean>;
reload(): boolean;
readonly status: Signal<ResourceStatus>;
readonly value: Signal<T>;
}
@ -2022,6 +2021,7 @@ export interface WritableResource<T> extends Resource<T> {
asReadonly(): Resource<T>;
// (undocumented)
hasValue(): this is WritableResource<Exclude<T, undefined>>;
reload(): boolean;
set(value: T): void;
update(updater: (value: T) => T): void;
// (undocumented)

View file

@ -289,4 +289,18 @@ describe('httpResource', () => {
await TestBed.inject(ApplicationRef).whenStable();
expect(res.value()).toBe(buffer);
});
it('should send request on reload', async () => {
const backend = TestBed.inject(HttpTestingController);
const res = httpResource(() => '/data', {injector: TestBed.inject(Injector)});
TestBed.tick();
let req = backend.expectOne('/data');
req.flush([]);
await TestBed.inject(ApplicationRef).whenStable();
res.reload();
TestBed.tick();
req = backend.expectOne('/data');
req.flush([]);
});
});

View file

@ -72,16 +72,6 @@ export interface Resource<T> {
* This function is reactive.
*/
hasValue(): this is Resource<Exclude<T, undefined>>;
/**
* 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;
}
/**
@ -105,6 +95,16 @@ export interface WritableResource<T> extends Resource<T> {
*/
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;
}
/**