Simple middleware to dispatch actions periodically
$ npm i --save redux-timer-middleware
import timerMiddleware from 'redux-timer-middleware'
applyMiddleware(timerMiddleware)(createStore)
You need to dispatch action START_TIMER and provide in payload:
- actionName (required) - action that will be dispatched each timer tick
- timerName (required) - timer name (need for stop feature)
- actionPayload - payload that will be provided to the actionName
- timerInterval - interval in ms (default 1000)
- timerPeriod - tick count after which timer ends, when timer ends - action name with _END will be dispatched
You need to dispatch action STOP_TIMER and provide in payload:
- timerName (required) - timer name that we stop
- timerNames [] - timer names in array if we need to stop multiple timers
Infinite timer:
import {START_TIMER} from 'redux-timer-middleware';
dispatch({
type: START_TIMER,
payload: {
actionName: 'SOME_ACTION_TICK',
timerName: 'infiniteTimer'
}
});
With a payload:
import {START_TIMER} from 'redux-timer-middleware';
dispatch({
type: START_TIMER,
payload: {
actionName: 'SOME_ACTION_TICK',
actionPayload: { /* my cool payload */ },
timerName: 'infiniteTimer'
}
});
To stop this timer:
import {STOP_TIMER} from 'redux-timer-middleware';
dispatch({
type: STOP_TIMER,
payload: {
timerName: 'infiniteTimer'
}
});
Timer that ends after 10 seconds:
import {START_TIMER} from 'redux-timer-middleware';
dispatch({
type: START_TIMER,
payload: {
actionName: 'SOME_ACTION_TICK',
timerName: 'testTimer',
timerPeriod: 10
}
});
After timer ends action with type 'SOME_ACTION_TICK_END' will be dispatched