File tree 2 files changed +21
-1
lines changed
2 files changed +21
-1
lines changed Original file line number Diff line number Diff line change @@ -19,6 +19,7 @@ import {
19
19
Resource ,
20
20
WritableSignal ,
21
21
ResourceStreamItem ,
22
+ type ValueEqualityFn ,
22
23
} from '@angular/core' ;
23
24
import { Subscription } from 'rxjs' ;
24
25
@@ -240,6 +241,7 @@ function makeHttpResourceFn<TRaw>(responseType: 'arraybuffer' | 'blob' | 'json'
240
241
( ) => normalizeRequest ( request , responseType ) ,
241
242
options ?. defaultValue ,
242
243
options ?. parse as ( value : unknown ) => TResult ,
244
+ options ?. equal as ValueEqualityFn < unknown > ,
243
245
) as HttpResourceRef < TResult > ;
244
246
} ;
245
247
}
@@ -313,6 +315,7 @@ class HttpResourceImpl<T>
313
315
request : ( ) => HttpRequest < T > | undefined ,
314
316
defaultValue : T ,
315
317
parse ?: ( value : unknown ) => T ,
318
+ equal ?: ValueEqualityFn < unknown > ,
316
319
) {
317
320
super (
318
321
request ,
@@ -364,7 +367,7 @@ class HttpResourceImpl<T>
364
367
return promise ;
365
368
} ,
366
369
defaultValue ,
367
- undefined ,
370
+ equal ,
368
371
injector ,
369
372
) ;
370
373
this . client = injector . get ( HttpClient ) ;
Original file line number Diff line number Diff line change @@ -165,6 +165,23 @@ describe('httpResource', () => {
165
165
expect ( res . value ( ) ) . toEqual ( '[1,2,3]' ) ;
166
166
} ) ;
167
167
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
+
168
185
it ( 'should support text responses' , async ( ) => {
169
186
const backend = TestBed . inject ( HttpTestingController ) ;
170
187
const res = httpResource . text (
You can’t perform that action at this time.
0 commit comments