flux with immutable data
It would be awesome to have a kind of reference implementation of flux utilizing immutable data and cursors with immutable.js or something similar... (immutablity helpers + cursors...)
There are quite a few implementations but they either differ or re-implement/change the original ideas of flux...
I have a bit of a hard time to understand, if all the state (even a components transient UI state) should be treated as application state (in a store) when using immutables... in the standard flux implementation with mutable data, we have UI state within react components and application state in stores... In my view with immutable data, everything (even a components UI state) should be handled in stores and are combined to a single big application state object given to a single controller-view component at the top.
It would be great to see flux and immutable objects in action...
https://github.com/optimizely/nuclear-js is a flux implementation built on top of ImmutableJS and utilitizes a singular immutable app state map.
On Friday, January 23, 2015, tindli [email protected] wrote:
It would be awesome to have a kind of reference implementation of flux utilizing immutable data and cursors with immutable.js or something similar... (immutablity helpers + cursors...)
There are quite a few implementations but they either differ or re-implement/change the original ideas of flux...
I have a bit of a hard time to understand, if all the state (even a components transient UI state) should be treated as application state (in a store) when using immutables... in the standard flux implementation with mutable data, we have UI state within react components and application state in stores... In my view with immutable data, everything (even a components UI state) should be handled in stores and are combined to a single big application state object given to a single controller-view component at the top.
It would be great to see flux and immutable objects in action...
— Reply to this email directly or view it on GitHub https://github.com/facebook/flux/issues/134.
@jordangarcia thanks - I have already found nuclear.js as a possible implementation of flux with immutables; sadly, the examples are a bit out of date so they are a bit hard to follow ;)
I also agree that this would be helpful. Is this something you might be interested in a pull request for @fisherwebdev?
@fisherwebdev @RileyTomasek @jordangarcia I am open for all ideas/suggestions regarding proper flux + immutable data...
For me, the main topics of concern are:
- Generic immutable library and cursors? Aside from immutable.js (which is great but pretty complex) there are react's immutablity helpers. If the immutablility helpers are used, a proper cursor implementation is required - but thats a bit a problem. There are quite a few of there but they are often very opinionated about calling setState() directly together with updating the cursor... I think that manipulating state outside stores is a anti-pattern when using flux. In order to maintain the proper flow, one should be able to manipulate cursors and when finished emit the change to controller views (it is flux after all... isn't it)...
- Proper use of actions? When cursors are used throughout the component hierarchy, it makes sense to use them within stores as well - so instead of passing an ID of a deeply nested dataset (and searching it within the store), why not passing the cursor in the action payload directly? The store could then use this cursor to directly update itself at the right position... when done, emit the change event. Sometimes, searching stuff in stores is pretty costly thats why I would prefer passing cursors directly into stores (via actions of course). I don't know if this violates flux principles... but it would definitely help.
- Is local setState() a thing of the past when using immutable data structures? When immutable data is used, one can limit the shouldComponentUpdate() checks to props as there would not be any local component state within components (this would be perfect when speaking of stateless components)... Now that seems to be fine most of the time but how can you do an efficient implementation of something like controlled components? Firing an action on every change, alter the store (with a given cursor), emit the change and run through each components shouldComponentUpdate() checks? This approach seems then pretty costly for something that simple... Of course one can call setState() in a component but since shouldComponentUpdate() only looks at props, there is a chance that the UI gets out of sync.... (ignoring all local state changes)... So having transient UI state that would normally require local state is pretty hard when having only application state.
- Immutable data on the server side? When flux is used with immutables, we should really keep an eye on server side rendering since I don't know if it is a good idea to use common immutable libraries on long running servers... but this might just be a bad feeling... (GC cleanup on unused immutables) If mutable and immutables objects are used in an isomorphic environment, one might implement a wrapper around immutables to allow the same code to be performed server and client side - with mutable and immutable data... Yahoo's excellent isomorphic flux libs are awesome and I want to think about flux + immutables in a context like that too.
So these are my basic thoughts about the topic... After all, utilizing immutables would definitely help to build more robust apps as data cannot be changed accidentally...
@tindli to answer your questions:
1 & 2: I'm definitely interested in cursors, but I am not sure if a cursor-based example should be the next example app. Just ImmutableJS should be good enough for our next step. Not to say we can't start thinking about a cursor example -- I'd just prefer to focus on the immediate next example right now. I do like the ideas you are putting forth about cursors in actions, etc. But to be honest here, I have not done it yet. We'll be teaching each other on that.
3:
A local setState() is definitely not a thing of the past when using immutable data. Controlled input components in particular, as you noted, often have a bit of local component state. It is not a bad practice.
There are a couple of warning signs that imply that some local state should get moved to a store:
- the data in this.state gets passed to a child
- the data in this.state needs to be known by another part of the application
- the logic surrounding the management of this.state becomes more than trivial.
The checks on these values are typically quite minimal. How many values would we be checking? And PureRenderMixin makes it quite easy, by using shallowEqual():
https://github.com/facebook/react/blob/master/src/addons/ReactComponentWithPureRenderMixin.js
https://github.com/facebook/react/blob/master/src/utils/shallowEqual.js
4: My understanding is that the GC for ImmutableJS is quite good and was definitely on the mind of @leebyron while he was developing it. Also, memory usage is low due to "structural sharing" of data between versions of the same data structure. See Lee's excellent React.js Conf talk: https://www.youtube.com/watch?v=I7IdS-PbEgI
I can't comment too much on server-side Flux other than to say there are definitely different ways to approach it. You can go the route of Yahoo! and do full blown Flux on the server, or you can do something more like SoundCloud and use Flux as a rendering middleware. I'm sure there are other approaches as well.
Building an immutable example:
I think we could try to build a small example together. I am open to suggestions on what that application should be, or what the process of collaboration should be. But I do think we should keep it relatively small and simple -- it is an example, after all, and as such it should be readable, understandable, and free of distractions. I believe @kevinold was interested in contributing too.
I'd suggest that "immutable" be used in the example name. Like flux-chat or flux-todomvc, something like flux-immutable or flux-immutable-[whatever] should be the name. Also, I'd ask that any prefix on the files to identify them as belonging to the same app should be ~7 characters or less (e.g. the use of "Chat" here: https://github.com/facebook/flux/tree/master/examples/flux-chat/js/utils).
I was looking for such examples, but also failed to get a satisfying one.
Once I thought I got one but told that it's being switched to cursor... https://github.com/olslash/cmdv.io/issues/1
Hi @fisherwebdev,
Yes, I'd love to contribute. As I mentioned, I would love to see a complete client to server example. I would be ok extending the flux-chat or flux-todomvc to add immutable data and a simple hapi or express server to return static json.
If we'd like to disconnect from the chat or todomvc apps, I would suggest:
- Invoice
- Recipe Book (or Card)
- Team Roster
The common theme among these would be that they have an Entity with a bit of complexity (as much as we'd like to add) and are typically managed in a List of some sort.
What if you just did the chat or todo example using immutable data? That would make it relatively easy for people to compare the pros/cons of each approach. It is also a very small and focused step that could be released quickly.
@fisherwebdev Thanks for your detailed answers and hints! Really helpful. It is nice to brainstorm about examples that make use of immutables.
@kevinold Regarding the complete client/server example: As @fisherwebdev already said, Yahoo is doing a super awesome job with demonstrating how flux can be used client + server side. Their fluxible apps combines a lot of useful stuff! I don't know if we should overload an example with client/server side implementation - there has to be done quite a bit of work - e.g. transfer data from server stores to client stores - all this has already been solved very elegantly by fluxible.
To keep it as simple as possible/and focused to immutables, I would prefer something basic like one of the things @kevinold mentioned or something like a digital stopwatch. There could be a way to log the current time and put it in a list below the running watch...
I guess we would need a self managing store that would hold the time and emits change events pretty often to update the time. Due to reference equality (thanks to immutables) this would be pretty efficient (although updates happen every 1/10 of a second or something like that). For the log of captured timestamps below the watch, a undo feature could be used to undo/redo logged timestamps - one of the things immutables make easy.
An example could use immutable.js or the simpler immutable helpers from react addons... As the project should be something tiny, even the react update helpers would do a nice job. Structural sharing and all the other awesome benefits immutable.js brings are definitely nice though...
@RileyTomasek Yeah why not :+1: @leebyron shows immutable todos in his talk...
About cursors: I think they would really help to have a nice way to update stores... but I am happy to ignore them for now. They add another layer of complexity; not really required for something simple.
I am a bit busy currently but if something nice comes up, I am definitely looking forward to contribute.
I will try and find time over the next week to do an immutable version of the todo example.
Even if the difference isn't worth adding another example, it will hopefully be a good starting point for which parts of immutability should and shouldn't be focused on in the Flux specific examples.
Cursors are very interesting. But two questions arise with them. Do we need flux stores at all if we have common big application immutable state and transfer parts of it (cursors) to children components as props? And what if one day some grandchild component wants additional data from another part of application state - change its parent and grandparent to nip off this data and transfer it down to that grandchild component?
@klimashkin Depending on your needs, you may or may not need flux if you are using global immutable state and passing things via cursors. You might still want to use flux or events so that the global app state is mutated in the proper way before the cursor triggers a render, but you might not need as many listeners on the render side (just passing cursors to props and use efficient shouldComponentUpdate).
As for the grandchildren needing additional data, you may want to simply add the data and any additional props or for certain use cases you might want to use a reference cursor. Reference cursors are similar to lenses or zippers in that they provide access deep into a structure. In some cases you may want to use reference cursors directly or in others maybe passing down through ancestors is more appropriate.
For more info on reference cursors see:
- https://github.com/omniscientjs/immstruct#references - reference cursors in immstruct
- https://github.com/omniscientjs/omniscient-immstruct-reference repo demonstrating use of a reference cursor
- https://github.com/omcljs/om/wiki/Advanced-Tutorial#reference-cursors - original idea used in Om
- http://onbeyondlambda.blogspot.com/2014/10/trying-out-reference-cursors.html
- http://slides.com/thebearmayor/reference-cursors-in-om#/
Thank you for reporting this issue and appreciate your patience. We've notified the core team for an update on this issue. We're looking for a response within the next 30 days or the issue may be closed.
http://facebook.github.io/flux/docs/flux-utils.html#content is built with immutability in mind, and here is an example using it: https://github.com/staltz/flux-challenge/tree/master/submissions/kyldvs