48 lines
1.3 KiB
JavaScript
48 lines
1.3 KiB
JavaScript
|
import moize from 'moize';
|
||
|
import u from '@yanick/updeep';
|
||
|
|
||
|
import { buildInitial } from './buildInitial/index.js';
|
||
|
import { buildActions } from './buildActions/index.js';
|
||
|
import { action } from './actions.js';
|
||
|
|
||
|
|
||
|
/**
|
||
|
* @public
|
||
|
* `Updux` is a way to minimize and simplify the boilerplate associated with the
|
||
|
* creation of a `Redux` store. It takes a shorthand configuration
|
||
|
* object, and generates the appropriate reducer, actions, middleware, etc.
|
||
|
* In true `Redux`-like fashion, upduxes can be made of sub-upduxes (`subduxes` for short) for different slices of the root state.
|
||
|
*/
|
||
|
export class Updux {
|
||
|
#initial = {};
|
||
|
#subduxes = {};
|
||
|
#actions = {};
|
||
|
|
||
|
constructor(config) {
|
||
|
this.#initial = config.initial ?? {};
|
||
|
|
||
|
this.#subduxes = config.subduxes ?? {};
|
||
|
|
||
|
this.#actions = config.actions ?? {};
|
||
|
}
|
||
|
|
||
|
#memoInitial = moize( buildInitial );
|
||
|
#memoActions = moize(buildActions);
|
||
|
|
||
|
get initial() {
|
||
|
return this.#memoInitial(this.#initial,this.#subduxes);
|
||
|
}
|
||
|
|
||
|
get actions() {
|
||
|
return this.#memoActions(this.#actions, this.#subduxes);
|
||
|
}
|
||
|
|
||
|
addAction(type, payloadFunc) {
|
||
|
const theAction = action(type,payloadFunc);
|
||
|
|
||
|
this.#actions = u( { [type]: theAction }, this.#actions );
|
||
|
|
||
|
return theAction;
|
||
|
}
|
||
|
}
|