UNPKG

react-script-loader-hoc

Version:

A higher-order React component that assists in the asynchronous loading of third party JS libraries (eg. Stripe.js)

218 lines (178 loc) 6.33 kB
'use strict'; function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; } var React = _interopDefault(require('react')); var hoistStatics = _interopDefault(require('hoist-non-react-statics')); function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); } function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } function _possibleConstructorReturn(self, call) { if (call && (typeof call === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); } var cachedScripts = []; var scriptLoader = function scriptLoader() { for (var _len = arguments.length, scriptSrcs = new Array(_len), _key = 0; _key < _len; _key++) { scriptSrcs[_key] = arguments[_key]; } return function (WrappedComponent) { var ScriptLoader = /*#__PURE__*/ function (_React$Component) { _inherits(ScriptLoader, _React$Component); function ScriptLoader() { var _getPrototypeOf2; var _this; _classCallCheck(this, ScriptLoader); for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { args[_key2] = arguments[_key2]; } _this = _possibleConstructorReturn(this, (_getPrototypeOf2 = _getPrototypeOf(ScriptLoader)).call.apply(_getPrototypeOf2, [this].concat(args))); _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "state", { scriptsLoaded: false, scriptsLoadedSuccessfully: false }); _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "_isMounted", false); _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "loadScripts", function (srcs) { var promises = srcs.filter(function (src) { return !cachedScripts.includes(src); }).map(function (src) { return _this.loadScript(src); }); var success = true; Promise.all(promises).catch(function () { success = false; }).then(function () { if (!_this._isMounted) { return; } _this.setState({ scriptsLoaded: true, scriptsLoadedSuccessfully: success }); }); }); _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "loadScript", function (src) { cachedScripts.push(src); var script = document.createElement('script'); script.src = src; script.async = true; var promise = new Promise(function (resolve, reject) { script.addEventListener('load', function () { return resolve(src); }); script.addEventListener('error', function (e) { return reject(e); }); }).catch(function (e) { var index = cachedScripts.indexOf(src); if (index >= 0) cachedScripts.splice(index, 1); script.remove(); throw e; }); document.body.appendChild(script); return promise; }); return _this; } _createClass(ScriptLoader, [{ key: "componentDidMount", value: function componentDidMount() { this._isMounted = true; this.loadScripts(scriptSrcs); } }, { key: "componentWillUnmount", value: function componentWillUnmount() { this._isMounted = false; } }, { key: "render", value: function render() { var props = _objectSpread({}, this.props, this.state); return React.createElement(WrappedComponent, props); } }]); return ScriptLoader; }(React.Component); return hoistStatics(ScriptLoader, WrappedComponent); }; }; module.exports = scriptLoader;