In Dart, if I listen to a click event with two listeners, how do I know what happens first?

If I write the following Dart code, how do I know which click handler occurs first?

main() { var button = new ButtonElement(); var stream = button.onClick.asBroadcastStream(); stream.listen(clickHandler1); stream.listen(clickHandler2); } 

Let's say I'm in another code that knows nothing about the first two click handlers, but I'm registering another one.

  • Can I find out that a thread has two listeners?
  • Can I pause or cancel all other subscribers?
  • If I write button.onClick.asBroadcastStream() again in another place, does it point to the same stream that was used in main ?
  • Can I tell in one of the handlers so as not to transfer the event to another broadcast listener? Is it a consumer?
+6
source share
2 answers

Let's say I'm in another code that knows nothing about the first two click handlers, but I'm registering another one.

Can I find out that a thread has two listeners?

No, you canโ€™t. You can extend the class of the stream or wrap it and provide this function yourself, but this does not seem like a good design choice, because I do not think that the listener should know about other listeners. What are you trying to do for sure? Perhaps there is a better way than letting the audience know about each other.

Can I pause or cancel all other subscribers?

You can cancel / pause / resume only the subscriber with whom you are dealing. Again, you probably shouldn't touch other listeners, but I think you could wrap / extend the Stream class to have this behavior.

If I write button.onClick.asBroadcastStream () again in another place, does it point to the same stream that was used mainly?

No, at least not in the current version of the SDK. Therefore, unfortunately, you need to store the link to this broadcast stream somewhere and refer to it, because calling asBroadcastStream() several times will not produce the result that you expect. (Note: at least based on quick testing: http://d.pr/i/Ip0K , although the documentation seems to point to different ones, I still have to check a little more when I find the time).

Can I tell in one of the handlers so as not to transfer the event to another broadcast listener?

Well, there is stopPropagation() on the HTML land, which means the event will not propagate to other elements, but that is probably not what you were looking for.

In order to stop events from starting in other listeners, it is necessary that the order of the listeners be called. I believe that order is the procedure for registering these students. From a design point of view, I donโ€™t think it would be nice to let the listener cancel / pause others.

Distributing events in HTML makes sense, since it is connected with a hierarchy, but we donโ€™t have it here (and even in the case of events in HTML, there can be several listeners for one element).

It is not possible to assign weight to listeners or determine the order of importance, so it is not surprising that there is no way to stop the event.

Instead of letting the listeners know about each other and manipulate each other, perhaps you should try to think of another way to approach your problem (whatever that may be).

Is it a consumer?

StreamConsumer is just a class that you can implement if you want to allow the transfer of other streams to your class.

+6
source

Can I find out that a thread has two listeners?

No, you have a "thread" that wraps DOM event processing. There are no such functions.

Can I pause or cancel all other subscribers?

See Event.stopPropagation () and Event.stopImmediatePropagation () and possibly Event.preventDefault () .

If I write button.onClick.asBroadcastStream () again in another place, does it point to the same stream that was used mainly?

[Updated] No, the current implementation does not give you the same stream, since onClick getter returns a new stream every time that is called. However, the returned stream is already a broadcast stream, so you should not call asBroadcastStream() on it. If you do this, you can simply get the link to the same object back.

 Stream<T> asBroadcastStream() => this; 

Can I tell one of the handlers to pass the event to another broadcast listener? Is it a consumer?

Take a look at Event.stopPropagation () and Event .stopImmediatePropagation () again, and possibly Event.preventDefault () .

+1
source

All Articles