diff --git a/package.json b/package.json index 5703d48..6f0aeb1 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ "updeep": "^1.2.1" }, "license": "MIT", - "main": "dist/index.js", + "main": "src/index.js", "name": "updux", "description": "Updeep-friendly Redux helper framework", "scripts": { diff --git a/src/Updux.js b/src/Updux.js index dd58c72..939a717 100644 --- a/src/Updux.js +++ b/src/Updux.js @@ -107,8 +107,8 @@ export class Updux { * 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). * @param {Function} mutation - Mutating function. - * @param {bool} terminal - If true, subduxes' mutations won't be invoked on - * the action. + * @param {bool} terminal - If true, subduxes' mutations won't be invoked on + * the action. * @return {void} */ setMutation(action, mutation, terminal = false) { @@ -126,11 +126,11 @@ export class Updux { action = action.type; } - if (!this.#actions[action]) { + if (!this.#actions[action] && action !== '*') { throw new Error(`action '${action}' is not defined`); } - if( terminal ) { + if (terminal) { const originalMutation = mutation; mutation = (...args) => originalMutation(...args); mutation.terminal = true; diff --git a/src/mutations.test.js b/src/mutations.test.js index d1c532a..da6fb13 100644 --- a/src/mutations.test.js +++ b/src/mutations.test.js @@ -71,3 +71,20 @@ test('strings and generators', async () => { 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]); +}); diff --git a/src/upreducer.js b/src/upreducer.js index 85ac320..8e9817f 100644 --- a/src/upreducer.js +++ b/src/upreducer.js @@ -4,9 +4,13 @@ import u from 'updeep'; const localMutation = (mutations) => (action) => (state) => { 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) => { @@ -28,7 +32,7 @@ const subMutations = (subduxes) => (action) => (state) => { export function buildUpreducer(mutations, subduxes) { return (action) => (state) => { - if( ! mutations[action.type]?.terminal ) + if (!mutations[action.type]?.terminal) state = subMutations(subduxes)(action)(state); return localMutation(mutations)(action)(state);