Skip to content

Commit

Permalink
(update): Optimized code.
Browse files Browse the repository at this point in the history
  • Loading branch information
zishang520 committed Nov 6, 2023
1 parent dd9051c commit 4596c55
Showing 1 changed file with 36 additions and 18 deletions.
54 changes: 36 additions & 18 deletions socket/broadcast-operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,35 @@ type BroadcastOperator struct {
flags *BroadcastFlags
}

func MakeBroadcastOperator() *BroadcastOperator {
b := &BroadcastOperator{
rooms: types.NewSet[Room](),
exceptRooms: types.NewSet[Room](),
flags: &BroadcastFlags{},
}

return b
}

func NewBroadcastOperator(adapter Adapter, rooms *types.Set[Room], exceptRooms *types.Set[Room], flags *BroadcastFlags) *BroadcastOperator {
b := &BroadcastOperator{}
b := MakeBroadcastOperator()

b.Construct(adapter, rooms, exceptRooms, flags)

return b
}

func (b *BroadcastOperator) Construct(adapter Adapter, rooms *types.Set[Room], exceptRooms *types.Set[Room], flags *BroadcastFlags) {
b.adapter = adapter
if rooms == nil {
b.rooms = types.NewSet[Room]()
} else {
if rooms != nil {
b.rooms = rooms
}
if exceptRooms == nil {
b.exceptRooms = types.NewSet[Room]()
} else {
if exceptRooms != nil {
b.exceptRooms = exceptRooms
}
if flags == nil {
b.flags = &BroadcastFlags{}
} else {
if flags != nil {
b.flags = flags
}

return b
}

// Targets a room when emitting.
Expand All @@ -54,7 +63,8 @@ func NewBroadcastOperator(adapter Adapter, rooms *types.Set[Room], exceptRooms *
// io.To("room-101").To("room-102").Emit("foo", "bar")
//
// Param: Room - a `Room`, or a `Room` slice to expand
// Return: a new `*BroadcastOperator` instance for chaining
//
// Return: a new [BroadcastOperator] instance for chaining
func (b *BroadcastOperator) To(room ...Room) *BroadcastOperator {
rooms := types.NewSet(b.rooms.Keys()...)
rooms.Add(room...)
Expand All @@ -67,7 +77,8 @@ func (b *BroadcastOperator) To(room ...Room) *BroadcastOperator {
// io.In("room-101").DisconnectSockets(false)
//
// Param: Room - a `Room`, or a `Room` slice to expand
// Return: a new `*BroadcastOperator` instance for chaining
//
// Return: a new [BroadcastOperator] instance for chaining
func (b *BroadcastOperator) In(room ...Room) *BroadcastOperator {
return b.To(room...)
}
Expand All @@ -85,7 +96,8 @@ func (b *BroadcastOperator) In(room ...Room) *BroadcastOperator {
// io.Except("room-101").Except("room-102").Emit("foo", "bar")
//
// Param: Room - a `Room`, or a `Room` slice to expand
// Return: a new `*BroadcastOperator` instance for chaining
//
// Return: a new [BroadcastOperator] instance for chaining
func (b *BroadcastOperator) Except(room ...Room) *BroadcastOperator {
exceptRooms := types.NewSet(b.exceptRooms.Keys()...)
exceptRooms.Add(room...)
Expand All @@ -95,6 +107,10 @@ func (b *BroadcastOperator) Except(room ...Room) *BroadcastOperator {
// Sets the compress flag.
//
// io.Compress(false).Emit("hello")
//
// Param: compress - if `true`, compresses the sending data
//
// Return: a new [BroadcastOperator] instance
func (b *BroadcastOperator) Compress(compress bool) *BroadcastOperator {
flags := *b.flags
flags.Compress = compress
Expand All @@ -117,7 +133,7 @@ func (b *BroadcastOperator) Volatile() *BroadcastOperator {
// // the “foo” event will be broadcast to all connected clients on this node
// io.Local().Emit("foo", "bar")
//
// Return: a new `*BroadcastOperator` instance for chaining
// Return: a new [BroadcastOperator] instance for chaining
func (b *BroadcastOperator) Local() *BroadcastOperator {
flags := *b.flags
flags.Local = true
Expand All @@ -133,6 +149,8 @@ func (b *BroadcastOperator) Local() *BroadcastOperator {
// fmt.Println(args) // one response per client
// }
// })
//
// Param: timeout
func (b *BroadcastOperator) Timeout(timeout time.Duration) *BroadcastOperator {
flags := *b.flags
flags.Timeout = &timeout
Expand Down Expand Up @@ -261,7 +279,7 @@ func (b *BroadcastOperator) EmitWithAck(ev string, args ...any) func(func([]any,

// Gets a list of clients.
//
// Deprecated: this method will be removed in the next major release, please use [Server.ServerSideEmit] or [FetchSockets] instead.
// Deprecated: this method will be removed in the next major release, please use [Server#serverSideEmit] or [FetchSockets] instead.
func (b *BroadcastOperator) AllSockets() (*types.Set[SocketId], error) {
if b.adapter == nil {
return nil, errors.New("No adapter for this namespace, are you trying to get the list of clients of a dynamic namespace?")
Expand All @@ -271,7 +289,7 @@ func (b *BroadcastOperator) AllSockets() (*types.Set[SocketId], error) {

// Returns the matching socket instances. This method works across a cluster of several Socket.IO servers.
//
// Note: this method also works within a cluster of multiple Socket.IO servers, with a compatible Adapter.
// Note: this method also works within a cluster of multiple Socket.IO servers, with a compatible [Adapter].
//
// io.FetchSockets()(func(sockets []*RemoteSocket, _ error){
// // return all Socket instances
Expand Down

0 comments on commit 4596c55

Please sign in to comment.