40 lines
1.1 KiB
TypeScript
40 lines
1.1 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() {
|
|
return (...args) => R.purry((state : DuxAggregateState<TState,TSubduxes>, _action : any) => state, args);
|
|
}
|
|
}
|