UNPKG

@colyseus/core

Version:

Multiplayer Framework for Node.js.

55 lines (53 loc) 1.94 kB
"use strict"; 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); // packages/core/src/presence/Presence.ts var Presence_exports = {}; __export(Presence_exports, { createScopedPresence: () => createScopedPresence }); module.exports = __toCommonJS(Presence_exports); function createScopedPresence(room, presence) { const subscriptions = []; const scopedPresence = Object.create(presence); scopedPresence.subscribe = async function(topic, callback) { subscriptions.push({ topic, callback }); await presence.subscribe(topic, callback); return scopedPresence; }; scopedPresence.unsubscribe = function(topic, callback) { const index = subscriptions.findIndex( (sub) => sub.topic === topic && (!callback || sub.callback === callback) ); if (index !== -1) { subscriptions.splice(index, 1); } presence.unsubscribe(topic, callback); }; room["_events"].on("dispose", () => { for (const { topic, callback } of subscriptions) { presence.unsubscribe(topic, callback); } subscriptions.length = 0; }); return scopedPresence; } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { createScopedPresence });