Skip to content

Instantly share code, notes, and snippets.

@Horusiath
Created July 13, 2015 08:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Horusiath/c4bbf4d441fbc7063d26 to your computer and use it in GitHub Desktop.
Save Horusiath/c4bbf4d441fbc7063d26 to your computer and use it in GitHub Desktop.
Akkling perisistence example
open System
open System.Threading
open Akkling
open Akkling.Persistence
let system = System.create "persistent-system" (Configuration.defaultConfig())
type Event =
| Added of int
| Removed of int
type Command =
| Add of int
| Remove of int
| GetState
let apply state = function
| Added i -> i::state
| Removed i ->
let rec remove i list =
match list with
| [] -> []
| x::xs when x = i -> xs
| x::xs -> x::(remove i xs)
remove i state
let testSpawn () =
spawnPersist system "pid-1" []
<| fun mailbox ->
{
apply = apply
exec =
let rec loop () =
actor {
let! msg = mailbox.Receive()
match msg with
| Add i -> mailbox.PersistEvent (apply (mailbox.State())) [Added i]
| Remove i -> mailbox.PersistEvent (apply (mailbox.State())) [Removed i]
| GetState -> mailbox.Sender() <! mailbox.State()
return! loop ()
}
loop ()
}
<| []
let pref = testSpawn ()
pref <! Add 1
pref <! Add 2
pref <! Add 3
pref <! Remove 2
Thread.Sleep 1000
async {
let! (data : int list) = pref <? GetState
printfn "Current state: %A" data
} |> Async.RunSynchronously
Console.ReadLine()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment