Skip to content

Commit e1d6a70

Browse files
committed
Use task CE for task commands
1 parent c9b20dc commit e1d6a70

1 file changed

Lines changed: 48 additions & 9 deletions

File tree

src/Fabulous/Cmd.fs

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -172,21 +172,60 @@ 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 ([<InlineIfLambda>] task: 'a -> Task<_>) (arg: 'a) ([<InlineIfLambda>] ofSuccess: _ -> 'msg) ([<InlineIfLambda>] ofError: _ -> 'msg) : Cmd<'msg> =
176+
[
177+
fun dispatch ->
178+
backgroundTask {
179+
try
180+
let! r = task arg
181+
ofSuccess r |> dispatch
182+
with e ->
183+
ofError e |> dispatch
184+
}
185+
|> ignore<Task<_>>
186+
]
177187

178188
/// 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
189+
let inline perform ([<InlineIfLambda>] task: 'a -> Task<_>) (arg: 'a) ([<InlineIfLambda>] ofSuccess: _ -> 'msg) : Cmd<'msg> =
190+
[
191+
fun dispatch ->
192+
backgroundTask {
193+
try
194+
let! r = task arg
195+
ofSuccess r |> dispatch
196+
with _ ->
197+
()
198+
}
199+
|> ignore<Task<_>>
200+
]
181201

182202
/// 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
185-
186-
let inline msg (task: Task<'msg>) = OfAsync.msg(task |> Async.AwaitTask)
203+
let inline attempt ([<InlineIfLambda>] task: 'a -> #Task) (arg: 'a) ([<InlineIfLambda>] ofError: _ -> 'msg) : Cmd<'msg> =
204+
[
205+
fun dispatch ->
206+
backgroundTask {
207+
try
208+
do! task arg
209+
with e ->
210+
ofError e |> dispatch
211+
}
212+
|> ignore<Task<_>>
213+
]
214+
215+
let inline msg (task: Task<'msg>) = perform (fun () -> task) () id
187216

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

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

0 commit comments

Comments
 (0)