tovarish-relayer/lib/services/routerMsg.js

46 lines
1.5 KiB
JavaScript
Raw Normal View History

2024-11-13 04:42:13 +03:00
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.sendMessage = sendMessage;
exports.resolveMessages = resolveMessages;
/* eslint-disable @typescript-eslint/no-explicit-any */
/**
* Send and receive messages from worker to main thread
*/
const process_1 = __importDefault(require("process"));
const crypto_1 = require("crypto");
const core_1 = require("@tornado/core");
function sendMessage(router, msg) {
const msgId = (0, core_1.bytesToHex)(crypto_1.webcrypto.getRandomValues(new Uint8Array(8)));
return new Promise((resolve, reject) => {
if (!process_1.default.send) {
reject(new Error('Not worker'));
return;
}
const msgJson = JSON.parse(JSON.stringify(msg));
msgJson.msgId = msgId;
process_1.default.send(msgJson);
router.messages.push({
msgId,
resolve,
reject,
resolved: false,
});
});
}
function resolveMessages(router) {
process_1.default.on('message', (msg) => {
const message = router.messages.find((w) => w.msgId === msg.msgId);
if (!message) {
return;
}
const msgJson = JSON.parse(JSON.stringify(msg));
delete msgJson.msgId;
message.resolve(msgJson);
message.resolved = true;
router.messages = router.messages.filter((w) => !w.resolved);
});
}