splat mutation
This commit is contained in:
parent
23724931e9
commit
dd0dda0970
@ -8,7 +8,7 @@
|
|||||||
"updeep": "^1.2.1"
|
"updeep": "^1.2.1"
|
||||||
},
|
},
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"main": "dist/index.js",
|
"main": "src/index.js",
|
||||||
"name": "updux",
|
"name": "updux",
|
||||||
"description": "Updeep-friendly Redux helper framework",
|
"description": "Updeep-friendly Redux helper framework",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
@ -126,7 +126,7 @@ export class Updux {
|
|||||||
action = action.type;
|
action = action.type;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!this.#actions[action]) {
|
if (!this.#actions[action] && action !== '*') {
|
||||||
throw new Error(`action '${action}' is not defined`);
|
throw new Error(`action '${action}' is not defined`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -71,3 +71,20 @@ test('strings and generators', async () => {
|
|||||||
|
|
||||||
expect(foo.actions.d).toBeTypeOf('function');
|
expect(foo.actions.d).toBeTypeOf('function');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('splat mutation', () => {
|
||||||
|
const myDux = new Updux({
|
||||||
|
initial: [],
|
||||||
|
actions: { one: null, two: null },
|
||||||
|
mutations: {
|
||||||
|
'*': (payload) => (state) => payload ? [...state, payload] : state,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
const store = myDux.createStore();
|
||||||
|
expect(store.getState()).toEqual([]);
|
||||||
|
|
||||||
|
store.dispatch.one(11);
|
||||||
|
store.dispatch.two(22);
|
||||||
|
|
||||||
|
expect(store.getState()).toEqual([11, 22]);
|
||||||
|
});
|
||||||
|
@ -4,9 +4,13 @@ import u from 'updeep';
|
|||||||
const localMutation = (mutations) => (action) => (state) => {
|
const localMutation = (mutations) => (action) => (state) => {
|
||||||
const mutation = mutations[action.type];
|
const mutation = mutations[action.type];
|
||||||
|
|
||||||
if (!mutation) return state;
|
const splatMutation = mutations['*'];
|
||||||
|
|
||||||
return mutation(action.payload, action)(state);
|
if (mutation) state = mutation(action.payload, action)(state);
|
||||||
|
|
||||||
|
if (splatMutation) state = splatMutation(action.payload, action)(state);
|
||||||
|
|
||||||
|
return state;
|
||||||
};
|
};
|
||||||
|
|
||||||
const subMutations = (subduxes) => (action) => (state) => {
|
const subMutations = (subduxes) => (action) => (state) => {
|
||||||
|
Loading…
Reference in New Issue
Block a user