feat: publish Regex Tools 0.1.0
This commit is contained in:
73
src/regex/execution/EngineSupervisor.ts
Normal file
73
src/regex/execution/EngineSupervisor.ts
Normal file
@@ -0,0 +1,73 @@
|
||||
import type {
|
||||
RegexExecutionRequest,
|
||||
RegexExecutionResult,
|
||||
RegexReplacementRequest,
|
||||
RegexReplacementResult,
|
||||
} from "../model/match";
|
||||
import { WorkerSupervisor } from "./WorkerSupervisor";
|
||||
import type {
|
||||
EngineWorkerOperation,
|
||||
EngineWorkerResult,
|
||||
} from "./worker-protocol";
|
||||
|
||||
export type EngineRuntimeState =
|
||||
| { readonly status: "unloaded" }
|
||||
| { readonly status: "running"; readonly operation: "execute" | "replace" }
|
||||
| { readonly status: "ready" }
|
||||
| { readonly status: "recovering"; readonly reason: string }
|
||||
| { readonly status: "failed"; readonly error: Error };
|
||||
|
||||
export class EngineSupervisor {
|
||||
private readonly supervisor = new WorkerSupervisor<
|
||||
EngineWorkerOperation,
|
||||
EngineWorkerResult
|
||||
>(
|
||||
"ECMAScript engine",
|
||||
() =>
|
||||
new Worker(
|
||||
new URL("../../workers/ecmascript.worker.ts", import.meta.url),
|
||||
{
|
||||
type: "module",
|
||||
name: "regex-tools-ecmascript",
|
||||
},
|
||||
),
|
||||
);
|
||||
|
||||
async execute(
|
||||
request: RegexExecutionRequest,
|
||||
timeoutMs: number,
|
||||
): Promise<RegexExecutionResult> {
|
||||
const response = await this.supervisor.run(
|
||||
{ kind: "execute", request },
|
||||
timeoutMs,
|
||||
{ supersede: true },
|
||||
);
|
||||
if (response.kind !== "execute") {
|
||||
throw new Error("Engine worker returned the wrong response kind");
|
||||
}
|
||||
return response.result;
|
||||
}
|
||||
|
||||
async replace(
|
||||
request: RegexReplacementRequest,
|
||||
timeoutMs: number,
|
||||
): Promise<RegexReplacementResult> {
|
||||
const response = await this.supervisor.run(
|
||||
{ kind: "replace", request },
|
||||
timeoutMs,
|
||||
{ supersede: true },
|
||||
);
|
||||
if (response.kind !== "replace") {
|
||||
throw new Error("Engine worker returned the wrong response kind");
|
||||
}
|
||||
return response.result;
|
||||
}
|
||||
|
||||
cancel(): void {
|
||||
this.supervisor.cancel();
|
||||
}
|
||||
|
||||
dispose(): void {
|
||||
this.supervisor.dispose();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user