Further fixes; improve error handling
This commit is contained in:
20
ui/ops.js
20
ui/ops.js
@@ -4,29 +4,31 @@ import initModule from '../libapi.mjs';
|
||||
|
||||
const ModulePromise = initModule();
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
export async function connect() {
|
||||
const Module = await ModulePromise;
|
||||
|
||||
let context = await new Module.Context();
|
||||
let supportedOps = await context.supportedOps();
|
||||
|
||||
/** @type {Promise<unknown>} */
|
||||
let queue = Promise.resolve();
|
||||
|
||||
/** Schedules an exclusive async operation on the global context.
|
||||
* @template T
|
||||
* @template T,T2
|
||||
* @param {(ctx: Context) => Promise<T>} op
|
||||
* @returns {Promise<T>}
|
||||
*/
|
||||
function schedule(op) {
|
||||
let res = queue.then(() => op(context));
|
||||
|
||||
// Queue should ignore result values as well as errors from singular ops.
|
||||
queue = res.then(
|
||||
() => {},
|
||||
() => {}
|
||||
);
|
||||
|
||||
// Result should contain the unwrapped value or error.
|
||||
queue = res.catch(rethrowIfCritical);
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user