Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optional commands (Cmd.ofMsgOption / Cmd.ofAsyncMsgOption) #224

Merged
merged 3 commits into from
Nov 7, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
42 changes: 42 additions & 0 deletions docs/update.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,45 @@ let backgroundCmd =
return msg
})
```

Optional commands
------

There might be cases where before a message is sent, you need to check if you want to send it (e.g. check user's preferences, ask user's permission, ...)

Fabulous has 2 helper functions for this:

- `Cmd.ofMsgOption`

```fsharp
let autoSaveCmd =
match userPreference.IsAutoSaveEnabled with
| false -> None
| true ->
autoSave()
Some Msg.AutoSaveDone

let update msg model =
match msg with
| TimedTick -> model, (Cmd.ofMsgOption autoSaveCmd)
| AutoSaveDone -> ...
```

- `Cmd.ofAsyncMsgOption`

```fsharp
let takePictureCmd = async {
try
let! picture = takePictureAsync()
Some (Msg.PictureTaken picture)
with
| exn ->
do! displayAlert("Exception: " + exn.Message)
None
}

let update msg model =
match msg with
| TakePicture -> model, (Cmd.ofAsyncMsgOption takePictureCmd)
| PictureTaken -> ...
```
9 changes: 9 additions & 0 deletions src/Fabulous.Core/ElmishCmd.fs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ module Cmd =
let ofMsg (msg:'msg) : Cmd<'msg> =
[fun dispatch -> dispatch msg]

/// Command to issue a specific message, only when Option.IsSome = true
let ofMsgOption (msg:'msg option) : Cmd<'msg> =
[fun dispatch -> match msg with None -> () | Some msg -> dispatch msg]
TimLariviere marked this conversation as resolved.
Show resolved Hide resolved

/// When emitting the message, map to another type
let map (f: 'a -> 'msg) (cmd: Cmd<'a>) : Cmd<'msg> =
cmd |> List.map (fun g -> (fun dispatch -> f >> dispatch) >> g)
Expand All @@ -35,8 +39,13 @@ module Cmd =

let dispatch d (cmd: Cmd<_>) = for sub in cmd do sub d

/// Command to issue a message at the end of an asynchronous task
let ofAsyncMsg (p: Async<'msg>) : Cmd<'msg> =
[ fun dispatch -> async { let! msg = p in dispatch msg } |> Async.StartImmediate ]

/// Command to issue a message at the end of an asynchronous task, only when Option.IsSome = true
let ofAsyncMsgOption (p: Async<'msg option>) : Cmd<'msg> =
[ fun dispatch -> async { let! msg = p in match msg with None -> () | Some msg -> dispatch msg } |> Async.StartImmediate ]
TimLariviere marked this conversation as resolved.
Show resolved Hide resolved

//let ofAsyncMsgs p : Cmd<_> =
// [ fun dispatch -> p |> AsyncSeq.iter dispatch |> Async.StartImmediate ]
Expand Down