mirror of
https://github.com/Monibuca/engine.git
synced 2025-10-22 16:19:22 +08:00
使用反射改造hook
This commit is contained in:
37
hook.go
37
hook.go
@@ -2,6 +2,7 @@ package engine
|
||||
|
||||
import (
|
||||
"context"
|
||||
"reflect"
|
||||
"sync"
|
||||
)
|
||||
|
||||
@@ -21,7 +22,7 @@ const (
|
||||
var Hooks = make(map[string]*RingBuffer)
|
||||
var hookLocker sync.Mutex
|
||||
|
||||
func AddHooks(hooks map[string]func(interface{})) {
|
||||
func AddHooks(hooks map[string]interface{}) {
|
||||
hookLocker.Lock()
|
||||
for name, hook := range hooks {
|
||||
rl, ok := Hooks[name]
|
||||
@@ -30,9 +31,13 @@ func AddHooks(hooks map[string]func(interface{})) {
|
||||
rl.Init(4)
|
||||
Hooks[name] = rl
|
||||
}
|
||||
go func(hooks *RingBuffer, callback func(interface{})) {
|
||||
go func(hooks *RingBuffer, callback interface{}) {
|
||||
vf := reflect.ValueOf(callback)
|
||||
if vf.Kind() != reflect.Func {
|
||||
panic("callback is not a function")
|
||||
}
|
||||
for {
|
||||
callback(hooks.Read())
|
||||
vf.Call(hooks.Read().([]reflect.Value))
|
||||
hooks.MoveNext()
|
||||
}
|
||||
}(rl.Clone(), hook)
|
||||
@@ -40,7 +45,7 @@ func AddHooks(hooks map[string]func(interface{})) {
|
||||
hookLocker.Unlock()
|
||||
}
|
||||
|
||||
func AddHook(name string, callback func(interface{})) {
|
||||
func AddHook(name string, callback interface{}) {
|
||||
hookLocker.Lock()
|
||||
rl, ok := Hooks[name]
|
||||
if !ok {
|
||||
@@ -49,12 +54,16 @@ func AddHook(name string, callback func(interface{})) {
|
||||
Hooks[name] = rl
|
||||
}
|
||||
hookLocker.Unlock()
|
||||
vf := reflect.ValueOf(callback)
|
||||
if vf.Kind() != reflect.Func {
|
||||
panic("callback is not a function")
|
||||
}
|
||||
for hooks := rl.Clone(); ; hooks.MoveNext() {
|
||||
callback(hooks.Read())
|
||||
vf.Call(hooks.Read().([]reflect.Value))
|
||||
}
|
||||
}
|
||||
|
||||
func AddHookWithContext(ctx context.Context, name string, callback func(interface{})) {
|
||||
func AddHookWithContext(ctx context.Context, name string, callback interface{}) {
|
||||
hookLocker.Lock()
|
||||
rl, ok := Hooks[name]
|
||||
if !ok {
|
||||
@@ -62,19 +71,27 @@ func AddHookWithContext(ctx context.Context, name string, callback func(interfac
|
||||
Hooks[name] = rl
|
||||
}
|
||||
hookLocker.Unlock()
|
||||
vf := reflect.ValueOf(callback)
|
||||
if vf.Kind() != reflect.Func {
|
||||
panic("callback is not a function")
|
||||
}
|
||||
for hooks := rl.Clone(); ctx.Err() == nil; hooks.MoveNext() {
|
||||
callback(hooks.Read())
|
||||
vf.Call(hooks.Read().([]reflect.Value))
|
||||
}
|
||||
}
|
||||
|
||||
func TriggerHook(name string, payload interface{}) {
|
||||
func TriggerHook(name string, payload ...interface{}) {
|
||||
args := make([]reflect.Value, len(payload))
|
||||
for i, arg := range payload {
|
||||
args[i] = reflect.ValueOf(&arg).Elem()
|
||||
}
|
||||
hookLocker.Lock()
|
||||
defer hookLocker.Unlock()
|
||||
if rl, ok := Hooks[name]; ok {
|
||||
rl.Write(payload)
|
||||
rl.Write(args)
|
||||
} else {
|
||||
rl = NewRingBuffer(4)
|
||||
Hooks[name] = rl
|
||||
rl.Write(payload)
|
||||
rl.Write(args)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user