Header menu logo Sutil

Store Module

Functions for working with stores

Functions and values

Function or value Description

Store.current store

Full Usage: Store.current store

Parameters:
Returns: 'T
store : IObservable<'T>
Returns: 'T

Store.distinct source

Full Usage: Store.distinct source

Parameters:
Returns: IObservable<'T>

Provides an observable that will emit a value only when the updated store value is different from the previous one

source : IObservable<'T>
Returns: IObservable<'T>
Example

     let store = Store.make 0
     let whenDistinct = Store.distinct store
     let sub1 = store.subscribe(printfn "number: %i")
     let sub2 = whenDistinct.subscribe(printfn "number: %i")
     Store.set 0 store // store emits
     Store.set 0 store // when distinct doesn't emit
     Store.set 1 store // both store and distinct emit
     Store.set 1 store // when distinct doesn't emit
val store: obj
val whenDistinct: obj
val sub1: obj
val printfn: format: Printf.TextWriterFormat<'T> -> 'T
val sub2: obj

Store.filter predicate store

Full Usage: Store.filter predicate store

Parameters:
Returns: IObservable<'A>

Applies a predicate function to obtain an observable of the elements that evaluated to true

predicate : 'A -> bool
store : IObservable<'A>
Returns: IObservable<'A>
Example

     let usersOver18: IObservable<string> =
         Store.filter (fun user -> user.age > 18) usersStore

     (* after you are done with the subscription *)

     over18.Dispose()
val usersOver18: obj
Multiple items
val string: value: 'T -> string

--------------------
type string = System.String

Store.get store

Full Usage: Store.get store

Parameters:
Returns: 'T

Obtains the current value of the store

store : IStore<'T>
Returns: 'T
Example

     let value = Store.get initStore
     value = 1 // true
     let value2 = Store.get anonymousStore
     Option.isNone value2.prop2 // true
val value: int
val value2: obj
module Option from Microsoft.FSharp.Core
val isNone: option: 'T option -> bool

Store.getMap callback store

Full Usage: Store.getMap callback store

Parameters:
    callback : 'a -> 'b
    store : IStore<'a>

Returns: 'b

Takes a store and applies a mapping function then returns the value from the evaluated function

This might be called foldMap

callback : 'a -> 'b
store : IStore<'a>
Returns: 'b
Example

     let store: IStore<{| name: string; budget: decimal |}> =
     Store.make {| name = "Frank"; budget = 547863.26M |}

     let formattedBudget: string =
         Store.getMap
             (fun model -> sprintf $"$ %0.00M{model.budget}")
             store
     printf %"Budget available: {formattedBudget}
val store: 'a
Multiple items
val string: value: 'T -> string

--------------------
type string = System.String
Multiple items
val decimal: value: 'T -> decimal (requires member op_Explicit)

--------------------
type decimal = System.Decimal

--------------------
type decimal<'Measure> = decimal
val formattedBudget: string
val sprintf: format: Printf.StringFormat<'T> -> 'T
val printf: format: Printf.TextWriterFormat<'T> -> 'T

Store.iter callback source

Full Usage: Store.iter callback source

Parameters:
Returns: IDisposable

Invoke callback for each observed value from source

callback : 'A -> unit
source : IObservable<'A>
Returns: IDisposable
Example

    source |> Store.iter (fun v -> printfn $"New value: {v}")
val printfn: format: Printf.TextWriterFormat<'T> -> 'T

Store.make modelInit

Full Usage: Store.make modelInit

Parameters:
    modelInit : 'T

Returns: IStore<'T>

Create a new store

modelInit : 'T
Returns: IStore<'T>
Example

     let intStore: IStore<int> = Store.make 1

     let anonymousStore:
         IStore<{| prop1: int;
                   prop2: string option |}>
         = Store.make {| prop1 = 10; prop2 = None |}
     (* After using the store *)
     intStore.Dispose()
     anonymousStore.Dispose()
