@itwin/core-frontend
Version:
iTwin.js frontend components
39 lines • 1.72 kB
JavaScript
;
/*---------------------------------------------------------------------------------------------
* 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
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.registerWorker = registerWorker;
const core_bentley_1 = require("@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
*/
function registerWorker(impl) {
onmessage = async (e) => {
const req = e.data;
const msgId = req.msgId;
try {
(0, core_bentley_1.assert)(typeof req === "object" && "operation" in req && "payload" in req && "msgId" in req);
const func = impl[req.operation];
(0, core_bentley_1.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