How do I change the Rx Builder implementation to fix the stack overflow exception?

前端 未结 5 992
醉话见心
醉话见心 2021-02-20 02:50

I\'m trying to come up with an Rx Builder to use Reactive Extension within the F# Computation Expression syntax. How do I fix it so that it doesnt blow the stack? Like the Seq e

5条回答
  •  终归单人心
    2021-02-20 03:35

    A short answer is that Rx Framework doesn't support generating observables using a recursive pattern like this, so it cannot be easily done. The Combine operation that is used for F# sequences needs some special handling that observables do not provide. The Rx Framework probably expects that you'll generate observables using Observable.Generate and then use LINQ queries/F# computation builder to process them.

    Anyway, here are some thoughts -

    First of all, you need to replace Observable.merge with Observable.Concat. The first one runs both observables in parallel, while the second first yields all values from the first observable and then produces values from the second observable. After this change, the snippet will at least print ~800 numbers before the stack overflow.

    The reason for the stack overflow is that Concat creates an observable that calls Concat to create another observable that calls Concat etc. One way to solve this is to add some synchronization. If you're using Windows Forms, then you can modify Delay so that it schedules the observable on the GUI thread (which discards the current stack). Here is a sketch:

    type RxBuilder() =   
      member this.Delay f = 
          let sync = System.Threading.SynchronizationContext.Current 
          let res = Observable.Defer f
          { new IObservable<_> with
              member x.Subscribe(a) = 
                sync.Post( (fun _ -> res.Subscribe(a) |> ignore), null)
                // Note: This is wrong, but we cannot easily get the IDisposable here
                null }
      member this.Combine (xs, ys) = Observable.Concat(xs, ys)
      member this.Yield x = Observable.Return x
      member this.YieldFrom (xs:IObservable<_>) = xs
    

    To implement this properly, you would have to write your own Concat method, which is quite complicated. The idea would be that:

    • Concat returns some special type e.g. IConcatenatedObservable
    • When the method is called recursively you'll create a chain of IConcatenatedObservable that reference each other
    • The Concat method will look for this chain and when there are e.g. three objects, it will drop the middle one (to always keep chain of length at most 2).

    That's a bit too complex for a StackOverflow answer, but it may be a useful feedback for the Rx team.

提交回复
热议问题