8 ms·
In my experience, observing individual properties on model objects just isn't very useful in real world apps. Cocoa has long offered "key-value observing" (KVO)
by pavlov 11y ago
In my experience, observing individual properties on model objects just isn't very useful in real world apps. Cocoa has long offered "key-value observing" (KVO) for this purpose, but other mechanisms like notifications tend to be more practical, usually because listeners need some context in order to do the best thing in response to a change, and an individual property change won't carry any of that context.
Here's a practical example... Imagine an editor where an on-screen element is deleted. The controller might do a bunch of stuff such as:
element.parent = nil
scene.clearReferencesToElement(element)
container.delete(element)
self.selection = []
If there's a listener on "container", it will get notified even before the selection was cleared. That's probably not the intention.
If we know that's going to happen, we could move the selection manipulation before the container access... But then we have fragile code that depends on implicit behavior in a model class. Always better to send an explicit event.
- tolmasky 11y agoI now subscribe to the React-style "wait, why are you listening to anything?" mindset. However, if you're going to go down the mutable path, I think KVO was particularly well designed. The main reason was that it made composition pretty sane and created clear places to handle changes. With KVO, you implement your KVO-compliant mutation methods, and all changes would then get funneled through there (most notable container ones). Compare the mess that is NSView (addSubview/insertSubview:positioned:relativeTo:/removeFromSuperview/etc) to the post-KVO NSTreeNode. NSTreeNode simply exposes .mutableChildNodes, which you treat as a normal array (addObject:/etc) and after adding a child, it then has parentNode set to the parent after. This was great for lots of reasons: 1. No need to re-create API, you just used NSMutableArray's existing API. So [someNode.mutableChildNodes removeObjectsWithPredicate:somePredicate] and all the parentNode relationships get handled for free. Compare this to pre-KVO methods where every time you wanted to take advantage of a neat NSMutableArray feature, you'd have to re-expose it yourself like removeSubviewsWithPredicate:, or wind up having to copy out the array, mutate, then re-assign with setSubviews: 2. Less room for error since all your cleanup code just happens in removeObjectFrom<Key>AtIndex:, one funnel. Again, I believe this to be a great local maxima for this style of programming, one that was never that fully explored since the move to more KVO-ness was eclipsed by the attention to iOS. I think most the problems are better solved through React's top-down approach.
- haberman 11y agoI like the React model, but it does make one key assumption. It assumes that DOM mutation is so slow that re-rendering and diffing the entire virtual DOM for every update is basically free in comparison. I like the simplicity of React's approach, but this assumption makes me a little uncomfortable.
- Silhouette 11y agoAs an anecdotal data point, that assumption survived approximately no time at all during a recent exercise I was involved with evaluating how to build a new and reasonably large web app. With similar scepticism but trying to keep an open mind, we did an experiment allowing the entire DOM to rerender from a top-level React component when the underlying data model changed. It performed pretty much as badly as everyone thought it obviously would. We then switched to exploring more standard approaches like having individual components monitor the relevant model events and manage their own state, using React's rendering model at a much finer granularity. We haven't finished our investigations yet and there are some other concerns about React's performance under certain conditions, but so far, the finer-grained approach seems more promising and is our most likely way forward for this particular project.
- haberman 11y ago:( I was afraid of that. React docs oversell this point a bit: https://facebook.github.io/react/docs/multiple-components.html#a-note-on-performance https://facebook.github.io/react/docs/multiple-components.ht... "You may be thinking that it's expensive to change data if there are a large number of nodes under an owner. The good news is that JavaScript is fast and render() methods tend to be quite simple, so in most applications this is extremely fast. Additionally, the bottleneck is almost always the DOM mutation and not JS execution. React will optimize this for you by using batching and change detection. [...] Don't underestimate how fast JavaScript is relative to the DOM."
- Silhouette 11y ago
- lewisl9029 11y agoI was thinking that observing properties could be useful for hooking on to changes to some internal state that a third party library maintains, but doesn't provide a callback for (for instance, for persistence and/or synchronization). Is there a more idiomatic way to work around this in JS other than Object.observe if you don't want to resort to dirty checking or patching the library to provide a callback?
- bcherny 11y agoO.o seems like a bad option in your case, since not only would you be relying on an internal API that could change at any point, but you'd be relying on the shape of its internal data staying the same too.
- i38608 11y agoI don't understand your example.