在Dart,如果我听到有两个监听器的点击事件,我怎么知道哪个首先发生? [英] In Dart, if I listen to a click event with two listeners, how do I know which happens first?

查看:112
本文介绍了在Dart,如果我听到有两个监听器的点击事件,我怎么知道哪个首先发生?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我写下面的Dart代码,我如何知道哪个点击处理程序先发生?

  main(){
var button = new ButtonElement();
var stream = button.onClick.asBroadcastStream();

stream.listen(clickHandler1);
stream.listen(clickHandler2);
}

假设我在其他代码中不知道前两个单击处理程序,但我注册另一个。




  • 我可以知道流有两个侦听器吗?

  • 我可以暂停或取消所有其他

  • 如果我在其他地方再次写入 button.onClick.asBroadcastStream(),是否指向与 main

  • 我可以说在其中一个处理程序中不将事件传递给其他广播侦听器?这是消费者吗?


解决方案


在其他代码中,不知道任何关于第一个
两个点击处理程序,但我注册另一个。



我可以知道流有两个监听器?


不,你不能。你可以扩展流类或者封装它,并自己提供这个功能,但它不觉得像一个好的设计选择,因为我不认为听众应该知道其他监听器。你想做什么?


我可以暂停或取消所有其他订阅者吗?


您可以取消/暂停/恢复仅与您正在处理的订户。再次,你可能不应该接触其他监听器,但我想你可以包装/扩展Stream类有这种行为。


如果我在其他地方再次编写button.onClick.asBroadcastStream(),是否指向与main中使用的相同的流?


至少不是在当前版本的SDK。所以,不幸的是,你需要存储一个引用这个广播流的地方,并参考它,因为调用 asBroadcastStream()多次不会产生在你可能期望的结果。 (注意:至少基于快速测试: http://d.pr/i/Ip0K 虽然文档似乎表明不同,我还没有测试我可以说在其中一个处理程序不传递事件到其他广播侦听器?


那么 stopPropagation() ,这意味着事件不会传播到其他元素,它可能不是你在寻找的。

为了能够停止其他侦听器中的事件触发,需要有一个顺序,监听器被调用。我相信的顺序是这些听众的注册顺序。从设计的角度来看,我不认为允许监听器取消/暂停其他监听器是一个好主意。



HTML中的事件传播是有意义的,因为它是关于层次结构,但是在这里我们没有这个(甚至在HTML中的事件可以有单个元素的多个监听器)。



没有办法重量给听众或定义重要性的顺序,因此没有办法阻止事件并不奇怪。



而不是让听众知道彼此,操纵彼此,也许你应该尝试另一种方式来处理你的问题(无论是什么)。


这是消费者吗?


StreamConsumer 只是一个类,你可以实现,如果你想允许其他流管道到你的类。


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

main() {
  var button = new ButtonElement();
  var stream = button.onClick.asBroadcastStream();

  stream.listen(clickHandler1);
  stream.listen(clickHandler2);
}

Let's say I'm in other code that doesn't know anything about the first two click handlers, but I register another one.

  • Can I know that the stream has two listeners?
  • Can I pause or cancel all other subscribers?
  • If I write button.onClick.asBroadcastStream() again elsewhere, does it point to the same stream as was used in main?
  • Can I say in one of the handlers to not pass event on to the other broadcast listener? Is that a consumer?

解决方案

Let's say I'm in other code that doesn't know anything about the first two click handlers, but I register another one.

Can I know that the stream has two listeners?

No, you can't. You could extend the stream class or wrap it and provide this functionality yourself, but it does not feel like a good design choice, because I don't think a listener should know about other listeners. What are you trying to do exactly? Perhaps there's a better way than letting listeners know about each other.

Can I pause or cancel all other subscribers?

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

If I write button.onClick.asBroadcastStream() again elsewhere, does it point to the same stream as was used in main?

No, at least not at the current version of SDK. So, unfortunately, you need to store a reference to this broadcast stream somewhere, and refer to it, because calling asBroadcastStream() multiple times will not yield in the result you might expect. (Note: at least based on quick testing: http://d.pr/i/Ip0K although the documentation seems to indicate different, I have yet to test a bit more when I find the time).

Can I say in one of the handlers to not pass event on to the other broadcast listener?

Well, there's stopPropagation() in the HTML land which means that the event won't propagate to other elements, but it's probably not what you were looking for.

For being able to stop an event firing in other listeners, there needs to be an order of which the listeners are getting called. I believe the order is the order of registration of those listeners. From the design perspective, I don't think it would be a good idea to allow a listener to cancel/pause others.

Event propagation in HTML makes sense since it's about hierarchy, but here we don't have that (and even in case of events in HTML there can be multiple listeners for the single element).

There's no way to assign weight to listeners or define the order of importance, therefore it's not surprising that there isn't a way to stop the event.

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

Is that a consumer?

The StreamConsumer is just a class that you can implement if you want to allow other streams to be piped into your class.

这篇关于在Dart,如果我听到有两个监听器的点击事件,我怎么知道哪个首先发生?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