MCPcopy Create free account
hub / github.com/dozoisch/react-async-script / componentDidMount

Method componentDidMount

src/async-script-loader.js:57–149  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

55 }
56
57 componentDidMount() {
58 const scriptURL = this.setupScriptURL();
59 const key = this.asyncScriptLoaderGetScriptLoaderID();
60 const { globalName, callbackName, scriptId } = options;
61
62 // check if global object already attached to window
63 if (globalName && typeof window[globalName] !== "undefined") {
64 SCRIPT_MAP[scriptURL] = { loaded: true, observers: {} };
65 }
66
67 // check if script loading already
68 if (SCRIPT_MAP[scriptURL]) {
69 let entry = SCRIPT_MAP[scriptURL];
70 // if loaded or errored then "finish"
71 if (entry && (entry.loaded || entry.errored)) {
72 this.asyncScriptLoaderHandleLoad(entry);
73 return;
74 }
75 // if still loading then callback to observer queue
76 entry.observers[key] = entry =>
77 this.asyncScriptLoaderHandleLoad(entry);
78 return;
79 }
80
81 /*
82 * hasn't started loading
83 * start the "magic"
84 * setup script to load and observers
85 */
86 let observers = {};
87 observers[key] = entry => this.asyncScriptLoaderHandleLoad(entry);
88 SCRIPT_MAP[scriptURL] = {
89 loaded: false,
90 observers
91 };
92
93 let script = document.createElement("script");
94
95 script.src = scriptURL;
96 script.async = true;
97
98 for (let attribute in options.attributes) {
99 script.setAttribute(attribute, options.attributes[attribute]);
100 }
101
102 if (scriptId) {
103 script.id = scriptId;
104 }
105
106 let callObserverFuncAndRemoveObserver = func => {
107 if (SCRIPT_MAP[scriptURL]) {
108 let mapEntry = SCRIPT_MAP[scriptURL];
109 let observersMap = mapEntry.observers;
110
111 for (let obsKey in observersMap) {
112 if (func(observersMap[obsKey])) {
113 delete observersMap[obsKey];
114 }

Callers

nothing calls this directly

Tested by

no test coverage detected