2021-09-28 22:13:22 +00:00
|
|
|
import moize from 'moize';
|
|
|
|
import u from '@yanick/updeep';
|
|
|
|
|
|
|
|
import { buildInitial } from './buildInitial/index.js';
|
2021-10-07 16:04:15 +00:00
|
|
|
import { buildActions } from './buildActions/index.js';
|
|
|
|
import { buildSelectors } from './buildSelectors/index.js';
|
2021-09-28 22:13:22 +00:00
|
|
|
import { action } from './actions.js';
|
2021-10-07 16:04:15 +00:00
|
|
|
import { buildUpreducer } from './buildUpreducer.js';
|
2021-09-28 22:13:22 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @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 = {};
|
2021-10-07 16:04:15 +00:00
|
|
|
|
|
|
|
/** @type Record<string,Function> */
|
2021-09-28 22:13:22 +00:00
|
|
|
#actions = {};
|
2021-09-29 14:21:17 +00:00
|
|
|
#selectors = {};
|
2021-10-07 16:04:15 +00:00
|
|
|
#mutations = {};
|
2021-09-28 22:13:22 +00:00
|
|
|
|
|
|
|
constructor(config) {
|
|
|
|
this.#initial = config.initial ?? {};
|
|
|
|
this.#subduxes = config.subduxes ?? {};
|
|
|
|
this.#actions = config.actions ?? {};
|
2021-09-29 14:21:17 +00:00
|
|
|
this.#selectors = config.selectors ?? {};
|
2021-10-07 16:04:15 +00:00
|
|
|
|
|
|
|
this.#mutations = config.mutations ?? {};
|
|
|
|
|
|
|
|
Object.keys(this.#mutations)
|
|
|
|
.filter((action) => action !== '*')
|
|
|
|
.filter((action) => !this.actions.hasOwnProperty(action))
|
|
|
|
.forEach((action) => {
|
|
|
|
throw new Error(`action '${action}' is not defined`);
|
|
|
|
});
|
2021-09-28 22:13:22 +00:00
|
|
|
}
|
|
|
|
|
2021-10-07 16:04:15 +00:00
|
|
|
#memoInitial = moize(buildInitial);
|
2021-09-28 22:13:22 +00:00
|
|
|
#memoActions = moize(buildActions);
|
2021-09-29 14:21:17 +00:00
|
|
|
#memoSelectors = moize(buildSelectors);
|
2021-10-07 16:04:15 +00:00
|
|
|
#memoUpreducer = moize(buildUpreducer);
|
2021-09-28 22:13:22 +00:00
|
|
|
|
|
|
|
get initial() {
|
2021-10-07 16:04:15 +00:00
|
|
|
return this.#memoInitial(this.#initial, this.#subduxes);
|
2021-09-28 22:13:22 +00:00
|
|
|
}
|
|
|
|
|
2021-10-07 16:04:15 +00:00
|
|
|
/**
|
|
|
|
* @return {Record<string,Function>}
|
|
|
|
*/
|
2021-09-28 22:13:22 +00:00
|
|
|
get actions() {
|
|
|
|
return this.#memoActions(this.#actions, this.#subduxes);
|
|
|
|
}
|
|
|
|
|
2021-09-29 13:40:02 +00:00
|
|
|
get selectors() {
|
2021-10-07 16:04:15 +00:00
|
|
|
return this.#memoSelectors(this.#selectors, this.#subduxes);
|
|
|
|
}
|
|
|
|
|
|
|
|
get upreducer() {
|
|
|
|
return this.#memoUpreducer(
|
|
|
|
this.initial,
|
|
|
|
this.#mutations,
|
|
|
|
this.#subduxes
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
get reducer() {
|
|
|
|
return (state, action) => this.upreducer(action)(state);
|
2021-09-29 13:40:02 +00:00
|
|
|
}
|
|
|
|
|
2021-09-28 22:13:22 +00:00
|
|
|
addAction(type, payloadFunc) {
|
2021-10-07 16:04:15 +00:00
|
|
|
const theAction = action(type, payloadFunc);
|
2021-09-28 22:13:22 +00:00
|
|
|
|
2021-10-07 16:04:15 +00:00
|
|
|
this.#actions = u({ [type]: theAction }, this.#actions);
|
2021-09-28 22:13:22 +00:00
|
|
|
|
|
|
|
return theAction;
|
|
|
|
}
|
2021-09-29 13:40:02 +00:00
|
|
|
|
|
|
|
addSelector(name, func) {
|
2021-09-29 14:21:17 +00:00
|
|
|
this.#selectors[name] = func;
|
|
|
|
return func;
|
2021-09-29 13:40:02 +00:00
|
|
|
}
|
2021-10-07 16:04:15 +00:00
|
|
|
|
|
|
|
addMutation(name, mutation) {
|
|
|
|
if (typeof name === 'function') name = name.type;
|
|
|
|
|
|
|
|
this.#mutations = {
|
|
|
|
...this.#mutations,
|
|
|
|
[name]: mutation,
|
|
|
|
};
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
2021-09-28 22:13:22 +00:00
|
|
|
}
|