Skip to content

Commit

Permalink
add generic ArrayIn function with minimal type casting (#411)
Browse files Browse the repository at this point in the history
  • Loading branch information
eos175 authored Aug 18, 2024
1 parent d56c19b commit 2cd4957
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 28 deletions.
2 changes: 1 addition & 1 deletion server/internal/desktop/clipboard/clipboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"unsafe"
)

var mu = sync.Mutex{}
var mu sync.Mutex

func Read() string {
mu.Lock()
Expand Down
18 changes: 8 additions & 10 deletions server/internal/session/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ func (manager *SessionManager) Clear() error {
return nil
}

func (manager *SessionManager) Broadcast(v interface{}, exclude interface{}) error {
func (manager *SessionManager) Broadcast(v interface{}, exclude []string) error {
manager.mu.Lock()
defer manager.mu.Unlock()

Expand All @@ -201,10 +201,8 @@ func (manager *SessionManager) Broadcast(v interface{}, exclude interface{}) err
continue
}

if exclude != nil {
if in, _ := utils.ArrayIn(id, exclude); in {
continue
}
if in, _ := utils.ArrayIn(id, exclude); in {
continue
}

if err := session.Send(v); err != nil {
Expand All @@ -215,7 +213,7 @@ func (manager *SessionManager) Broadcast(v interface{}, exclude interface{}) err
return nil
}

func (manager *SessionManager) AdminBroadcast(v interface{}, exclude interface{}) error {
func (manager *SessionManager) AdminBroadcast(v interface{}, exclude []string) error {
manager.mu.Lock()
defer manager.mu.Unlock()

Expand All @@ -224,10 +222,8 @@ func (manager *SessionManager) AdminBroadcast(v interface{}, exclude interface{}
continue
}

if exclude != nil {
if in, _ := utils.ArrayIn(id, exclude); in {
continue
}
if in, _ := utils.ArrayIn(id, exclude); in {
continue
}

if err := session.Send(v); err != nil {
Expand All @@ -241,3 +237,5 @@ func (manager *SessionManager) AdminBroadcast(v interface{}, exclude interface{}
func (manager *SessionManager) GetEventsChannel() chan types.SessionEvent {
return manager.eventsChannel
}

var _ types.SessionManager = (*SessionManager)(nil)
4 changes: 2 additions & 2 deletions server/internal/types/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ type SessionManager interface {
Admins() []*Member
Destroy(id string)
Clear() error
Broadcast(v interface{}, exclude interface{}) error
AdminBroadcast(v interface{}, exclude interface{}) error
Broadcast(v interface{}, exclude []string) error
AdminBroadcast(v interface{}, exclude []string) error
GetEventsChannel() chan SessionEvent
}
21 changes: 6 additions & 15 deletions server/internal/utils/array.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,13 @@
package utils

import (
"reflect"
)

func ArrayIn(val interface{}, array interface{}) (exists bool, index int) {
exists = false
func ArrayIn[T comparable](val T, array []T) (exists bool, index int) {
index = -1

switch reflect.TypeOf(array).Kind() {
case reflect.Slice:
s := reflect.ValueOf(array)
for i := 0; i < s.Len(); i++ {
if reflect.DeepEqual(val, s.Index(i).Interface()) {
index = i
exists = true
return
}
for i, v := range array {
if v == val {
index = i
exists = true
return
}
}

Expand Down

0 comments on commit 2cd4957

Please sign in to comment.