Skip to content

Commit

Permalink
refactor: load/appendJs/Css error handling & onto mountNode
Browse files Browse the repository at this point in the history
  • Loading branch information
fritx committed Apr 18, 2020
1 parent 4bbc9d1 commit 29bba49
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 14 deletions.
6 changes: 4 additions & 2 deletions src/playground/babelMaster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ let wrapCode = (code: string) => {
return `
(async () => {
setRendering(true)
mountNode.innerHTML = ''
try {
let { useRef, useMemo, useState, useEffect, useLayoutEffect, useReducer, useContext, useCallback, useImperativeHandle } = React
;;${code};;
Expand All @@ -20,6 +21,7 @@ let wrapCode = (code: string) => {
ReactDOM.render(<App />, mountNode)
}
} catch (err) {
console.error(['displayError', err])
displayError(err)
} finally {
setRendering(false)
Expand All @@ -33,8 +35,8 @@ export let babelTransform = async (code: string): Promise<string> => {
let fullCode = wrapCode(code);

worker.onerror = e => {
console.log(['worker error', e]);
reject(e);
console.error(['worker onerror', e]);
reject(new Error(`worker onerror - ${code.slice(0, 100)}`));
};

worker.onmessage = e => {
Expand Down
3 changes: 3 additions & 0 deletions src/playground/types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
interface Window {
mountNode: HTMLDivElement;
}
20 changes: 8 additions & 12 deletions src/playground/util.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,12 @@ export let loadJs = (url: string) => {
let el = document.createElement('script');
el.src = url;
el.onload = () => {
document.body.removeChild(el);
resolve();
};
el.onerror = e => {
document.body.removeChild(el);
reject(e);
reject(new Error(`loadJs onerror - ${url}`));
};
document.body.appendChild(el);
window.mountNode.appendChild(el);
});
};

Expand All @@ -59,9 +57,9 @@ export let loadCss = (url: string) => {
resolve();
};
el.onerror = e => {
reject(e);
reject(new Error(`loadCss onerror - ${url}`));
};
document.body.appendChild(el);
window.mountNode.appendChild(el);
});
};

Expand All @@ -70,14 +68,12 @@ export let appendJs = (code: string) => {
let el = document.createElement('script');
el.innerHTML = code;
el.onload = () => {
document.body.removeChild(el);
resolve();
};
el.onerror = e => {
document.body.removeChild(el);
reject(e);
reject(new Error(`appendJs onerror - ${code.slice(0, 100)}`));
};
document.body.appendChild(el);
window.mountNode.appendChild(el);
});
};

Expand All @@ -89,8 +85,8 @@ export let appendCss = (code: string) => {
resolve();
};
el.onerror = e => {
reject(e);
reject(new Error(`appendCss onerror - ${code.slice(0, 100)}`));
};
document.body.appendChild(el);
window.mountNode.appendChild(el);
});
};

0 comments on commit 29bba49

Please sign in to comment.