UNPKG

plotly-icons

Version:
365 lines (292 loc) 9.94 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.SameLoopPromise = undefined; var _from = require('babel-runtime/core-js/array/from'); var _from2 = _interopRequireDefault(_from); var _keys = require('babel-runtime/core-js/object/keys'); var _keys2 = _interopRequireDefault(_keys); var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of'); var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf); var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck'); var _classCallCheck3 = _interopRequireDefault(_classCallCheck2); var _createClass2 = require('babel-runtime/helpers/createClass'); var _createClass3 = _interopRequireDefault(_createClass2); var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn'); var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2); var _inherits2 = require('babel-runtime/helpers/inherits'); var _inherits3 = _interopRequireDefault(_inherits2); var _set = require('babel-runtime/core-js/set'); var _set2 = _interopRequireDefault(_set); exports.default = dynamicComponent; exports.registerChunk = registerChunk; exports.flushChunks = flushChunks; var _react = require('react'); var _react2 = _interopRequireDefault(_react); var _utils = require('./utils'); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } var currentChunks = new _set2.default(); function dynamicComponent(p, o) { var promise = void 0; var options = void 0; if (p instanceof SameLoopPromise) { promise = p; options = o || {}; } else { // Now we are trying to use the modules and render fields in options to load modules. if (!p.modules || !p.render) { var errorMessage = '`next/dynamic` options should contain `modules` and `render` fields'; throw new Error(errorMessage); } if (o) { var _errorMessage = 'Add additional `next/dynamic` options to the first argument containing the `modules` and `render` fields'; throw new Error(_errorMessage); } options = p; } return function (_React$Component) { (0, _inherits3.default)(DynamicComponent, _React$Component); function DynamicComponent() { var _ref; (0, _classCallCheck3.default)(this, DynamicComponent); for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } var _this = (0, _possibleConstructorReturn3.default)(this, (_ref = DynamicComponent.__proto__ || (0, _getPrototypeOf2.default)(DynamicComponent)).call.apply(_ref, [this].concat(args))); _this.LoadingComponent = options.loading ? options.loading : function () { return _react2.default.createElement( 'p', null, 'loading...' ); }; _this.ssr = options.ssr === false ? options.ssr : true; _this.state = { AsyncComponent: null, asyncElement: null }; _this.isServer = typeof window === 'undefined'; // This flag is used to load the bundle again, if needed _this.loadBundleAgain = null; // This flag keeps track of the whether we are loading a bundle or not. _this.loadingBundle = false; if (_this.ssr) { _this.load(); } return _this; } (0, _createClass3.default)(DynamicComponent, [{ key: 'load', value: function load() { if (promise) { this.loadComponent(); } else { this.loadBundle(this.props); } } }, { key: 'loadComponent', value: function loadComponent() { var _this2 = this; promise.then(function (m) { var AsyncComponent = m.default || m; // Set a readable displayName for the wrapper component var asyncCompName = (0, _utils.getDisplayName)(AsyncComponent); if (asyncCompName) { DynamicComponent.displayName = 'DynamicComponent for ' + asyncCompName; } if (_this2.mounted) { _this2.setState({ AsyncComponent: AsyncComponent }); } else { if (_this2.isServer) { registerChunk(m.__webpackChunkName); } _this2.state.AsyncComponent = AsyncComponent; } }); } }, { key: 'loadBundle', value: function loadBundle(props) { var _this3 = this; this.loadBundleAgain = null; this.loadingBundle = true; // Run this for prop changes as well. var modulePromiseMap = options.modules(props); var moduleNames = (0, _keys2.default)(modulePromiseMap); var remainingPromises = moduleNames.length; var moduleMap = {}; var renderModules = function renderModules() { if (_this3.loadBundleAgain) { _this3.loadBundle(_this3.loadBundleAgain); return; } _this3.loadingBundle = false; DynamicComponent.displayName = 'DynamicBundle'; var asyncElement = options.render(props, moduleMap); if (_this3.mounted) { _this3.setState({ asyncElement: asyncElement }); } else { _this3.state.asyncElement = asyncElement; } }; var loadModule = function loadModule(name) { var promise = modulePromiseMap[name]; promise.then(function (m) { var Component = m.default || m; if (_this3.isServer) { registerChunk(m.__webpackChunkName); } moduleMap[name] = Component; remainingPromises--; if (remainingPromises === 0) { renderModules(); } }); }; moduleNames.forEach(loadModule); } }, { key: 'componentDidMount', value: function componentDidMount() { this.mounted = true; if (!this.ssr) { this.load(); } } }, { key: 'componentWillReceiveProps', value: function componentWillReceiveProps(nextProps) { if (promise) return; this.setState({ asyncElement: null }); if (this.loadingBundle) { this.loadBundleAgain = nextProps; return; } this.loadBundle(nextProps); } }, { key: 'render', value: function render() { var _state = this.state, AsyncComponent = _state.AsyncComponent, asyncElement = _state.asyncElement; var LoadingComponent = this.LoadingComponent; if (asyncElement) return asyncElement; if (AsyncComponent) return _react2.default.createElement(AsyncComponent, this.props); return _react2.default.createElement(LoadingComponent, this.props); } }]); return DynamicComponent; }(_react2.default.Component); } function registerChunk(chunk) { currentChunks.add(chunk); } function flushChunks() { var chunks = (0, _from2.default)(currentChunks); currentChunks.clear(); return chunks; } var SameLoopPromise = exports.SameLoopPromise = function () { (0, _createClass3.default)(SameLoopPromise, null, [{ key: 'resolve', value: function resolve(value) { var promise = new SameLoopPromise(function (done) { return done(value); }); return promise; } }]); function SameLoopPromise(cb) { (0, _classCallCheck3.default)(this, SameLoopPromise); this.onResultCallbacks = []; this.onErrorCallbacks = []; this.cb = cb; } (0, _createClass3.default)(SameLoopPromise, [{ key: 'setResult', value: function setResult(result) { this.gotResult = true; this.result = result; this.onResultCallbacks.forEach(function (cb) { return cb(result); }); this.onResultCallbacks = []; } }, { key: 'setError', value: function setError(error) { this.gotError = true; this.error = error; this.onErrorCallbacks.forEach(function (cb) { return cb(error); }); this.onErrorCallbacks = []; } }, { key: 'then', value: function then(onResult, onError) { var _this4 = this; this.runIfNeeded(); var promise = new SameLoopPromise(); var handleError = function handleError() { if (onError) { promise.setResult(onError(_this4.error)); } else { promise.setError(_this4.error); } }; var handleResult = function handleResult() { promise.setResult(onResult(_this4.result)); }; if (this.gotResult) { handleResult(); return promise; } if (this.gotError) { handleError(); return promise; } this.onResultCallbacks.push(handleResult); this.onErrorCallbacks.push(handleError); return promise; } }, { key: 'catch', value: function _catch(onError) { var _this5 = this; this.runIfNeeded(); var promise = new SameLoopPromise(); var handleError = function handleError() { promise.setResult(onError(_this5.error)); }; var handleResult = function handleResult() { promise.setResult(_this5.result); }; if (this.gotResult) { handleResult(); return promise; } if (this.gotError) { handleError(); return promise; } this.onErrorCallbacks.push(handleError); this.onResultCallbacks.push(handleResult); return promise; } }, { key: 'runIfNeeded', value: function runIfNeeded() { var _this6 = this; if (!this.cb) return; if (this.ran) return; this.ran = true; this.cb(function (result) { return _this6.setResult(result); }, function (error) { return _this6.setError(error); }); } }]); return SameLoopPromise; }();