UNPKG

@itwin/core-frontend

Version:
36 lines 1.56 kB
/*--------------------------------------------------------------------------------------------- * Copyright (c) Bentley Systems, Incorporated. All rights reserved. * See LICENSE.md in the project root for license terms and full copyright notice. *--------------------------------------------------------------------------------------------*/ /** @packageDocumentation * @module Utils */ import { assert } from "@itwin/core-bentley"; /** Configure an implementation of the operations defined by `T` to execute on a worker thread. * See [this article]($docs/learning/frontend/WorkerProxy.md) for more details and examples. * @beta */ export function registerWorker(impl) { onmessage = async (e) => { const req = e.data; const msgId = req.msgId; try { assert(typeof req === "object" && "operation" in req && "payload" in req && "msgId" in req); const func = impl[req.operation]; assert(typeof func === "function"); let ret = func(req.payload); if (ret instanceof Promise) { ret = await ret; } if (typeof ret === "object" && "transfer" in ret) postMessage({ result: ret.result, msgId }, { transfer: ret.transfer }); else postMessage({ result: ret, msgId }); } catch (err) { const error = err instanceof Error ? err : new Error("Unknown worker error"); postMessage({ error, msgId }); } }; } //# sourceMappingURL=RegisterWorker.js.map