Skip to content

Commit 3706ba8

Browse files
committed
Task related fixes (#657)
1 parent dce71c1 commit 3706ba8

File tree

6 files changed

+719
-614
lines changed

6 files changed

+719
-614
lines changed

src/FSharpPlus/Control/Comonad.fs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ type Extract =
1616
#if FABLE_COMPILER_3 || FABLE_COMPILER_4
1717
Async.RunSynchronously x
1818
#else
19-
Async.AsTask(x).Result
19+
Async.AsTask(x).GetAwaiter().GetResult ()
2020
#endif
2121
static member Extract (x: Lazy<'T> ) = x.Value
2222
static member Extract ((_: 'W, a: 'T) ) = a
@@ -28,10 +28,10 @@ type Extract =
2828
static member inline Extract (f: 'Monoid -> 'T) = f (LanguagePrimitives.GenericZero)
2929
#endif
3030
#if !FABLE_COMPILER
31-
static member Extract (f: Task<'T> ) = f.Result
31+
static member Extract (f: Task<'T> ) = f.GetAwaiter().GetResult ()
3232
#endif
3333
#if !NET45 && !NETSTANDARD2_0 && !FABLE_COMPILER
34-
static member Extract (f: ValueTask<'T> ) = f.Result
34+
static member Extract (f: ValueTask<'T>) = f.GetAwaiter().GetResult ()
3535
#endif
3636
static member inline Invoke (x: '``Comonad<'T>``) : 'T =
3737
let inline call_2 (_mthd: ^M, x: ^I) = ((^M or ^I) : (static member Extract : _ -> _) x)
@@ -82,10 +82,10 @@ type Extend =
8282
| ValueTask.Canceled -> tcs.SetCanceled ()
8383
// nowarn here, this case has been handled already if g.IsCompleted
8484
else
85-
ValueTask.continueTask tcs g (fun _ ->
85+
g |> ValueTask.continueTask tcs (fun _ ->
8686
try tcs.SetResult (f g)
8787
with e -> tcs.SetException e)
88-
tcs.Task |> ValueTask<'U>
88+
ValueTask<'U> tcs.Task
8989

9090
#endif
9191

src/FSharpPlus/Extensions/Extensions.fs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,6 @@ module Extensions =
3838
open System.Threading.Tasks
3939
open FSharp.Core.CompilerServices
4040

41-
let private (|Canceled|Faulted|Completed|) (t: Task<'a>) =
42-
if t.IsCanceled then Canceled
43-
else if t.IsFaulted then Faulted (Unchecked.nonNull t.Exception)
44-
else Completed t.Result
45-
4641
type Task<'t> with
4742
static member WhenAll (tasks: Task<'a>[], ?cancellationToken: CancellationToken) =
4843
let tcs = TaskCompletionSource<'a[]> ()
@@ -53,9 +48,9 @@ module Extensions =
5348
tasks
5449
|> Seq.iteri (fun i t ->
5550
let continuation = function
56-
| Canceled -> tcs.TrySetCanceled () |> ignore
57-
| Faulted e -> tcs.TrySetException e |> ignore
58-
| Completed r ->
51+
| Task.Canceled -> tcs.TrySetCanceled () |> ignore
52+
| Task.Faulted e -> tcs.TrySetException e |> ignore
53+
| Task.Succeeded r ->
5954
results.[i] <- r
6055
if Interlocked.Decrement pending = 0 then
6156
tcs.SetResult results
@@ -132,7 +127,7 @@ module Extensions =
132127
computation,
133128
ts.SetResult,
134129
(function
135-
| :? AggregateException as agg -> ts.SetException agg.InnerExceptions
130+
| :? AggregateException as aex when aex.InnerExceptions.Count > 0 -> ts.SetException aex.InnerExceptions
136131
| exn -> ts.SetException exn),
137132
(fun _ -> ts.SetCanceled ()),
138133
cancellationToken)
@@ -198,7 +193,7 @@ module Extensions =
198193
/// Similar to Async.Sequential but the returned Async contains a sequence, which is lazily evaluated.
199194
static member SequentialLazy (t: seq<Async<'T>>) : Async<seq<_>> = async {
200195
let! ct = Async.CancellationToken
201-
return Seq.map (fun t -> Async.AsTask(t, ct).Result) t }
196+
return Seq.map (fun t -> Async.AsTask(t, ct).GetAwaiter().GetResult ()) t }
202197
[<Obsolete("Renamed to Async.Sequential or Async.SequentialLazy")>]static member Sequence (t: seq<Async<_>>) = Async.SequentialLazy t
203198
#endif
204199

0 commit comments

Comments
 (0)