Skip to content

Commit

Permalink
[test-dist-esm-node-import] Force testing the module import.
Browse files Browse the repository at this point in the history
  • Loading branch information
lgarron committed Jul 16, 2022
1 parent 756beba commit b6a03bf
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
3 changes: 3 additions & 0 deletions script/test/dist/esm/node-import/test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import "cubing/search";
import "cubing/stream";
import "cubing/twisty";

import { setDebug } from "cubing/search";
setDebug({ disableStringWorker: true });

import { randomScrambleForEvent } from "cubing/scramble";

(async () => {
Expand Down
18 changes: 14 additions & 4 deletions src/cubing/search/instantiator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ let forceStringWorker: boolean = false;
export function setForceStringWorker(force: boolean): void {
forceStringWorker = force;
}
let disableStringWorker: boolean = false;
export function setDisableStringWorker(disable: boolean): void {
disableStringWorker = disable;
}

export async function instantiateModuleWorker(): Promise<WorkerInsideAPI> {
// eslint-disable-next-line no-async-promise-executor
Expand Down Expand Up @@ -100,10 +104,16 @@ export async function instantiateWorker(): Promise<WorkerInsideAPI> {
// `await` is important for `catch` to work.
return await instantiateModuleWorker();
} catch (e) {
console.warn(
"Could not instantiate module worker (this may happen in Firefox, with `bundle-global`, or when using Parcel). Falling back to string worker.",
e,
);
const commonErrorPrefix =
"Could not instantiate module worker (this may happen in Firefox, with `bundle-global`, or when using Parcel).";
if (disableStringWorker) {
console.error(
`${commonErrorPrefix} Fallback to string worker is disabled.`,
e,
);
throw new Error(`Module worker instantiation failed.`);
}
console.warn(`${commonErrorPrefix} Falling back to string worker.`, e);
return instantiateClassicWorker();
}
}
10 changes: 9 additions & 1 deletion src/cubing/search/outside.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import { Alg } from "../alg";
// import { preInitialize222 } from "../implementations/2x2x2";
import { randomClockScrambleString } from "./inside/solve/puzzles/clock"; // TODO: don't reach into `inside` code.
import { randomMegaminxScrambleString } from "./inside/solve/puzzles/wca-minx"; // TODO: don't reach into `inside` code.
import { instantiateWorker, setForceStringWorker } from "./instantiator";
import {
instantiateWorker,
setDisableStringWorker,
setForceStringWorker,
} from "./instantiator";
import type { PrefetchLevel, WorkerInsideAPI } from "./inside/api";
import type { KState } from "../kpuzzle/KState";

Expand Down Expand Up @@ -106,6 +110,7 @@ export function setDebug(options: {
logPerf?: boolean;
scramblePrefetchLevel?: `${PrefetchLevel}`;
forceStringWorker?: boolean;
disableStringWorker?: boolean;
}): void {
const { logPerf, scramblePrefetchLevel } = options;
if (typeof logPerf !== "undefined") {
Expand All @@ -119,4 +124,7 @@ export function setDebug(options: {
if ("forceStringWorker" in options) {
setForceStringWorker(!!options.forceStringWorker);
}
if ("disableStringWorker" in options) {
setDisableStringWorker(!!options.disableStringWorker);
}
}

0 comments on commit b6a03bf

Please sign in to comment.