After the appearance of React in the browser landscape the community built numerous libraries like Om, Om Next, Reagent (used with or without Re-frame), Quiescent, Reacl and perhaps even more to make Reacts blessings available in ClojureScript. Some libraries clearly express their preferences about how a more complete frontend architecture should look like, and encourage us to add core.async, co-located queries, reactive programming or cursors to the mix.
Based on an understanding of the general problems to be solved within a single-page-application this talk will provide some design heuristics on how to make the right choices that add just enough complexity to your frontend.
9. App state: Update state after events
Update state
directly?
HTML inputApp state
Or send an
'update' event?
Component-external code
10. Where to start
●
Keep frontend state in single atom
●
Keep components stupid
●
Move presentation logic out of your components
●
Provide a communication channel to components
●
Avoid component assumptions about your state
●
Presentation logic is a set of functions
[state event] stateorchannel→
11. The Big LoopTM
App state
Component
hierarchy
DOMVDOM
Presentation
logic
Remote backend
Events
Dispatcher
Channel
12. The Om/Next (Relay+Falcor inspired) approach
Q
Q
Q
Q
Query Tree
Q
Backend
Tree-ish data
Q
Q
Q
Q
Q
Components +
co-located queries
13. Do I need the Om/Next approach?
Unpredictable data needs?
Provide an endpoint
accepting GraphQL-style
queries.
Co-locate your queries
with your components.
yesno
Having a generic query
endpoint might even then
be beneficial.
Top-level components decide
upon queries and push
state to child components.
16. Do I need dataflow and/or cursors?
Structural mismatches?
Transformations?
Add dataflow.
Keep it simple,
use cursors.
Need reusable
components on
derived state?
Make sure update logic
knows locations in root atom.
no
yes
It's not hard to make
components reusable.
17. How to synchronize app state with your backend?
Remote backend App state
Asynchronous
communication
18. Synchronization of state with your backend
Handle asynchronity with your event channel
App state
Presentation
logic
Remote backend Dispatcher
Channel
Other Events
Async response
27. Synchronization: Concurrency Control
Version numbers
+ conflicts unlikely
+ loosing work is acceptable
+ cheap
- affects your data
Locking
+ safety-first approach
- lock management
- dead client detection
Optimistic or pessimistic
concurrency control?
It really depends on your domain!
29. Wrap up
Optimistic or pessimistic
concurrency control?
How (un)predictable are your data needs?
Do your components require expensive data transformations?
Examine decisions that your favorite
React-wrapping lib has made on behalf of you!
30. Thank you for listening!
Questions?
@friemens www.doctronic.de