UNPKG

@colyseus/core

Version:

Multiplayer Framework for Node.js.

30 lines (29 loc) 903 B
// packages/core/src/presence/Presence.ts 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; } export { createScopedPresence };