r/Clojure • u/thheller • Feb 20 '21
shadow-arborist: Braindump
Lately I find myself trying to think about too many topics at once and my productivity has tanked in the process. I do think these topics are worth thinking about but I can't do them all at once on my own.
The first 2 are about component reusability and async rendering and I might make a few more in the upcoming weeks. I didn't want to turn this into a whole blog series and mostly wrote this down for myself. I'm only posting them here because people previously have expressed interest in my work on shadow-arborist and shadow-grove and might be interested in this. Currently that work lives in the shadow-experiments repo.
For those that missed it: shadow-arborist is my attempt at writing a react alternative in pure CLJS with no other dependencies. shadow-grove is the higher level "component library" on top. It would be fair to compare them to reagent being built on top of react. Sorry about the names, everything is somewhat related to trees.
https://github.com/thheller/shadow-experiments/blob/master/doc/reusable-components.md
https://github.com/thheller/shadow-experiments/blob/master/doc/async.md
In those posts I just mostly talked about the stuff that already exists and decisions I made in the process. Writing about the actual unformed ideas that I haven't settled on is difficult. I hope to get everything out of my head at some point so I can get back to focusing on specific issues that actually need solving.
Edit: Wrote another post.
12
1
u/den1k Feb 21 '21
u/thheller how does the << macro compare to https://github.com/lynaghk/hicada ?
1
u/thheller Feb 21 '21
No.
hicadacompiles toreactelements and attempts to compile regular CLJS forms such aslet,for, etc.The
shadow-arboristfragment macro does not attempt to do that at all. It stops "compiling" whenever it runs into anything not a static hiccup form. Thats why you need nested<<calls. I opted to have it always work that way instead of just "sometimes" since there is too much ambiguity otherwise.
1
u/lonelyfroginventor Feb 21 '21
What's your opinions on crankjs's approach for async rendering and on memoized dom as alternative to virtual dom?
1
u/thheller Feb 21 '21
I have not looked at crankjs in detail. From the docs it looks like regular old VDOM style just with some new generator magic for components.
1
u/dustingetz Feb 22 '21
For others: https://crank.js.org/guides/getting-started
it appears to be using async yield with JSX to model a dataflow like rendering construct; its pretty interesting but why do they need both yield and JSX? I guess it's yielding new view values in response to state changes, and then the JSX handles optimized rendering, I wonder if yield can do both
1
u/jackdbd Feb 25 '21
Did you have a look at Marko.js?I don't know why it's virtually unknown in the JS ecosystem, but as far as I know it's the only "framework" that supports async rendering with out-of-order flushing. I wrote framework in quotation marks because Marko, like Svelte, is really a language with its own compiler.
To be precise, Marko.js performs async rendering and HTML streaming only when it renders on a Node.js server, I guess because parsing streams client-side would be too much work for the browser. I think that for now they are still using virtual DOM diffing when rendering client-side, but this is going to change soon. Apparently they want to introduce some reactive primitives whose dependencies are known at compile time. And they are writing their own runtime to manage them. As far as I understood, this will allow them to do fine-grained updates and sub-component hydration.
See here for more details: https://medium.com/swlh/fluurt-re-inventing-marko-db265f07cc45 (Ryan Carniato is the author of Solid.js).
I have tried a lot of UI frameworks last year, but it's only when I read this article about Marko that I heard about progressive HTML rendering (even if it's a feature browsers have been supporting for a long time).
In Node.js you can stream HTML to the browser. And streaming is a pre-requisite for progressive HTML rendering. The Node.js server flushes out a HTML page in multiple chunks and sends them asynchronously to the client, so the browser can start painting the chunks as soon as they arrive.
A few articles/talks I found interesting about progressive HTML rendering:
2
u/thheller Feb 25 '21
I have not looked at marko much. Most of its "magic" seems to be happening in some bastardized mix of HTML+JS which is not something I'm interested in.
I will personally also not run any CLJS/JS code server-side anytime soon, so this entire "isomorphic" area doesn't interest me. I do have some ideas how to combine server-rendering in CLJ with all of this but nothing written on that front.
1
u/rufusthedogwoof Feb 20 '21
Thank you for your contributions.
Also, I loved your elements of clojure book. For me the first chapter was one of the best & brain-bending chapters I’ve come across in recent memory on programming and systems building.
4
u/thheller Feb 20 '21
I have never written or contributed to any books so you must have me confused with someone else. :)
3
u/pihkal Feb 20 '21
Elements of Clojure is by Zach Tellman, creator of Aleph and Manifold.
4
u/rufusthedogwoof Feb 20 '21
Yup, sorry for the mistake and thanks for the correction.
1
-5
Feb 20 '21
[removed] — view removed comment
4
u/pihkal Feb 20 '21
bad bot
1
u/B0tRank Feb 20 '21
Thank you, pihkal, for voting on Adventurous_Fortune8.
This bot wants to find the best and worst bots on Reddit. You can view results here.
Even if I don't reply to your comment, I'm still listening for votes. Check the webpage to see if your vote registered!
-4
2
u/slifin Feb 23 '21
This might be of interest:
https://reactjs.org/blog/2020/12/21/data-fetching-with-react-server-components.html
https://dev.to/nilanth/an-introduction-to-react-server-components-fk4
I'm hoping there will be a CLJS framework for this but at the moment some of the work requires changes to JS bundlers, personally, I'd prefer to be using shadow-cljs as my Webpack equivalent for this feature
0
Feb 20 '21
[deleted]
4
u/thheller Feb 20 '21
I have seen almost everything the
reactteam published and lots of that definitely informed a lot of decisions in my work. You do however have to consider that a lot of what they are doing only matters at facebook-scale and is maybe overkill for regular sized sites. I'm also doing this alone so far so I have to cut some corners. ;)solid seems to employ the nowadays common trick of
cloneNode(true)to perform a deep clone of the HTML "template" element. This will often be faster since it is one call going directly into native code. Currentlyshadow-arboristinstead creates all elements viacreateElementand then setting attributes and so on. I have not experimented withcloneNodeyet since earlier versions ofshadow-arboristwouldn't have supported it. Nowadays it could and I might try it at some point. I haven't looked at the rest of the implementation but I expect it to be somewhat similar. It'll however lack the extensibility because it isn't built on protocols. This does however make things slower overall so in the end doing the protocols may not be worth it.I might try to integrate into this benchmark suite since I'm curious too. Things will never beat the absolute fastest JS implementation since we are still using CLJS datastructures for most stuff and they do carry overhead.
2
u/thheller Feb 21 '21 edited Feb 22 '21
I got curious and implemented the benchmark using the whole "framework". This involves a normalized DB which is queried by EQL. Results are actually better than expected but still nowhere near competitive.
I'll see about adding a lower level version that doesn't use the DB/EQL stuff. The benchmarks that don't touch the DB much look promising.
EDIT: accidentally ran the benchmarks with wrong numbers. 10,000 instead of 1,000. numbers look more reasonable now but still bad. see Screenshot in link above.
4
u/lilactown Feb 22 '21
I've been thinking about these same questions for a long time too, /u/thheller - great to read your thoughts. I too am wondering about what comes after React. I'd like to try and do a brain dump too and then compare them with you :)
One thing at top of mind - w.r.t. scheduling, data loading, state, etc. there is a great amount of importance placed on the "component"; I wonder what you think of building out a graph of data similar to reagent's ratom/reaction and implementing scheduling in that graph of computations. Could we get away with allowing "components" be dumb things which subscribe to leaves of this graph and return or mutate DOM elements? Would that simplify some of our concerns?
I think what this leaves on the table are local UI state (e.g. text input, checkbox state) but it makes the "view" layer potentially easier to reason about.
FWIW I'm pretty bought into the component model but I've been thinking about experimenting more in this area. Some benefits I see are opening up other use cases, e.g. splitting computations between server and client w/o the need to serialize the DOM, and being useful for other applications of incremental computation.