Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | 2x 2x 57x 57x 57x 38x 3x 3x 3x 11x 11x 10x 4x 11x 34x 46x 34x 1x 3x | import EventEmitter from 'events';
export interface RealTimeConnection {
[key: string]: any;
}
export type ConnectionFilter = (connection: RealTimeConnection) => boolean;
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(first: ConnectionFilter|RealTimeConnection, ...rest: RealTimeConnection[]): Channel {
if (typeof first === 'function') {
const callback = first as ConnectionFilter;
const [ c, ...others ] = this.connections.filter(callback);
return this.leave(c, ...others);
}
[ first, ...rest ].forEach(current => {
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 (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);
}
}
|