Skip to content

Commit

Permalink
review fixes and bubble up error message
Browse files Browse the repository at this point in the history
  • Loading branch information
existentialism committed Aug 21, 2017
1 parent 59e0eac commit 848320b
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 45 deletions.
4 changes: 3 additions & 1 deletion js/repl/PresetLoadingAnimation.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ type PresetLoadingAnimationProps = {
className: ?string,
};

const PresetLoadingAnimation = ({ className }: PresetLoadingAnimationProps) =>
const PresetLoadingAnimation = ({
className = "",
}: PresetLoadingAnimationProps) =>
<div className={`${className || ""} ${styles.loadingAnimation}`}>
<div className={`${styles.loadingTick} ${styles.loadingTick1}`} />
<div className={`${styles.loadingTick} ${styles.loadingTick2}`} />
Expand Down
39 changes: 16 additions & 23 deletions js/repl/Repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,13 @@ export default class Repl extends React.Component {
const state = this.state;

if (!state.babel.isLoaded) {
const message = state.babel.isLoading
? "Loading Babel..."
: "An error occurred while loading Babel :(";
let message = "Loading Babel...";

if (!state.babel.isLoading && state.babel.didError) {
message =
state.babel.errorMessage ||
"An error occurred while loading Babel :(";
}

return (
<div className={styles.loader}>
Expand Down Expand Up @@ -184,26 +188,15 @@ export default class Repl extends React.Component {
}

_setupBabel() {
loadBabel(this.state.babel, success => {
this.setState(
state => {
const babelState = state.babel;

if (success) {
babelState.isLoaded = true;
babelState.isLoading = false;
} else {
babelState.didError = true;
babelState.isLoading = false;
}

return {
babel: babelState,
...this._compile(state.code, state),
};
},
() => this._checkForUnloadedPlugins()
);
loadBabel(this.state.babel, babelState => {
this.setState(state => ({
...babelState,
...(babelState.isLoaded ? this._compile(state.code, state) : null),
}));

if (babelState.isLoaded) {
this._checkForUnloadedPlugins();
}
});
}

Expand Down
1 change: 0 additions & 1 deletion js/repl/UriUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ const parseQuery = () => {
"prettier",
"showSidebar",
"targets",
"targets",
"version",
],
state
Expand Down
24 changes: 20 additions & 4 deletions js/repl/loadBabel.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,25 @@ import loadBuildArtifacts from "./loadBuildArtifacts";
import loadScript from "./loadScript";
import { BabelState, LoadScriptCallback } from "./types";

export default function loadBabel(config: BabelState, cb: LoadScriptCallback) {
function doLoad(url) {
loadScript(url, cb);
const DEFAULT_BABEL_VERSION = "6";

export default function loadBabel(
config: BabelState,
cb: (config: BabelState) => void
) {
function doLoad(url, error) {
loadScript(url, success => {
if (success) {
config.isLoaded = true;
config.isLoading = false;
} else {
config.didError = true;
config.errorMessage = error;
config.isLoading = false;
}

cb(config);
});
}

// See if a CircleCI build number was passed in the path
Expand Down Expand Up @@ -36,7 +52,7 @@ export default function loadBabel(config: BabelState, cb: LoadScriptCallback) {

// No specific version passed, so just download the latest release.
if (!version) {
version = "6"; // This should be changed to "latest" when Babel 7 is stable
version = DEFAULT_BABEL_VERSION;
}

doLoad(`https://unpkg.com/babel-standalone@${version}/babel.js`);
Expand Down
36 changes: 20 additions & 16 deletions js/repl/loadBuildArtifacts.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,42 @@
export default function loadBuildArtifacts(
repo: string = "babel/babel",
repo: string,
build: string,
cb: (url: string) => void
cb: (url: string, error: string) => void
) {
const urlRepo = repo && repo.length ? repo : "babel/babel";

// Loading a build from CircleCI (eg. for a pull request). We need to
// first call CircleCI's API to get the URL to the artifact.
const buildURL = `https://circleci.com/api/v1.1/project/github/${repo}/${build}/artifacts`;
const buildURL = `https://circleci.com/api/v1.1/project/github/${urlRepo}/${build}/artifacts`;

const xhr = new XMLHttpRequest();
xhr.open("get", buildURL, true);

xhr.onload = function() {
const response = JSON.parse(xhr.responseText);
if (response.message) {
alert(`Could not load Babel build #${build}: ${response.message}`);
return;
}

const artifacts = response.filter(x =>
/babel-standalone\/babel.js$/.test(x.path)
);
let url;
let error;

if (!artifacts || artifacts.length === 0) {
alert(
`Could not find valid babel-standalone artifact in build #${build}`
if (response.message) {
error = `Could not load Babel build #${build}: ${response.message}`;
} else {
const artifacts = response.filter(x =>
/babel-standalone\/babel.js$/.test(x.path)
);
return;

if (!artifacts || artifacts.length === 0) {
error = `Could not find valid babel-standalone artifact in build #${build}`;
} else {
url = artifacts[0].url;
}
}

cb(artifacts[0].url);
cb(url, error);
};

xhr.onerror = function() {
alert(`Could not load Babel build #${build} :(`);
cb(null, `Could not load Babel build #${build} :(`);
};

xhr.send();
Expand Down
1 change: 1 addition & 0 deletions js/repl/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type LazyLoadedState = {

export type BabelState = LazyLoadedState & {
build: string,
errorMessage?: string,
circleciRepo: string,
version: string,
};
Expand Down

0 comments on commit 848320b

Please sign in to comment.