updux/dist/Updux.js

211 lines
10 KiB
JavaScript
Raw Permalink Normal View History

2025-01-31 18:16:41 +00:00
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
if (kind === "m") throw new TypeError("Private method is not writable");
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
};
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var _Updux_subduxes, _Updux_memoInitialState, _Updux_memoBuildReducer, _Updux_memoBuildSelectors, _Updux_memoBuildEffects, _Updux_memoBuildReactions, _Updux_actions, _Updux_defaultMutation, _Updux_effects, _Updux_reactions, _Updux_mutations, _Updux_inheritedReducer, _Updux_selectors;
import * as rtk from '@reduxjs/toolkit';
const { configureStore, } = rtk;
import baseMoize from 'moize/mjs/index.mjs';
import { buildInitialState } from './initialState.js';
import { buildActions } from './actions.js';
import { D } from '@mobily/ts-belt';
import { buildReducer } from './reducer.js';
import { buildSelectors } from './selectors.js';
import { buildEffectsMiddleware, buildEffects, augmentMiddlewareApi } from './effects.js';
import { augmentGetState, augmentDispatch } from './createStore.js';
import { buildReactions } from './reactions.js';
const moize = (func) => baseMoize(func, { maxSize: 1 });
/**
* @description main Updux class
*/
export default class Updux {
constructor(duxConfig) {
var _a, _b, _c;
this.duxConfig = duxConfig;
/** @internal */
_Updux_subduxes.set(this, void 0);
/** @internal */
_Updux_memoInitialState.set(this, moize(buildInitialState));
/** @internal */
_Updux_memoBuildReducer.set(this, moize(buildReducer));
/** @internal */
_Updux_memoBuildSelectors.set(this, moize(buildSelectors));
/** @internal */
_Updux_memoBuildEffects.set(this, moize(buildEffects));
/** @internal */
_Updux_memoBuildReactions.set(this, moize(buildReactions));
/** @internal */
_Updux_actions.set(this, {});
/** @internal */
_Updux_defaultMutation.set(this, void 0);
/** @internal */
_Updux_effects.set(this, []);
/** @internal */
_Updux_reactions.set(this, []);
/** @internal */
_Updux_mutations.set(this, []);
/** @internal */
_Updux_inheritedReducer.set(this, void 0);
/** @internal */
_Updux_selectors.set(this, {});
if (duxConfig.subduxes)
__classPrivateFieldSet(this, _Updux_subduxes, D.map(duxConfig.subduxes, (s) => s instanceof Updux ? s : new Updux(s)), "f");
__classPrivateFieldSet(this, _Updux_inheritedReducer, duxConfig.reducer, "f");
__classPrivateFieldSet(this, _Updux_effects, (_a = duxConfig.effects) !== null && _a !== void 0 ? _a : [], "f");
__classPrivateFieldSet(this, _Updux_reactions, (_b = duxConfig.reactions) !== null && _b !== void 0 ? _b : [], "f");
__classPrivateFieldSet(this, _Updux_actions, buildActions(duxConfig.actions, __classPrivateFieldGet(this, _Updux_subduxes, "f")), "f");
__classPrivateFieldSet(this, _Updux_selectors, (_c = duxConfig.selectors) !== null && _c !== void 0 ? _c : {}, "f");
}
/**
* @description Initial state of the dux.
*/
get initialState() {
return __classPrivateFieldGet(this, _Updux_memoInitialState, "f").call(this, this.duxConfig.initialState, __classPrivateFieldGet(this, _Updux_subduxes, "f"));
}
get subduxes() {
return __classPrivateFieldGet(this, _Updux_subduxes, "f");
}
get actions() {
return __classPrivateFieldGet(this, _Updux_actions, "f");
}
createStore(options = {}) {
const preloadedState = options.preloadedState;
const effects = buildEffectsMiddleware(this.effects, this.actions, this.selectors);
let middleware = (gdm) => gdm().concat(effects);
const store = configureStore({
preloadedState,
reducer: this.reducer,
middleware,
});
store.dispatch = augmentDispatch(store.dispatch, this.actions);
store.getState = augmentGetState(store.getState, this.selectors);
store.actions = this.actions;
store.selectors = this.selectors;
for (const reaction of this.reactions) {
let unsub;
const r = reaction(store);
unsub = store.subscribe(() => r(unsub));
}
return store;
}
addAction(action) {
const { type } = action;
if (!__classPrivateFieldGet(this, _Updux_actions, "f")[type]) {
__classPrivateFieldSet(this, _Updux_actions, Object.assign(Object.assign({}, __classPrivateFieldGet(this, _Updux_actions, "f")), { [type]: action }), "f");
return;
}
if (__classPrivateFieldGet(this, _Updux_actions, "f")[type] !== action)
throw new Error(`redefining action ${type}`);
}
addSelector(name, selector) {
__classPrivateFieldSet(this, _Updux_selectors, Object.assign(Object.assign({}, __classPrivateFieldGet(this, _Updux_selectors, "f")), { [name]: selector }), "f");
return this;
}
addMutation(actionCreator, mutation, terminal = false) {
var _a;
if (typeof actionCreator === 'string') {
if (!this.actions[actionCreator])
throw new Error(`action ${actionCreator} not found`);
actionCreator = this.actions[actionCreator];
}
else {
this.addAction(actionCreator);
}
__classPrivateFieldSet(this, _Updux_mutations, __classPrivateFieldGet(this, _Updux_mutations, "f").concat({
terminal,
matcher: (_a = actionCreator.match) !== null && _a !== void 0 ? _a : actionCreator,
mutation,
}), "f");
return this;
}
setDefaultMutation(mutation, terminal = false) {
__classPrivateFieldSet(this, _Updux_defaultMutation, { terminal, mutation }, "f");
return this;
}
get reducer() {
return __classPrivateFieldGet(this, _Updux_memoBuildReducer, "f").call(this, this.initialState, __classPrivateFieldGet(this, _Updux_mutations, "f"), __classPrivateFieldGet(this, _Updux_defaultMutation, "f"), __classPrivateFieldGet(this, _Updux_subduxes, "f"), __classPrivateFieldGet(this, _Updux_inheritedReducer, "f"));
}
get selectors() {
return __classPrivateFieldGet(this, _Updux_memoBuildSelectors, "f").call(this, __classPrivateFieldGet(this, _Updux_selectors, "f"), __classPrivateFieldGet(this, _Updux_subduxes, "f"));
}
addEffect(...args) {
let effect;
if (args.length === 1) {
effect = args[0];
}
else {
let [actionCreator, originalEffect] = args;
if (typeof actionCreator === 'string') {
if (this.actions[actionCreator]) {
actionCreator = this.actions[actionCreator];
}
else {
throw new Error(`action '${actionCreator}' is unknown`);
}
}
if (!this.actions[actionCreator.type])
this.addAction(actionCreator);
const test = actionCreator.hasOwnProperty('match')
? actionCreator.match
: actionCreator;
effect = (api) => (next) => {
const e = originalEffect(api)(next);
return (action) => {
const func = test(action) ? e : next;
return func(action);
};
};
}
__classPrivateFieldSet(this, _Updux_effects, __classPrivateFieldGet(this, _Updux_effects, "f").concat(effect), "f");
return this;
}
get effects() {
return __classPrivateFieldGet(this, _Updux_memoBuildEffects, "f").call(this, __classPrivateFieldGet(this, _Updux_effects, "f"), __classPrivateFieldGet(this, _Updux_subduxes, "f"));
}
get upreducer() {
return (action) => (state) => this.reducer(state, action);
}
addReaction(reaction) {
let previous;
const memoized = (api) => {
api = augmentMiddlewareApi(api, this.actions, this.selectors);
const r = reaction(api);
const rMemoized = (localState, unsub) => {
let p = previous;
previous = localState;
r(localState, p, unsub);
};
return (unsub) => rMemoized(api.getState(), unsub);
};
__classPrivateFieldSet(this, _Updux_reactions, __classPrivateFieldGet(this, _Updux_reactions, "f").concat(memoized), "f");
return this;
}
get reactions() {
return __classPrivateFieldGet(this, _Updux_memoBuildReactions, "f").call(this, __classPrivateFieldGet(this, _Updux_reactions, "f"), __classPrivateFieldGet(this, _Updux_subduxes, "f"));
}
get defaultMutation() {
return __classPrivateFieldGet(this, _Updux_defaultMutation, "f");
}
/**
* @description Returns an object holding the Updux reducer and all its
* paraphernalia.
*/
get asDux() {
return {
initialState: this.initialState,
actions: this.actions,
effects: this.effects,
reactions: this.reactions,
reducer: this.reducer,
};
}
}
_Updux_subduxes = new WeakMap(), _Updux_memoInitialState = new WeakMap(), _Updux_memoBuildReducer = new WeakMap(), _Updux_memoBuildSelectors = new WeakMap(), _Updux_memoBuildEffects = new WeakMap(), _Updux_memoBuildReactions = new WeakMap(), _Updux_actions = new WeakMap(), _Updux_defaultMutation = new WeakMap(), _Updux_effects = new WeakMap(), _Updux_reactions = new WeakMap(), _Updux_mutations = new WeakMap(), _Updux_inheritedReducer = new WeakMap(), _Updux_selectors = new WeakMap();