From 7583f9e98b0e72bf9e0795b23ae22acf36af3fba Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Mon, 29 Aug 2022 10:57:59 -0400 Subject: [PATCH] add catch-all effect --- docs/tutorial-effects.test.js | 27 ++++++++++++++++++++++++++- docs/tutorial.md | 8 ++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/docs/tutorial-effects.test.js b/docs/tutorial-effects.test.js index de404e8..9ab10d3 100644 --- a/docs/tutorial-effects.test.js +++ b/docs/tutorial-effects.test.js @@ -1,7 +1,7 @@ import { test, expect } from 'vitest'; import u from 'updeep'; -import { action, Updux } from '../src/index.js'; +import { action, Updux, dux } from '../src/index.js'; const addTodoWithId = action('addTodoWithId'); const incNextId = action('incNextId'); @@ -42,3 +42,28 @@ test( "tutorial example", async () => { }) }); + +test( "catch-all effect", () => { + + let seen = []; + + const foo = new Updux({ + actions: { + one: null, + two: null, + }, + effects: { + '*': (api) => next => action => { + seen.push(action.type); + next(action); + } + } + } ); + + const store = foo.createStore(); + + store.dispatch.one(); + store.dispatch.two(); + + expect(seen).toEqual([ 'one', 'two' ]); +} ) diff --git a/docs/tutorial.md b/docs/tutorial.md index aaa6eb7..8315316 100644 --- a/docs/tutorial.md +++ b/docs/tutorial.md @@ -123,3 +123,11 @@ const store = todosDux.createStore(); store.dispatch.addTodo('Do the thing'); ``` +### Catch-all effect + +It is possible to have an effect match all actions via the special `*` token. +``` +todosUpdux.addEffect('*', () => next => action => { + console.log( 'seeing action fly by:', action ); + next(action); +});