3 people like it.
Like the snippet!
LazyAsync
Allows to expose an F# async value in a C#-friendly API with the semantics of Lazy<> (compute on demand and guarantee only one computation)
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:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
|
open System
open System.Threading
open System
open System.Threading
type private ComputationState<'a> =
| NotStarted
| Started
| Completed of 'a
/// Allows to expose a F# async value in a C#-friendly API with the
/// semantics of Lazy<> (compute on demand and guarantee only one computation)
type LazyAsync<'a>(asyncValue:Async<'a>) =
let state = ref NotStarted
let completed = Event<_>()
let icompleted = completed.Publish
member private __.doWhenCompleted f startIfNotRunning =
let continuation =
lock state <| fun () ->
match !state with
| Completed value ->
Some <| fun () -> f value
| Started ->
icompleted.Add f
None
| NotStarted ->
icompleted.Add f
if startIfNotRunning then
state := Started
Some <| fun () ->
async {
try
let! value = asyncValue
lock state <| fun () -> state := Completed value
completed.Trigger value
with _ -> ()
} |> Async.Start
else
None
match continuation with
| Some continuation -> continuation()
| _ -> ()
static member map f (x:LazyAsync<'a>) =
LazyAsync(async {
let ev = Event<_>()
x.GetValueAsync(fun value -> ev.Trigger value)
let! value = Async.AwaitEvent(ev.Publish)
return f value
})
member x.Then f =
x.doWhenCompleted f false
x
/// Will start calculation if not started
/// callBack will be called on the same thread that called GetValueAsync
member x.GetValueAsync(callBack:Action<'a>) =
let synchronizationContext = SynchronizationContext.Current
let doInOriginalThread f arg =
if synchronizationContext <> null then
synchronizationContext.Post ((fun _ -> f arg), null)
else
f arg
x.doWhenCompleted (doInOriginalThread callBack.Invoke) true
|
namespace System
namespace System.Threading
type private ComputationState<'a> =
| NotStarted
| Started
| Completed of 'a
Full name: Script.ComputationState<_>
union case ComputationState.NotStarted: ComputationState<'a>
union case ComputationState.Started: ComputationState<'a>
union case ComputationState.Completed: 'a -> ComputationState<'a>
Multiple items
type LazyAsync<'a> =
new : asyncValue:Async<'a> -> LazyAsync<'a>
member GetValueAsync : callBack:Action<'a> -> unit
member Then : f:('a -> unit) -> LazyAsync<'a>
member private doWhenCompleted : f:('a -> unit) -> startIfNotRunning:bool -> unit
static member map : f:('a -> 'a0) -> x:LazyAsync<'a> -> LazyAsync<'a0>
Full name: Script.LazyAsync<_>
Allows to expose a F# async value in a C#-friendly API with the
semantics of Lazy<> (compute on demand and guarantee only one computation)
--------------------
new : asyncValue:Async<'a> -> LazyAsync<'a>
val asyncValue : Async<'a>
Multiple items
type Async
static member AsBeginEnd : computation:('Arg -> Async<'T>) -> ('Arg * AsyncCallback * obj -> IAsyncResult) * (IAsyncResult -> 'T) * (IAsyncResult -> unit)
static member AwaitEvent : event:IEvent<'Del,'T> * ?cancelAction:(unit -> unit) -> Async<'T> (requires delegate and 'Del :> Delegate)
static member AwaitIAsyncResult : iar:IAsyncResult * ?millisecondsTimeout:int -> Async<bool>
static member AwaitTask : task:Task<'T> -> Async<'T>
static member AwaitWaitHandle : waitHandle:WaitHandle * ?millisecondsTimeout:int -> Async<bool>
static member CancelDefaultToken : unit -> unit
static member Catch : computation:Async<'T> -> Async<Choice<'T,exn>>
static member FromBeginEnd : beginAction:(AsyncCallback * obj -> IAsyncResult) * endAction:(IAsyncResult -> 'T) * ?cancelAction:(unit -> unit) -> Async<'T>
static member FromBeginEnd : arg:'Arg1 * beginAction:('Arg1 * AsyncCallback * obj -> IAsyncResult) * endAction:(IAsyncResult -> 'T) * ?cancelAction:(unit -> unit) -> Async<'T>
static member FromBeginEnd : arg1:'Arg1 * arg2:'Arg2 * beginAction:('Arg1 * 'Arg2 * AsyncCallback * obj -> IAsyncResult) * endAction:(IAsyncResult -> 'T) * ?cancelAction:(unit -> unit) -> Async<'T>
static member FromBeginEnd : arg1:'Arg1 * arg2:'Arg2 * arg3:'Arg3 * beginAction:('Arg1 * 'Arg2 * 'Arg3 * AsyncCallback * obj -> IAsyncResult) * endAction:(IAsyncResult -> 'T) * ?cancelAction:(unit -> unit) -> Async<'T>
static member FromContinuations : callback:(('T -> unit) * (exn -> unit) * (OperationCanceledException -> unit) -> unit) -> Async<'T>
static member Ignore : computation:Async<'T> -> Async<unit>
static member OnCancel : interruption:(unit -> unit) -> Async<IDisposable>
static member Parallel : computations:seq<Async<'T>> -> Async<'T []>
static member RunSynchronously : computation:Async<'T> * ?timeout:int * ?cancellationToken:CancellationToken -> 'T
static member Sleep : millisecondsDueTime:int -> Async<unit>
static member Start : computation:Async<unit> * ?cancellationToken:CancellationToken -> unit
static member StartAsTask : computation:Async<'T> * ?taskCreationOptions:TaskCreationOptions * ?cancellationToken:CancellationToken -> Task<'T>
static member StartChild : computation:Async<'T> * ?millisecondsTimeout:int -> Async<Async<'T>>
static member StartChildAsTask : computation:Async<'T> * ?taskCreationOptions:TaskCreationOptions -> Async<Task<'T>>
static member StartImmediate : computation:Async<unit> * ?cancellationToken:CancellationToken -> unit
static member StartWithContinuations : computation:Async<'T> * continuation:('T -> unit) * exceptionContinuation:(exn -> unit) * cancellationContinuation:(OperationCanceledException -> unit) * ?cancellationToken:CancellationToken -> unit
static member SwitchToContext : syncContext:SynchronizationContext -> Async<unit>
static member SwitchToNewThread : unit -> Async<unit>
static member SwitchToThreadPool : unit -> Async<unit>
static member TryCancelled : computation:Async<'T> * compensation:(OperationCanceledException -> unit) -> Async<'T>
static member CancellationToken : Async<CancellationToken>
static member DefaultCancellationToken : CancellationToken
Full name: Microsoft.FSharp.Control.Async
--------------------
type Async<'T>
Full name: Microsoft.FSharp.Control.Async<_>
val state : ComputationState<'a> ref
Multiple items
val ref : value:'T -> 'T ref
Full name: Microsoft.FSharp.Core.Operators.ref
--------------------
type 'T ref = Ref<'T>
Full name: Microsoft.FSharp.Core.ref<_>
val completed : Event<'a>
Multiple items
module Event
from Microsoft.FSharp.Control
--------------------
type Event<'T> =
new : unit -> Event<'T>
member Trigger : arg:'T -> unit
member Publish : IEvent<'T>
Full name: Microsoft.FSharp.Control.Event<_>
--------------------
type Event<'Delegate,'Args (requires delegate and 'Delegate :> Delegate)> =
new : unit -> Event<'Delegate,'Args>
member Trigger : sender:obj * args:'Args -> unit
member Publish : IEvent<'Delegate,'Args>
Full name: Microsoft.FSharp.Control.Event<_,_>
--------------------
new : unit -> Event<'T>
--------------------
new : unit -> Event<'Delegate,'Args>
val icompleted : IEvent<'a>
property Event.Publish: IEvent<'a>
member private LazyAsync.doWhenCompleted : f:('a -> unit) -> startIfNotRunning:bool -> unit
Full name: Script.LazyAsync`1.doWhenCompleted
val f : ('a -> unit)
val startIfNotRunning : bool
val continuation : (unit -> unit) option
val lock : lockObject:'Lock -> action:(unit -> 'T) -> 'T (requires reference type)
Full name: Microsoft.FSharp.Core.Operators.lock
val value : 'a
union case Option.Some: Value: 'T -> Option<'T>
member IObservable.Add : callback:('T -> unit) -> unit
union case Option.None: Option<'T>
val async : AsyncBuilder
Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.async
member Event.Trigger : arg:'T -> unit
static member Async.Start : computation:Async<unit> * ?cancellationToken:CancellationToken -> unit
val continuation : (unit -> unit)
static member LazyAsync.map : f:('a -> 'a0) -> x:LazyAsync<'a> -> LazyAsync<'a0>
Full name: Script.LazyAsync`1.map
val f : ('a -> 'a0)
val x : LazyAsync<'a>
val ev : Event<'a>
member LazyAsync.GetValueAsync : callBack:Action<'a> -> unit
Will start calculation if not started
callBack will be called on the same thread that called GetValueAsync
static member Async.AwaitEvent : event:IEvent<'Del,'T> * ?cancelAction:(unit -> unit) -> Async<'T> (requires delegate and 'Del :> Delegate)
member LazyAsync.Then : f:('a -> unit) -> LazyAsync<'a>
Full name: Script.LazyAsync`1.Then
member private LazyAsync.doWhenCompleted : f:('a -> unit) -> startIfNotRunning:bool -> unit
member LazyAsync.GetValueAsync : callBack:Action<'a> -> unit
Full name: Script.LazyAsync`1.GetValueAsync
Will start calculation if not started
callBack will be called on the same thread that called GetValueAsync
val callBack : Action<'a>
Multiple items
type Action =
delegate of unit -> unit
Full name: System.Action
--------------------
type Action<'T> =
delegate of 'T -> unit
Full name: System.Action<_>
--------------------
type Action<'T1,'T2> =
delegate of 'T1 * 'T2 -> unit
Full name: System.Action<_,_>
--------------------
type Action<'T1,'T2,'T3> =
delegate of 'T1 * 'T2 * 'T3 -> unit
Full name: System.Action<_,_,_>
--------------------
type Action<'T1,'T2,'T3,'T4> =
delegate of 'T1 * 'T2 * 'T3 * 'T4 -> unit
Full name: System.Action<_,_,_,_>
--------------------
type Action<'T1,'T2,'T3,'T4,'T5> =
delegate of 'T1 * 'T2 * 'T3 * 'T4 * 'T5 -> unit
Full name: System.Action<_,_,_,_,_>
--------------------
type Action<'T1,'T2,'T3,'T4,'T5,'T6> =
delegate of 'T1 * 'T2 * 'T3 * 'T4 * 'T5 * 'T6 -> unit
Full name: System.Action<_,_,_,_,_,_>
--------------------
type Action<'T1,'T2,'T3,'T4,'T5,'T6,'T7> =
delegate of 'T1 * 'T2 * 'T3 * 'T4 * 'T5 * 'T6 * 'T7 -> unit
Full name: System.Action<_,_,_,_,_,_,_>
--------------------
type Action<'T1,'T2,'T3,'T4,'T5,'T6,'T7,'T8> =
delegate of 'T1 * 'T2 * 'T3 * 'T4 * 'T5 * 'T6 * 'T7 * 'T8 -> unit
Full name: System.Action<_,_,_,_,_,_,_,_>
--------------------
type Action<'T1,'T2,'T3,'T4,'T5,'T6,'T7,'T8,'T9> =
delegate of 'T1 * 'T2 * 'T3 * 'T4 * 'T5 * 'T6 * 'T7 * 'T8 * 'T9 -> unit
Full name: System.Action<_,_,_,_,_,_,_,_,_>
--------------------
type Action<'T1,'T2,'T3,'T4,'T5,'T6,'T7,'T8,'T9,'T10> =
delegate of 'T1 * 'T2 * 'T3 * 'T4 * 'T5 * 'T6 * 'T7 * 'T8 * 'T9 * 'T10 -> unit
Full name: System.Action<_,_,_,_,_,_,_,_,_,_>
--------------------
type Action<'T1,'T2,'T3,'T4,'T5,'T6,'T7,'T8,'T9,'T10,'T11> =
delegate of 'T1 * 'T2 * 'T3 * 'T4 * 'T5 * 'T6 * 'T7 * 'T8 * 'T9 * 'T10 * 'T11 -> unit
Full name: System.Action<_,_,_,_,_,_,_,_,_,_,_>
--------------------
type Action<'T1,'T2,'T3,'T4,'T5,'T6,'T7,'T8,'T9,'T10,'T11,'T12> =
delegate of 'T1 * 'T2 * 'T3 * 'T4 * 'T5 * 'T6 * 'T7 * 'T8 * 'T9 * 'T10 * 'T11 * 'T12 -> unit
Full name: System.Action<_,_,_,_,_,_,_,_,_,_,_,_>
--------------------
type Action<'T1,'T2,'T3,'T4,'T5,'T6,'T7,'T8,'T9,'T10,'T11,'T12,'T13> =
delegate of 'T1 * 'T2 * 'T3 * 'T4 * 'T5 * 'T6 * 'T7 * 'T8 * 'T9 * 'T10 * 'T11 * 'T12 * 'T13 -> unit
Full name: System.Action<_,_,_,_,_,_,_,_,_,_,_,_,_>
--------------------
type Action<'T1,'T2,'T3,'T4,'T5,'T6,'T7,'T8,'T9,'T10,'T11,'T12,'T13,'T14> =
delegate of 'T1 * 'T2 * 'T3 * 'T4 * 'T5 * 'T6 * 'T7 * 'T8 * 'T9 * 'T10 * 'T11 * 'T12 * 'T13 * 'T14 -> unit
Full name: System.Action<_,_,_,_,_,_,_,_,_,_,_,_,_,_>
--------------------
type Action<'T1,'T2,'T3,'T4,'T5,'T6,'T7,'T8,'T9,'T10,'T11,'T12,'T13,'T14,'T15> =
delegate of 'T1 * 'T2 * 'T3 * 'T4 * 'T5 * 'T6 * 'T7 * 'T8 * 'T9 * 'T10 * 'T11 * 'T12 * 'T13 * 'T14 * 'T15 -> unit
Full name: System.Action<_,_,_,_,_,_,_,_,_,_,_,_,_,_,_>
--------------------
type Action<'T1,'T2,'T3,'T4,'T5,'T6,'T7,'T8,'T9,'T10,'T11,'T12,'T13,'T14,'T15,'T16> =
delegate of 'T1 * 'T2 * 'T3 * 'T4 * 'T5 * 'T6 * 'T7 * 'T8 * 'T9 * 'T10 * 'T11 * 'T12 * 'T13 * 'T14 * 'T15 * 'T16 -> unit
Full name: System.Action<_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_>
val synchronizationContext : SynchronizationContext
Multiple items
type SynchronizationContext =
new : unit -> SynchronizationContext
member CreateCopy : unit -> SynchronizationContext
member IsWaitNotificationRequired : unit -> bool
member OperationCompleted : unit -> unit
member OperationStarted : unit -> unit
member Post : d:SendOrPostCallback * state:obj -> unit
member Send : d:SendOrPostCallback * state:obj -> unit
member Wait : waitHandles:nativeint[] * waitAll:bool * millisecondsTimeout:int -> int
static member Current : SynchronizationContext
static member SetSynchronizationContext : syncContext:SynchronizationContext -> unit
Full name: System.Threading.SynchronizationContext
--------------------
SynchronizationContext() : unit
property SynchronizationContext.Current: SynchronizationContext
val doInOriginalThread : (('a -> unit) -> 'a -> unit)
val arg : 'a
SynchronizationContext.Post(d: SendOrPostCallback, state: obj) : unit
Action.Invoke(obj: 'a) : unit
More information