@@ -405,64 +405,102 @@ export class RxState<T extends object> implements OnDestroy, Subscribable<T> {
405
405
| Signal < T [ K ] | V > ,
406
406
projectValueFn ?: ProjectValueReducer < T , K , V >
407
407
) : void {
408
- let inputOrSlice$ : Observable < Partial < T > | V > | undefined ;
409
- if ( ! isKeyOf < T > ( keyOrInputOrSlice$ ) ) {
410
- if ( isObservable ( keyOrInputOrSlice$ ) ) {
411
- inputOrSlice$ = keyOrInputOrSlice$ ;
412
- } else {
413
- // why can't typescript infer the correct type?
414
- inputOrSlice$ = toObservable (
415
- keyOrInputOrSlice$ as Signal < Partial < T > | V > ,
416
- { injector : this . injector }
417
- ) ;
418
- }
408
+ /**
409
+ * From top to bottom the overloads are handled.
410
+ */
411
+ if (
412
+ isObservable ( keyOrInputOrSlice$ ) &&
413
+ ! projectOrSlices$ &&
414
+ ! projectValueFn
415
+ ) {
416
+ this . accumulator . nextSliceObservable ( keyOrInputOrSlice$ ) ;
419
417
}
420
- const key : K | null =
421
- ! inputOrSlice$ && isKeyOf < T > ( keyOrInputOrSlice$ )
422
- ? keyOrInputOrSlice$
423
- : null ;
418
+
419
+ if ( isSignal ( keyOrInputOrSlice$ ) && ! projectOrSlices$ && ! projectValueFn ) {
420
+ this . accumulator . nextSliceObservable (
421
+ toObservable ( keyOrInputOrSlice$ , { injector : this . injector } )
422
+ ) ;
423
+ }
424
+
424
425
if (
425
- projectValueFn === undefined &&
426
- projectOrSlices$ === undefined &&
427
- inputOrSlice$
426
+ isObservable ( keyOrInputOrSlice$ ) &&
427
+ projectOrSlices$ &&
428
+ typeof projectOrSlices$ === 'function' &&
429
+ ! projectValueFn
428
430
) {
429
- this . accumulator . nextSliceObservable ( inputOrSlice$ ) ;
431
+ const projectionStateFn = projectOrSlices$ ;
432
+ const slice$ = keyOrInputOrSlice$ . pipe (
433
+ map ( ( v ) => projectionStateFn ( this . accumulator . state , v as V ) )
434
+ ) ;
435
+ this . accumulator . nextSliceObservable ( slice$ ) ;
430
436
return ;
431
437
}
432
438
433
- let slices$ : Observable < T [ K ] | V > | null = null ;
434
- let stateReducer : ProjectStateReducer < T , V > | undefined ;
435
-
436
- if ( projectOrSlices$ ) {
437
- if ( isObservable ( projectOrSlices$ ) ) {
438
- slices$ = projectOrSlices$ ;
439
- } else if ( isSignal ( projectOrSlices$ ) ) {
440
- slices$ = toObservable ( projectOrSlices$ , { injector : this . injector } ) ;
441
- } else {
442
- stateReducer = projectOrSlices$ ;
443
- }
439
+ if (
440
+ isSignal ( keyOrInputOrSlice$ ) &&
441
+ projectOrSlices$ &&
442
+ typeof projectOrSlices$ === 'function' &&
443
+ ! projectValueFn
444
+ ) {
445
+ const projectionStateFn = projectOrSlices$ ;
446
+ const slice$ = toObservable ( keyOrInputOrSlice$ , {
447
+ injector : this . injector ,
448
+ } ) . pipe ( map ( ( v ) => projectionStateFn ( this . accumulator . state , v as V ) ) ) ;
449
+ this . accumulator . nextSliceObservable ( slice$ ) ;
450
+ return ;
444
451
}
445
452
446
453
if (
447
- inputOrSlice$ &&
448
- projectValueFn === undefined &&
449
- stateReducer !== undefined
454
+ isKeyOf < T > ( keyOrInputOrSlice$ ) &&
455
+ isObservable ( projectOrSlices$ ) &&
456
+ ! projectValueFn
450
457
) {
451
- const slice$ = inputOrSlice $. pipe (
452
- map ( ( v ) => stateReducer ! ( this . get ( ) , v as V ) )
458
+ const slice$ = projectOrSlices $. pipe (
459
+ map ( ( value ) => ( { ... { } , [ keyOrInputOrSlice$ ] : value } ) )
453
460
) ;
454
461
this . accumulator . nextSliceObservable ( slice$ ) ;
455
462
return ;
456
463
}
457
464
458
- if ( projectValueFn === undefined && key && slices$ ) {
459
- const slice$ = slices$ . pipe ( map ( ( value ) => ( { ...{ } , [ key ] : value } ) ) ) ;
465
+ if (
466
+ isKeyOf < T > ( keyOrInputOrSlice$ ) &&
467
+ isSignal ( projectOrSlices$ ) &&
468
+ ! projectValueFn
469
+ ) {
470
+ const slice$ = toObservable ( projectOrSlices$ , {
471
+ injector : this . injector ,
472
+ } ) . pipe ( map ( ( value ) => ( { ...{ } , [ keyOrInputOrSlice$ ] : value } ) ) ) ;
460
473
this . accumulator . nextSliceObservable ( slice$ ) ;
461
474
return ;
462
475
}
463
476
464
- if ( typeof projectValueFn === 'function' && key && slices$ ) {
465
- const slice$ = slices$ . pipe (
477
+ if (
478
+ projectValueFn &&
479
+ typeof projectValueFn === 'function' &&
480
+ isKeyOf < T > ( keyOrInputOrSlice$ ) &&
481
+ isObservable ( projectOrSlices$ )
482
+ ) {
483
+ const key : K = keyOrInputOrSlice$ ;
484
+ const slice$ = projectOrSlices$ . pipe (
485
+ map ( ( value ) => ( {
486
+ ...{ } ,
487
+ [ key ] : projectValueFn ( this . get ( ) , value as V ) ,
488
+ } ) )
489
+ ) ;
490
+ this . accumulator . nextSliceObservable ( slice$ ) ;
491
+ return ;
492
+ }
493
+
494
+ if (
495
+ projectValueFn &&
496
+ typeof projectValueFn === 'function' &&
497
+ isKeyOf ( keyOrInputOrSlice$ ) &&
498
+ isSignal ( projectOrSlices$ )
499
+ ) {
500
+ const key : K = keyOrInputOrSlice$ ;
501
+ const slice$ = toObservable ( projectOrSlices$ , {
502
+ injector : this . injector ,
503
+ } ) . pipe (
466
504
map ( ( value ) => ( {
467
505
...{ } ,
468
506
[ key ] : projectValueFn ( this . get ( ) , value as V ) ,
0 commit comments