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 29ce9e1 commit 42a34fd
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 8 deletions.
19 changes: 18 additions & 1 deletion events/events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,8 +402,25 @@ func Test_EventStructDataFilter(t *testing.T) {
args := "index data"
res := filter.Trigger(TestEventStructDataFilter{
Data: initData,
}, nil, args)
}, args)

check := "init => index data"
eq(res, check, "Test_EventStructDataFilter")
}

func Test_FilterPanic(t *testing.T) {
defer func() {
if e := recover(); e == nil {
t.Error("should panic error")
}
}()

filter := NewFilter()

// 事件注册
filter.Listen("panic", func(val string) string {
return val
}, DefaultSort)

_ = filter.Trigger("panic")
}
11 changes: 10 additions & 1 deletion events/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,22 @@ func (this *Filter) Subscribe(subscribers ...any) *Filter {
return this
}

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

var value any

eventName := formatName(event)
if this.pool.IsStruct(event) {
value = event
} else {
if len(params) == 0 {
panic("go-events: Filter trigger func need value")
}

value = params[0]
params = params[1:]
}

listeners := this.listener[eventName]
Expand Down
4 changes: 2 additions & 2 deletions events/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ func AddFilter(event any, listener any, sort int) {
}

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

// 移除过滤器
Expand Down
8 changes: 4 additions & 4 deletions events/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func NewPool() *Pool {
func (this *Pool) CallFunc(fn any, args []any) any {
fnObject := reflect.ValueOf(fn)
if !(fnObject.IsValid() && fnObject.Kind() == reflect.Func) {
panic("pool: func type error")
panic("go-events: call func type error")
}

return this.Call(fnObject, args)
Expand All @@ -32,12 +32,12 @@ func (this *Pool) CallStructMethod(in any, method string, args []any) any {
typ := reflect.TypeOf(in)

if typ.Kind() != reflect.Pointer && typ.Kind() != reflect.Struct {
panic("pool: struct type error")
panic("go-events: call struct type error")
}

newMethod, ok := typ.MethodByName(method)
if !ok {
panic("pool: method not exists")
panic("go-events: call method not exists")
}

args = append([]any{in}, args...)
Expand All @@ -50,7 +50,7 @@ func (this *Pool) Call(fn reflect.Value, args []any) any {

numIn := fnType.NumIn()
if len(args) != numIn {
err := fmt.Sprintf("pool: func params error (args %d, func args %d)", len(args), numIn)
err := fmt.Sprintf("go-events: func params error (args %d, func args %d)", len(args), numIn)
panic(err)
}

Expand Down

0 comments on commit 42a34fd

Please sign in to comment.