File tree Expand file tree Collapse file tree 2 files changed +21
-1
lines changed
Expand file tree Collapse file tree 2 files changed +21
-1
lines changed Original file line number Diff line number Diff line change @@ -19,6 +19,7 @@ import {
1919 Resource ,
2020 WritableSignal ,
2121 ResourceStreamItem ,
22+ type ValueEqualityFn ,
2223} from '@angular/core' ;
2324import { Subscription } from 'rxjs' ;
2425
@@ -240,6 +241,7 @@ function makeHttpResourceFn<TRaw>(responseType: 'arraybuffer' | 'blob' | 'json'
240241 ( ) => normalizeRequest ( request , responseType ) ,
241242 options ?. defaultValue ,
242243 options ?. parse as ( value : unknown ) => TResult ,
244+ options ?. equal as ValueEqualityFn < unknown > ,
243245 ) as HttpResourceRef < TResult > ;
244246 } ;
245247}
@@ -313,6 +315,7 @@ class HttpResourceImpl<T>
313315 request : ( ) => HttpRequest < T > | undefined ,
314316 defaultValue : T ,
315317 parse ?: ( value : unknown ) => T ,
318+ equal ?: ValueEqualityFn < unknown > ,
316319 ) {
317320 super (
318321 request ,
@@ -364,7 +367,7 @@ class HttpResourceImpl<T>
364367 return promise ;
365368 } ,
366369 defaultValue ,
367- undefined ,
370+ equal ,
368371 injector ,
369372 ) ;
370373 this . client = injector . get ( HttpClient ) ;
Original file line number Diff line number Diff line change @@ -165,6 +165,23 @@ describe('httpResource', () => {
165165 expect ( res . value ( ) ) . toEqual ( '[1,2,3]' ) ;
166166 } ) ;
167167
168+ it ( 'should allow defining an equality function' , async ( ) => {
169+ const backend = TestBed . inject ( HttpTestingController ) ;
170+ const res = httpResource < number > ( '/data' , {
171+ injector : TestBed . inject ( Injector ) ,
172+ equal : ( _a , _b ) => true ,
173+ } ) ;
174+ TestBed . flushEffects ( ) ;
175+ const req = backend . expectOne ( '/data' ) ;
176+ req . flush ( 1 ) ;
177+
178+ await TestBed . inject ( ApplicationRef ) . whenStable ( ) ;
179+ expect ( res . value ( ) ) . toEqual ( 1 ) ;
180+
181+ res . value . set ( 5 ) ;
182+ expect ( res . value ( ) ) . toBe ( 1 ) ; // equality blocked writes
183+ } ) ;
184+
168185 it ( 'should support text responses' , async ( ) => {
169186 const backend = TestBed . inject ( HttpTestingController ) ;
170187 const res = httpResource . text (
You can’t perform that action at this time.
0 commit comments