val intStore: obj
Multiple items
val int: value: 'T -> int (requires member op_Explicit)

--------------------
type int = int32

--------------------
type int<'Measure> = int
val anonymousStore: obj
Multiple items
val string: value: 'T -> string

--------------------
type string = System.String
type 'T option = Option<'T>
union case Option.None: Option<'T>

Store.makeElmish init update dispose

Full Usage: Store.makeElmish init update dispose

Parameters:
    init : 'Props -> 'Model * Cmd<'Msg>
    update : 'Msg -> 'Model -> 'Model * Cmd<'Msg>
    dispose : 'Model -> unit

Returns: 'Props -> IStore<'Model> * Dispatch<'Msg>

Creates a store and a dispatch function as Store.makeElmishSimple the difference being that this version handles [Elmish commands](https://elmish.github.io/elmish/index.html#Commands) as well, generally used in more complex UIs given that with commands you can also handle asynchronous code like fetching resources from a server or calling any function that returns a promise or async

init : 'Props -> 'Model * Cmd<'Msg>
update : 'Msg -> 'Model -> 'Model * Cmd<'Msg>
dispose : 'Model -> unit
Returns: 'Props -> IStore<'Model> * Dispatch<'Msg>
Example

     type State = { count: int }
     type Msg =
         | Increment
         | Decrement
         | Reset
         | AsyncIncrement
         | AsyncDecrement
     let init _ = { count = 0 }, Cmd.ofMsg AsyncIncrement

     let wait1S () =
         async {
             do! Async.Sleep 1000
         }

     let upddate msg state =
         match msg with
         | Increment -> { state = state.count + 1 }, Cmd.none
         | Decrement -> { state = state.count - 1 }, Cmd.none
         | AsyncIncrement ->
             state, Cmd.ofAsync.perform () wait1S Increment
         | AsyncDecrement->
             state, Cmd.ofAsync.perform () wait1S Decrement
         | Reset -> { state = 0 } Cmd.none

     let view() =
         let state, dispatch = Store.makeElmish init update ignore ()

         Html.article [
             disposeOnUnmount [ state ]
             bindFragment state <| fun state -> text $"Count: {state.count}"

             Html.button [ text "Increment"; onClick (fun _ -> dispatch Increment) [] ]
             Html.button [ text "Async Increment"; onClick (fun _ -> dispatch AsyncIncrement) [] ]
             Html.button [ text "Decrement"; onClick (fun _ -> dispatch Decrement) [] ]
             Html.button [ text "Async Decrement"; onClick (fun _ -> dispatch AsyncDecrement) [] ]
             Html.button [ text "Reset"; onClick (fun _ -> dispatch Reset) [] ]
         ]
type State = { count: int }
Multiple items
val int: value: 'T -> int (requires member op_Explicit)

--------------------
type int = int32

--------------------
type int<'Measure> = int
type Msg = | Increment | Decrement | Reset | AsyncIncrement | AsyncDecrement
val init: 'a -> State * 'b
union case Msg.AsyncIncrement: Msg
val wait1S: unit -> Async<unit>
val async: AsyncBuilder
Multiple items
type Async = static member AsBeginEnd: computation: ('Arg -> Async<'T>) -> ('Arg * AsyncCallback * obj -> IAsyncResult) * (IAsyncResult -> 'T) * (IAsyncResult -> unit) static member AwaitEvent: event: IEvent<'Del,'T> * ?cancelAction: (unit -> unit) -> Async<'T> (requires delegate and 'Del :> Delegate) static member AwaitIAsyncResult: iar: IAsyncResult * ?millisecondsTimeout: int -> Async<bool> static member AwaitTask: task: Task<'T> -> Async<'T> + 1 overload static member AwaitWaitHandle: waitHandle: WaitHandle * ?millisecondsTimeout: int -> Async<bool> static member CancelDefaultToken: unit -> unit static member Catch: computation: Async<'T> -> Async<Choice<'T,exn>> static member Choice: computations: Async<'T option> seq -> Async<'T option> static member FromBeginEnd: beginAction: (AsyncCallback * obj -> IAsyncResult) * endAction: (IAsyncResult -> 'T) * ?cancelAction: (unit -> unit) -> Async<'T> + 3 overloads static member FromContinuations: callback: (('T -> unit) * (exn -> unit) * (OperationCanceledException -> unit) -> unit) -> Async<'T> ...

--------------------
type Async<'T>
static member Async.Sleep: dueTime: System.TimeSpan -> Async<unit>
static member Async.Sleep: millisecondsDueTime: int -> Async<unit>
val upddate: msg: Msg -> state: 'a -> 'a * 'b
val msg: Msg
val state: 'a
union case Msg.Increment: Msg
union case Msg.Decrement: Msg
union case Msg.AsyncDecrement: Msg
union case Msg.Reset: Msg
val view: unit -> 'a
val state: obj
val dispatch: obj
val ignore: value: 'T -> unit

Store.makeElmishSimple init update dispose

Full Usage: Store.makeElmishSimple init update dispose

Parameters:
    init : 'Props -> 'Model
    update : 'Msg -> 'Model -> 'Model
    dispose : 'Model -> unit

Returns: 'Props -> IStore<'Model> * Dispatch<'Msg>

Creates a store and a dispatch method commonly used in elmish programs, this can be used to model more complex views that require better control flow and a predictable state.

init : 'Props -> 'Model
update : 'Msg -> 'Model -> 'Model
dispose : 'Model -> unit
Returns: 'Props -> IStore<'Model> * Dispatch<'Msg>
Example

     type State = { count: int }
     type Msg =
         | Increment
         | Decrement
         | Reset
     let init _ = { count = 0 }

     let upddate msg state =
         match msg with
         | Increment -> { state = state.count + 1 }
         | Decrement -> { state = state.count - 1 }
         | Reset -> { state = 0 }

     let view() =
         let state, dispatch = Store.makeElmishSimple init update ignore ()

         Html.article [
             disposeOnUnmount [ state ]
             bindFragment state <| fun state -> text $"Count: {state.count}"

             Html.button [ text "Increment"; onClick (fun _ -> dispatch) [] ]
             Html.button [ text "Decrement"; onClick (fun _ -> dispatch) [] ]
             Html.button [ text "Reset"; onClick (fun _ -> dispatch Reset) [] ]
         ]
type State = { count: int }
Multiple items
val int: value: 'T -> int (requires member op_Explicit)

--------------------
type int = int32

--------------------
type int<'Measure> = int
type Msg = | Increment | Decrement | Reset
val init: 'a -> State
val upddate: msg: Msg -> state: 'a -> 'b
val msg: Msg
val state: 'a
union case Msg.Increment: Msg
union case Msg.Decrement: Msg
union case Msg.Reset: Msg
val view: unit -> 'a
val state: obj
val dispatch: obj
val ignore: value: 'T -> unit

Store.map callback store

Full Usage: Store.map callback store

Parameters:
Returns: IObservable<'B>

Returns an observable that will resolve to the result of said callback

callback : 'A -> 'B
store : IObservable<'A>
Returns: IObservable<'B>
Example

     let subscription: IObservable<string> =
         Store.map (fun value -> $"{value}") intStore

     (* after you are done with the subscription *)

     subscription.Dispose()
val subscription: obj
Multiple items
val string: value: 'T -> string

--------------------
type string = System.String

Store.map2 callback storeA storeB

Full Usage: Store.map2 callback storeA storeB

Parameters:
Returns: IObservable<'Res>

Returns an observable that will resolve to the result of said callback applied to two observables

callback : 'A -> 'B -> 'Res
storeA : IObservable<'A>
storeB : IObservable<'B>
Returns: IObservable<'Res>
Example

     let subscription: IObservable<int> =
         Store.map2 (fun value1 value2 -> value1 * value2) intStore1 intStore2

     (* after you are done with the subscription *)

     subscription.Dispose()
val subscription: obj
Multiple items
val int: value: 'T -> int (requires member op_Explicit)

--------------------
type int = int32

--------------------
type int<'Measure> = int

Store.mapDistinct callback store

Full Usage: Store.mapDistinct callback store

Parameters:
Returns: IObservable<'B>

Helper function for commonly used form

     store |> Store.map (fun s -> s.Thing) |> Observable.distinctUntilChanged
module Observable from Microsoft.FSharp.Control

callback : 'A -> 'B
store : IObservable<'A>
Returns: IObservable<'B>

Store.modify callback store

Full Usage: Store.modify callback store

Parameters:
    callback : 'T -> 'T
    store : Store<'T>

Modify the store by mapping its current value with a callback

callback : 'T -> 'T
store : Store<'T>
Example

     let store: IStore<int> = Store.make 2

     let squareMe() =
         Store.modify (fun model -> model * model) store

     Html.div [
         bindFragment store <| fun model -> text $"The value is {model}"
         Html.button [
             onClick (fun _ -> squareMe()) []
             text "Square me"
         ]
     ]
val store: obj
Multiple items
val int: value: 'T -> int (requires member op_Explicit)

--------------------
type int = int32

--------------------
type int<'Measure> = int
val squareMe: unit -> 'a

Store.set store newValue

Full Usage: Store.set store newValue

Parameters:
    store : IStore<'T>
    newValue : 'T

Replaces the current value of the store

store : IStore<'T>
newValue : 'T
Example

     Store.set 2 intStore
     let value = Store.get intStore
     value = 1 // false
val value: int

Store.subscribe callback store

Full Usage: Store.subscribe callback store

Parameters:
Returns: IDisposable

Provides a subscription that invokes a callback every time the store value is updated

callback : 'T -> unit
store : IObservable<'T>
Returns: IDisposable
Example

     let subscription =
         Store.subscribe (fun value -> printfn $"{value}") intStore

     (* after you are done with the subscription *)

     subscription.Dispose()
val subscription: obj
val printfn: format: Printf.TextWriterFormat<'T> -> 'T

Store.subscribe2 source1 source2 callback

Full Usage: Store.subscribe2 source1 source2 callback

Parameters:
Returns: IDisposable

Takes two observables and subscribes to both with a single callback, both values will be cached individually and on every notify they will be updated and emitted, every notification can come from any of the observables

source1 : IObservable<'A>
source2 : IObservable<'B>
callback : 'A * 'B -> unit
Returns: IDisposable
Example

     let player1Score = Store.make 0
     let player2Score = Store.make 0

     let printPlayerScores (score1: int * score2: int) =
         printfn $"Player 1: {score1}\nPlayer2: {score2}"

     let scores =
         Store.subscribe2
             player1Score
             player2Score
             printPlayerScore
     (* Game Finished, dispose the observables *)
     scores.Dispose()
val player1Score: obj
val player2Score: obj
val printPlayerScores: score1: int -> obj
Multiple items
val int: value: 'T -> int (requires member op_Explicit)

--------------------
type int = int32

--------------------
type int<'Measure> = int
val printfn: format: Printf.TextWriterFormat<'T> -> 'T
val scores: obj

Store.zip source1 source2

Full Usage: Store.zip source1 source2

Parameters:
Returns: IObservable<'a * 'b>

Merges two stores into a single tupled observable

source1 : IObservable<'a>
source2 : IObservable<'b>
Returns: IObservable<'a * 'b>
Example

     let tableInfo =
     Observable.zip
         (Strore.map(fun model -> model.rows) model)
         (Strore.map(fun model -> model.columns) model)

     (* once done with tableInfo *)

     tableInfo.Dispose()
val tableInfo: 'a
module Observable from Microsoft.FSharp.Control

Type something to start searching.