Skip to content

Instantly share code, notes, and snippets.

@brase
Created March 5, 2016 22:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brase/ed70fbe22e3d9536181c to your computer and use it in GitHub Desktop.
Save brase/ed70fbe22e3d9536181c to your computer and use it in GitHub Desktop.
#r "C:/Users/brase/code/fs/bloc.akka/src/Bloc.Application/bin/Debug/Akka.dll"
#r "C:/Users/brase/code/fs/bloc.akka/src/Bloc.Application/bin/Debug/Wire.dll"
#r "C:/Users/brase/code/fs/bloc.akka/src/Bloc.Application/bin/Debug/Newtonsoft.Json.dll"
#r "C:/Users/brase/code/fs/bloc.akka/src/Bloc.Application/bin/Debug/FSharp.PowerPack.dll"
#r "C:/Users/brase/code/fs/bloc.akka/src/Bloc.Application/bin/Debug/FSharp.PowerPack.Linq.dll"
#r "C:/Users/brase/code/fs/bloc.akka/src/Bloc.Application/bin/Debug/Akkling.dll"
#r "C:/Users/brase/code/fs/bloc.akka/src/Bloc.Application/bin/Debug/Google.ProtocolBuffers.dll"
#r "C:/Users/brase/code/fs/bloc.akka/src/Bloc.Application/bin/Debug/Akka.Persistence.dll"
#r "C:/Users/brase/code/fs/bloc.akka/src/Bloc.Application/bin/Debug/Akkling.Persistence.dll"
open System
open Akkling
open Akkling.Persistence
let system = System.create "persisting-sys" <| Configuration.defaultConfig()
type CounterChanged =
{ Delta : int }
type CounterCommand =
| Inc
| Dec
| GetState
type CounterMessage =
| Command of CounterCommand
| Event of CounterChanged
let counter =
spawn system "counter-1" <| propsPersist(fun mailbox ->
let rec loop state =
actor {
let! msg = mailbox.Receive()
match msg with
| Event(changed) -> return! loop (state + changed.Delta)
| Command(cmd) ->
match cmd with
| GetState ->
printfn "%A" mailbox.Pid
mailbox.Sender() <! state
return! loop state
| Inc -> return Persist (Event { Delta = 1 })
| Dec -> return Persist (Event { Delta = -1 })
}
loop 0)
//counter <! Command Inc
//counter <! Command Inc
//counter <! Command Dec
async { let! reply = counter <? Command GetState
printfn "Current state of %A: %i" counter reply.Value } |> Async.RunSynchronously
let viewProp = propsView "counter-1" (fun mailbox ->
let rec loop state =
actor{
let! msg = mailbox.Receive()
match msg with
| Event(changed) -> return! loop (state + changed.Delta)
| Command(cmd) ->
match cmd with
| GetState ->
printfn "%A" mailbox.Pid
mailbox.Sender() <! state
return! loop state
| _ -> return Unhandled
}
loop 0)
let view = spawn system "view-1" <| viewProp
counter <! Command Inc
counter <! Command Inc
counter <! Command Inc
counter <! Command Inc
counter <! Command Inc
async { let! reply = view <? Command GetState
printfn "Current state of %A: %i" view reply.Value } |> Async.RunSynchronously
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment