-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInfrastructure.fs
31 lines (27 loc) · 1.26 KB
/
Infrastructure.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
namespace Propulsion.EventStoreDB
module private Array =
let last (array: _ array) = array.[array.Length - 1]
let emptyAsNull (arr: _ array) =
match arr with
| null -> null
| arr when Array.isEmpty arr -> null
| arr -> arr
[<AutoOpen>]
module private AsyncHelpers =
open System
open System.Threading.Tasks
type Async with
static member Sleep(t : TimeSpan) : Async<unit> = Async.Sleep(int t.TotalMilliseconds)
/// Re-raise an exception so that the current stacktrace is preserved
static member Raise(e : #exn) : Async<'T> = Async.FromContinuations (fun (_,ec,_) -> ec e)
static member AwaitTaskCorrect (task : Task<'T>) : Async<'T> =
Async.FromContinuations <| fun (k,ek,_) ->
task.ContinueWith (fun (t:Task<'T>) ->
if t.IsFaulted then
let e = t.Exception
if e.InnerExceptions.Count = 1 then ek e.InnerExceptions.[0]
else ek e
elif t.IsCanceled then ek (TaskCanceledException("Task wrapped with Async has been cancelled."))
elif t.IsCompleted then k t.Result
else ek(Exception "invalid Task state!"))
|> ignore