daily pastebin goal
79%
SHARE
TWEET

F# akka fsm

a guest Jan 30th, 2017 19 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. open System
  2. open Akka.FSharp
  3.  
  4. type PersonName = PersonName of string
  5. type Person = {Name:PersonName; Age:int}
  6. type ActorMessage =
  7.     | BeginProcess of PersonName
  8.     | PersonDetails of Person
  9.  
  10. [<EntryPoint>]
  11. let main argv =
  12.     let system = System.create "my-system" (Configuration.defaultConfig())
  13.    
  14.     let detailsActor =
  15.         spawn system "my-child-actor" <| actorOf2
  16.             (fun mailbox name ->
  17.                 // ... Normally we have some query here for the user ...
  18.                 // But we are just going to Tell the sender actor about a Person
  19.                 mailbox.Sender() <! (PersonDetails {Name=name; Age=99}) )
  20.  
  21.     let rootActor =
  22.         spawn system "my-parent-actor"
  23.         <| fun mailbox ->
  24.             let rec idle () =
  25.                 actor {
  26.                     let! message = mailbox.Receive()
  27.                     match message with
  28.                     | BeginProcess name ->
  29.                         detailsActor <! name
  30.                         return! awaitingdetails name
  31.                     | _ ->
  32.                         return! idle ()
  33.                 }
  34.             and awaitingdetails name =
  35.                 actor {
  36.                     let! message = mailbox.Receive()
  37.                     match message with
  38.                     | PersonDetails person ->
  39.                         printfn "Received person :: %A" person
  40.                         mailbox.UnstashAll()
  41.                         return! idle ()
  42.                     | _ ->
  43.                         mailbox.Stash()
  44.                         return! awaitingdetails name
  45.                 }
  46.  
  47.             idle ()
RAW Paste Data
Top