feat: add once listeners, ability to remove listeners and remove listener on error

new-emitter
TorchedSammy 2022-08-01 23:52:59 -04:00
parent ea2791859c
commit e4110fa4bc
Signed by: sammyette
GPG Key ID: 904FC49417B44DCD
2 changed files with 43 additions and 6 deletions

View File

@ -13,10 +13,11 @@ const (
luaListener
)
type Recoverer func(event string, handler, err interface{})
type Recoverer func(event string, handler *Listener, err interface{})
type Listener struct{
typ listenerType
once bool
caller func(...interface{})
luaCaller *rt.Closure
}
@ -74,6 +75,10 @@ func (b *Bait) Emit(event string, args ...interface{}) {
} else {
handle.caller(args...)
}
if handle.once {
b.Off(event, handle)
}
}
}
@ -97,6 +102,38 @@ func (b *Bait) OnLua(event string, handler *rt.Closure) *Listener {
return listener
}
func (b *Bait) Off(event string, listener *Listener) {
handles := b.handlers[event]
for i, handle := range handles {
if handle == listener {
b.handlers[event] = append(handles[:i], handles[i + 1:]...)
}
}
}
func (b *Bait) Once(event string, handler func(...interface{})) *Listener {
listener := &Listener{
typ: goListener,
once: true,
caller: handler,
}
b.addListener(event, listener)
return listener
}
func (b *Bait) OnceLua(event string, handler *rt.Closure) *Listener {
listener := &Listener{
typ: luaListener,
once: true,
luaCaller: handler,
}
b.addListener(event, listener)
return listener
}
func (b *Bait) SetRecoverer(recoverer Recoverer) {
b.recoverer = recoverer
}
@ -108,7 +145,8 @@ func (b *Bait) addListener(event string, listener *Listener) {
b.handlers[event] = append(b.handlers[event], listener)
}
func (b *Bait) callRecoverer(event string, handler, err interface{}) {
func (b *Bait) callRecoverer(event string, handler *Listener, err interface{}) {
if b.recoverer == nil {
panic(err)
}
@ -204,9 +242,7 @@ func (b *Bait) bcatchOnce(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
}
// todo: add once
b.On(name, func(args ...interface{}) {
handleHook(t, c, name, catcher, args...)
})
b.OnceLua(name, catcher)
return c.Next(), nil
}

3
lua.go
View File

@ -43,8 +43,9 @@ func luaInit() {
lib.LoadLibs(l, cmds.Loader)
hooks = bait.New(l)
hooks.SetRecoverer(func(event string, handler, err interface{}) {
hooks.SetRecoverer(func(event string, handler *bait.Listener, err interface{}) {
fmt.Println("Error in", event, "event:", err)
hooks.Off(event, handler)
})
lib.LoadLibs(l, hooks.Loader)