Skip to content

Commit

Permalink
Reverts #129 and fixes flow
Browse files Browse the repository at this point in the history
  • Loading branch information
tajo committed Aug 9, 2018
1 parent 8992fd5 commit 8865dbe
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 56 deletions.
1 change: 1 addition & 0 deletions .flowconfig
@@ -1,4 +1,5 @@
[ignore]
<PROJECT_ROOT>/node_modules/chalk/.*

[include]

Expand Down
122 changes: 66 additions & 56 deletions src/index.js
@@ -1,19 +1,19 @@
'use strict';
const React = require('react');
const PropTypes = require('prop-types');
"use strict";
const React = require("react");
const PropTypes = require("prop-types");

const ALL_INITIALIZERS = [];
const READY_INITIALIZERS = [];

function isWebpackReady(getModuleIds) {
if (typeof __webpack_modules__ !== 'object') {
if (typeof __webpack_modules__ !== "object") {
return false;
}

return getModuleIds().every(moduleId => {
return (
typeof moduleId !== 'undefined' &&
typeof __webpack_modules__[moduleId] !== 'undefined'
typeof moduleId !== "undefined" &&
typeof __webpack_modules__[moduleId] !== "undefined"
);
});
}
Expand All @@ -27,15 +27,17 @@ function load(loader) {
error: null
};

state.promise = promise.then(loaded => {
state.loading = false;
state.loaded = loaded;
return loaded;
}).catch(err => {
state.loading = false;
state.error = err;
throw err;
});
state.promise = promise
.then(loaded => {
state.loading = false;
state.loaded = loaded;
return loaded;
})
.catch(err => {
state.loading = false;
state.error = err;
throw err;
});

return state;
}
Expand All @@ -62,23 +64,27 @@ function loadMap(obj) {

promises.push(result.promise);

result.promise.then(res => {
state.loaded[key] = res;
}).catch(err => {
state.error = err;
});
result.promise
.then(res => {
state.loaded[key] = res;
})
.catch(err => {
state.error = err;
});
});
} catch (err) {
state.error = err;
}

state.promise = Promise.all(promises).then(res => {
state.loading = false;
return res;
}).catch(err => {
state.loading = false;
throw err;
});
state.promise = Promise.all(promises)
.then(res => {
state.loading = false;
return res;
})
.catch(err => {
state.loading = false;
throw err;
});

return state;
}
Expand All @@ -93,18 +99,21 @@ function render(loaded, props) {

function createLoadableComponent(loadFn, options) {
if (!options.loading) {
throw new Error('react-loadable requires a `loading` component')
throw new Error("react-loadable requires a `loading` component");
}

let opts = Object.assign({
loader: null,
loading: null,
delay: 200,
timeout: null,
render: render,
webpack: null,
modules: null,
}, options);
let opts = Object.assign(
{
loader: null,
loading: null,
delay: 200,
timeout: null,
render: render,
webpack: null,
modules: null
},
options
);

let res = null;

Expand All @@ -117,7 +126,7 @@ function createLoadableComponent(loadFn, options) {

ALL_INITIALIZERS.push(init);

if (typeof opts.webpack === 'function') {
if (typeof opts.webpack === "function") {
READY_INITIALIZERS.push(() => {
if (isWebpackReady(opts.webpack)) {
return init();
Expand All @@ -141,8 +150,8 @@ function createLoadableComponent(loadFn, options) {

static contextTypes = {
loadable: PropTypes.shape({
report: PropTypes.func.isRequired,
}),
report: PropTypes.func.isRequired
})
};

static preload() {
Expand All @@ -165,7 +174,7 @@ function createLoadableComponent(loadFn, options) {
return;
}

if (typeof opts.delay === 'number') {
if (typeof opts.delay === "number") {
if (opts.delay === 0) {
this.setState({ pastDelay: true });
} else {
Expand All @@ -175,7 +184,7 @@ function createLoadableComponent(loadFn, options) {
}
}

if (typeof opts.timeout === 'number') {
if (typeof opts.timeout === "number") {
this._timeout = setTimeout(() => {
this.setState({ timedOut: true });
}, opts.timeout);
Expand All @@ -195,12 +204,13 @@ function createLoadableComponent(loadFn, options) {
this._clearTimeouts();
};

res.promise.then(() => {
update();
}).catch(err => {
update();
throw err;
});
res.promise
.then(() => {
update();
})
.catch(err => {
update();
});
}

componentWillUnmount() {
Expand All @@ -217,7 +227,7 @@ function createLoadableComponent(loadFn, options) {
this.setState({ error: null, loading: true, timedOut: false });
res = loadFn(opts.loader);
this._loadModule();
}
};

render() {
if (this.state.loading || this.state.error) {
Expand All @@ -242,8 +252,8 @@ function Loadable(opts) {
}

function LoadableMap(opts) {
if (typeof opts.render !== 'function') {
throw new Error('LoadableMap requires a `render(loaded, props)` function');
if (typeof opts.render !== "function") {
throw new Error("LoadableMap requires a `render(loaded, props)` function");
}

return createLoadableComponent(loadMap, opts);
Expand All @@ -253,20 +263,20 @@ Loadable.Map = LoadableMap;

class Capture extends React.Component {
static propTypes = {
report: PropTypes.func.isRequired,
report: PropTypes.func.isRequired
};

static childContextTypes = {
loadable: PropTypes.shape({
report: PropTypes.func.isRequired,
}).isRequired,
report: PropTypes.func.isRequired
}).isRequired
};

getChildContext() {
return {
loadable: {
report: this.props.report,
},
report: this.props.report
}
};
}

Expand Down

0 comments on commit 8865dbe

Please sign in to comment.