From 18aad1e771b425577a954efd7a758a9d75486964 Mon Sep 17 00:00:00 2001 From: Wang Guan Date: Mon, 19 Feb 2024 00:30:41 +0900 Subject: [PATCH 1/2] parent-namespace: change inline Adapter-like to file-private class --- lib/parent-namespace.ts | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/lib/parent-namespace.ts b/lib/parent-namespace.ts index f44b052e3d..07ff881f09 100644 --- a/lib/parent-namespace.ts +++ b/lib/parent-namespace.ts @@ -6,6 +6,7 @@ import type { DefaultEventsMap, EventNamesWithoutAck, } from "./typed-events"; +import {Adapter} from "socket.io-adapter"; import type { BroadcastOptions } from "socket.io-adapter"; import debugModule from "debug"; @@ -33,7 +34,7 @@ export class ParentNamespace< SocketData = any > extends Namespace { private static count: number = 0; - private children: Set< + private readonly children: Set< Namespace > = new Set(); @@ -47,13 +48,7 @@ export class ParentNamespace< * @private */ _initAdapter(): void { - const broadcast = (packet: any, opts: BroadcastOptions) => { - this.children.forEach((nsp) => { - nsp.adapter.broadcast(packet, opts); - }); - }; - // @ts-ignore FIXME is there a way to declare an inner class in TypeScript? - this.adapter = { broadcast }; + this.adapter = new ParentBroadcastAdapter(this, this.children); } public emit>( @@ -112,3 +107,19 @@ export class ParentNamespace< throw new Error("fetchSockets() is not supported on parent namespaces"); } } + +/** + * A dummy adapter that only supports broadcasting to child (concrete) namespaces. + * @private file + */ +class ParentBroadcastAdapter extends Adapter { + constructor(parentNsp: any, private readonly children: Set) { + super(parentNsp); + } + + broadcast(packet: any, opts: BroadcastOptions) { + this.children.forEach((nsp) => { + nsp.adapter.broadcast(packet, opts); + }); + }; +} From 9269b70eef3870ad2047e057095222a684bdc1c5 Mon Sep 17 00:00:00 2001 From: Wang Guan Date: Mon, 19 Feb 2024 23:35:10 +0900 Subject: [PATCH 2/2] make prettier happy --- lib/parent-namespace.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/parent-namespace.ts b/lib/parent-namespace.ts index 07ff881f09..63e7ce56e2 100644 --- a/lib/parent-namespace.ts +++ b/lib/parent-namespace.ts @@ -6,7 +6,7 @@ import type { DefaultEventsMap, EventNamesWithoutAck, } from "./typed-events"; -import {Adapter} from "socket.io-adapter"; +import { Adapter } from "socket.io-adapter"; import type { BroadcastOptions } from "socket.io-adapter"; import debugModule from "debug"; @@ -121,5 +121,5 @@ class ParentBroadcastAdapter extends Adapter { this.children.forEach((nsp) => { nsp.adapter.broadcast(packet, opts); }); - }; + } }