当子流之一发出时,Dart StreamZip 发出

发布于 2025-01-11 14:22:12 字数 885 浏览 1 评论 0原文

我有两个提供流的数据源(a 和 b)。新数据将独立添加到它们中。 现在我想创建一个流 (z),它将这两个流合并为一个,从而组合元素。

因此,当“a”第一次发出时,“b”还没有值,应该不会发生任何事情(无论我是否必须实现它或者它是否集成在库中都没关系......)。当“b”首先发出时也是如此。

当两个流至少发出一次时,我希望“z”将这两个流彼此合并。这适用于 StreamZip。

但现在的问题是,如果“a”再次发出,但“b”没有发出,StreamZip 会等待,直到“b”再次发出,直到它自己(z)再次发出。 我猜这是设计使然,因为源代码显示了这种行为:

// taken from: stream_zip.dart, shortened for better readability
...

    void handleData(int index, T data) {
      dataCount++;
      // subscriptions has the length 2, as I have two input streams,
      // but dataCOunt is 1, as only one source stream emitted
      if (dataCount == subscriptions.length) {
        // handle the data
        // ...
      } else {
        // don't do anything
        subscriptions[index].pause();
      }
    }

有没有办法在不自己编写逻辑的情况下处理这个问题?我想使用 RxDart 是一种选择吗? 或者我使用/解释了错误的东西?

I have two datasources (a and b) which provide streams. To them new data will be added independently.
Now I want to create a stream (z) which takes these two streams and merges them to one, combining the elements.

So when "a" emits for the first time there's no value for "b" yet, nothing should happen (doesn't matter if I have to implement it or if it is integrated in the library...). Same thing when "b" emits first.

When both streams have emitted at least once, I want "z" to merge these two into each other. This works with a StreamZip.

But the problem is now, if "a" emits again, but "b" does not, StreamZip waits until "b" emits again, until itself(z) emits again.
This is by design I guess as the source shows this behavior:

// taken from: stream_zip.dart, shortened for better readability
...

    void handleData(int index, T data) {
      dataCount++;
      // subscriptions has the length 2, as I have two input streams,
      // but dataCOunt is 1, as only one source stream emitted
      if (dataCount == subscriptions.length) {
        // handle the data
        // ...
      } else {
        // don't do anything
        subscriptions[index].pause();
      }
    }

Is there any way how to handle this without writing the logic myself? I guess using RxDart would be one option?
Or am I using/interpreting something wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文