"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
High Performance web apps in Om, React and ClojureScript
1. High Performance Web UI's with
Om and React
LambdaJam - Brisbane, 2014
Leonardo Borges
@leonardo_borges
www.leonardoborges.com
www.thoughtworks.com
2. About
‣ ThoughtWorker
‣ Functional Programming & Clojure
advocate
‣ Founder of the Sydney Clojure User
Group
‣ Currently writing “Clojure Reactive
Programming”
9. What we’ll see
‣ An Overview of React and how it enables fast rendering
‣ Meet Om, a ClojureScript interface to React
‣ Boosting React’s rendering performance with immutable data structures
‣ A simple demo featuring “data binding” and undo functionality
10. React
‣ Created by Facebook for building user interfaces
‣ The V in MVC
‣ Self-contained components
‣ Doesn’t make assumptions about your stack - can be used with anything
11. Self-contained components
‣ React combines display logic and DOM generation
‣ Components are themselves loosely coupled
‣ The whole app is re-rendered on every update
‣ Virtual DOM
‣ Efficient diff algorithm
12. Efficient diff algorithm
‣ Creates a virtual version of the DOM
‣ As the application state changes new DOM trees are generated
‣ React diffs the trees and computes the minimal set of changes
‣ Finally it applies the changes to the real DOM
20. IShouldUpdate
‣ Called on app state changes but before rendering
‣ This is where React uses its fast diff algorithm
‣ Om components implement the fastest algorithm possible: a simple
reference equality check
‣ Generally, you won’t have to implement this
21. IInitState & IRenderState
‣ Initialise component local state using IInitState
‣ Use IRenderState to work with it and render the component
31. The Sessions view
Same deal as before
(defn sessions-view [app owner]!
(reify!
om/IRender!
(render [_]!
(dom/div #js {:id "sessions"}!
(dom/h2 nil "Sessions")!
(apply dom/ul nil!
(map #(om/build editable %) (vals (:sessions
app))))))))!
32. Apps can have multiple roots
(om/root speakers-view app-state!
{:target (. js/document (getElementById "speakers"))})!
!
(om/root sessions-view app-state!
{:target (. js/document (getElementById "sessions"))})!
You can have multiple “mini-apps” inside your main app
Makes it easy to try Om in a specific section/feature
40. Summary
‣ With Om, you’re not using a crippled template language, you can
leverage all of Clojurescript (including other DOM libraries)
‣ Rendering and display logic are inevitably coupled. Om/React
acknowledges that a bundles them in components
‣ The whole app is re-rendered on every state change, making it easier to
reason about
‣ This is efficient thanks to immutable data structures
41. Summary
‣ Clojurescript also provides a better development experience with a
powerful browser REPL much like what you’d get with Clojure on the JVM
‣ Source maps are here today
‣ Bottom line is that Clojurescript is a serious contender