Created
July 5, 2017 08:45
-
-
Save skayred/697d7ffbc62d2dec7fb36f4bb2ca3f65 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
| private addScriptPromise(src: string) { | |
| let scriptResolve: () => void; | |
| let scriptReject: () => void; | |
| const scriptPromise = new Promise((resolve, reject) => { | |
| scriptResolve = resolve; | |
| scriptReject = reject; | |
| }); | |
| const scriptComponent = ( | |
| <Script | |
| url={src} | |
| onError={() => scriptReject()} | |
| onLoad={() => scriptResolve()} /> | |
| ); | |
| ReactDOM.render( | |
| scriptComponent, | |
| document.getElementById('aux-scripts') as Element, | |
| ); | |
| this.scriptPromises.push(scriptPromise); | |
| } | |
| private executeScriptWhenReady(content: string) { | |
| Promise.all(this.scriptPromises).then(() => { | |
| if (typeof window !== 'undefined') { | |
| window.eval(content); | |
| } | |
| }).catch(() => { | |
| console.warn('Could not load the script from the content!'); | |
| }); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment