File tree Expand file tree Collapse file tree 2 files changed +22
-4
lines changed
examples/abort-reload/src Expand file tree Collapse file tree 2 files changed +22
-4
lines changed Original file line number Diff line number Diff line change 11import * as webllm from "@mlc-ai/web-llm" ;
2+ import { error } from "loglevel" ;
23
34let engine ;
45
@@ -25,5 +26,7 @@ async function main() {
2526main ( ) ;
2627setTimeout ( ( ) => {
2728 console . log ( "calling unload" ) ;
28- engine . unload ( ) ;
29+ engine . unload ( ) . catch ( ( err ) => {
30+ console . log ( err ) ;
31+ } ) ;
2932} , 5000 ) ;
Original file line number Diff line number Diff 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" ,
You can’t perform that action at this time.
0 commit comments