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) { this.#localInitial = config.initial ?? {}; this.#subduxes = config.subduxes ?? {}; this.#actions = config.actions ?? ([] as any); } get actions() { return this.#actions; } get initial(): DuxAggregateState{ 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, action: Action ) => DuxAggregateState { return (state,action) => this.upreducer(action)(state); } get upreducer(): (action: Action) => (state: DuxAggregateState) => DuxAggregateState { return (action) => state => state; } }