Created
July 13, 2015 08:30
-
-
Save Horusiath/c4bbf4d441fbc7063d26 to your computer and use it in GitHub Desktop.
Akkling perisistence example
This file contains hidden or 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
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