The LEAST powerful frontend architecture
speaker: Vu Viet Tung
Agenda
-
Introduction
-
When and When not to use it
-
Building blocks
-
Dynamic view
-
Reactivity
live at: least-power.tungv.com
about me
-
Product Engineer at BodiData Inc.
-
playing with JavaScript in browser since 2005
-
working with JavaScript fullstack since 2012
-
FE: sammy, knockout, angular, react (thanks god! no backbone)
-
BE: express, meteor, redis, mongo, neo4j, amqp
-
Love to build web-scale apps and services
@tung__vu - github.com/tungv
Principle of Least Power
Axioms of Web Architecture
by
Tim Berners-Lee
Given a choice of solutions, pick the least powerful solution capable of solving your problem
Q&A Links
goo.gl/JIvO2i
Functional Frontend Architecture 101
Core Concepts
Pure functions
&
Unidirectional
data flow
Pure function
A pure function is a function that:
given a same set of params, always returns a same value
has no side effect
requires absolute immutability
Functional Frontend Architecture 101

Components Tree

Non-unidirectional
images from css-tricks.com
Functional Frontend Architecture 101
Unidirectional data flow
images from css-tricks.com
Functional Frontend Architecture 101
Functional Frontend Architecture 101
Free-style mutation + asynchronicity = DOOMED!
Is it for you?
Benefits
Predictability
Reproducibility
Modularity
Testability
Is it for you?
Trade-offs
In FFA, you must describe:
-
application state as plain objects and arrays.
-
changes in the system as plain objects.
-
the logic for handling changes as pure functions.
-
components in a declarative way
Is it for you?
Declarative
-
Tell computer what the result should look like
-
You write expressions
-
Functions are mathematical functions
Imperative
-
Tell computer how to achieve a result in steps
-
You write statements
-
Functions are coroutines
Is it for you?
Principle of
*power* and *principle*
or PPAP
FFA
in details
Building blocks
1) a pure view function
2) a pure state function
building blocks
View Function V
view = V(state)
building blocks
State Function R
nextState = R(previousState, nextAction)
building blocks
State Function R
currentState = actions.reduce(R, initialState)
building blocks
State + View
currentView = V(actions.reduce(R, initialState))
building blocks
State + View
consumableState = S(rawState)
In real-life apps, view needs data to be transformed before being consumed. We need another pure function `S` that:
currentView = V(S(actions.reduce(R, initialState)))
building blocks
Dynamic View
When to change the view
Changes arrive in many forms:
- user interactions with the view
- data arrival from server
- timer ticks
But in general they are events.
dynamic view
How to change the view
In the formula we formed before:
- V, S and R are pure functions
-
initialState is a constant object that never changes
The only way to change view, is to update the "array of actions" when an event occurs
currentView = V(S(actions.reduce(R, initialState)))
dynamic view
Pushing event to "array of actions"
while (true) {
event = waitForEvent();
actions.push(event);
}
dynamic view
Changes as plain objects
action = A(event)
while (true) {
event = waitForEvent();
actions.push(A(event));
}
dynamic view
The ultimate formula
the ultimate formula

the ultimate formula
Bonus: Reactivity
Reactive Functional Programming
- Reactive Function Programming = Extension of FP
- RFP solves absolute immutability problems
- Everything is a stream
- Stream is just a representation of value over time
- You might be using it without even knowing so
bonus: reactivity

Actions as a stream
while (true) {
action = actionCreator(waitForEvent())
actions$.next(action)
}
bonus: reactivity
View as a subscription
actions$
.scan(reducer)
.startWith(initialState)
.map(selector)
.map(view)
.subscribe(setHTML)
bonus: reactivity
The ultimate reactive formula
events$
.map(actionCreator)
.scan(reducer)
.startWith(initialState)
.map(selector)
.map(view)
.subscribe(setHTML)
while (true) {
events$.next(waitForEvent());
}
bonus: reactivity
Thank you
❤️🙏❤️
Q&A
Functional Front-end architecture
By tungv
Functional Front-end architecture
An attempt to explain the ideas behind React and Redux. A slide for barcamp Saigon 2016
- 1,257