UNPKG

@colyseus/core

Version:

Multiplayer Framework for Node.js.

112 lines (111 loc) 3.9 kB
var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var IPC_exports = {}; __export(IPC_exports, { requestFromIPC: () => requestFromIPC, subscribeIPC: () => subscribeIPC, subscribeWithTimeout: () => subscribeWithTimeout }); module.exports = __toCommonJS(IPC_exports); var import_Debug = require("./Debug.js"); var import_Protocol = require("./Protocol.js"); var import_Utils = require("./utils/Utils.js"); async function requestFromIPC(presence, publishToChannel, method, args, rejectionTimeout = import_Utils.REMOTE_ROOM_SHORT_TIMEOUT) { return new Promise(async (resolve, reject) => { let unsubscribeTimeout; const requestId = (0, import_Utils.generateId)(); const channel = `ipc:${requestId}`; const unsubscribe = () => { presence.unsubscribe(channel); clearTimeout(unsubscribeTimeout); }; await presence.subscribe(channel, (message) => { const [code, data] = message; if (code === import_Protocol.IpcProtocol.SUCCESS) { resolve(data); } else if (code === import_Protocol.IpcProtocol.ERROR) { let error = data; try { error = JSON.parse(data); } catch (e) { } if (typeof error === "string") { error = new Error(error); } reject(error); } unsubscribe(); }); presence.publish(publishToChannel, [method, requestId, args]); unsubscribeTimeout = setTimeout(() => { unsubscribe(); reject(new Error("ipc_timeout")); }, rejectionTimeout); }); } async function subscribeIPC(presence, channel, replyCallback) { await presence.subscribe(channel, (message) => { const [method, requestId, args] = message; const reply = (code, data) => { presence.publish(`ipc:${requestId}`, [code, data]); }; let response; try { response = replyCallback(method, args); } catch (e) { (0, import_Debug.debugAndPrintError)(e); const error = typeof e.code !== "undefined" ? { code: e.code, message: e.message } : e.message; return reply(import_Protocol.IpcProtocol.ERROR, JSON.stringify(error)); } if (!(response instanceof Promise)) { return reply(import_Protocol.IpcProtocol.SUCCESS, response); } response.then((result) => reply(import_Protocol.IpcProtocol.SUCCESS, result)).catch((e) => { const err = e && e.message || e; reply(import_Protocol.IpcProtocol.ERROR, err); }); }); } function subscribeWithTimeout(presence, channel, timeout) { return new Promise((resolve, reject) => { let timeoutHandle; let resolved = false; const unsubscribe = () => { presence.unsubscribe(channel); clearTimeout(timeoutHandle); }; presence.subscribe(channel, (roomId) => { if (resolved) return; resolved = true; unsubscribe(); resolve(roomId); }); timeoutHandle = setTimeout(() => { if (resolved) return; resolved = true; unsubscribe(); reject(new Error("timeout")); }, timeout); }); } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { requestFromIPC, subscribeIPC, subscribeWithTimeout });