dart:如何实现保留传入项目顺序的流联接? [英] dart: how do I implement stream join that preserves the order of incoming items?

查看:50
本文介绍了dart:如何实现保留传入项目顺序的流联接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现一个联接运算符,该运算符获取流列表并输出单个流.问题是输出顺序与输入顺序不同.StreamController.add是异步的,所以如果我这样做

I'm trying to implement a join operator that takes a list of Streams and outputs a single stream. The problem is the output order is not the same as the input order. StreamController.add is asynchronous, so if I do

sc1.add(1)
sc2.add(2)
sc2.add(3)
sc2.add(4)
sc1.add(5)
...

分别调用流ondata回调的顺序类似于1,2,5,3,4,这基本上是sc1和sc2的交错.这种顺序在执行过程中是相当一致的,这使我相信该实现是在单个线程上进行循环调度.(这是在dart VM上)

the order in which the respective stream ondata callbacks get invoked is something like 1,2,5,3,4 which is basically an interleaving of sc1 and sc2. This ordering is pretty consistent across executions, which leads me to believe the implementation is doing round robin dispatch on a single thread. (this is on dart VM)

如果在我的联接实现中调用ondata回调时该顺序已经被打乱了,那么我将无法正确实现此联接.有人对如何实现这一点有好主意吗?

If the ordering is already scrambled by the time the ondata callback is called in my join implementation, I can't implement this join correctly. Does anyone have good ideas on how to implement this?

推荐答案

我相信您使用的是SDK的最新版本.

I believe that you are using some recent version of the SDK.

sync 标志,它控制了此行为(请参阅文档

sync flag was introduced for SteamController constructors since v0.5.11, and it controls this behavior (see docs here and here). By default it is set to false, which means that listeners are called asynchronously, i.e. some time after the add call has finished:

如果sync为false,则不保证何时出现多个侦听器获取事件,但每个侦听器将获取所有事件事件以正确的顺序进行.如果两个事件是异步发送的具有两个侦听器的控制器,其中一个侦听器可能会同时获得两个侦听器其他监听者获得任何事件之前发生的事件

If sync is false, no guarantees are given with regard to when multiple listeners get the events, except that each listener will get all events in the correct order. If two events are sent on an async controller with two listeners, one of the listeners may get both events before the other listener gets any

如果您不拥有控制器,那么我不确定您是否可以做很多事情.

If you don't own the controllers, then I am not sure if there is much you can do.

这篇关于dart:如何实现保留传入项目顺序的流联接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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