From 07811ddd7d4f027e60cb33ecf341e8d1d5675a8a Mon Sep 17 00:00:00 2001 From: Maciej Sawicki Date: Fri, 7 Mar 2025 15:32:20 +0100 Subject: [PATCH] fix(core): move reload method from Resource to WritableResource (#61441) Now only mutable resources can be reloaded. PR Close #61441 --- goldens/public-api/core/index.api.md | 2 +- packages/common/http/test/resource_spec.ts | 14 ++++++++++++++ packages/core/src/resource/api.ts | 20 ++++++++++---------- 3 files changed, 25 insertions(+), 11 deletions(-) diff --git a/goldens/public-api/core/index.api.md b/goldens/public-api/core/index.api.md index bba2a74217a..a9cad029e73 100644 --- a/goldens/public-api/core/index.api.md +++ b/goldens/public-api/core/index.api.md @@ -1610,7 +1610,6 @@ export interface Resource { readonly error: Signal; hasValue(): this is Resource>; readonly isLoading: Signal; - reload(): boolean; readonly status: Signal; readonly value: Signal; } @@ -2022,6 +2021,7 @@ export interface WritableResource extends Resource { asReadonly(): Resource; // (undocumented) hasValue(): this is WritableResource>; + reload(): boolean; set(value: T): void; update(updater: (value: T) => T): void; // (undocumented) diff --git a/packages/common/http/test/resource_spec.ts b/packages/common/http/test/resource_spec.ts index 08cc88cc955..407f8bbeeee 100644 --- a/packages/common/http/test/resource_spec.ts +++ b/packages/common/http/test/resource_spec.ts @@ -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([]); + }); }); diff --git a/packages/core/src/resource/api.ts b/packages/core/src/resource/api.ts index 38cb3d597a2..ff8aa785e56 100644 --- a/packages/core/src/resource/api.ts +++ b/packages/core/src/resource/api.ts @@ -72,16 +72,6 @@ export interface Resource { * This function is reactive. */ hasValue(): this is Resource>; - - /** - * 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 extends Resource { */ update(updater: (value: T) => T): void; asReadonly(): Resource; + + /** + * 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; } /**