|
|
|
|
|
by findjashua
4054 days ago
|
|
So Reagent is like React + Baobab (https://github.com/Yomguithereal/baobab)? Since Clojurescript has to compile to Javascript anyway, I can't think of a reason why Javascript can't have a counterpart to Reagent. I'm not familiar with Clojure, but it seems like atom is the data tree that holds the state of the entire application. If so, could you give an example use case of when you'd pass the data tree between threads, and what advantages it has over promises or es7 async/await. Just to be sure, I'm not questioning the validity of your claims, I'm genuinely curious about how things work in reagent. |
|
An example of passing data between threads: imagine a forum that allowed indefinitely nested comments. It also loads comments lazily; one must click to load a comment's child comments, in which case they'll be loaded from the backend. However, if the child comments are minimised again, then expanded again, another backend request won't be made, as the data is already loaded.
I approached this by having a separate 'thread' running that just listened for messages (maps) on a channel. If it was a :get-children message, this thread would check the local cache for the parent comment's ID, and if it found it then it'd add the data to the atom provided in the request, otherwise it would fetch the data, add it to the cache, and then add it to the provided atom.
The comment component would then take a copy of that channel, which it'd pass to its children, and to get comment children data it'd just pass a :get-children message into the channel with an empty atom for the other thread to fill. If a button was clicked that changed a comment somehow, then it could send a :update message through the channel with the parent id, and the other thread would re-fetch the relevant data from the backend.
Of course, there's nothing here that couldn't be done with promises/async. I just personally find Go/Erlang style message passing between threads to be simpler and easier to reason about than async/await, and I believe Reagant/Clojurescript would appeal to others who feel similarly.