Reaction 0.1.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Reaction --version 0.1.0
NuGet\Install-Package Reaction -Version 0.1.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Reaction" Version="0.1.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Reaction --version 0.1.0
#r "nuget: Reaction, 0.1.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Reaction as a Cake Addin
#addin nuget:?package=Reaction&version=0.1.0

// Install Reaction as a Cake Tool
#tool nuget:?package=Reaction&version=0.1.0

¡Re:action!

Re:action is a lightweight Async Reactive (Rx) Elmish-ish library for F# targeting Fable and React. This means that the code is transpiled to JavaScript and thus the same code may be used both client and server side for full stack software development.

Currently a playground project for experimenting with MVU-based web applications using async reactive functional programming (Async Observables) in F#. The project is heavily inspired by Elm and Elmish but currently a separate project since it does not have any dependencies to any of these projects. The inspiration for Async Observables and plain old functions (POF) comes from my own work with aioreactive.

Async Observables

The difference between an "Async Observable" and an "Observable" is that with "Async Observables" you need to await operations such as subscribe, OnNext, OnError, OnCompleted. This enables subscribe in create type operators to do async operations i.e setup network connections, and observers may finally do async operations such as writing to disk (observers are all about side-effects right?).

Re:action uses simple functions instead of classes and the traditional Rx interfaces. Some of the operators uses mailbox processors (actors) to implement the observer pipeline in order to avoid locks and mutables. This makes the code more Fable friendly for easily transpiling to JavaScript. Here are the core types:

type Notification<'a> =
    | OnNext of 'a
    | OnError of exn
    | OnCompleted

type AsyncDisposable = unit -> Async<unit>
type AsyncObserver<'a> = Notification<'a> -> Async<unit>
type AsyncObservable<'a> = AsyncObserver<'a> -> Async<AsyncDisposable>

type AsyncMapper<'a, 'b> = 'a -> Async<'b>
type AsyncPredicate<'a> = 'a -> Async<bool>
type AsyncAccumulator<'s, 't> = 's -> 't -> Async<'s>

Operators

The following parameterized async observerable returning functions (operators) are currently supported. Other operators may be implemented on-demand, but there are currently no plans to make this into a full featured Rx implementation.

  • map, mapIndexed, mapAsync, mapIndexedAsync
  • filter, filterAsync
  • scan, scanAsync
  • merge
  • flatMap, flatMapIndexed, flatMapAsync, flatMapIndexedAsync
  • concat
  • startWith
  • distinctUntilChanged
  • delay
  • debounce
  • combineLatest
  • withLatestFrom (TBD)
  • switch (TBD)

Elmish-ish example

Reactive MVU archtecture example (source code) using Re:action for impl. the classic Time Flies example from RxJS. No jQuery or other js-libraries in this example. This code is very simplar to Elmish but the difference is that we can compose powerful reactive queries to transform, filter, aggregate and time-shift our stream of messages.

// Messages for updating the model
type Msg =
    | MouseEvent of MouseEvent
    | LetterMove of int * char * float * float

// Model (state) for updating the view
type Model = {
    Pos: Map<int, char * float * float>
}

let update (currentModel : Model) (msg : Msg) =
    match currentModel.Pos, msg with
    | _, LetterMove (i, c, x, y) ->
        { currentModel with Pos = currentModel.Pos.Add (i, (c, x, y)) }
    | _ ->
        currentModel

// View for being observed by React
let view (model : Model) =
    let letters = model.Pos

    div [ Style [ FontFamily "Consolas, monospace"]] [
        for KeyValue(i, (c, x, y)) in letters do
            yield span [ Style [Top y; Left x; Position "absolute"] ] [
                str (string c)
            ]
    ]

let main = async {
    let initialModel = { Pos = Map.empty }

    // Query
    let moves = from <| Seq.toList "TIME FLIES LIKE AN ARROW"
                |> flatMapIndexed (fun x i ->
                        fromMouseMoves ()
                        |> delay (100.0 * float i)
                        |> map (fun m -> LetterMove (i, x, m.clientX + float i * 10.0 + 15.0, m.clientY))
                   )
                |> scan initialModel update
                |> map view

    // React observer
    let render n =
        async {
            match n with
            | OnNext dom -> renderReact "elmish-app" dom
            | _ -> ()
        }

    // Subscribe
    do! moves render |> Async.Ignore
}

main |> Async.StartImmediate
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Reaction:

Package Downloads
Reaction.Giraffe

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0 2,032 10/20/2018
0.13.1 1,360 10/1/2018
0.13.0 770 9/30/2018
0.12.3 925 9/27/2018
0.11.0 895 9/20/2018
0.10.0 765 9/15/2018
0.9.1 954 9/5/2018
0.9.0 704 9/5/2018
0.8.2 918 9/4/2018
0.8.1 758 9/2/2018
0.8.0 850 9/2/2018
0.7.0 743 9/1/2018
0.6.0 729 9/1/2018
0.5.0 717 8/31/2018
0.4.0 845 8/29/2018
0.3.0 694 8/28/2018
0.2.0 692 8/26/2018
0.1.0 774 8/15/2018