Last active
February 9, 2023 23:13
-
-
Save ruxo/1e0632d9f707fb9ee962df1550e74747 to your computer and use it in GitHub Desktop.
(temp) F# Akka extension
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module RZ.FSharp.Akka.Extension | |
open System.Threading.Tasks | |
open Akka.Actor | |
open Akka.FSharp | |
open System.Runtime.CompilerServices | |
type ReceiveActorFs() = | |
inherit ReceiveActor() | |
member _.Context = ActorBase.Context | |
member _.Scheduler = ActorBase.Context.System.Scheduler | |
member this.PipeToSelf (success: unit -> 'a) (failure: exn option -> 'b) (work: Async<unit>) = | |
let t = work |> Async.StartAsTask | |
t.PipeTo(this.Self, ActorRefs.NoSender, (fun _ -> success() :> obj), (fun ex -> (Option.ofObj(ex) |> failure) :> obj)) |> ignore | |
member this.ReceiveAsync<'T>(predicate: 'T -> bool, handler: 'T -> Async<unit>) = | |
this.ReceiveAsync<'T>(predicate, (fun x -> Async.StartImmediateAsTask(handler x) :> Task)) | |
[<Extension>] | |
type ActorMessageExtension = | |
[<Extension>] | |
static member PipeToSelf(mailbox: Actor<'Any>, success: 'Result -> 'a, failure: exn -> 'b) = fun (work: Async<'Result>) -> | |
let work_task = work |> Async.StartAsTask | |
work_task.ContinueWith(fun (t: Task<'Result>) -> | |
if t.IsCompletedSuccessfully then | |
mailbox.Self <! success(t.Result) | |
else | |
mailbox.Self <! failure(if t.IsCanceled then TaskCanceledException() :> exn else t.Exception :> exn) | |
) |> ignore |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment