Created
September 15, 2021 09:18
-
-
Save altbodhi/46e7ac07c7645301c304eab796810e11 to your computer and use it in GitHub Desktop.
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
type State = | |
| Ping | |
| Pong | |
type Command = | Send | |
type Event = | Received | |
let apply item = | |
function | |
| Received -> | |
printfn "Received %A" item | |
match item with | |
| Ping -> Pong | |
| Pong -> Ping | |
let exec state = | |
function | |
| Send -> Received | |
type Aggregate<'TState, 'TCommand, 'TEvent> = | |
{ zero: 'TState | |
apply: 'TState -> 'TEvent -> 'TState | |
exec: 'TState -> 'TCommand -> 'TEvent } | |
let makeHandler (aggregate: Aggregate<'TState, 'TCommand, 'TEvent>) events command = | |
let state = | |
Seq.fold aggregate.apply aggregate.zero events | |
aggregate.exec state command | |
let aggregate = | |
{ zero = Ping | |
apply = apply | |
exec = exec } | |
let events = | |
seq { | |
while true do | |
yield Received | |
Async.Sleep 1000 |> Async.RunSynchronously | |
} | |
let handler = makeHandler aggregate events | |
let x = handler Send |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment