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:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
|
module FSharp.Control
open System
open System.Threading
type AsyncState<'a> =
| NotStarted of Async<'a>
| 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>(state:AsyncState<'a>) =
let state = ref state
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 asyncValue ->
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()
| _ -> ()
/// Will start calculation if not started
/// callBack will be called on the same thread that called GetValueAsync
member x.GetValueAsync(callBack:Action<_>) =
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
member x.Subscribe f =
x.doWhenCompleted f false
x
[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
module LazyAsync =
let fromAsync asyncValue =
LazyAsync(NotStarted asyncValue)
let fromValue value =
LazyAsync(Completed value)
let map f (x:LazyAsync<'a>) =
async {
let ev = Event<_>()
x.GetValueAsync(fun value -> ev.Trigger value)
let! value = Async.AwaitEvent(ev.Publish)
return f value
} |> fromAsync
let subscribe f (x:LazyAsync<'a>) =
x.Subscribe f
|
Multiple items
namespace FSharp
--------------------
namespace Microsoft.FSharp
Multiple items
module Control
from FSharp
--------------------
namespace Microsoft.FSharp.Control
namespace System
namespace System.Threading
type AsyncState<'a> =
| NotStarted of Async<'a>
| Started
| Completed of 'a
Full name: FSharp.Control.AsyncState<_>
union case AsyncState.NotStarted: Async<'a> -> AsyncState<'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<_>
union case AsyncState.Started: AsyncState<'a>
union case AsyncState.Completed: 'a -> AsyncState<'a>
Multiple items
type LazyAsync<'a> =
new : state:AsyncState<'a> -> LazyAsync<'a>
member GetValueAsync : callBack:Action<'a> -> unit
member Subscribe : f:('a -> unit) -> LazyAsync<'a>
member private doWhenCompleted : f:('a -> unit) -> startIfNotRunning:bool -> unit
Full name: FSharp.Control.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 : state:AsyncState<'a> -> LazyAsync<'a>
val state : AsyncState<'a>
val state : AsyncState<'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: FSharp.Control.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 asyncValue : Async<'a>
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)
val x : LazyAsync<'a>
member LazyAsync.GetValueAsync : callBack:Action<'a> -> unit
Full name: FSharp.Control.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 : (('b -> unit) -> 'b -> unit)
val f : ('b -> unit)
val arg : 'b
SynchronizationContext.Post(d: SendOrPostCallback, state: obj) : unit
member private LazyAsync.doWhenCompleted : f:('a -> unit) -> startIfNotRunning:bool -> unit
Action.Invoke(obj: 'a) : unit
member LazyAsync.Subscribe : f:('a -> unit) -> LazyAsync<'a>
Full name: FSharp.Control.LazyAsync`1.Subscribe
Multiple items
type CompilationRepresentationAttribute =
inherit Attribute
new : flags:CompilationRepresentationFlags -> CompilationRepresentationAttribute
member Flags : CompilationRepresentationFlags
Full name: Microsoft.FSharp.Core.CompilationRepresentationAttribute
--------------------
new : flags:CompilationRepresentationFlags -> CompilationRepresentationAttribute
type CompilationRepresentationFlags =
| None = 0
| Static = 1
| Instance = 2
| ModuleSuffix = 4
| UseNullAsTrueValue = 8
| Event = 16
Full name: Microsoft.FSharp.Core.CompilationRepresentationFlags
CompilationRepresentationFlags.ModuleSuffix: CompilationRepresentationFlags = 4
Multiple items
module LazyAsync
from FSharp.Control
--------------------
type LazyAsync<'a> =
new : state:AsyncState<'a> -> LazyAsync<'a>
member GetValueAsync : callBack:Action<'a> -> unit
member Subscribe : f:('a -> unit) -> LazyAsync<'a>
member private doWhenCompleted : f:('a -> unit) -> startIfNotRunning:bool -> unit
Full name: FSharp.Control.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 : state:AsyncState<'a> -> LazyAsync<'a>
val fromAsync : asyncValue:Async<'a> -> LazyAsync<'a>
Full name: FSharp.Control.LazyAsyncModule.fromAsync
val fromValue : value:'a -> LazyAsync<'a>
Full name: FSharp.Control.LazyAsyncModule.fromValue
val map : f:('a -> 'a0) -> x:LazyAsync<'a> -> LazyAsync<'a0>
Full name: FSharp.Control.LazyAsyncModule.map
val f : ('a -> 'a0)
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)
val subscribe : f:('a -> unit) -> x:LazyAsync<'a> -> LazyAsync<'a>
Full name: FSharp.Control.LazyAsyncModule.subscribe
member LazyAsync.Subscribe : f:('a -> unit) -> LazyAsync<'a>
More information