fix(web): handle 'unload' message while attempting model 'load'

Fixes: KEYMAN-WEB-MD

Build-bot: skip build:web
Test-bot: skip
This commit is contained in:
Joshua Horton 2026-09-09 13:49:12 -05:00
parent 5ed4ccb7ef
commit 18f334a60e

View file

@ -267,7 +267,11 @@ export class LMLayerWorker {
try {
this._importScripts(url);
} catch (err) {
// Does not catch errors thrown within the imported script.
// Does catch errors with the model script's file-path.
this.error("Error occurred when attempting to load dictionary", err);
// Remain in the model-unloaded state; the load attempt was unsuccessful.
}
}
@ -312,7 +316,14 @@ export class LMLayerWorker {
this.state = {
name: 'modelless',
handleMessage: (payload) => {
// ...that message must have been 'load'!
// It is possible to remain in this state after a model loading error.
// In such cases, the hosting engine may signal a model-unload.
if (payload.message === 'unload') {
// We are already in a "model unloaded" state; no work needed!
return;
}
// ...otherwise, that message must have been 'load'!
if (payload.message !== 'load') {
throw new Error(`invalid message; expected 'load' but got ${payload.message}`);
}