我是F#的新手并尝试使用MailboxProcessor来确保状态更改是孤立完成的.
简而言之,我将动作(描述状态通道的不可变对象)发布到MailboxProcessor,在递归函数中我读取消息并生成新状态(即在下面的示例中将项添加到集合中)并将该状态发送到下一次递归.
open System
type AppliationState =
{
Store : string list
}
static member Default =
{
Store = List.empty
}
member this.HandleAction (action:obj) =
match action with
| :? string as a -> { this with Store = a :: this.Store }
| _ -> this
type Agent<'T> = MailboxProcessor<'T>
[<AbstractClass; Sealed>]
type AppHolder private () =
static member private Processor = Agent.Start(fun inbox ->
let rec loop (s : AppliationState) =
async {
let! action = inbox.Receive() …Run Code Online (Sandbox Code Playgroud)