You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If you create multiple closures using the same anonymous function, they all have the same pointer address because of how Go works. If you add these anonymous functions to the event emitter, removal of one of the listeners results in the removal of all of them.
package main
import (
"fmt""github.com/chuckpreslar/emission"
)
typeactionfunc()
funcgetCallback(myNumberuint32) action {
fn:=func() {
fmt.Printf("number is %d\n", myNumber)
}
returnfn
}
funcmain() {
five:=getCallback(5)
six:=getCallback(6)
five()
six()
fmt.Printf("five addr %p six addr %p\n", five, six) // <-- same address printedemitter:=emission.NewEmitter()
emitter.On("test",five)
emitter.On("test",six)
emitter.RemoveListener("test",five) // <-- removes both five and sixemitter.EmitSync("test") // <-- does nothing (no listeners)
}
In this example, local vars five and six hold unique closures, which if executed will print different messages; however they have the same address. As the emitter tracks listeners by address, if we add both of these to the emitter on the same event, then remove one, it will remove both.
The text was updated successfully, but these errors were encountered:
tul
added a commit
to tul/emission
that referenced
this issue
Apr 25, 2018
required changing the interface, you can no longer remove listeners by
function reference, but have to use an explicit `handle` which is returned
when your listener is added
I've proposed a fix in pull request #16, but as it breaks the existing interface it probably isn't something you'll want to merge - see what you think. Thanks!
If you create multiple closures using the same anonymous function, they all have the same pointer address because of how Go works. If you add these anonymous functions to the event emitter, removal of one of the listeners results in the removal of all of them.
In this example, local vars
five
andsix
hold unique closures, which if executed will print different messages; however they have the same address. As the emitter tracks listeners by address, if we add both of these to the emitter on the same event, then remove one, it will remove both.The text was updated successfully, but these errors were encountered: