Created
March 7, 2018 19:42
-
-
Save lmorchard/11e8baaadbf41f5616ed8ac69c18c445 to your computer and use it in GitHub Desktop.
Hacky loader that waits until initial Redux store changes settle down
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
const unsubscribeLoader = store.subscribe(() => { | |
if (selectors.loaderDelayExpired(store.getState())) { | |
// State settled down long enough for timer to expire - stop listening. | |
unsubscribeLoader(); | |
} else { | |
// Reset the timer again. | |
startLoaderDelay(); | |
} | |
}); | |
// Utility to (re)start up a timer to dismiss the loading indicator | |
let loaderTimer = null; | |
function startLoaderDelay() { | |
if (loaderTimer) { | |
clearTimeout(loaderTimer); | |
} | |
loaderTimer = setTimeout( | |
() => store.dispatch(actions.ui.setLoaderDelayExpired(true)), | |
LOADER_DELAY_PERIOD | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment