Object.observe withdrawn from TC39
mail.mozilla.org
mail.mozilla.org
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.
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.
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.
If you wanted to be more adventurous you should look at alternative virtual dom implementations http://vdom-benchmark.github.io/vdom-benchmark/. What you'll find is that React is actually the slowest one out there based on performance. For instance, on dbmonster InfernoJS sees 50-100% more fps and on my machine ~20x better benchmark performance. If you want the next popular implementations look at virtual-dom, which is leveraged in a variety of frameworks like https://github.com/ashaffer/vdux, or Google's Incremental DOM, https://github.com/google/incremental-dom.
But I don't think that really matters. Fundamentally, once we have a moderately complicated data model such as configuring a few thousand entities of perhaps 50-100 different types with assorted properties each and assorted constraints between them, even a relatively passive scan over the model to rerender the UI from the top following each transaction on the data model does not appear to be a viable architecture today.
Fortunately, that architecture is also unnecessary, as a modest amount of finer-grained integration work presents a much more realistic alternative. The declarative style of specifying a UI still has considerable potential as a way of composing manageable components, we just need a scalable way of decomposing the overall design to match, and it seems for now that means smaller-scale components need to be aware of their own concerns in some cases.
React docs oversell this point a bit: 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."
That's really not how you'd write a large idiomatic React application.
Conceptually, you should be running render() methods on the actual items which have changed, and a very small number of parent components with little-to-no DOM elements. You should also be executing a relatively small number of shouldComponentUpdate() methods which should be extremely fast to execute.
If you are doing that, then where exactly was the performance bottleneck, and how was your alternative approach avoiding it? I mean, sure, if there's simply no possible way for you to figure out what to re-render cheaply, then your app will have poor performance. But that doesn't seem to be true from your other comments.
In fact, it sounds like your second approach might be closer to a normal idiomatic React app than your first implementation. Incidentally, are you using some sort of flux architecture?
That's a bit No True Scotsman for me. Just in the replies to my previous comment, there are several quite clear yet apparently contradictory views expressed about how these things are supposed to work. I doubt that anyone, probably including the React developers themselves, yet has enough experience to sensibly determine what is and isn't idiomatic or best practice here. In any case, I'm far more interested in effective results than dogma, which is why we've been doing practical investigations into what really happens when the tools are used in different ways.
Conceptually, you should be running render() methods on the actual items which have changed, and a very small number of parent components with little-to-no DOM elements. You should also be executing a relatively small number of shouldComponentUpdate() methods which should be extremely fast to execute.
And this is where the assumptions started breaking down in our experiment.
For one thing, we have a somewhat complicated data model and a somewhat complicated UI to present it. Managing the relevant shouldComponentUpdate mechanisms was getting tedious even in our short term experiment. If you're rerendering top-down in a heavily nested UI then it seems you need to provide shouldComponentUpdate in quite a lot of places just to deal with the parent-child relationships with acceptable efficiency, and each of those implementations is a potential source of errors because now you have multiple sources of truth.
Also, for things that aren't extremely fast to execute because they rely on derived data, you wind up with questions of what intermediate results to cache and where. Caching intermediate results goes against the kind of approach much of the React community seems to advocate, hence props in getInitialState being an antipattern and all that.
Time will tell, but once the abstraction started leaking in these kinds of ways, it wasn't clear to us that trying to follow the pure, declarative approach that a lot of React advocacy promotes was really better than other strategies.
If you are doing that, then where exactly was the performance bottleneck, and how was your alternative approach avoiding it?
Bottlenecks were as above, among others.
Avoiding them comes down to the simple principle that if each component attaches to listen to events in a modular way and sets its own state accordingly, then the default is essentially not to rerender anything and has zero overhead. As soon as you start making active decisions about what to rerender, you immediately have the overhead of those decisions to worry about, and sometimes that overhead is significant if you're, say, responding to every character typed in a text box.
If you respond at a more local level, you can choose the granularity of responses to keep the overheads more controlled, which it seems so far is effectively getting the best of both worlds.
In fact, it sounds like your second approach might be closer to a normal idiomatic React app than your first implementation. Incidentally, are you using some sort of flux architecture?
In the sense that user interactions send meaningful messages to an internal component, that component is responsible for updating some stored data accordingly, and the internal component then signals changes that other parts of the UI can respond to, yes, it's somewhat like Flux in overall architecture.
It's always been clear that React alone isn't enough to write a full application; you need to combine it with some other pieces. The official recommendation from Facebook is to use flux, and the most popular flux implementation (redux) is quite good, but you can also use Om-style approaches (Omniscient, Baobab, etc.), or wire it up to legacy Backbone models, or whatever. But you need something; you can't write an MVC app with only the V and expect good results.
You guys tried to write a large "pure" React application, and it failed in all the ways that large React applications always fail. And you learned from it, and hacked together a kinda-sorta-fluxish solution, and it's working for you, and that's great.
...but if you want to take another look at it someday, I'd highly suggest checking out redux. It's very slick, and it was explicitly designed to solve the exact issues you ran into.
(In particular, a redux app is built of a small number of smart components which respond to to a specific subset of events, each managing a tree of "dumb" components, which gives you the granularity you were looking for. And then you can use the PureRenderMixin and immutable data, which gives you the very fast shouldComponentUpdate methods you were looking for for free, no need to ever write your own since, as you found, that way lies madness. The docs even cover how to properly cache derived data, another issue you noted running into. And so on.)
(...of course, that doesn't really tell you much about React; seems like all you really learned is that badly architected apps perform badly. Not sure that one needed much testing. And of course, your initial comment seemed to be making a very different claim.)
In any case, our current view is that there is a useful middle ground where React may work well for us. The app in question predates just about all of the modern JS UI libraries/frameworks, at least in anything close to their current forms, so much of the rendering and event management code right at the front end is built with jQuery, early toolkits, and in many cases a lot of home-grown code. All of this still works pretty well, but quite a large proportion of it is functionality that every UI library in the universe provides today, so there's little reason to maintain another version any more. The rest of the app does have a good architecture that has stood the test of time, but for the next big update we're trying to solve much the same issues as everyone else with managing UI code as the app grows.
React is of interest to us for several reasons, from being a useful templating mechanism, through providing a way of composing components that are relatively clean and modular in design, to its efficient DOM updates and declarative nature. These too are presumably the features that make it attractive to many of its users. But it is also of interest for this project precisely because its scope is tightly confined to just the rendering parts of the system and it doesn't try to lock the whole app into its own framework.
We just confirmed early that some of the hype really is only that, and that scaling up to the kind of UI that has more than a few hundred data points on screen at one time is not as easy as some of the advocacy might suggest, so we're exploring how we can use the tool to take advantage of what it clearly does offer and play to its strengths without paying too high a price. In that respect, it's really no different to any other library we'd evaluate.
React isn't magically fast, it's just O(n_size_of_diff). Getting it to perform well here requires you to figure out how to prune the size of the diff. You're doing it by splitting the app state into finer grained trees (each tree is pruning the branches it's not listening to) but any gains you're picking up from doing this could be done on the single root solution with appropriate shouldComponentUpdate methods.
The alternative (and easier) way of reducing the diff size is to simply window your data before passing it into the render, which can work for things like grids and lists but doesn't sound like your problem.
You also need to occasionally reshape your data to make the diff smaller. I work on a search app that thrashed for 1.2s every time new search results came in. The problem was that the results were invalidating a recursive menu component which was re-rendering 4 times per update. Avoiding the invalidation fixed the specific delay but the menu was still slow. Switching the menu's data organization from at tree of objects with `open` attrs to a static tree + open path and only rendering the visible nodes solved the perf problems in the app.
The most efficient way of doing that is by never doing an unnecessary diff in the first place.
Everything after that is a trade-off between convenience and overhead. Sometimes it will be worth incurring some overhead for greater convenience, sometimes it won't.
You're doing it by splitting the app state into finer grained trees (each tree is pruning the branches it's not listening to) but any gains you're picking up from doing this could be done on the single root solution with appropriate shouldComponentUpdate methods.
But shouldComponentUpdate is the wart that makes the whole abstraction leaky. The assumption that such tests have negligible cost is essential to the assumption that React used in the style you're advocating is efficient, but with non-trivial data models that won't necessarily be the case. Even if that assumption holds under any particular set of circumstances, you still now have two sources of truth, with more code to write and more scope for introducing errors.
At that point, it seems you're not necessarily any better off than you would be with a more traditional, event-driven architectural style, you're just making different trade-offs. For example, given a parent-child relationship between components, rendering top-down using React might avoid some of the need to co-ordinate updates if underlying data is appearing or disappearing. On the other hand, it might also mean writing a bunch of boilerplate shouldComponentUpdate functions just to keep rendering efficient.
> At that point, it seems you're not necessarily any better off than you would be with a more traditional, event-driven architectural style
Not having to implement the dom state updates and rAF update batching are fairly big wins. The only other way I know of doing this is via data binding and the difference there is that individual updates are more efficient but establishing the bindings is usually O(n_size_of_input) and in practice every app I've worked on that did data binding eventually broke the approach. Having the shouldComponentUpdate escape hatch has allowed me to push much larger volumes of data through the system without it breaking.
I've implemented a number of reasonably complex apps but the two largest have been on immutable datastructures where React's assumptions always hold unless you screw something up. The largest non-immutable app I've written was a 25k LoC layout builder where the two shouldComponentUpdates for vdom pruning were pretty straightforward.
Good luck with your app.
I agree, and these are two of the main reason we're interested in React. We've had in-house code doing some similar things for a long time, more the batching than rAF in our case, but for much the same reasons. Now that mainstream libraries are offering similar tools and with reasonable trade-offs in terms of functionality offered versus level of dependency, there's not much reason to maintain our own in the next generation of the UI.
In case you're interested, our model code also presents its interface in the form of immutable data structures, with quite fine-grained events available to monitor quite specific parts. But even with that, in something like our small multiples test case, where we're plotting a number of interactive SVG charts using modestly derived data but thousands of underlying data points, it appears that the indirections and tests do still add up to a noticeable level of overhead. Of course that's more demanding than what a lot of web apps will ever need, and our conclusions for our project won't necessarily be anyone else's conclusions for theirs.
If you use something like Immutable.js you can avoid a lot (most? all?) of the diffing since comparing trees of objects becomes a simple object reference comparison. This is common in the Om world, and becoming more popular in JS too.
However, what is true is that:
1. Both JS and the DOM are fast enough that you can lay out most reasonable pages within the threshold of visual perception (~150-200ms).
2. If you need to layout the page during an animation frame, you're hosed anyway. You have a frame budget of 16.67ms and layout will take 100ms+ on mobile.
And so the practical, qualitative conclusion is the same: you can use React for your basic page transformations & rendering, but you need to use CSS transforms and offload the work to the GPU if you want to have a prayer of making animations work on mobile. And React also protects you from completely fucking up performance (which is trivially easy with JQuery), so many real-world pages from inexperienced webdevs experience a very significant speedup.
I'm not the least bit surprised they discarded bindings in iOS, then (effectively) discarded KVO in Swift.
Next time, try playing with creating an observer protocol:
@protocol MyThingObserver
@optional
- (void)myThing:(MyThing *)thing didChangeIntegerValue:(NSUInteger)newValue;
- (void)numberOfItemsDidChangeInThing:(MyThing *)thing;
@end
Then you have strong typing, compile-time validation, additional context, and plays much nicer with the land of tomorrow (Swift).Coupled with immutable value types, you're going to make your own life way easier, I promise.
As I mentioned in my previous post, I felt KVO was a really interesting design path to be sure, but I agree with you it had its fair share of issues. That being said, observer patterns are just incremental improvements on this. Conceptually its still the same: figuring out ways of getting different parts of a system to eventually "settle" on the same value. Sure, strict typing and whatnot can make this safer, but its ultimately a lot of code that has no real connection to the actual problem your program is trying to solve.
Personally I find immutable everything types (through persistent data structures) combined with immediate mode programming for rendering has more or less taken away the cognitive overhead I used to have around this set of "problems". You have a model, that can lead to a new model in the future. You then have a render function that is a function of this model. Its completely deterministic, there is only one path things can go down, you can grab the state of your program at any time and keep re-rendering it to figure out whats going on -- there's no "implicit" conglomeration of state through the value of all the free variables in your program at any particular point in time and their knowledge of each other.
If I were to return to iOS programming today, I'd probably give React Native a stab. To me tomorrow land is being able to change code and see it live update without recompile or provisioning profiles or what have you.
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?
It may well be that Object.observer was the wrong level of granularity, and I agree that language specs should evolve cautiously.
That said, I think it is far too early to make generalisations as broad your statement above. React-style development is still a tiny fraction of overall web development, and React itself isn't even at version 1.0 yet and IMHO still has significant fundamental architectural concerns that haven't been fully explored yet. It promotes some interesting ideas, and I'm glad someone with the profile of Facebook is challenging assumptions, but in turn reasonable people could challenge a lot of the ideas coming out of the React team and we're still far from understanding the long-term trade-offs of that style of building for the web.
see my comment here https://github.com/sebmarkbage/ecmascript-immutable-data-str...
The platforms that do not shy away from data and embrace mutability at all times -- I believe those are called databases, and when necessarily distributed for scale, that mutability is the enemy of consistency.
Any misunderstandings within this system are a misunderstanding regarding DOM events. HTMLIFrameElements don't change on their own. If they change based on the window resizing, you listen to the "resize" event, etc.
It actually does now that Promise got added to the language. For extra fun the interaction of the JavaScript event loop and the HTML/DOM event loop is not actually specified and getting it specified will be rather exciting: both sides want to "own" the event loop...
Animations, though, do need to be worked into this somehow. And they need to be able to run on a completely independent event loop to some extent so you can do animations on the compositor thread, not the DOM thread.
I guess the only issue is that what happens if you have unresolved promises when your Job queues go empty is implementation-defined, because an implementation is allowed to terminate when there are no Jobs. But if it doesn't terminate, resolving the Promise will enqueue a PromiseReactionJob. And I guess not actually call NextJob; that part is up to the implementation.
https://developer.mozilla.org/en/docs/Web/JavaScript/Referen...
What's the state of Proxy? Are the people behind it still working on it? Are we going to be able to use it? Will it be fast?
see https://github.com/bcherny/auditable for an implementation, sans O.o.
IMHO, it would be much more useful if it were synchronous.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
So before proxies, we used to do it as:
await server.rpc.run("methodName", [argumentOne, argumentTwo])
Now, we just do: await server.rpc.methodName(one, two)
The proxy traps that method call and returns a promise that runs that on the server. It's very slick and has made our code a lot more readable.For a while now I wanted to use proxies, but they are only supported in Firefox, and no timeline when they get to Chrome. I'll have to see what I can use as replacement.
I think this is a major blow to web performance. We are bound to perform O(every object on page) computations on every event, instead of O(tiny bit that's actually changed). This is how having a device with gigs of RAM, close to ghz CPU we still measure FPS of a page with one table and 5 images
I hope nobody uses that stuff in production ... even if there are polyfills, their performances are nowhere near the performances of the native implementation.
> After much discussion with the parties involved
Would be interesting to know the content of these discussion.
> Polymer rewrote from the ground up for its 1.0 release, and in that rebuilding did not utilize O.o
Any seasoned developer would see the ignorance in implementing such a feature.
Invariably a system acquires enough stuff going on that you get "observer storms" of feedback, or people find really weird couplings that are not immediately obvious because something several modules away is observing something it doesn't need to.
It sounds like the legacy code you got stuck maintaining put logic inside the observers, which isn't the Observer Pattern's fault. In my own experience, not using event patterns like the Observer Pattern is probably the biggest cause of difficult-to-maintain code.
Moving from raw native ui frameworks very similar to todays js UI libs to HTML pages was a big paradigm shift, but was IMO better for most standard apps.
I've been thinking for a long time that it would be a good idea to build a client-side library where each state has an URL. Essentially just take the classic backend view rendering technique and move it to the client. Could work out really well I think. Event-driven techniques can then be reserved for really interactive apps like games.