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": {
|
||||||
|
@ -107,8 +107,8 @@ export class Updux {
|
|||||||
* updux if not already present (the idea being that making a typo on a string
|
* updux if not already present (the idea being that making a typo on a string
|
||||||
* is easy, but passing a wrong function very more unlikely).
|
* is easy, but passing a wrong function very more unlikely).
|
||||||
* @param {Function} mutation - Mutating function.
|
* @param {Function} mutation - Mutating function.
|
||||||
* @param {bool} terminal - If true, subduxes' mutations won't be invoked on
|
* @param {bool} terminal - If true, subduxes' mutations won't be invoked on
|
||||||
* the action.
|
* the action.
|
||||||
* @return {void}
|
* @return {void}
|
||||||
*/
|
*/
|
||||||
setMutation(action, mutation, terminal = false) {
|
setMutation(action, mutation, terminal = false) {
|
||||||
@ -126,11 +126,11 @@ 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`);
|
||||||
}
|
}
|
||||||
|
|
||||||
if( terminal ) {
|
if (terminal) {
|
||||||
const originalMutation = mutation;
|
const originalMutation = mutation;
|
||||||
mutation = (...args) => originalMutation(...args);
|
mutation = (...args) => originalMutation(...args);
|
||||||
mutation.terminal = true;
|
mutation.terminal = true;
|
||||||
|
@ -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) => {
|
||||||
@ -28,7 +32,7 @@ const subMutations = (subduxes) => (action) => (state) => {
|
|||||||
|
|
||||||
export function buildUpreducer(mutations, subduxes) {
|
export function buildUpreducer(mutations, subduxes) {
|
||||||
return (action) => (state) => {
|
return (action) => (state) => {
|
||||||
if( ! mutations[action.type]?.terminal )
|
if (!mutations[action.type]?.terminal)
|
||||||
state = subMutations(subduxes)(action)(state);
|
state = subMutations(subduxes)(action)(state);
|
||||||
|
|
||||||
return localMutation(mutations)(action)(state);
|
return localMutation(mutations)(action)(state);
|
||||||
|
Loading…
Reference in New Issue
Block a user