import { AdapterInterface } from './adapter-interface' ; import { Namespace } from '../namespace' ; import { PresenceMemberInfo } from '../channels/presence-channel-manager' ; import { Server } from '../server' ; import { WebSocket } from 'uWebSockets.js' ; export class LocalAdapter implements AdapterInterface { /**
     * The app connections storage class to manage connections.
     */ public namespaces : Map < string , Namespace > = new Map < string , Namespace > ( ) ; /**
     * Initialize the adapter.
     */ constructor ( protected server ) { } /**
     * Get the app namespace.
     */ getNamespace ( appId ) { if ( ! this . namespaces . has ( appId ) ) { this . namespaces . set ( appId , new Namespace ( appId ) ) ; } return this . namespaces . get ( appId ) ; } /**
     * Get all namespaces.
     */ getNamespaces ( ) { return this . namespaces ; } /**
     * Get all sockets from the namespace.
     */ async getSockets ( appId , onlyLocal = false ) { return this . getNamespace ( appId ) . getSockets ( ) ; } /**
     * Get total sockets count.
     */ async getSocketsCount ( appId , onlyLocal ? ) { return this . getNamespace ( appId ) . getSockets ( ) . then ( sockets => { return sockets . size ; } ) ; } /**
     * Get all sockets from the namespace.
     */ async getChannels ( appId , onlyLocal = false ) { return this . getNamespace ( appId ) . getChannels ( ) ; } /**
     * Get all the channel sockets associated with a namespace.
     */ async getChannelSockets ( appId , channel , onlyLocal = false ) { return this . getNamespace ( appId ) . getChannelSockets ( channel ) ; } /**
     * Get a given channel's total sockets count.
     */ async getChannelSocketsCount ( appId , channel , onlyLocal ? ) { return this . getNamespace ( appId ) . getChannelSockets ( channel ) . then ( sockets => { return sockets . size ; } ) ; } /**
     * Get a given presence channel's members.
     */ async getChannelMembers ( appId , channel , onlyLocal = false ) { return this . getNamespace ( appId ) . getChannelMembers ( channel ) ; } /**
     * Get a given presence channel's members count
     */ async getChannelMembersCount ( appId , channel , onlyLocal ? ) { return this . getNamespace ( appId ) . getChannelMembers ( channel ) . then ( members => { return members . size ; } ) ; } /**
     * Check if a given connection ID exists in a channel.
     */ async isInChannel ( appId , channel , wsId , onlyLocal ? ) { return this . getNamespace ( appId ) . isInChannel ( wsId , channel ) ; } /**
     * Send a message to a namespace and channel.
     */ send ( appId , channel , data , exceptingId = null ) { let nsp = this . namespaces . get ( appId ) ; if ( ! nsp ) { return ; } nsp . getChannelSockets ( channel ) . then ( sockets => { sockets . forEach ( ( ws ) => { if ( exceptingId && exceptingId === ws . id ) { return ; } ws . sendJson ( JSON . parse ( data ) ) ; } ) ; } ) ; } /**
     * Clear the local namespaces.
     */ clear ( namespaceId ? , closeConnections = false ) { if ( namespaceId ) { this . namespaces . set ( namespaceId , new Namespace ( namespaceId ) ) ; } else { this . namespaces = new Map < string , Namespace > ( ) ; } return Promise . resolve ( ) ; } }