Skip to content

Instantly share code, notes, and snippets.

@cmonacaps
Last active April 18, 2020 14:36
Show Gist options
  • Save cmonacaps/2dd39d9b3d7d9b2306b7dd5812484b84 to your computer and use it in GitHub Desktop.
Save cmonacaps/2dd39d9b3d7d9b2306b7dd5812484b84 to your computer and use it in GitHub Desktop.
exception presentation fails when actual error is lost, possibly during threadsjs post-error-to-window
const App = () => (
<Worker>
{props => (
<ErrorBoundary {...props}>
<Juicy>
<ApplicationComponents />
</Juicy>
</ErrorBoundary>
)}
</Worker>
);
export default App;
import React from 'react';
import { isNil, transpose } from 'ramda';
import styles from './ErrorBoundary.module.css';
class ErrorBoundary extends React.Component {
state = {
error: null,
info: null,
};
componentDidCatch(error, info) {
this.setState({ error, info });
// maybe I should do something here to swallow it?
}
render() {
const { error } = this.state;
if (isNil(error)) {
return <>{this.props.children}</>;
} else if (typeof error === 'string') {
// I'm not sure if this is correct or even if this case happens
// I think all the time it's doing the isNil case if there's no
// error, just above, or the render case if there's an error,
// just below
return [`${error}`];
} else {
const [heading, ...stack] = (error => {
// bunch of string array maps mishmash
return mishmash;
})(error);
return (
<div className={styles.errorScreen}>
{/* bunch of flexbox */}
</div>
);
}
}
}
export default ErrorBoundary;
import React from 'react';
import { spawn, Thread, Worker } from 'threads';
/**
* WorkerManager - an owner of workers, that supplies their entries via props
*
*/
class WorkerManager extends React.PureComponent {
constructor(props) {
super(props);
this.state = { workers: null };
}
componentDidMount() {
Promise.all([
spawn(new Worker('./workerOps')),
// .. spawn is repeated
]).then((workers) =>
this.setState({
workers,
})
);
}
componentWillUnmount() {
const waitWorkerTerm = async () =>
await Promise.all(
this.state.workers.map((worker) =>
worker ? Thread.terminate(worker) : null
)
);
waitWorkerTerm();
}
render() {
return (
<>
{this.props.children({
// makeOpsMapping, elided, produces like,
// { reticulateSplines, findTheCourageToLoveAgain, ... }
workerOps: makeOpsMapping(this.state.workers ?? []),
...this.props,
})}
</>
);
}
}
export default WorkerManager;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment