Skip to content

Instantly share code, notes, and snippets.

@NickDarvey
Created May 25, 2020 01:46
Show Gist options
  • Save NickDarvey/5e03eb22c396f23c736992c7694a43a0 to your computer and use it in GitHub Desktop.
Save NickDarvey/5e03eb22c396f23c736992c7694a43a0 to your computer and use it in GitHub Desktop.
Bolero (and Blazor and Elmish) and Rx.NET lazy component
open Bolero
open Elmish
open System.Reactive.Disposables
open System.Reactive.Subjects
type ReactiveLazyComponent<'model, 'event, 'msg>() =
inherit Bolero.ElmishComponent<'model, 'msg>()
let subject = new Subject<'event>()
let observer = subject :> IObserver<_>
let observable = subject :> IObservable<_>
let mutable subscription = Disposable.Empty
[<Parameter>]
member val ViewFunction =
Unchecked.defaultof<'model -> Dispatch<'event> -> Node> with get, set
[<Parameter>]
member val StreamFunction : IObservable<'event> -> IObservable<'msg> =
Unchecked.defaultof<IObservable<'event> -> IObservable<'msg>> with get, set
override this.View model dispatch =
do if obj.ReferenceEquals(subscription, Disposable.Empty)
then subscription <- this.StreamFunction observable |> Observable.subscribe dispatch
this.ViewFunction model observer.OnNext
interface System.IDisposable with
member _.Dispose() =
subject.Dispose()
subscription.Dispose()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment