add catch-all effect
This commit is contained in:
parent
e3dce45f50
commit
7583f9e98b
@ -1,7 +1,7 @@
|
|||||||
import { test, expect } from 'vitest';
|
import { test, expect } from 'vitest';
|
||||||
|
|
||||||
import u from 'updeep';
|
import u from 'updeep';
|
||||||
import { action, Updux } from '../src/index.js';
|
import { action, Updux, dux } from '../src/index.js';
|
||||||
|
|
||||||
const addTodoWithId = action('addTodoWithId');
|
const addTodoWithId = action('addTodoWithId');
|
||||||
const incNextId = action('incNextId');
|
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' ]);
|
||||||
|
} )
|
||||||
|
@ -123,3 +123,11 @@ const store = todosDux.createStore();
|
|||||||
store.dispatch.addTodo('Do the thing');
|
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);
|
||||||
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user