Skip to content

Commit

Permalink
优化
Browse files Browse the repository at this point in the history
  • Loading branch information
deatil committed Jul 26, 2024
1 parent ea4e04a commit 85441f5
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
5 changes: 4 additions & 1 deletion events/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ func (this *Action) Subscribe(subscribers ...any) *Action {
}

func (this *Action) Trigger(event any, params ...any) {
this.mu.RLock()
defer this.mu.RUnlock()

eventName := formatName(event)

listeners := this.listener[eventName]
Expand All @@ -64,6 +67,6 @@ func (this *Action) Trigger(event any, params ...any) {
this.listenerSort(listeners, "desc")

for _, listener := range listeners {
this.Dispatch(listener.Listener, params)
this.dispatch(listener.Listener, params)
}
}
28 changes: 27 additions & 1 deletion events/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package events

import (
"sort"
"sync"
"strings"
"reflect"
)
Expand Down Expand Up @@ -33,6 +34,7 @@ type Listener struct {
}

type Event struct {
mu sync.RWMutex
listener map[string][]Listener
pool *Pool
}
Expand Down Expand Up @@ -71,6 +73,9 @@ func (this *Event) Observe(observer any, prefix string, sort int) *Event {

// 注册事件监听
func (this *Event) Listen(event any, listener any, sort int) {
this.mu.Lock()
defer this.mu.Unlock()

eventName := formatName(event)

if _, ok := this.listener[eventName]; !ok {
Expand All @@ -86,6 +91,9 @@ func (this *Event) Listen(event any, listener any, sort int) {

// 移除监听事件
func (this *Event) RemoveListener(event string, listener any, sort int) bool {
this.mu.Lock()
defer this.mu.Unlock()

key := formatName(listener)

_, exists := this.listener[event]
Expand All @@ -106,6 +114,9 @@ func (this *Event) HasListener(event string, listener any) bool {
return this.HasListeners()
}

this.mu.RLock()
defer this.mu.RUnlock()

if _, exists := this.listener[event]; !exists {
return false
}
Expand All @@ -126,6 +137,9 @@ func (this *Event) HasListener(event string, listener any) bool {

// 是否有事件监听
func (this *Event) HasListeners() bool {
this.mu.RLock()
defer this.mu.RUnlock()

for _, listener := range this.listener {
if len(listener) > 0 {
return true
Expand All @@ -137,11 +151,17 @@ func (this *Event) HasListeners() bool {

// 获取所有事件监听
func (this *Event) GetListeners() map[string][]Listener {
this.mu.RLock()
defer this.mu.RUnlock()

return this.listener
}

// 是否存在事件监听点
func (this *Event) Exists(event string) bool {
this.mu.RLock()
defer this.mu.RUnlock()

if _, exists := this.listener[event]; exists {
return true
}
Expand All @@ -151,16 +171,22 @@ func (this *Event) Exists(event string) bool {

// 移除事件监听点
func (this *Event) Remove(event string) {
this.mu.Lock()
defer this.mu.Unlock()

delete(this.listener, event)
}

// 清空
func (this *Event) Clear() {
this.mu.Lock()
defer this.mu.Unlock()

this.listener = make(map[string][]Listener)
}

// 执行事件调度
func (this *Event) Dispatch(event any, params []any) any {
func (this *Event) dispatch(event any, params []any) any {
if this.pool.IsFunc(event) {
return this.pool.CallFunc(event, params)
} else if eventMethod, ok := event.(EventSubscribe); ok {
Expand Down
5 changes: 4 additions & 1 deletion events/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ func (this *Filter) Subscribe(subscribers ...any) *Filter {
}

func (this *Filter) Trigger(event any, value any, params ...any) any {
this.mu.RLock()
defer this.mu.RUnlock()

eventName := formatName(event)
if this.pool.IsStruct(event) {
value = event
Expand All @@ -71,7 +74,7 @@ func (this *Filter) Trigger(event any, value any, params ...any) any {
for _, listener := range listeners {
tmp = append([]any{result}, tmp...)

result = this.Dispatch(listener.Listener, tmp)
result = this.dispatch(listener.Listener, tmp)
tmp = params
}

Expand Down

0 comments on commit 85441f5

Please sign in to comment.