44 lines
1.3 KiB
TypeScript
44 lines
1.3 KiB
TypeScript
import R from 'remeda';
|
|
|
|
import { DuxAggregateState, UpduxConfig } from './types';
|
|
import { Action, ActionGenerator } from './actions.js';
|
|
|
|
export class Updux<
|
|
TState extends any = {},
|
|
TActions extends { [key: string]: ActionGenerator } = {},
|
|
TSubduxes = {}
|
|
> {
|
|
#localInitial: any = {};
|
|
#subduxes;
|
|
#actions : TActions;
|
|
|
|
constructor(config: UpduxConfig<TState, TActions, TSubduxes>) {
|
|
this.#localInitial = config.initial ?? {};
|
|
this.#subduxes = config.subduxes ?? {};
|
|
|
|
this.#actions = config.actions ?? ([] as any);
|
|
}
|
|
|
|
get actions() {
|
|
return this.#actions;
|
|
}
|
|
|
|
get initial(): DuxAggregateState<TState,TSubduxes>{
|
|
if (Object.keys(this.#subduxes).length === 0) return this.#localInitial;
|
|
|
|
return Object.assign(
|
|
{},
|
|
this.#localInitial,
|
|
R.mapValues(this.#subduxes, ({ initial }) => initial),
|
|
);
|
|
}
|
|
|
|
get reducer(): ( state: DuxAggregateState<TState,TSubduxes>, action: Action ) => DuxAggregateState<TState,TSubduxes> {
|
|
return (state,action) => this.upreducer(action)(state);
|
|
}
|
|
|
|
get upreducer(): (action: Action) => (state: DuxAggregateState<TState,TSubduxes>) => DuxAggregateState<TState,TSubduxes> {
|
|
return (action) => state => state;
|
|
}
|
|
}
|