Skip to content

Instantly share code, notes, and snippets.

@RafalFilipek
Created October 23, 2015 15:31
Show Gist options
  • Save RafalFilipek/d0980980de71aaeeef49 to your computer and use it in GitHub Desktop.
Save RafalFilipek/d0980980de71aaeeef49 to your computer and use it in GitHub Desktop.
import { createAction } from 'redux-actions'
/**
This will store timeoutId
*/
export const DELAY_FUNCTION_INIT = 'DELAY_FUNCTION_INIT'
export const initDelay = createAction(DELAY_FUNCTION_INIT)
/**
This clear store timeoutId
*/
export const DELAY_FUNCTION_CLEAR = 'DELAY_FUNCTION_CLEAR'
export const clearDelay = createAction(DELAY_FUNCTION_CLEAR)
export const clear = (key) => {
clearTimeout(key)
return clearDelay({key})
}
export const delay = (key, fnc, seconds = 0) => {
return (dispatch) => {
const id = setTimeout(() => {
dispatch(clear(key))
dispatch(fnc())
}, seconds)
return dispatch(initDelay({key, id}))
}
}
//later in other action...
dispatch(delay('fetchStream', fetchStream, 2000))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment