Files
socket.io/lib/parent-namespace.ts
Damien Arrachequesne 50671d984a fix(typings): update the signature of the emit method
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
```
2020-11-08 00:07:56 +01:00

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;
}
}