如何在早期完成时合并两个可观察对象

编程入门 行业动态 更新时间:2024-10-23 09:27:55
本文介绍了如何在早期完成时合并两个可观察对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

内置Merge的行为两个源都完成后,操作符才完成.我正在寻找这个运算符的一个变体,它产生一个在两个源 observable 中的 any 完成时完成的 observable.例如,如果第一个 observable 成功完成,稍后第二个 observable 完成并出现异常,我希望忽略此异常.

The behavior of the built-in Merge operator is to complete when both sources are completed. I am searching for a variant of this operator that produces an observable that completes when any of the two source observables completes. For example if the first observable complete successfully and later the second observable complete with an exception, I want this exception to be ignored.

我想出了一个实现,将一个特殊的哨兵异常连接到两个可枚举项,然后合并的序列捕获并抑制这个异常.我想知道我是否缺少一个更简单的解决方案.

I came up with an implementation that concatenates a special sentinel exception to both enumerables, and then the merged sequence catches and suppresses this exception. I wonder if I am missing a simpler solution.

/// <summary>
/// Merges elements from two observable sequences into a single observable sequence,
/// that completes as soon as any of the source observable sequences completes.
/// </summary>
public static IObservable<T> MergeUntilAnyCompletes<T>(this IObservable<T> first,
    IObservable<T> second)
{
    var sentinel = new Exception();
    first = first.Concat(Observable.Throw<T>(sentinel));
    second = second.Concat(Observable.Throw<T>(sentinel));
    // Concat: Concatenates the second observable sequence to the first
    // observable sequence upon successful termination of the first.
    return first.Merge(second)
        .Catch(handler: (Exception ex) =>
        ex == sentinel ? Observable.Empty<T>() : Observable.Throw<T>(ex));
    // Catch: Continues an observable sequence that is terminated by an exception
    // of the specified type with the observable sequence produced by the handler.
}

推荐答案

Fun hack:

public static IObservable<T> MergeUntilAnyCompletes<T>(this IObservable<T> first,
    IObservable<T> second)
{
    return Observable.Merge(
        first.Materialize(),
        second.Materialize()
    ).Dematerialize();
}

Materialize 将 observable 变成通知的 observable,所以 Merge 将不再抑制 OnCompleted 通知.当您 Dematerialize 时,该操作员将看到 OnCompleted 并停止.

Materialize turns the observable into an observable of notifications, so Merge will no longer suppress the OnCompleted notification. When you Dematerialize, then that operator will see the OnCompleted and stops.

旁注:如果你想要一些关于 Materialize/Dematerialize 的有趣的、略带学术性的阅读,请阅读 这篇博文.他在写关于 Ix 的文章,但同样的事情也适用于 Rx.

Side note: If you want some fun, slightly academic reading about Materialize/Dematerialize read this blog post. He's writing about Ix, but the same thing applies to Rx.

这篇关于如何在早期完成时合并两个可观察对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-05-01 12:22:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1409371.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对象   两个   如何在

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!