Skip to content

Commit 04d2d53

Browse files
committed
feat(rx-stateful): allow custom accumulation function
closes #53
1 parent 83f1c51 commit 04d2d53

File tree

4 files changed

+20
-9
lines changed

4 files changed

+20
-9
lines changed

libs/rx-stateful/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ export {
66
RxStatefulWithError,
77
RxStatefulConfig,
88
} from './lib/types/types';
9+
export { RxStatefulAccumulationFn } from './lib/types/accumulation-fn';

libs/rx-stateful/src/lib/rx-stateful$.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,26 @@ import {
1818
} from 'rxjs';
1919
import {InternalRxState, RxStateful, RxStatefulConfig, RxStatefulWithError,} from './types/types';
2020
import {_handleSyncValue} from './util/handle-sync-value';
21+
import {defaultAccumulationFn} from "./types/accumulation-fn";
2122

2223
/**
2324
* @publicApi
2425
*/
2526
export function rxStateful$<T, E = unknown>(source$: Observable<T>): RxStateful<T, E>;
26-
export function rxStateful$<T, E = unknown>(source$: Observable<T>, config: RxStatefulConfig): RxStateful<T, E>;
27+
export function rxStateful$<T, E = unknown>(source$: Observable<T>, config: RxStatefulConfig<T,E>): RxStateful<T, E>;
2728
export function rxStateful$<T, E = unknown>(
2829
source$: Observable<T>,
29-
config?: RxStatefulConfig
30+
config?: RxStatefulConfig<T,E>
3031
): RxStateful<T, E> {
3132
const error$$ = new Subject<RxStatefulWithError<T, E>>();
32-
const mergedConfig: RxStatefulConfig = {
33+
const mergedConfig: RxStatefulConfig<T,E> = {
3334
keepValueOnRefresh: true,
3435
...config,
3536
};
37+
const accumulationFn = mergedConfig.accumulationFn ?? defaultAccumulationFn;
3638

37-
const refreshTriggerIsBehaivorSubject = (config: RxStatefulConfig) =>
39+
40+
const refreshTriggerIsBehaivorSubject = (config: RxStatefulConfig<T,E>) =>
3841
config.refreshTrigger$ instanceof BehaviorSubject;
3942

4043
const refresh$ = mergedConfig?.refreshTrigger$ ?? new Subject<unknown>();
@@ -87,10 +90,7 @@ export function rxStateful$<T, E = unknown>(
8790

8891
const state$ = merge(request$, refreshedRequest$, error$$).pipe(
8992
scan(
90-
// @ts-ignore
91-
(acc, curr) => {
92-
return { ...acc, ...curr };
93-
},
93+
accumulationFn,
9494
{ isLoading: false, isRefreshing: false, value: undefined, error: undefined, context: 'idle' }
9595
),
9696
distinctUntilChanged(),
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import {InternalRxState} from './types';
2+
3+
export type RxStatefulAccumulationFn<T, E> = (
4+
acc: InternalRxState<T, E>,
5+
val: Partial<InternalRxState<T, E>>
6+
) => InternalRxState<T, E>;
7+
8+
export const defaultAccumulationFn: RxStatefulAccumulationFn<any, any> = (acc, val) => ({ ...acc, ...val });

libs/rx-stateful/src/lib/types/types.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {Observable, Subject} from 'rxjs';
2+
import {RxStatefulAccumulationFn} from "./accumulation-fn";
23

34
/**
45
* @publicApi
@@ -53,7 +54,8 @@ export interface InternalRxState<T, E> {
5354
/**
5455
* @publicApi
5556
*/
56-
export interface RxStatefulConfig {
57+
export interface RxStatefulConfig<T, E> {
5758
refreshTrigger$?: Subject<any>;
5859
keepValueOnRefresh?: boolean;
60+
accumulationFn?: RxStatefulAccumulationFn<T, E>;
5961
}

0 commit comments

Comments
 (0)