Skip to content

Commit

Permalink
refactor: create specific adapter for parent namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
zishang520 committed Apr 30, 2024
1 parent 8068acc commit 045bf0f
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 99 deletions.
93 changes: 0 additions & 93 deletions socket/broadcast-adapter.go

This file was deleted.

45 changes: 45 additions & 0 deletions socket/parent-broadcast-adapter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package socket

import (
"github.com/zishang520/engine.io/v2/types"
"github.com/zishang520/socket.io-go-parser/v2/parser"
)

type (
// A dummy adapter that only supports broadcasting to child (concrete) namespaces.
parentBroadcastAdapter struct {
Adapter

children *types.Set[*Namespace]
}
)

func MakeParentBroadcastAdapter(children *types.Set[*Namespace]) Adapter {
s := &parentBroadcastAdapter{
Adapter: MakeAdapter(),

children: children,
}

s.Prototype(s)

return s
}

func NewParentBroadcastAdapter(nsp NamespaceInterface, children *types.Set[*Namespace]) Adapter {
s := MakeParentBroadcastAdapter(children)

s.Construct(nsp)

return s
}

func (s *parentBroadcastAdapter) Construct(nsp NamespaceInterface) {
s.Adapter.Construct(nsp)
}

func (s *parentBroadcastAdapter) Broadcast(packet *parser.Packet, opts *BroadcastOptions) {
for _, nsp := range s.children.Keys() {
nsp.Adapter().Broadcast(packet, opts)
}
}
7 changes: 1 addition & 6 deletions socket/parent-namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

"github.com/zishang520/engine.io/v2/log"
"github.com/zishang520/engine.io/v2/types"
"github.com/zishang520/socket.io-go-parser/v2/parser"
)

var (
Expand Down Expand Up @@ -55,11 +54,7 @@ func NewParentNamespace(server *Server) *ParentNamespace {
}

func (p *ParentNamespace) InitAdapter() {
p.adapter = BroadcastAdapter(func(packet *parser.Packet, opts *BroadcastOptions) {
for _, nsp := range p.children.Keys() {
nsp.Adapter().Broadcast(packet, opts)
}
})
p.adapter = NewParentBroadcastAdapter(p, p.children)
}

func (p *ParentNamespace) Emit(ev string, args ...any) error {
Expand Down

0 comments on commit 045bf0f

Please sign in to comment.