add immer to the mix

This commit is contained in:
Yanick Champoux 2023-04-21 13:57:51 -04:00
parent 5ff07b2e2f
commit bb0bc14873
3 changed files with 14 additions and 6 deletions

View File

@ -22,6 +22,7 @@ import { buildInitial, AggregateState } from './initial.js';
import { buildReducer, MutationCase } from './reducer.js'; import { buildReducer, MutationCase } from './reducer.js';
import { augmentGetState, augmentMiddlewareApi, buildEffectsMiddleware, EffectMiddleware } from './effects.js'; import { augmentGetState, augmentMiddlewareApi, buildEffectsMiddleware, EffectMiddleware } from './effects.js';
import { ToolkitStore } from '@reduxjs/toolkit/dist/configureStore.js'; import { ToolkitStore } from '@reduxjs/toolkit/dist/configureStore.js';
import prepare from 'immer';
type MyActionCreator = { type: string } & ((...args: any) => any); type MyActionCreator = { type: string } & ((...args: any) => any);
@ -132,7 +133,9 @@ export default class Updux<
Object.entries(this.#subduxes) Object.entries(this.#subduxes)
.filter(([slice, { selectors }]) => selectors) .filter(([slice, { selectors }]) => selectors)
.map(([slice, { selectors }]) => .map(([slice, { selectors }]) =>
R.mapValues(selectors, (s) => (state) => s(state[slice])), R.mapValues(selectors, (s) => (state = {}) => {
return s(state?.[slice])
}),
), ),
); );
@ -218,6 +221,7 @@ export default class Updux<
} }
(store as any).actions = this.actions; (store as any).actions = this.actions;
(store as any).selectors = this.selectors;
return store as ToolkitStore< return store as ToolkitStore<
@ -276,10 +280,12 @@ export default class Updux<
matcher = matcher.match; matcher = matcher.match;
} }
const immerMutation = (...args) => prepare(mutation(...args));
this.#localMutations.push({ this.#localMutations.push({
terminal, terminal,
matcher, matcher,
mutation, mutation: immerMutation,
}); });
} }

View File

@ -21,7 +21,7 @@ export function buildActions(localActions, subduxes) {
if (!subdux) continue; if (!subdux) continue;
for (const a in subdux) { for (const a in subdux) {
if (actions[a]) { if (actions[a] && subduxes[actions[a]].actions[a] !== subdux[a]) {
throw new Error( throw new Error(
`action '${a}' defined both in subduxes '${actions[a]}' and '${slice}'`, `action '${a}' defined both in subduxes '${actions[a]}' and '${slice}'`,
); );

View File

@ -4,6 +4,7 @@ import * as R from 'remeda';
import { Dux } from './types.js'; import { Dux } from './types.js';
import { Mutation } from './Updux.js'; import { Mutation } from './Updux.js';
import u from '@yanick/updeep-remeda'; import u from '@yanick/updeep-remeda';
import prepare from 'immer';
export type MutationCase = { export type MutationCase = {
matcher: (action: Action) => boolean; matcher: (action: Action) => boolean;
@ -35,9 +36,10 @@ export function buildReducer(
.forEach(({ mutation, terminal: t }) => { .forEach(({ mutation, terminal: t }) => {
if (t) terminal = true; if (t) terminal = true;
didSomething = true; didSomething = true;
// state = prepare(
// TODO wrap mutations in immer state,
state = mutation((action as any).payload, action)(state); mutation((action as any).payload, action),
);
}); });
if (!didSomething && defaultMutation) { if (!didSomething && defaultMutation) {