Created
October 23, 2015 15:31
-
-
Save RafalFilipek/d0980980de71aaeeef49 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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