-
-
Notifications
You must be signed in to change notification settings - Fork 205
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
|
@@ -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' | ||
); | ||
} | ||
}), | ||
filter((state) => state !== undefined), | ||
map((state) => { | ||
// forward null | ||
|
@@ -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` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
); | ||
} | ||
|
||
} |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 :)