UNPKG

@litert/televoke

Version:
70 lines 2.56 kB
"use strict"; /** * Copyright 2025 Angus.Fenying <fenying@litert.org> * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * https://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.connectToMainThreadServer = connectToMainThreadServer; exports.connectToWorkerThreadServer = connectToWorkerThreadServer; const NodeWorker = require("node:worker_threads"); const eWT = require("./WorkerThread.Errors"); const WorkerThread_Constants_1 = require("./WorkerThread.Constants"); const WorkerThread_Transporter_1 = require("./WorkerThread.Transporter"); class MainServerConnector { constructor() { this._transporter = null; } async connect() { return Promise.resolve(this._transporter ?? (this._transporter = new WorkerThread_Transporter_1.WorkerThreadTransporter(WorkerThread_Constants_1.MAIN_THREAD_PROTOCOL_NAME))); } } class WorkerServerConnector { constructor(worker) { this._worker = null; this._transporter = null; this._worker = worker .on('exit', () => { this._worker = null; }); } async connect() { if (!this._worker) { throw new eWT.E_WORKER_THREAD_OFFLINE(); } this._transporter ?? (this._transporter = new WorkerThread_Transporter_1.MainThreadTransporter(WorkerThread_Constants_1.MAIN_THREAD_PROTOCOL_NAME, this._worker)); return Promise.resolve(this._transporter); } } let inst = null; /** * Create a connector to connect to the server running in the main thread. * * @experimental */ function connectToMainThreadServer() { if (NodeWorker.isMainThread) { throw new eWT.E_WORKER_THREAD_ONLY(); } return inst ?? (inst = new MainServerConnector()); } /** * Create a connector to connect to the server running in the worker thread. * * @experimental */ function connectToWorkerThreadServer(worker) { if (!NodeWorker.isMainThread) { throw new eWT.E_MAIN_THREAD_ONLY(); } return new WorkerServerConnector(worker); } //# sourceMappingURL=WorkerThread.Client.js.map