UNPKG

@rx-angular/state

Version:

@rx-angular/state is a light-weight, flexible, strongly typed and tested tool dedicated to reduce the complexity of managing component state and side effects in angular

79 lines 3.03 kB
import { OperatorFunction } from 'rxjs'; import { KeyCompareMap, PickSlice } from '../interfaces/index'; /** * @description * * Returns an Observable that emits only the provided `keys` emitted by the source Observable. Each key will get * filtered to only emit _defined_ values as well as checked for distinct emissions. * Comparison will be done for each set key in the `keys` array. * * `selectSlice` will only emit _valid_ selections. A selection is _valid_ if every * selected key exists and is defined in the source Observable. This ensures that the `selectSlice` * operator will always return a complete slice with all values defined. * * You can fine grain your distinct checks by providing a `KeyCompareMap` with those keys you want to compute * explicitly different * * @example * * // An example with a custom comparison applied to each key * import { of } from 'rxjs'; * import { selectSlice } from 'rx-angular/state'; * * * const state$: Observable<MyState> = of( * { title: 'myTitle', panelOpen: true}, * { title: 'myTitle2', panelOpen: true}, * { title: 'newTitle', panelOpen: true}, * { title: 'newTitle', panelOpen: false} * ) * .pipe( * selectSlice(['title', 'panelOpen']), * ) * .subscribe(x => console.log(x)); * * // displays: * // { title: 'myTitle', panelOpen: true }, * // { title: 'myTitle2', panelOpen: true }, * // { title: 'newTitle', panelOpen: true }, * // { title: 'newTitle', panelOpen: false } * * @example * * import { of, Observable } from 'rxjs'; * import { tap } from 'rxjs/operators'; * import { selectSlice } from 'rx-angular/state'; * * interface MyState { * title: string; * items: string[]; * panelOpen: boolean; * } * // Select items and title. * // apply custom compare logic for the items array * const customComparison: KeyCompareMap<MyState> = { * items: (oldItems, newItems) => compareItems(oldItems, newItems) * }; * const state$: Observable<MyState> = of( * { title: 'myTitle', items: ['foo', 'bar'], panelOpen: true }, * { title: 'myTitle', items: ['foo', 'bar'], panelOpen: false }, * { title: 'nextTitle', items: ['foo', 'baR'], panelOpen: true }, * { title: 'nextTitle', items: ['fooRz', 'boo'], panelOpen: false }, * ); * const slice$ = state$.pipe(selectSlice(['title', 'items'], customComparison), tap(console.log)).subscribe(); * * // displays: * // { title: 'myTitle', items: ['foo', 'bar'] } * // { title: 'nextTitle', items: ['foo', 'baR'] } * // { title: 'nextTitle', items: ['fooRz', 'boo'] } * * @param {(K)[]} keys - the array of keys which should be selected * @param {KeyCompareMap<{ [P in K]: T[P] }>} [keyCompareMap] Optional KeyCompareMap to provide custom compare logic * for some the keys * @docsPage selectSlice * @docsCategory operators */ export declare function selectSlice<T extends object, K extends keyof T>(keys: K[], keyCompareMap?: KeyCompareMap<{ [P in K]: T[P]; }>): OperatorFunction<T, PickSlice<T, K>>; //# sourceMappingURL=selectSlice.d.ts.map