Skip to content

Commit 240fa70

Browse files
authored
Merge pull request #1104 from kerams/task
Use task CE for task commands
2 parents ae87b8e + b18fe25 commit 240fa70

File tree

1 file changed

+44
-8
lines changed

1 file changed

+44
-8
lines changed

src/Fabulous/Cmd.fs

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -172,21 +172,57 @@ module Cmd =
172172

173173
module OfTask =
174174
/// Command to call a task and map the results
175-
let inline either (task: 'a -> Task<_>) (arg: 'a) (ofSuccess: _ -> 'msg) (ofError: _ -> 'msg) : Cmd<'msg> =
176-
OfAsync.either (task >> Async.AwaitTask) arg ofSuccess ofError
175+
let inline either
176+
([<InlineIfLambda>] task: 'a -> Task<_>)
177+
(arg: 'a)
178+
([<InlineIfLambda>] ofSuccess: _ -> 'msg)
179+
([<InlineIfLambda>] ofError: _ -> 'msg)
180+
: Cmd<'msg> =
181+
[ fun dispatch ->
182+
backgroundTask {
183+
try
184+
let! r = task arg
185+
ofSuccess r |> dispatch
186+
with e ->
187+
ofError e |> dispatch
188+
}
189+
|> ignore<Task<_>> ]
177190

178191
/// Command to call a task and map the success
179-
let inline perform (task: 'a -> Task<_>) (arg: 'a) (ofSuccess: _ -> 'msg) : Cmd<'msg> =
180-
OfAsync.perform (task >> Async.AwaitTask) arg ofSuccess
192+
let inline perform ([<InlineIfLambda>] task: 'a -> Task<_>) (arg: 'a) ([<InlineIfLambda>] ofSuccess: _ -> 'msg) : Cmd<'msg> =
193+
[ fun dispatch ->
194+
backgroundTask {
195+
try
196+
let! r = task arg
197+
ofSuccess r |> dispatch
198+
with _ ->
199+
()
200+
}
201+
|> ignore<Task<_>> ]
181202

182203
/// Command to call a task and map the error
183-
let inline attempt (task: 'a -> #Task) (arg: 'a) (ofError: _ -> 'msg) : Cmd<'msg> =
184-
OfAsync.attempt (task >> Async.AwaitTask) arg ofError
204+
let inline attempt ([<InlineIfLambda>] task: 'a -> #Task) (arg: 'a) ([<InlineIfLambda>] ofError: _ -> 'msg) : Cmd<'msg> =
205+
[ fun dispatch ->
206+
backgroundTask {
207+
try
208+
do! task arg
209+
with e ->
210+
ofError e |> dispatch
211+
}
212+
|> ignore<Task<_>> ]
185213

186-
let inline msg (task: Task<'msg>) = OfAsync.msg(task |> Async.AwaitTask)
214+
let inline msg (task: Task<'msg>) = perform (fun () -> task) () id
187215

188216
let inline msgOption (task: Task<'msg option>) =
189-
OfAsync.msgOption(task |> Async.AwaitTask)
217+
[ fun dispatch ->
218+
backgroundTask {
219+
let! r = task
220+
221+
match r with
222+
| Some x -> dispatch x
223+
| None -> ()
224+
}
225+
|> ignore<Task<_>> ]
190226

191227
/// Command to issue a message if no other message has been issued within the specified timeout
192228
let debounce (timeout: int) (fn: 'value -> 'msg) : 'value -> Cmd<'msg> =

0 commit comments

Comments
 (0)