Skip to content

Timer

The timer task handler allows creating simple timers.

Task: Default

This task describes a timer and the action that should be dispatched at the specified interval.

{
  type: 'timer',
  interval: number, // time in ms at which the tick action should be dispatched
  actions: {
    tick: string    // action type to dispatch when the number has been generated
  }
}

Tip

To stop a timer, simply remove the task (see how in the example).

Actions

Event Type Meta Payload
tick None None

Example

Run this example »

import {
  addTask, delTasks, reduceReducers, taskReducer
} from 'redux-agent'

const reducer = (state, action) => {
  switch (action.type) {
    case 'START_TIMER':
      return addTask(state, {
        type: 'timer',
        interval: 500,
        actions: {
          tick: 'TICK'
        }
      })

    case 'TICK':
      return {
        ...state,
        counter: state.counter + 1
      }

    case 'STOP_ALL_TIMERS':
      return delTasks(state,
        (t) => t.type === 'timer')

    default:
      return state
  }
}

export default reduceReducers(reducer, taskReducer)