dojo / interfaces

🚀 Dojo 2 - common interfaces and types.
http://dojo.io
Other
1 stars 14 forks source link

Evented and targetted event objects #27

Closed kitsonk closed 8 years ago

kitsonk commented 8 years ago

Currently Evented is constrained to only accept an EventTargettedObject being passed to an event listener in Evented#on, though Evented#emit does not constrain it the same way.

Even though ideally most events on Evented should be EventTargettedObject, in situations where other events, or foreign events (like DOM events) are relayed, the event.target may not exist, or may not be constrained to the same type of the emitter.

agubler commented 8 years ago

@kitsonk do you think something more like below be more suitable? I'd like to keep the typing for the target but it not just be assumed as this?

on<T>(type: string, listener: EventedListenerOrArray<T, EventTargettedObject<T>>): Handle;

it is currently

on(type: string, listener: EventedListenerOrArray<this, EventTargettedObject<this>>): Handle;
kitsonk commented 8 years ago

That still wouldn't work for DOM events, because the target will never be a widget in those events, it will be a DOM node. We could have a TargettedEventedListenerOrArray<T, E> and an EventedListenerrArray<E> and make "root" on be TargettedEventedListenerOrArray<T, E> | EventedListenerrArray<E> and then each of the overloads could choose if they are targetted or not, like:

on(type: 'state:completed', listener: TargettedEventedListenerArray<this, EventTargettedObject<this>>): Handle;
on(type: 'click', listener: EventedListenerOrArray<MouseEvent>): Handle;
agubler commented 8 years ago

It certainly did work when I tried it locally, for dom events I passed any as the target type

kitsonk commented 8 years ago

It certainly did work when I tried it locally, for dom events I passed any as the target type

Hmmm... What about if you passed HTMLElement then?

agubler commented 8 years ago

@kitsonk you can use EventTarget but not HTMLElement

kitsonk commented 8 years ago

That sounds like a winner!