8000 feat(state): improve DX by logging undefined keys in selectSlice by mrkpks · Pull Request #852 · rx-angular/rx-angular · GitHub
[go: up one dir, main page]

Skip to content

feat(state): improve DX by logging undefined keys in selectSlice #852

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
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
30 changes: 22 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 { Observable, OperatorFunction } from 'rxjs';
import { filter, map } from 'rxjs/operators';
import { filter, map, tap } from 'rxjs/operators';
import { KeyCompareMap, PickSlice } from '../interfaces';
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,18 +112,24 @@ 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)
);
}

}
0