add immer to the mix
This commit is contained in:
parent
5ff07b2e2f
commit
bb0bc14873
10
src/Updux.ts
10
src/Updux.ts
@ -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,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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}'`,
|
||||||
);
|
);
|
||||||
|
@ -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) {
|
||||||
|
Loading…
Reference in New Issue
Block a user