Bloc icon indicating copy to clipboard operation
Bloc copied to clipboard

#when:do API

Open ELePors opened this issue 1 year ago • 5 comments

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...

ELePors avatar May 23 '23 12:05 ELePors

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 ?

tinchodias avatar Jun 02 '23 00:06 tinchodias

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

labordep avatar Nov 01 '23 13:11 labordep

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.

labordep avatar Nov 01 '23 13:11 labordep

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

plantec avatar Nov 01 '23 13:11 plantec

when:doOnce: is ok because it manage the lifecycle of the event handler itself.

labordep avatar Nov 01 '23 13:11 labordep