Extract some modules

This commit is contained in:
Ingvar Stepanyan
2021-07-21 14:37:57 +00:00
parent 5e54c25c55
commit 70a35d1b70
5 changed files with 447 additions and 436 deletions

44
ui/ops.js Normal file
View File

@@ -0,0 +1,44 @@
import initModule from '../libapi.mjs';
/** @typedef {import('../libapi.mjs').Context} Context */
/** This function should be called once user has selected the camera and other operations can begin. */
export let start;
let started = new Promise(resolve => {
start = resolve;
});
let queue = (async () => {
let { Context } = await initModule();
await started;
let ctx = await new Context();
addEventListener('beforeunload', e => {
ctx.delete();
});
return ctx;
})();
/** Schedules an exclusive async operation on the global context.
* @template T
* @param {(ctx: Context) => Promise<T>} op
* @returns {Promise<T>}
*/
export function scheduleOp(op) {
let caughtRes = queue.then(async ctx => {
let resultPromise = op(ctx);
try {
await resultPromise;
} catch {}
return {
ctx,
resultPromise
};
});
// Queue should ignore result values as well as errors from singular ops.
queue = caughtRes.then(res => res.ctx);
// Result should contain the unwrapped value or error.
return caughtRes.then(res => res.resultPromise);
}