add catch-all effect

This commit is contained in:
Yanick Champoux 2022-08-29 10:57:59 -04:00
parent e3dce45f50
commit 7583f9e98b
2 changed files with 34 additions and 1 deletions

View File

@ -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' ]);
} )

View File

@ -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);
});