UNPKG

@feathersjs/transport-commons

Version:

Shared functionality for websocket providers

59 lines (45 loc) 1.35 kB
import { EventEmitter } from 'events' import { RealTimeConnection } from '@feathersjs/feathers' export class Channel extends EventEmitter { connections: RealTimeConnection[] data: any constructor(connections: RealTimeConnection[] = [], data: any = null) { super() this.connections = connections this.data = data } get length() { return this.connections.length } leave(...connections: RealTimeConnection[]) { connections.forEach((current) => { if (typeof current === 'function') { const callback = current as (connection: RealTimeConnection) => boolean this.leave(...this.connections.filter(callback)) } else { const index = this.connections.indexOf(current) if (index !== -1) { this.connections.splice(index, 1) } } }) if (this.length === 0) { this.emit('empty') } return this } join(...connections: RealTimeConnection[]) { connections.forEach((connection) => { if (connection && this.connections.indexOf(connection) === -1) { this.connections.push(connection) } }) return this } filter(fn: (connection: RealTimeConnection) => boolean) { return new Channel(this.connections.filter(fn), this.data) } send(data: any) { return new Channel(this.connections, data) } }