Skip to content

Commit

Permalink
(repair webworkers) replace promise.resolve().then with setTimeout
Browse files Browse the repository at this point in the history
- Turns out promise.resolve().then() doesn't actually accomplish the equivalent of a setImmediate.  I was mislead by something online, oh well...  setTimeout works.
- The main simulation worker was breaking when you used the 'run' functionality because the thread would lock. A setTimeout (and previously, setImmediate) was needed to allow other events in the thread.
  • Loading branch information
judeallred committed Dec 12, 2023
1 parent 6eb3fdb commit 526ef58
Show file tree
Hide file tree
Showing 6 changed files with 6 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ export const BehaviorKeysForm: FC<{

const onAddField = () => {
setData((draft) => addField(draft, projection.length === 0));
Promise.resolve().then(() => {
setTimeout(() => {
if (listRef.current) {
scrollToEnd(listRef.current);
focusLast();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ export const HashCoreFiles: FC = () => {
/**
* @todo don't rely on querying for ids for this
*/
Promise.resolve().then(() => {
setTimeout(() => {
document
.querySelector<HTMLLIElement>(`#${getDomIdByFileId(file.id)}`)
?.scrollIntoView({ block: "center", inline: "center" });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const routes: HookRouter.RouteObject = {
* @todo route handlers should be side effect free – handle this elsewhere
*/
"/:buildstamp/index.html": () => {
Promise.resolve().then(() => {
setTimeout(() => {
/**
* hookrouter's navigate has a bug where it incorrectly strips queries
* included in the path, so we have to separate them.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1391,7 +1391,7 @@ export const ExperimentModal: FC<{

fields.push(optimizationFieldTemplate());
setFormData(clone);
Promise.resolve().then(() => {
setTimeout(() => {
document
.querySelector<HTMLInputElement>(
`[name="fields.${fields.length - 1}.name"]`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ const useRepositionPopoverOnElementResize = () =>
*/
scrollBy(0, 1);

Promise.resolve().then(() => {
setTimeout(() => {
scrollBy(0, -1);
});
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const runSim = async (runner: RunnerState) => {
runner.stepsLeft -= 1;

// awaiting the runner might be instant, so we need to prevent the thread from locking
await new Promise((resolve) => Promise.resolve().then(resolve));
await new Promise((resolve) => setTimeout(resolve));

if (runner.stepHandler) {
await runner.stepHandler(runner.stepsTaken + 1, newState);
Expand Down

0 comments on commit 526ef58

Please sign in to comment.