📦 sleepyfran / duets

📄 State.fs · 64 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64module Duets.Agents.State

open Duets.Entities

type Handler = State -> unit

type StateMessage =
    | Get of AsyncReplyChannel<State>
    | Set of State
    | Subscribe of id: Identity * handler: Handler
    | Unsubscribe of id: Identity

type StateAgent() =
    let state =
        MailboxProcessor.Start
        <| fun inbox ->
            let rec loop (state, listeners) =
                async {
                    let! msg = inbox.Receive()

                    let notifyAll value =
                        listeners |> Map.iter (fun _ handler -> handler value)

                    match msg with
                    | Get channel ->
                        channel.Reply state
                        return! loop (state, listeners)
                    | Set value ->
                        notifyAll value
                        return! loop (value, listeners)
                    | Subscribe(id, handler) ->
                        let updated = listeners |> Map.add id handler

                        handler state
                        return! loop (state, updated)
                    | Unsubscribe id ->
                        let updated = listeners |> Map.remove id
                        return! loop (state, updated)
                }

            loop (State.empty, Map.empty)

    member this.Agent = state

let private staticAgent = StateAgent()

type private Subscription(id: Identity) =
    interface System.IDisposable with
        member this.Dispose() =
            Unsubscribe id |> staticAgent.Agent.Post

/// Returns the state of the game.
let get () = staticAgent.Agent.PostAndReply Get

/// Sets the state of the game.
let set value = Set value |> staticAgent.Agent.Post

/// Subscribes to changes on the state and returns the ID that identifies the
/// subscription.
let subscribe handler : System.IDisposable =
    let id = Identity.create ()
    Subscribe(id, handler) |> staticAgent.Agent.Post
    new Subscription(id)