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

Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 25 additions & 8 deletions libs/state/src/lib/rxjs/operators/selectSlice.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { KeyCompareMap } from '../interfaces';
import { Observable, OperatorFunction } from 'rxjs';
import { filter, map } from 'rxjs/operators';
import { filter, map, tap } from 'rxjs/operators';
import { distinctUntilSomeChanged } from './distinctUntilSomeChanged';
import { isDevMode } from '@angular/core';

/**
* @description
Expand Down Expand Up @@ -82,6 +83,13 @@ export function selectSlice<T extends object, K extends keyof T>(
): OperatorFunction<T, PickSlice<T, K>> {
return (o$: Observable<T>): Observable<PickSlice<T, K>> =>
o$.pipe(
tap((state) => {
if (isDevMode() && state === undefined) {
console.warn(
'@rx-angular/state#selectSlice WARNING: state is undefined'
);
}
}),
Comment on lines +86 to +92
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lazy state initialization is a key concept and warning for an undefined state is contradictory to that from my perspective.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@edbzn true, lazy state is a key concept. the warning in devMode is still very helpful. especially when u are doing your first steps with rx-angular and cannot wrap your head around why selectSlice won't emit any values. The issue gets worse when your state interface has 100's of properties.

We have seen such cases, thats why the PR was opened :)

filter((state) => state !== undefined),
map((state) => {
// forward null
Expand All @@ -104,20 +112,29 @@ export function selectSlice<T extends object, K extends keyof T>(
// {str: undefined} => state.select(selectSlice(['str'])) => no emission
// {str: 'test', foo: undefined } => state.select(selectSlice(['foo'])) => no emission
if (definedKeys.length < keys.length) {
if (isDevMode()) {
const undefinedKeys = keys.filter((k) => !definedKeys.includes(k));
console.warn(
`@rx-angular/state#selectSlice WARNING: undefined value(s) of selected key(s): "${undefinedKeys.join(
'", "'
)}" selectSlice operator will return undefined`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically, selectSlice will not return undefined because the next operator will filter undefined values, it will not emit any notification instead.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agree, selectSlice won't emit any notification

);
}
return undefined;
}

// create the selected slice
return definedKeys
.reduce((vm, key) => {
vm[key] = state[key];
return vm;
}, {} as PickSlice<T, K>);
return definedKeys.reduce((vm, key) => {
vm[key] = state[key];
return vm;
}, {} as PickSlice<T, K>);
}),
filter((v) => v !== undefined),
distinctUntilSomeChanged(keys, keyCompareMap)
);
}

type PickSlice<T extends object, K extends keyof T> = Pick<T,
{ [I in K]: I }[K]>;
type PickSlice<T extends object, K extends keyof T> = Pick<
T,
{ [I in K]: I }[K]
>;