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 f15e31f commit 6ae0e94
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 7 deletions.
16 changes: 12 additions & 4 deletions 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
17 changes: 17 additions & 0 deletions 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,6 +192,7 @@ 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)
Expand All @@ -190,6 +206,7 @@ func (this *Event) dispatch(event any, params []any) any {
}

// 排序
// listeners sort
func (this *Event) listenerSort(listeners []Listener, typ string) {
sort.Slice(listeners, func(i, j int) bool {
if typ == "desc" {
Expand Down
52 changes: 52 additions & 0 deletions events/events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -519,3 +519,55 @@ func Test_FilterStar(t *testing.T) {
check := "init => run test33 => run test5 => run test3 => "
eq(test3, check, "Test_FilterStar")
}

func Test_ActionSort(t *testing.T) {
eq := assertDeepEqualT(t)

action := NewAction()

test3 := ""
listener3 := func() {
test3 += "run test3 => "
}
listener33 := func() {
test3 += "run test33 => "
}
listener5 := func() {
test3 += "run test5 => "
}

action.Listen("Test_ActionSort", listener3, 1)
action.Listen("Test_ActionSort", listener33, 6)
action.Listen("Test_ActionSort", listener5, 5)

action.Trigger("Test_ActionSort")

check := "run test33 => run test5 => run test3 => "
eq(test3, check, "Test_ActionStar")
}

func Test_FilterSort(t *testing.T) {
eq := assertDeepEqualT(t)

filter := NewFilter()

listener3 := func(val string) string {
return val + "run test22 => "
}
listener33 := func(val string) string {
return val + "run test33 => "
}
listener5 := func(val string) string {
return val + "run test55 => "
}

filter.Listen("Test_FilterSort", listener3, 1)
filter.Listen("Test_FilterSort", listener33, 6)
filter.Listen("Test_FilterSort", listener5, 5)

init := "init => "
test3 := filter.Trigger("Test_FilterSort", init)

check := "init => run test33 => run test55 => run test22 => "
eq(test3, check, "Test_FilterStar")
}
14 changes: 11 additions & 3 deletions events/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import (
)

// 接口
// Filter Subscribe interface
type IFilterSubscribe interface {
Subscribe(*Filter)
}

// Filter
type Filter struct {
Event
}
Expand All @@ -23,6 +25,7 @@ func NewFilter() *Filter {
}

// 注册事件订阅者
// Subscribe
func (this *Filter) Subscribe(subscribers ...any) *Filter {
if len(subscribers) == 0 {
return this
Expand All @@ -48,6 +51,7 @@ func (this *Filter) Trigger(event any, params ...any) any {
var value any

eventName := formatName(event)

if this.pool.IsStruct(event) {
value = event
} else {
Expand All @@ -62,19 +66,23 @@ func (this *Filter) Trigger(event any, params ...any) 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")

tmp := params
result := value
for _, listener := range listeners {
Expand Down
8 changes: 8 additions & 0 deletions events/helper.go
Original file line number Diff line number Diff line change
@@ -1,41 +1,49 @@
package events

// 注册操作
// Add Action
func AddAction(event any, listener any, sort int) {
Default.Action().Listen(event, listener, sort)
}

// 触发操作
// Do Action
func DoAction(event any, params ...any) {
Default.Action().Trigger(event, params...)
}

// 移除操作
// Remove Action
func RemoveAction(event string, listener any, sort int) bool {
return Default.Action().RemoveListener(event, listener, sort)
}

// 是否有操作
// Has Action
func HasAction(event string, listener any) bool {
return Default.Action().HasListener(event, listener)
}

// 注册过滤器
// Add Filter
func AddFilter(event any, listener any, sort int) {
Default.Filter().Listen(event, listener, sort)
}

// 触发过滤器
// Apply Filters
func ApplyFilters(event any, params ...any) any {
return Default.Filter().Trigger(event, params...)
}

// 移除过滤器
// Remove Filter
func RemoveFilter(event string, listener any, sort int) bool {
return Default.Filter().RemoveListener(event, listener, sort)
}

// 是否有过滤器
// Has Filter
func HasFilter(event string, listener any) bool {
return Default.Filter().HasListener(event, listener)
}

0 comments on commit 6ae0e94

Please sign in to comment.