@zuze/modifiable-effects
v1.0.1
Published
Simple side effects helper for @zuze/modifiable
Downloads
7
Readme
modifiable-effects
Side Effects
Simple side-effects implementation for a modifiable.
Given a modifiable
, an effect
is just a plain modifier that returns the identity
function, so you can do this:
import { effect } from '@zuze/modiable-effects',
import { modifiable } from '@zuze/modifiable';
const myEffect1 = async (context,setContext,getState) => { ... }
const myEffect2 = async (context,setContext,getState) => { ... }
const m = modifiable(initialState,{
modifiers:[
effect(myEffect1),
[ effect(myEffect2), 'dep1', 'dep2', ({dep3}) => dep3[0] ],
...otherModifiers
]
});
Or you can use the default export function that accepts a
import withEffects from '@zuze/modifiable-effects';
import { modifiable } from '@zuze/modifiable';
const m = modifiable(...);
const myEffect = async => (context,setContext,getState) => {
try {
await someAsyncCall();
setContext({...})
} catch(err) {
setContext({...})
}
};
const myEffect2 = async => ...
const removeEffect = withEffects(modifiable, myEffect, myEffect2, ...otherEffects);
// returns a function that accepts a function reference and removes a side effect
removeEffect(myEffect);
With Dependencies
import withEffects from '@zuze/modifiable-effects';
import { modifiable } from '@zuze/modifiable';
const m = modifiable(...);
const myEffect = [async (context,setContext,getState) => {
try {
await someAsyncCall();
setContext({...})
} catch(err) {
setContext({...})
}
// this effect will run only when context changes
}, ['dep1', 'dep2', ({dep3}) => dep3[0] ]
withEffects(modifiable,myEffect);