Last active
April 17, 2018 01:30
-
-
Save 0xch4z/a42386d0d1a8fdc9a9d34722cc2e27ee to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| /** | |
| * @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