Bloc
Bloc copied to clipboard
#when:do API
Hello, when you call a #when:do: on a BlElement, the method calls a #addEventHandlerOn:do: without returning the instance of eventHandler recorded. It must be returned in order do removal on some events handlers.
example :
self when: BlMouseMoveEvent do: [ :event | self mouseMovedTo: event ]
returns the receiver (self) of the message...
It happened to me once, I agree we can answer the handler.
Also, I wonder we need an additional change in this API... In Pharo 12, Announcer>>when:do:
is deprecated in favor of Announcer>>when:do:for:
, with a third argument. I'm not sure if the reason is the ephemerons, or the clean blocks.
@tesonep ?
How to remove a when:do: listener? when:do: should return the listener to remove it later if necessary.
I propose to do that:
BlElement>>when: anEventClass do: aBlock
^ self addEventHandlerOn: anEventClass do: aBlock
Of course this on TBlEventTarget trait
After to discuss with @plantec, it's appears than when:do: should be deprecated because this is not symmetric and a little confusing because the add handler is not clear.
instead of when:do: one should use addEventHandler: as in: clickHandler := (BlEventHandler on: BlClickEvent do: [ :event | do something ]). self addEventHandler: clickHandler.
then one can remove the handler nicely with: self removeHandler: clickHandler
when:doOnce:
is ok because it manage the lifecycle of the event handler itself.