Skip to content

Instantly share code, notes, and snippets.

@0xch4z
Last active April 17, 2018 01:30
Show Gist options
  • Select an option

  • Save 0xch4z/a42386d0d1a8fdc9a9d34722cc2e27ee to your computer and use it in GitHub Desktop.

Select an option

Save 0xch4z/a42386d0d1a8fdc9a9d34722cc2e27ee to your computer and use it in GitHub Desktop.
/**
* @param {Number} maxTries - max tries polling for store
* @return {Promise<Redux.Store>} store
*
* @example
* (async function() {
* const store = await window.getReactReduxStore();
* store.subscribe(() => {
* console.log('store updated =>', store);
* });
* })();
*/
window.getReactReduxStore = (maxTries = 0) =>
new Promise((resolve, reject) => {
const root = document.querySelector('#root > div');
let tryCount = 0;
const interval = setTimeout(() => {
const { store } = ReactDOM.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED
.ReactDOMComponentTree
.getInstanceFromNode(root)
.child
.stateNode
.context;
tryCount++;
if (store) {
clearInterval(interval);
return resolve(store);
}
if (tryCount >= maxTries)
reject(new Error('exceeded max tries'));
}, 50);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment