Is there a generalized approach to handling notifications of changes from large persistent data structure?
First the motivation for this question: anyone who uses persistent data structures and a gui runs into this problem. So, my questions is kind of: do people just work it out on a case by case basis, or are their libraries, utilities, fra开发者_如何学Cmeworks, something that abstracts the general case to make it easier to do.
The specific language I'm targeting is Scala, but I think this is a general question related to persistent data structures.
Let's say you have a large data structure displayed in a complex gui with trees, tables, property sheets, etc. Various user or system actions result in modifications to the data structure.
Is there a general strategy for handling notifications to tell the gui to update itself?
One of the intriguing ideas with persistent data structures is one could handle undo/redo by just switching between roots. That works for the data, but is there a general mechanism for telling listeners (eg. a gui) about the change if one were to switch roots?
My understanding is that it needs to be incremental--I need to know what changed. I can't just tell everything to do a complete refresh of the gui since that could be very expensive (and some controls don't handle it well (or at all) because of maintaining gui state).
You'd update the GUI at the same time you switch roots, since if the structure is truly persistent, then any change deep down in the structure will incur a new root. That, however, will be done imperatively (or in some kind of monadic construction; I've never done GUI programming in Haskell).
E.g., if your GUI code is imperative/OO but uses a persistent State
structure:
// poor man's zipper for unbounded undo/redo
var curState : State
var prevStates : List[State]
var nextStates : List[State]
def undo = {
val prev = prevStates.head
prevStates = prevStates.tail
nextStates = curState :: nextStates
curState = prev
updateGui(curState)
}
See this question about Functional Reactive Programming in Scala. And also see the Cells menifesto and the Wikipedia article about FRP for background.
And read the paper "Deprecating Observer Pattern using Scala.React": http://lamp.epfl.ch/~imaier/pub/DeprecatingObserversTR2010.pdf mentioned on an answer by magicduncan on the above SO question.
精彩评论