updux/src/Updux.js

122 lines
3.2 KiB
JavaScript

import R from 'remeda';
import { createStore as reduxCreateStore } from 'redux';
import { action } from './actions.js';
function isActionGen(action) {
return typeof action === 'function' && action.type;
}
/**
* Updux configuration object
* @typedef {Object} Updux config
* @property {Object} actions - Actions to be made available to the updux.
*/
export class Updux {
#localInitial = {};
#subduxes = {};
#actions;
#mutations = {};
#config = {};
constructor(config = {}) {
this.#config = config;
this.#localInitial = config.initial ?? {};
this.#subduxes = config.subduxes ?? {};
this.#actions = R.mapValues(config.actions ?? {}, (arg, name) =>
isActionGen(arg) ? arg : action(name, arg),
);
Object.entries(this.#subduxes).forEach(([slice, sub]) =>
this.#addSubduxActions(slice, sub),
);
}
#addSubduxActions(_slice, subdux) {
if (!subdux.actions) return;
// TODO action 'blah' defined multiple times: <where>
Object.entries(subdux.actions).forEach(([action, gen]) => {
if (this.#actions[action]) {
if (this.#actions[action] === gen) return;
throw new Error(`action '${action}' already defined`);
}
this.#actions[action] = gen;
});
}
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;
}
createStore(initial = undefined, enhancerGenerator = undefined) {
// const enhancer = (enhancerGenerator ?? applyMiddleware)(
// this.middleware
// );
const store = reduxCreateStore(
this.reducer,
initial ?? this.initial,
//enhancer
);
store.actions = this.actions;
// store.selectors = this.selectors;
// store.getState = R.merge(
// store.getState,
// R.mapValues(this.selectors, (selector) => {
// return (...args) => {
// let result = selector(store.getState());
// if (typeof result === 'function') return result(...args);
// return result;
// };
// })
// );
for (const action in this.actions) {
store.dispatch[action] = (...args) => {
return store.dispatch(this.actions[action](...args));
};
}
//this.subscribeAll(store);
return store;
}
}