From d0841c4c853b3758c2a4e8d60456d0f4bba246aa Mon Sep 17 00:00:00 2001 From: ICheered Date: Fri, 4 Aug 2023 14:15:01 +0200 Subject: [PATCH] Lazily load ModulePromise, make rethrowIfCritical standalone --- ui/camera.js | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/ui/camera.js b/ui/camera.js index 7c4a1e1..8d315b5 100644 --- a/ui/camera.js +++ b/ui/camera.js @@ -33,13 +33,13 @@ async function initializeModule() { } } -const ModulePromise = initializeModule().then(initModule => { - if (initModule) { - return initModule(); - } else { - return null; +export function rethrowIfCritical(err) { + // If it's precisely Error, it's a custom error; anything else - SyntaxError, + // WebAssembly.RuntimeError, TypeError, etc. - is treated as critical here. + if (err.constructor !== Error) { + throw err; } -}); +} class Camera { constructor() { @@ -47,18 +47,20 @@ class Camera { this.queue = Promise.resolve(); this.Module = null; this.context = null; - } - - rethrowIfCritical(err) { - // If it's precisely Error, it's a custom error; anything else - SyntaxError, - // WebAssembly.RuntimeError, TypeError, etc. - is treated as critical here. - if (err.constructor !== Error) { - throw err; - } + this.ModulePromise = null; } async connect() { - this.Module = await ModulePromise; + if (!this.ModulePromise) { + this.ModulePromise = initializeModule().then(initModule => { + if (initModule) { + return initModule(); + } else { + return null; + } + }); + } + this.Module = await this.ModulePromise; this.context = await new this.Module.Context(); } @@ -69,7 +71,7 @@ class Camera { */ async schedule(op) { let res = this.queue.then(() => op(this.context)); - this.queue = res.catch(this.rethrowIfCritical); + this.queue = res.catch(rethrowIfCritical); return res; }