@cfasim-ui/wasm 0.4.8 → 0.4.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +2 -2
- package/src/wasm.worker.ts +1 -1
- package/src/wasmWorkerApi.ts +45 -5
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cfasim-ui/wasm",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.10",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "WebAssembly integration for cfasim-ui",
|
|
6
6
|
"license": "Apache-2.0",
|
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
"./vite": "./src/vitePlugin.js"
|
|
21
21
|
},
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"@cfasim-ui/shared": "0.4.
|
|
23
|
+
"@cfasim-ui/shared": "0.4.10"
|
|
24
24
|
},
|
|
25
25
|
"peerDependencies": {
|
|
26
26
|
"vue": "^3.5.0"
|
package/src/wasm.worker.ts
CHANGED
package/src/wasmWorkerApi.ts
CHANGED
|
@@ -10,23 +10,62 @@ interface WorkerMessage {
|
|
|
10
10
|
|
|
11
11
|
let lastId = 0;
|
|
12
12
|
|
|
13
|
+
/**
|
|
14
|
+
* Convert a worker `ErrorEvent` into a human-readable message. Cross-origin
|
|
15
|
+
* worker failures sanitize the event so every field is null/empty (e.g.
|
|
16
|
+
* the worker's module fails to evaluate); fall back to a hint rather than
|
|
17
|
+
* an empty string.
|
|
18
|
+
*/
|
|
19
|
+
function describeWorkerError(e: ErrorEvent): string {
|
|
20
|
+
const msg = e.message || e.error?.message || "";
|
|
21
|
+
if (msg) {
|
|
22
|
+
if (e.filename) return `${msg} (${e.filename}:${e.lineno})`;
|
|
23
|
+
return msg;
|
|
24
|
+
}
|
|
25
|
+
return "Worker failed to load (no error details available — most often a cross-origin script load failure or a syntax error in the worker module)";
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// Rejects keyed by request id so `cancelWasm` can fail in-flight promises
|
|
29
|
+
// when we terminate the worker mid-run, and so worker-level errors can drain
|
|
30
|
+
// every pending call at once.
|
|
31
|
+
const pendingRejects = new Map<number, (reason: Error) => void>();
|
|
32
|
+
|
|
33
|
+
// Set once the worker fires an `error` / `messageerror` event. Subsequent
|
|
34
|
+
// `runWasm` calls short-circuit with this error so the caller sees a real
|
|
35
|
+
// rejection instead of waiting forever on a worker that will never reply.
|
|
36
|
+
let workerDeadError: Error | null = null;
|
|
37
|
+
|
|
13
38
|
function newWorker(): Worker {
|
|
14
|
-
|
|
39
|
+
const w = new Worker(new URL("./wasm.worker.ts", import.meta.url), {
|
|
15
40
|
type: "module",
|
|
16
41
|
});
|
|
42
|
+
function failAll(message: string) {
|
|
43
|
+
workerDeadError = new Error(message);
|
|
44
|
+
const rejects = Array.from(pendingRejects.values());
|
|
45
|
+
pendingRejects.clear();
|
|
46
|
+
for (const reject of rejects) reject(workerDeadError);
|
|
47
|
+
}
|
|
48
|
+
w.addEventListener("error", (e: ErrorEvent) => {
|
|
49
|
+
failAll(describeWorkerError(e));
|
|
50
|
+
});
|
|
51
|
+
w.addEventListener("messageerror", () => {
|
|
52
|
+
failAll(
|
|
53
|
+
"Worker received a message it could not deserialize (messageerror)",
|
|
54
|
+
);
|
|
55
|
+
});
|
|
56
|
+
return w;
|
|
17
57
|
}
|
|
18
58
|
|
|
19
59
|
let worker = newWorker();
|
|
20
60
|
|
|
21
|
-
// Rejects keyed by request id so `cancelWasm` can fail in-flight promises
|
|
22
|
-
// when we terminate the worker mid-run.
|
|
23
|
-
const pendingRejects = new Map<number, (reason: Error) => void>();
|
|
24
|
-
|
|
25
61
|
export function runWasm(
|
|
26
62
|
model: string,
|
|
27
63
|
fn: string,
|
|
28
64
|
...args: string[]
|
|
29
65
|
): Promise<unknown> {
|
|
66
|
+
if (workerDeadError) {
|
|
67
|
+
return Promise.reject(workerDeadError);
|
|
68
|
+
}
|
|
30
69
|
return new Promise((resolve, reject) => {
|
|
31
70
|
const id = ++lastId;
|
|
32
71
|
// Capture the current worker so a later `cancelWasm` that swaps the
|
|
@@ -63,6 +102,7 @@ export function cancelWasm(): void {
|
|
|
63
102
|
worker.terminate();
|
|
64
103
|
const rejects = Array.from(pendingRejects.values());
|
|
65
104
|
pendingRejects.clear();
|
|
105
|
+
workerDeadError = null;
|
|
66
106
|
worker = newWorker();
|
|
67
107
|
for (const reject of rejects) {
|
|
68
108
|
reject(new Error("cancelled"));
|