53 lines
1.2 KiB
JavaScript
53 lines
1.2 KiB
JavaScript
import R from 'remeda';
|
|
|
|
import { Action, ActionGenerator } from './actions.js';
|
|
|
|
export class Updux {
|
|
#localInitial = {};
|
|
#subduxes = {};
|
|
#actions ;
|
|
#mutations = {};
|
|
#config = {};
|
|
|
|
constructor(config = {}) {
|
|
this.#config = config;
|
|
this.#localInitial = config.initial ?? {};
|
|
this.#subduxes = config.subduxes ?? {};
|
|
this.#actions = config.actions ?? {};
|
|
}
|
|
|
|
get actions() {
|
|
return this.#actions;
|
|
}
|
|
|
|
get initial() {
|
|
if (Object.keys(this.#subduxes).length === 0) return this.#localInitial;
|
|
|
|
return Object.assign(
|
|
{},
|
|
this.#localInitial,
|
|
R.mapValues(this.#subduxes, ({ initial }) => initial),
|
|
);
|
|
}
|
|
|
|
get reducer() {
|
|
return (state,action) => this.upreducer(action)(state);
|
|
}
|
|
|
|
get upreducer() {
|
|
return (action) => state => {
|
|
const mutation = this.#mutations[action.type];
|
|
|
|
if( mutation ) {
|
|
state = mutation(action.payload,action)(state);
|
|
}
|
|
|
|
return state;
|
|
};
|
|
}
|
|
|
|
setMutation( action, mutation ) {
|
|
this.#mutations[ action.type ] = mutation;
|
|
}
|
|
}
|