-
Notifications
You must be signed in to change notification settings - Fork 29
Hoc pattern, test fixes and clean-ups #34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
341ccc7
7b4a6b8
e4b6a95
5287917
e1744a0
a7b264d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,23 @@ | ||
import React from "react"; | ||
import { Component, createElement } from "react"; | ||
import PropTypes from "prop-types"; | ||
|
||
let SCRIPT_MAP = {}; | ||
|
||
// A counter used to generate a unique id for each component that uses the function | ||
let idCount = 0; | ||
|
||
export default function makeAsyncScript(Component, getScriptURL, options) { | ||
export default function makeAsyncScript(getScriptURL, options) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The below diff looks intense, but mostly just indentation due to HOC 😢 biggest changes are:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can remove the whitespace from the diff => https://github.com/dozoisch/react-async-script/pull/34/files?w=1 . w=1 makes it easier to review in this case :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ^ that's awesome... i need to remember that for all time.. lol |
||
options = options || {}; | ||
return function wrapWithAsyncScript(WrappedComponent) { | ||
const wrappedComponentName = | ||
Component.displayName || Component.name || "Component"; | ||
WrappedComponent.displayName || WrappedComponent.name || "Component"; | ||
|
||
class AsyncScriptLoader extends React.Component { | ||
constructor() { | ||
super(); | ||
class AsyncScriptLoader extends Component { | ||
constructor(props, context) { | ||
super(props, context) | ||
this.state = {}; | ||
this.__scriptURL = ""; | ||
this.assignChildComponent = this.assignChildComponent.bind(this); | ||
} | ||
|
||
asyncScriptLoaderGetScriptLoaderID() { | ||
|
@@ -31,12 +33,19 @@ export default function makeAsyncScript(Component, getScriptURL, options) { | |
return this.__scriptURL; | ||
} | ||
|
||
assignChildComponent(ref) { | ||
this.__childComponent = ref; | ||
} | ||
|
||
getComponent() { | ||
return this.__childComponent; | ||
} | ||
|
||
asyncScriptLoaderHandleLoad(state) { | ||
this.setState(state, this.props.asyncScriptOnLoad); | ||
// use reacts setState callback to fire props.asyncScriptOnLoad with new state/entry | ||
this.setState(state, | ||
() => this.props.asyncScriptOnLoad && this.props.asyncScriptOnLoad(this.state) | ||
); | ||
} | ||
|
||
asyncScriptLoaderTriggerOnScriptLoaded() { | ||
|
@@ -54,20 +63,30 @@ export default function makeAsyncScript(Component, getScriptURL, options) { | |
const scriptURL = this.setupScriptURL(); | ||
const key = this.asyncScriptLoaderGetScriptLoaderID(); | ||
const { globalName, callbackName } = options; | ||
|
||
// check if global object already attached to window | ||
if (globalName && typeof window[globalName] !== "undefined") { | ||
SCRIPT_MAP[scriptURL] = { loaded: true, observers: {} }; | ||
} | ||
|
||
// check if script loading already | ||
if (SCRIPT_MAP[scriptURL]) { | ||
let entry = SCRIPT_MAP[scriptURL]; | ||
// if loaded or errored then "finish" | ||
if (entry && (entry.loaded || entry.errored)) { | ||
this.asyncScriptLoaderHandleLoad(entry); | ||
return; | ||
} | ||
// if still loading then callback to observer queue | ||
entry.observers[key] = entry => this.asyncScriptLoaderHandleLoad(entry); | ||
return; | ||
} | ||
|
||
/* | ||
* hasn't started loading | ||
* start the "magic" | ||
* setup script to load and observers | ||
*/ | ||
let observers = {}; | ||
observers[key] = entry => this.asyncScriptLoaderHandleLoad(entry); | ||
SCRIPT_MAP[scriptURL] = { | ||
|
@@ -122,19 +141,6 @@ export default function makeAsyncScript(Component, getScriptURL, options) { | |
} | ||
}; | ||
|
||
// (old) MSIE browsers may call "onreadystatechange" instead of "onload" | ||
script.onreadystatechange = () => { | ||
if (this.readyState === "loaded") { | ||
// wait for other events, then call onload if default onload hadn't been called | ||
window.setTimeout(() => { | ||
const mapEntry = SCRIPT_MAP[scriptURL]; | ||
if (mapEntry && mapEntry.loaded !== true) { | ||
script.onload(); | ||
} | ||
}, 0); | ||
} | ||
}; | ||
|
||
document.body.appendChild(script); | ||
} | ||
|
||
|
@@ -163,22 +169,16 @@ export default function makeAsyncScript(Component, getScriptURL, options) { | |
|
||
render() { | ||
const globalName = options.globalName; | ||
// remove asyncScriptOnLoad from childprops | ||
let { asyncScriptOnLoad, ...childProps } = this.props; | ||
// remove asyncScriptOnLoad from childProps | ||
let { asyncScriptOnLoad, ...childProps } = this.props; // eslint-disable-line no-unused-vars | ||
if (globalName && typeof window !== "undefined") { | ||
childProps[globalName] = | ||
typeof window[globalName] !== "undefined" | ||
? window[globalName] | ||
: undefined; | ||
} | ||
return ( | ||
<Component | ||
ref={comp => { | ||
this.__childComponent = comp; | ||
}} | ||
{...childProps} | ||
/> | ||
); | ||
childProps.ref = this.assignChildComponent; | ||
return createElement(WrappedComponent, childProps); | ||
} | ||
} | ||
AsyncScriptLoader.displayName = `AsyncScriptLoader(${wrappedComponentName})`; | ||
|
@@ -195,3 +195,4 @@ export default function makeAsyncScript(Component, getScriptURL, options) { | |
} | ||
return AsyncScriptLoader; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh wow that was an old example X) Didn't realize I never update it to classes.