我目前正在用 F# 重新开发一个应用程序,虽然体验非常好,但在控制可变性方面我发现自己有点困惑。
以前,我的 C# 程序使用的文档模型是高度可变的,并且实现了 ObservableCollections 和 INotifyPropertyChanged, View 之间的共享状态不会出错。显然,这不是一个理想的选择,特别是如果我想要一种完全不可变的设计方法。
考虑到这一点,我为我的底层应用程序内核创建了一个不可观察、不可变的文档模型,但是,因为我希望 UI 订阅者看到更改,我立即发现自己实现了事件驱动模式:
// Raw data.
type KernelData = { DocumentContent : List<string> }
// Commands that act on the data.
type KernelCommands = { AddString : string -> () }
// A command implementation. Performs a state change, echos the new state through the event.
let addStringCommand (kernelState : KernelData) (kernelChanged : Event<KernelData>) (newString : string) =
kernelState with { DocumentContent=oldList |> List.add newString }
|> kernelChanged.Trigger
// Time to wire this up.
do
// Create some starting state.
let kernelData = { DocumentContent=List.Empty }
// Create a shared event that commands may use to inform observers (UI).
let kernelChangedEvent = new Event<KernelData>()
// Create the command, it uses the event to inform observers.
let kernelCommands = { AddString=addString kernelData kernelChangedEvent }
// Create a UI element that uses the commands to initialize data transformations. UI elements subscribed to the data use the event to listen.
let myUI = new UiObject(kernelData, kernelChangedEvent.Publish, kernelCommands)
myUI.Show()
所以这是我将新状态传递给相关听众的解决方案。然而,更理想的是我可以用转换函数“ Hook ”的“盒子”。当盒子发生变化时,会调用函数来处理新的状态并在 UI 组件中产生相应的变化状态。
do
// Lambda called whenever the box changes.
idealBox >>= (fun newModel -> new UIComponent(newModel))
所以我想我在问是否有一个可观察的模式来处理这些情况。可变状态通常使用 monad 处理,但我只看到涉及执行操作的示例(例如管道控制台 IO monad、加载文件等),而实际上并未处理持续变异状态。
最佳答案
对于这些场景,我的一般解决方案是在纯功能设置中构建所有业务逻辑,然后提供具有同步和传播更改所需功能的瘦服务层。这是您的KernelData
的纯接口(interface)示例。类型:
type KernelData = { DocumentContent : List<string> }
let emptyKernelData = {DocumentContent = []}
let addDocument c kData = {kData with DocumentContent = c :: kData.DocumentContent}
然后,我将定义一个服务层接口(interface),其中包含用于修改和订阅更改的功能:
type UpdateResult =
| Ok
| Error of string
/// Service interface
type KernelService =
{
/// Gets the current kernel state.
Current : unit -> KernelData
/// Subscribes to state changes.
Subscribe : (KernelData -> unit) -> IDisposable
/// Modifies the current kernel state.
Modify : (KernelData -> KernelData) -> Async<UpdateResult>
}
Async
响应启用非阻塞更新。 UpdateResult
type 用于指示更新操作是否成功。为了打造声音KernelService
object 重要的是要意识到修改请求需要通过同步来避免并行更新造成的数据丢失。为此MailboxProcessor
s 派上用场。这是 buildKernelService
给定初始 KernelData
构造服务接口(interface)的函数目的。// Builds a service given an initial kernel data value.
let builKernelService (def: KernelData) =
// Keeps track of the current kernel data state.
let current = ref def
// Keeps track of update events.
let changes = new Event<KernelData>()
// Serves incoming requests for getting the current state.
let currentProc : MailboxProcessor<AsyncReplyChannel<KernelData>> =
MailboxProcessor.Start <| fun inbox ->
let rec loop () =
async {
let! chn = inbox.Receive ()
chn.Reply current.Value
return! loop ()
}
loop ()
// Serves incoming 'modify requests'.
let modifyProc : MailboxProcessor<(KernelData -> KernelData) * AsyncReplyChannel<UpdateResult>> =
MailboxProcessor.Start <| fun inbox ->
let rec loop () =
async {
let! f, chn = inbox.Receive ()
let v = current.Value
try
current := f v
changes.Trigger current.Value
chn.Reply UpdateResult.Ok
with
| e ->
chn.Reply (UpdateResult.Error e.Message)
return! loop ()
}
loop ()
{
Current = fun () -> currentProc.PostAndReply id
Subscribe = changes.Publish.Subscribe
Modify = fun f -> modifyProc.PostAndAsyncReply (fun chn -> f, chn)
}
请注意,上面的实现中没有任何东西是
KernelData
独有的。因此服务接口(interface)和构建函数可以推广到任意类型的内部状态。最后,用
KernelService
编程的一些例子对象:// Build service object.
let service = builKernelService emptyKernelData
// Print current value.
let curr = printfn "Current state: %A" service.Current
// Subscribe
let dispose = service.Subscribe (printfn "New State: %A")
// Non blocking update adding a document
service.Modify <| addDocument "New Document 1"
// Non blocking update removing all existing documents.
service.Modify (fun _ -> emptyKernelData)
// Blocking update operation adding a document.
async {
let! res = service.Modify (addDocument "New Document 2")
printfn "Update Result: %A" res
return ()
}
|> Async.RunSynchronously
// Blocking update operation eventually failing.
async {
let! res =
service.Modify (fun kernelState ->
System.Threading.Thread.Sleep 10000
failwith "Something terrible happened"
)
printfn "Update Result: %A" res
return ()
}
|> Async.RunSynchronously
除了更多的技术细节之外,我相信与您的原始解决方案最重要的区别是不需要特殊的命令功能。使用服务层,任何在
KernelData
上运行的纯函数(例如 addDocument)可以使用 Modify
提升到有状态计算中。功能。
关于f# - 可变状态和观察者模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18814700/