perf: reslice listener slice instead of trying to do ordered move with append

new-emitter
TorchedSammy 2022-08-02 15:01:12 -04:00
parent e4110fa4bc
commit d6a86b0829
Signed by: sammyette
GPG Key ID: 904FC49417B44DCD
1 changed files with 8 additions and 2 deletions

View File

@ -107,7 +107,7 @@ func (b *Bait) Off(event string, listener *Listener) {
for i, handle := range handles { for i, handle := range handles {
if handle == listener { if handle == listener {
b.handlers[event] = append(handles[:i], handles[i + 1:]...) b.removeListener(event, i)
} }
} }
} }
@ -146,6 +146,13 @@ func (b *Bait) addListener(event string, listener *Listener) {
b.handlers[event] = append(b.handlers[event], listener) b.handlers[event] = append(b.handlers[event], listener)
} }
func (b *Bait) removeListener(event string, idx int) {
b.handlers[event][idx] = b.handlers[event][len(b.handlers[event]) - 1]
b.handlers[event] = b.handlers[event][:len(b.handlers[event]) - 1]
}
func (b *Bait) callRecoverer(event string, handler *Listener, err interface{}) { func (b *Bait) callRecoverer(event string, handler *Listener, err interface{}) {
if b.recoverer == nil { if b.recoverer == nil {
panic(err) panic(err)
@ -241,7 +248,6 @@ func (b *Bait) bcatchOnce(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
return nil, err return nil, err
} }
// todo: add once
b.OnceLua(name, catcher) b.OnceLua(name, catcher)
return c.Next(), nil return c.Next(), nil