Skip to content

Commit fac8634

Browse files
committed
try catch AbortError
1 parent c083598 commit fac8634

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

examples/abort-reload/src/get_started.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as webllm from "@mlc-ai/web-llm";
2+
import { error } from "loglevel";
23

34
let engine;
45

@@ -25,5 +26,7 @@ async function main() {
2526
main();
2627
setTimeout(() => {
2728
console.log("calling unload");
28-
engine.unload();
29+
engine.unload().catch((err) => {
30+
console.log(err);
31+
});
2932
}, 5000);

src/engine.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,25 @@ export class MLCEngine implements MLCEngineInterface {
145145
*/
146146
async reload(modelId: string, chatOpts?: ChatOptions): Promise<void> {
147147
await this.unload();
148+
this.reloadController = new AbortController();
148149

150+
try {
151+
await this.reloadInternal(modelId, chatOpts);
152+
} catch (error) {
153+
if (error instanceof DOMException && error.name === "AbortError") {
154+
log.warn("Reload() is aborted.", error.message);
155+
return;
156+
}
157+
throw error;
158+
} finally {
159+
this.reloadController = undefined;
160+
}
161+
}
162+
163+
private async reloadInternal(
164+
modelId: string,
165+
chatOpts?: ChatOptions,
166+
): Promise<void> {
149167
this.logitProcessor = this.logitProcessorRegistry?.get(modelId);
150168
const tstart = performance.now();
151169

@@ -157,8 +175,6 @@ export class MLCEngine implements MLCEngineInterface {
157175
throw new ModelNotFoundError(modelId);
158176
};
159177

160-
this.reloadController = new AbortController();
161-
162178
const modelRecord = findModelRecord();
163179
const baseUrl =
164180
typeof document !== "undefined"
@@ -209,7 +225,6 @@ export class MLCEngine implements MLCEngineInterface {
209225
// rely on the normal caching strategy
210226
return (await fetch(new URL(wasmUrl, baseUrl).href)).arrayBuffer();
211227
} else {
212-
// use cache
213228
return await wasmCache.fetchWithCache(
214229
wasmUrl,
215230
"arraybuffer",

0 commit comments

Comments
 (0)