mirror of
https://github.com/socketio/socket.io.git
synced 2026-01-14 01:17:55 -05:00
The previous signature was not compatible with EventEmitter.emit(). The typescript compilation threw:
```
node_modules/socket.io/dist/namespace.d.ts(89,5): error TS2416: Property 'emit' in type 'Namespace' is not assignable to the same property in base type 'EventEmitter'.
Type '(ev: string, ...args: any[]) => Namespace' is not assignable to type '(event: string | symbol, ...args: any[]) => boolean'.
Type 'Namespace' is not assignable to type 'boolean'.
node_modules/socket.io/dist/socket.d.ts(84,5): error TS2416: Property 'emit' in type 'Socket' is not assignable to the same property in base type 'EventEmitter'.
Type '(ev: string, ...args: any[]) => this' is not assignable to type '(event: string | symbol, ...args: any[]) => boolean'.
Type 'this' is not assignable to type 'boolean'.
Type 'Socket' is not assignable to type 'boolean'.
```
Note: the emit calls cannot be chained anymore:
```js
socket.emit("hello").emit("world"); // will not work anymore
```
41 lines
993 B
TypeScript
41 lines
993 B
TypeScript
import { Namespace } from "./namespace";
|
|
|
|
export class ParentNamespace extends Namespace {
|
|
private static count: number = 0;
|
|
private children: Set<Namespace> = new Set();
|
|
|
|
constructor(server) {
|
|
super(server, "/_" + ParentNamespace.count++);
|
|
}
|
|
|
|
_initAdapter() {}
|
|
|
|
public emit(...args: any[]): boolean {
|
|
this.children.forEach(nsp => {
|
|
nsp._rooms = this._rooms;
|
|
nsp._flags = this._flags;
|
|
nsp.emit.apply(nsp, args);
|
|
});
|
|
this._rooms.clear();
|
|
this._flags = {};
|
|
|
|
return true;
|
|
}
|
|
|
|
createChild(name) {
|
|
const namespace = new Namespace(this.server, name);
|
|
namespace._fns = this._fns.slice(0);
|
|
this.listeners("connect").forEach(listener =>
|
|
// @ts-ignore
|
|
namespace.on("connect", listener)
|
|
);
|
|
this.listeners("connection").forEach(listener =>
|
|
// @ts-ignore
|
|
namespace.on("connection", listener)
|
|
);
|
|
this.children.add(namespace);
|
|
this.server._nsps.set(name, namespace);
|
|
return namespace;
|
|
}
|
|
}
|