$ whoami
render = (Data) => UI;
update = (State, Action) => NewState;
Actions = [Update This, Add That, Delete Shit, ...];
AppState = Actions.reduce(update, InitialState);
AppState.subscribe(render);
The only thing that didn't change!
const Store = Redux.createStore(...);
Store.dispatch(action);
Store.subscribe(() => ...);
const Dispatcher = new Flux.Dispatcher();
Dispatcher.dispatch(action);
Dispatcher.register(() => ...);
Horizontal surface area — the number of components in your application layer, component layer, layout layer, rendering layer.
Vertical surface area — the number of layers for a feature.
Verticals are fixed. Horizontal grows with app complexity.
Always remains the same.
npm i --save flux
@2.1.1
import { Dispatcher } from 'flux';
import { Container, ReduceStore } from 'flux/utils';
import { Dispatcher } from 'flux';
export default new Dispatcher();
npm i --save immutable
const Shape = { firstName: 'John', lastName: 'Doe',
age: 0, isActive: true };
class UserRecord extends Record(Shape) {
get fullName() {
return `${this.firstName} ${this.lastName}`;
}
}
class Users {
async create(data) { ... }
async retrieve(id) { ... }
async update(id, data) { ... }
async destroy(id) { ... }
}
All methods return immutable record defined before.
class Users {
async retrieve(id) {
try {
const response = await get(`/users/${id}`);
const user = await response.json();
return new UserRecord(user);
} catch (error) {
throw new AccessError('Oops');
}
}
}
class UserListStore extends ReduceStore {
getInitialState() {
return new Map();
}
reduce(users, action) {
switch (action.type) {
case USER_RETRIEVED:
return users.set(action.id, action.user);
default:
return users;
}
}
}
export default new UserListStore(Dispatcher);
class UserListStore extends ReduceStore {
// ...
getOnlineUsersCount() {
return this.getState().count(u => u.isOnline);
}
// ...
}
async function retrieveUser(id) {
try {
const user = await users.retrieve(id);
Dispatcher.dispatch({ type: USER_RETRIEVED,
id, user });
} catch (error) {
Dispatcher.dispatch({ type: USER_RETRIEVE_FAILED,
error });
}
}
class UserListContainer extends React.Component {
static getStores() {
return [UserListStore];
}
static calculateState() {
const users = UserListStore.getUsers();
const onlineCount = UserListStore.getOnlineUsersCount();
return { users, onlineCount };
}
render() {
return <UserList users={this.state.users}
onlineCount={this.state.onlineCount} />;
}
}
export default Container.create(UserListContainer);
function getStores() {
return [UserListStore];
}
function calculateState() {
const users = UserListStore.getUsers();
const onlineCount = UserListStore.getOnlineUsersCount();
return { users, onlineCount };
}
export default Container.create(UserList,
getStores,
calculateState);
@3.0.0
Link to these slides — alexeyraspopov.github.io/long-live-flux/