|
| 1 | +import React from 'react'; |
| 2 | +import RPT from 'prop-types'; |
| 3 | + |
| 4 | +export interface LoadScriptProps { |
| 5 | + attributes: Partial<HTMLScriptElement>; |
| 6 | + onCreate: () => void; |
| 7 | + onError: () => void; |
| 8 | + onLoad: () => void; |
| 9 | + url: string; |
| 10 | +} |
| 11 | + |
| 12 | +class LoadScript extends React.Component<LoadScriptProps> { |
| 13 | + static propTypes = { |
| 14 | + attributes: RPT.object, |
| 15 | + onCreate: RPT.func, |
| 16 | + onError: RPT.func.isRequired, |
| 17 | + onLoad: RPT.func.isRequired, |
| 18 | + url: RPT.string.isRequired, |
| 19 | + }; |
| 20 | + |
| 21 | + static defaultProps: Partial<LoadScriptProps> = { |
| 22 | + onCreate: () => {}, |
| 23 | + onError: () => {}, |
| 24 | + onLoad: () => {}, |
| 25 | + }; |
| 26 | + |
| 27 | + static scriptObservers: { |
| 28 | + [url: string]: { |
| 29 | + [scriptLoaderId: string]: LoadScriptProps; |
| 30 | + }; |
| 31 | + } = {}; |
| 32 | + |
| 33 | + static loadedScripts: Record<string, boolean> = {}; |
| 34 | + |
| 35 | + static erroredScripts: Record<string, boolean> = {}; |
| 36 | + |
| 37 | + static idCount = 0; |
| 38 | + |
| 39 | + scriptLoaderId: string = ''; |
| 40 | + |
| 41 | + constructor(props: LoadScriptProps) { |
| 42 | + super(props); |
| 43 | + this.scriptLoaderId = `id${LoadScript.idCount++}`; // eslint-disable-line space-unary-ops, no-plusplus |
| 44 | + } |
| 45 | + |
| 46 | + componentDidMount() { |
| 47 | + const { onError, onLoad, url } = this.props; |
| 48 | + |
| 49 | + if (LoadScript.loadedScripts[url]) { |
| 50 | + onLoad(); |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + if (LoadScript.erroredScripts[url]) { |
| 55 | + onError(); |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + if (LoadScript.scriptObservers[url]) { |
| 60 | + LoadScript.scriptObservers[url][this.scriptLoaderId] = this.props; |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + LoadScript.scriptObservers[url] = { |
| 65 | + [this.scriptLoaderId]: this.props, |
| 66 | + }; |
| 67 | + |
| 68 | + this.createScript(); |
| 69 | + } |
| 70 | + |
| 71 | + componentWillUnmount() { |
| 72 | + const { url } = this.props; |
| 73 | + const observers = LoadScript.scriptObservers[url]; |
| 74 | + |
| 75 | + if (observers) { |
| 76 | + delete observers[this.scriptLoaderId]; |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + createScript() { |
| 81 | + const { onCreate, url, attributes } = this.props; |
| 82 | + const script = document.createElement('script'); |
| 83 | + |
| 84 | + onCreate(); |
| 85 | + |
| 86 | + if (attributes) { |
| 87 | + Object.keys(attributes).forEach((prop) => { |
| 88 | + script.setAttribute(prop, attributes[prop as keyof HTMLScriptElement] as string); |
| 89 | + }); |
| 90 | + } |
| 91 | + |
| 92 | + script.src = url; |
| 93 | + |
| 94 | + if (!script.hasAttribute('async')) { |
| 95 | + script.async = true; |
| 96 | + } |
| 97 | + |
| 98 | + const callObserverFuncAndRemoveObserver = ( |
| 99 | + shouldRemoveObserver: (observer: LoadScriptProps) => boolean, |
| 100 | + ) => { |
| 101 | + const observers = LoadScript.scriptObservers[url]; |
| 102 | + Object.keys(observers).forEach((key) => { |
| 103 | + if (shouldRemoveObserver(observers[key])) { |
| 104 | + delete LoadScript.scriptObservers[url][this.scriptLoaderId]; |
| 105 | + } |
| 106 | + }); |
| 107 | + }; |
| 108 | + script.onload = () => { |
| 109 | + LoadScript.loadedScripts[url] = true; |
| 110 | + callObserverFuncAndRemoveObserver((observer) => { |
| 111 | + observer.onLoad(); |
| 112 | + return true; |
| 113 | + }); |
| 114 | + }; |
| 115 | + |
| 116 | + script.onerror = () => { |
| 117 | + LoadScript.erroredScripts[url] = true; |
| 118 | + callObserverFuncAndRemoveObserver((observer) => { |
| 119 | + observer.onError(); |
| 120 | + return true; |
| 121 | + }); |
| 122 | + }; |
| 123 | + |
| 124 | + document.body.appendChild(script); |
| 125 | + } |
| 126 | + |
| 127 | + render() { |
| 128 | + return null; |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +export default LoadScript; |
0 commit comments