subdux mutations are go
This commit is contained in:
parent
769b70dfce
commit
1df6ac5329
@ -1,45 +0,0 @@
|
|||||||
import u from 'updeep';
|
|
||||||
import { mapValues } from 'lodash-es';
|
|
||||||
|
|
||||||
export function buildUpreducer(
|
|
||||||
initial,
|
|
||||||
mutations,
|
|
||||||
subduxes = {},
|
|
||||||
wrapper = undefined,
|
|
||||||
) {
|
|
||||||
const subReducers =
|
|
||||||
Object.keys(subduxes).length > 0
|
|
||||||
? mapValues(subduxes, ({ upreducer }) => upreducer)
|
|
||||||
: null;
|
|
||||||
|
|
||||||
const upreducer = (action) => (state) => {
|
|
||||||
if (!action?.type)
|
|
||||||
throw new Error('upreducer called with a bad action');
|
|
||||||
|
|
||||||
let newState = state ?? initial;
|
|
||||||
|
|
||||||
if (subReducers) {
|
|
||||||
if (subduxes['*']) {
|
|
||||||
newState = u.updateIn(
|
|
||||||
'*',
|
|
||||||
subduxes['*'].upreducer(action),
|
|
||||||
newState,
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
const update = mapValues(subReducers, (upReducer) =>
|
|
||||||
upReducer(action),
|
|
||||||
);
|
|
||||||
|
|
||||||
newState = u(update, newState);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const a = mutations[action.type] || mutations['+'];
|
|
||||||
|
|
||||||
if (!a) return newState;
|
|
||||||
|
|
||||||
return a(action.payload, action)(newState);
|
|
||||||
};
|
|
||||||
|
|
||||||
return wrapper ? wrapper(upreducer) : upreducer;
|
|
||||||
}
|
|
@ -1,32 +0,0 @@
|
|||||||
import schema from 'json-schema-shorthand';
|
|
||||||
import u from 'updeep';
|
|
||||||
|
|
||||||
import { action } from './actions.js';
|
|
||||||
|
|
||||||
import { Updux, dux } from './Updux.js';
|
|
||||||
|
|
||||||
|
|
||||||
test('strings and generators', async () => {
|
|
||||||
const actionA = action('a');
|
|
||||||
|
|
||||||
const foo = dux({
|
|
||||||
actions: {
|
|
||||||
b: null,
|
|
||||||
a: actionA,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
// as a string and defined
|
|
||||||
expect(() => foo.setMutation('a', () => {})).not.toThrow();
|
|
||||||
|
|
||||||
// as a generator and defined
|
|
||||||
expect(() => foo.setMutation(actionA, () => {})).not.toThrow();
|
|
||||||
|
|
||||||
// as a string, not defined
|
|
||||||
expect(() => foo.setMutation('c', () => {})).toThrow();
|
|
||||||
|
|
||||||
foo.setMutation(action('d'), () => {});
|
|
||||||
|
|
||||||
expect(foo.actions.d).toBeTypeOf('function');
|
|
||||||
});
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user