Reactive approach to simple imperative task

后端 未结 3 1289
予麋鹿
予麋鹿 2021-01-05 07:54

Application requirements:

  • subscribe to two event streams A and B
  • for each A event there should be corresponding B event some time later
  • the a
3条回答
  •  有刺的猬
    2021-01-05 08:24

    Assumptions:

    • the B stream is hot,
    • You have a return type that can represent a Match or a Mismatch (instead of using OnError/Timeout which will terminate the subscription)

    Then this would be fine.

    AStream.SelectMany(a =>
        BStream.Where(b => b == a)
            .Select(b => new MatchMade(a, b))
            .Take(1)
            .Timeout(matchTimeout)
            .Catch(ex=>Observable.Return(new NoMatchMade(a)))
    )
    

提交回复
热议问题