Skip to content

Commit

Permalink
优化
Browse files Browse the repository at this point in the history
  • Loading branch information
deatil committed Jul 28, 2024
1 parent 06cbdc2 commit 18a86c6
Show file tree
Hide file tree
Showing 6 changed files with 511 additions and 73 deletions.
16 changes: 12 additions & 4 deletions pkg/lakego-pkg/go-events/events/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@ import (
"strings"
)

// 接口
// Action Subscribe interface
type IActionSubscribe interface {
Subscribe(*Action)
}

// Action event
type Action struct {
Event
}

// new Action event
func NewAction() *Action {
return &Action{
Event: Event{
Expand All @@ -23,6 +25,7 @@ func NewAction() *Action {
}

// 注册事件订阅者
// Subscribe
func (this *Action) Subscribe(subscribers ...any) *Action {
if len(subscribers) == 0 {
return this
Expand All @@ -40,6 +43,7 @@ func (this *Action) Subscribe(subscribers ...any) *Action {
return this
}

// Trigger funcs
func (this *Action) Trigger(event any, params ...any) {
this.mu.RLock()
defer this.mu.RUnlock()
Expand All @@ -49,19 +53,23 @@ func (this *Action) Trigger(event any, params ...any) {
listeners := this.listener[eventName]

if _, ok := event.(string); ok {
if strings.Contains(eventName, ".") {
if strings.Contains(eventName, ".*") {
needSort := false
events := strings.SplitN(eventName, ".", 2)

for e, listener := range this.listener {
if events[1] == "*" && strings.HasPrefix(e, events[0] + ".") {
listeners = append(listeners, listener...)
needSort = true
}
}

if needSort {
this.listenerSort(listeners, "desc")
}
}
}

this.listenerSort(listeners, "desc")

for _, listener := range listeners {
this.dispatch(listener.Listener, params)
}
Expand Down
35 changes: 28 additions & 7 deletions pkg/lakego-pkg/go-events/events/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,26 @@ import (
)

// 前缀接口
// SubscribePrefix Interface
type ISubscribePrefix interface {
EventPrefix() string
}

// 排序接口
// SubscribeSort Interface
type ISubscribeSort interface {
EventSort() int
}

// 监听器数据
// Listener data
type Listener struct {
Listener any
Sort int
Key string
}

// Event
type Event struct {
mu sync.RWMutex
listener map[string][]Listener
Expand Down Expand Up @@ -63,6 +67,7 @@ func (this *Event) Observe(observer any, prefix string, sort int) *Event {
}

// 注册事件监听
// add one Event Listen
func (this *Event) Listen(event any, listener any, sort int) {
this.mu.Lock()
defer this.mu.Unlock()
Expand All @@ -78,9 +83,13 @@ func (this *Event) Listen(event any, listener any, sort int) {
Sort: sort,
Key: formatName(listener),
})

// run sort
this.listenerSort(this.listener[eventName], "desc")
}

// 移除监听事件
// remove one Event Listen
func (this *Event) RemoveListener(event string, listener any, sort int) bool {
this.mu.Lock()
defer this.mu.Unlock()
Expand All @@ -100,6 +109,7 @@ func (this *Event) RemoveListener(event string, listener any, sort int) bool {
}

// 事件是否在监听
// exists one Event Listen
func (this *Event) HasListener(event string, listener any) bool {
if listener == nil {
return this.HasListeners()
Expand Down Expand Up @@ -127,6 +137,7 @@ func (this *Event) HasListener(event string, listener any) bool {
}

// 是否有事件监听
// exists has some listeners
func (this *Event) HasListeners() bool {
this.mu.RLock()
defer this.mu.RUnlock()
Expand All @@ -141,6 +152,7 @@ func (this *Event) HasListeners() bool {
}

// 获取所有事件监听
// return all listeners
func (this *Event) GetListeners() map[string][]Listener {
this.mu.RLock()
defer this.mu.RUnlock()
Expand All @@ -149,6 +161,7 @@ func (this *Event) GetListeners() map[string][]Listener {
}

// 是否存在事件监听点
// has set event
func (this *Event) Exists(event string) bool {
this.mu.RLock()
defer this.mu.RUnlock()
Expand All @@ -161,6 +174,7 @@ func (this *Event) Exists(event string) bool {
}

// 移除事件监听点
// remove event
func (this *Event) Remove(event string) {
this.mu.Lock()
defer this.mu.Unlock()
Expand All @@ -169,6 +183,7 @@ func (this *Event) Remove(event string) {
}

// 清空
// clear all events
func (this *Event) Clear() {
this.mu.Lock()
defer this.mu.Unlock()
Expand All @@ -177,19 +192,25 @@ func (this *Event) Clear() {
}

// 执行事件调度
// dispatch event listeners
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.(reflect.Value); ok {
return this.pool.Call(eventMethod, params)
var call any

if _, ok := event.([]any); ok {
call = event
} else if this.pool.IsFunc(event) {
call = event
} else if _, ok := event.(reflect.Value); ok {
call = event
} else {
method := "Handle"

return this.pool.CallStructMethod(event, method, params)
call = []any{event, "Handle"}
}

return this.pool.Call(call, params)
}

// 排序
// listeners sort
func (this *Event) listenerSort(listeners []Listener, typ string) {
sort.Slice(listeners, func(i, j int) bool {
if typ == "desc" {
Expand Down
Loading

0 comments on commit 18a86c6

Please sign in to comment.