Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b247d37

Browse files
committed
refactor(state): simplify connect implementation
1 parent 9e2dbc0 commit b247d37

File tree

1 file changed

+77
-39
lines changed

1 file changed

+77
-39
lines changed

libs/state/src/lib/rx-state.service.ts

Lines changed: 77 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -405,64 +405,102 @@ export class RxState<T extends object> implements OnDestroy, Subscribable<T> {
405405
| Signal<T[K] | V>,
406406
projectValueFn?: ProjectValueReducer<T, K, V>
407407
): 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$);
419417
}
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+
424425
if (
425-
projectValueFn === undefined &&
426-
projectOrSlices$ === undefined &&
427-
inputOrSlice$
426+
isObservable(keyOrInputOrSlice$) &&
427+
projectOrSlices$ &&
428+
typeof projectOrSlices$ === 'function' &&
429+
!projectValueFn
428430
) {
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$);
430436
return;
431437
}
432438

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;
444451
}
445452

446453
if (
447-
inputOrSlice$ &&
448-
projectValueFn === undefined &&
449-
stateReducer !== undefined
454+
isKeyOf<T>(keyOrInputOrSlice$) &&
455+
isObservable(projectOrSlices$) &&
456+
!projectValueFn
450457
) {
451-
const slice$ = inputOrSlice$.pipe(
452-
map((v) => stateReducer!(this.get(), v as V))
458+
const slice$ = projectOrSlices$.pipe(
459+
map((value) => ({ ...{}, [keyOrInputOrSlice$]: value }))
453460
);
454461
this.accumulator.nextSliceObservable(slice$);
455462
return;
456463
}
457464

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 })));
460473
this.accumulator.nextSliceObservable(slice$);
461474
return;
462475
}
463476

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(
466504
map((value) => ({
467505
...{},
468506
[key]: projectValueFn(this.get(), value as V),

0 commit comments

Comments
 (0)