mirror of
https://github.com/angular/angular
synced 2026-05-24 09:28:37 +00:00
fix(common): support equality function in httpResource (#60026)
The `equal` option was not passed to the underlying resource. PR Close #60026
This commit is contained in:
parent
8d770eccee
commit
90a16a1088
2 changed files with 21 additions and 1 deletions
|
|
@ -19,6 +19,7 @@ import {
|
|||
Resource,
|
||||
WritableSignal,
|
||||
ResourceStreamItem,
|
||||
type ValueEqualityFn,
|
||||
} from '@angular/core';
|
||||
import {Subscription} from 'rxjs';
|
||||
|
||||
|
|
@ -240,6 +241,7 @@ function makeHttpResourceFn<TRaw>(responseType: 'arraybuffer' | 'blob' | 'json'
|
|||
() => normalizeRequest(request, responseType),
|
||||
options?.defaultValue,
|
||||
options?.parse as (value: unknown) => TResult,
|
||||
options?.equal as ValueEqualityFn<unknown>,
|
||||
) as HttpResourceRef<TResult>;
|
||||
};
|
||||
}
|
||||
|
|
@ -313,6 +315,7 @@ class HttpResourceImpl<T>
|
|||
request: () => HttpRequest<T> | undefined,
|
||||
defaultValue: T,
|
||||
parse?: (value: unknown) => T,
|
||||
equal?: ValueEqualityFn<unknown>,
|
||||
) {
|
||||
super(
|
||||
request,
|
||||
|
|
@ -364,7 +367,7 @@ class HttpResourceImpl<T>
|
|||
return promise;
|
||||
},
|
||||
defaultValue,
|
||||
undefined,
|
||||
equal,
|
||||
injector,
|
||||
);
|
||||
this.client = injector.get(HttpClient);
|
||||
|
|
|
|||
|
|
@ -165,6 +165,23 @@ describe('httpResource', () => {
|
|||
expect(res.value()).toEqual('[1,2,3]');
|
||||
});
|
||||
|
||||
it('should allow defining an equality function', async () => {
|
||||
const backend = TestBed.inject(HttpTestingController);
|
||||
const res = httpResource<number>('/data', {
|
||||
injector: TestBed.inject(Injector),
|
||||
equal: (_a, _b) => true,
|
||||
});
|
||||
TestBed.flushEffects();
|
||||
const req = backend.expectOne('/data');
|
||||
req.flush(1);
|
||||
|
||||
await TestBed.inject(ApplicationRef).whenStable();
|
||||
expect(res.value()).toEqual(1);
|
||||
|
||||
res.value.set(5);
|
||||
expect(res.value()).toBe(1); // equality blocked writes
|
||||
});
|
||||
|
||||
it('should support text responses', async () => {
|
||||
const backend = TestBed.inject(HttpTestingController);
|
||||
const res = httpResource.text(
|
||||
|
|
|
|||
Loading…
Reference in a new issue