feat(bait): add release function to remove event listener

new-emitter
TorchedSammy 2022-08-02 15:08:24 -04:00
parent d6a86b0829
commit a6f53b3c43
Signed by: sammyette
GPG Key ID: 904FC49417B44DCD
1 changed files with 22 additions and 0 deletions

View File

@ -112,6 +112,16 @@ func (b *Bait) Off(event string, listener *Listener) {
}
}
func (b *Bait) OffLua(event string, handler *rt.Closure) {
handles := b.handlers[event]
for i, handle := range handles {
if handle.luaCaller == handler {
b.removeListener(event, i)
}
}
}
func (b *Bait) Once(event string, handler func(...interface{})) *Listener {
listener := &Listener{
typ: goListener,
@ -165,6 +175,7 @@ func (b *Bait) loaderFunc(rtm *rt.Runtime) (rt.Value, func()) {
"catch": util.LuaExport{b.bcatch, 2, false},
"catchOnce": util.LuaExport{b.bcatchOnce, 2, false},
"throw": util.LuaExport{b.bthrow, 1, true},
"release": util.LuaExport{b.brelease, 2, false},
}
mod := rt.NewTable()
util.SetExports(rtm, mod, exports)
@ -252,3 +263,14 @@ func (b *Bait) bcatchOnce(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
return c.Next(), nil
}
func (b *Bait) brelease(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
name, catcher, err := util.HandleStrCallback(t, c)
if err != nil {
return nil, err
}
b.OffLua(name, catcher)
return c.Next(), nil
}