Flux

I’m obsessed by making my code simpler. I didn’t say smaller because having less code doesn’t mean that is simple and easy to work with. I believe that big part of the problems in the software industry come from the unnecessary complexity. Complexity which is a result of our own abstractions. You know, we (the programmers) like to abstract. We like placing things in black boxes and hope that these boxes work together.

Flux is an architectural design pattern for building user interfaces. It was introduced by Facebook at their F8 conference. Since then, lots of companies adopted the idea and it seems like a good pattern for building front-end apps. Flux is very often used with React. Another library released by Facebook. I myself use React+Flux/Redux in my daily job and I could say that it is simple and really flexible. The pattern helps creating apps faster and at the same time keeps the code well organized.

Flux architecture and its main characteristics

Basic flux architecture

The main actor in this pattern is the dispatcher. It acts as a hub for all the events in the system. Its job is to receive notifications that we call actions and pass them to all the stores. The store decides if it is interested or not and reacts by changing its internal state/data. That change is triggering re-rendering of the views which are (in our case) React components. If we have to compare Flux to the well known MVC we may say that the store is similar to the model. It keeps the data and its mutations.

The actions are coming to the dispatcher either from the views or from other parts of the system, like services. For example a module that performs a HTTP request. When it receives the result it may fire an action saying that the request was successful.

Implementing a Flux architecture

As every other popular concept Flux also has some variations. Very often to understand something we have to implement it. In the next few sections we will create a library that provides helpers for building the Flux pattern.

The dispatcher

In most of the cases we need a single dispatcher. Because it acts as a glue for the rest of the parts it makes sense that we have only one. The dispatcher needs to know about two things - actions and stores. The actions are simply forwarded to the stores so we don’t necessary have to keep them. The stores however should be tracked inside the dispatcher so we can loop through them:

the dispatcher

That’s what I started with:

  1. var Dispatcher = function () {
  2. return {
  3. _stores: [],
  4. register: function (store) {
  5. this._stores.push({ store: store });
  6. },
  7. dispatch: function (action) {
  8. if (this._stores.length > 0) {
  9. this._stores.forEach(function (entry) {
  10. entry.store.update(action);
  11. });
  12. }
  13. }
  14. }
  15. };

The first thing that we notice is that we expect to see an update method in the passed stores. It will be nice to throw an error if such method is not there:

  1. register: function (store) {
  2. if (!store || !store.update) {
  3. throw new Error('You should provide a store that has an `update` method.');
  4. } else {
  5. this._stores.push({ store: store });
  6. }
  7. }

Bounding the views and the stores

The next logical step is to connect our views to the stores so we re-render when the state in the stores is changed.

Bounding the views and the stores

Using a helper

Some of the flux implementations available out there provide a helper function that does the job. For example:

  1. Framework.attachToStore(view, store);

However, I don’t quite like this approach. To make attachToStore works we expect to see a specific API in the view and in the store. We kind of strictly define new public methods. Or in other words we say “Your views and store should have such APIs so we are able to wire them together”. If we go down this road then we will probably define our own base classes which could be extended so we don’t bother the developer with Flux details. Then we say “All your classes should extend our classes”. This doesn’t sound good either because the developer may decide to switch to another Flux provider and has to amend everything.

With a mixin

What if we use React’s mixins.

  1. var View = React.createClass({
  2. mixins: [Framework.attachToStore(store)]
  3. ...
  4. });

That’s a “nice” way to define behavior of existing React component. So, in theory we may create a mixin that does the bounding for us. To be honest, I don’t think that this is a good idea. And it looks like it’s not only me. My reason of not liking mixins is that they modify the components in a non-predictable way. I have no idea what is going on behind the scenes. So I’m crossing this option.

Using a context

Another technique that may answer the question is React’s context. It is a way to pass props to child components without the need to specify them in every level of the tree. Facebook suggests context in the cases where we have data that has to reach deeply nested components.

Occasionally, you want to pass data through the component tree without having to pass the props down manually at every level. React’s “context” feature lets you do this.

I see similarity with the mixins here. The context is defined somewhere at the top and magically serves props for all the children below. It’s not immediately clear where the data comes from.

Higher-Order components concept

Higher-Order components pattern is introduced by Sebastian Markbåge and it’s about creating a wrapper component that returns ours. While doing it, it has the opportunity to send properties or apply additional logic. For example:

  1. function attachToStore(Component, store, consumer) {
  2. const Wrapper = React.createClass({
  3. getInitialState() {
  4. return consumer(this.props, store);
  5. },
  6. componentDidMount() {
  7. store.onChangeEvent(this._handleStoreChange);
  8. },
  9. componentWillUnmount() {
  10. store.offChangeEvent(this._handleStoreChange);
  11. },
  12. _handleStoreChange() {
  13. if (this.isMounted()) {
  14. this.setState(consumer(this.props, store));
  15. }
  16. },
  17. render() {
  18. return <Component {...this.props} {...this.state} />;
  19. }
  20. });
  21. return Wrapper;
  22. };

Component is the view that we want attached to the store. The consumer function says what part of the store’s state should be fetched and sent to the view. A simple usage of the above function could be:

  1. class MyView extends React.Component {
  2. ...
  3. }
  4. ProfilePage = connectToStores(MyView, store, (props, store) => ({
  5. data: store.get('key')
  6. }));

That is an interesting pattern because it shifts the responsibilities. It is the view fetching data from the store and not the store pushing something to the view. This of course has it’s own pros and cons. It is nice because it makes the store dummy. A store that only mutates the data and says “Hey, my state is changed”. It is not responsible for sending anything to anyone. The downside of this approach is maybe the fact that we have one more component (the wrapper) involved. We also need the three things - view, store and consumer to be in one place so we can establish the connection.

My choice

The last option above, higher-order components, is really close to what I’m searching for. I like the fact that the view decides what it needs. That knowledge anyway exists in the component so it makes sense to keep it there. That’s also why the functions that generate higher-order components are usually kept in the same file as the view. What if we can use similar approach but not passing the store at all. Or in other words, a function that accepts only the consumer. And that function is called every time when there is a change in the store.

So far our implementation interacts with the store only in the register method.

  1. register: function (store) {
  2. if (!store || !store.update) {
  3. throw new Error('You should provide a store that has an `update` method.');
  4. } else {
  5. this._stores.push({ store: store });
  6. }
  7. }

By using register we keep a reference to the store inside the dispatcher. However, register returns nothing. And instead of nothing it may return a subscriber that will accept our consumer functions.

Fluxiny - connect store and view

I decided to send the whole store to the consumer function and not the data that the store keeps. Like in the higher-order components pattern the view should say what it needs by using store’s getters. This makes the store really simple and there is no trace of presentational logic.

Here is how the register method looks like after the changes:

  1. register: function (store) {
  2. if (!store || !store.update) {
  3. throw new Error(
  4. 'You should provide a store that has an `update` method.'
  5. );
  6. } else {
  7. var consumers = [];
  8. var subscribe = function (consumer) {
  9. consumers.push(consumer);
  10. };
  11. this._stores.push({ store: store });
  12. return subscribe;
  13. }
  14. return false;
  15. }

The last bit in the story is how the store says that its internal state is changed. It’s nice that we collect the consumer functions but right now there is no code that executes them.

According to the basic principles of the flux architecture the stores change their state in response of actions. In the update method we send the action but we could also send a function change. Calling that function should trigger the consumers:

  1. register: function (store) {
  2. if (!store || !store.update) {
  3. throw new Error(
  4. 'You should provide a store that has an `update` method.'
  5. );
  6. } else {
  7. var consumers = [];
  8. var change = function () {
  9. consumers.forEach(function (consumer) {
  10. consumer(store);
  11. });
  12. };
  13. var subscribe = function (consumer) {
  14. consumers.push(consumer);
  15. };
  16. this._stores.push({ store: store, change: change });
  17. return subscribe;
  18. }
  19. return false;
  20. },
  21. dispatch: function (action) {
  22. if (this._stores.length > 0) {
  23. this._stores.forEach(function (entry) {
  24. entry.store.update(action, entry.change);
  25. });
  26. }
  27. }

Notice how we push change together with store inside the _stores array. Later in the dispatch method we call update by passing the action and the change function.

A common use case is to render the view with the initial state of the store. In the context of our implementation this means firing all the consumers at least once when they land in the library. This could be easily done in the subscribe method:

  1. var subscribe = function (consumer, noInit) {
  2. consumers.push(consumer);
  3. !noInit ? consumer(store) : null;
  4. };

Of course sometimes this is not needed so we added a flag which is by default falsy. Here is the final version of our dispatcher:

  1. var Dispatcher = function () {
  2. return {
  3. _stores: [],
  4. register: function (store) {
  5. if (!store || !store.update) {
  6. throw new Error(
  7. 'You should provide a store that has an `update` method'
  8. );
  9. } else {
  10. var consumers = [];
  11. var change = function () {
  12. consumers.forEach(function (consumer) {
  13. consumer(store);
  14. });
  15. };
  16. var subscribe = function (consumer, noInit) {
  17. consumers.push(consumer);
  18. !noInit ? consumer(store) : null;
  19. };
  20. this._stores.push({ store: store, change: change });
  21. return subscribe;
  22. }
  23. return false;
  24. },
  25. dispatch: function (action) {
  26. if (this._stores.length > 0) {
  27. this._stores.forEach(function (entry) {
  28. entry.store.update(action, entry.change);
  29. });
  30. }
  31. }
  32. }
  33. };

The actions

You probably noticed that we didn’t talk about the actions. What are they? The convention is that they should be simple objects having two properties - type and payload:

  1. {
  2. type: 'USER_LOGIN_REQUEST',
  3. payload: {
  4. username: '...',
  5. password: '...'
  6. }
  7. }

The type says what exactly the action is and the payload contains the information associated with the event. And in some cases we may leave the payload empty.

It’s interesting that the type is well known in the beginning. We know what type of actions should be floating in our app, who is dispatching them and which of the stores are interested. Thus, we can apply partial application and avoid passing the action object here and there. For example:

  1. var createAction = function (type) {
  2. if (!type) {
  3. throw new Error('Please, provide action\'s type.');
  4. } else {
  5. return function (payload) {
  6. return dispatcher.dispatch({
  7. type: type,
  8. payload: payload
  9. });
  10. }
  11. }
  12. }

createAction leads to the following benefits:

  • We no more need to remember the exact type of the action. We now have a function which we call passing only the payload.
  • We no more need an access to the dispatcher which is a huge benefit. Otherwise, think about how we have to pass it to every single place where we need to dispatch an action.
  • In the end we don’t have to deal with objects but with functions which is much nicer. The objects are static while the functions describe a process.

Fluxiny actions creators

This approach for creating actions is actually really popular and functions like the one above are usually called action creators.

The final code

In the section above we successfully hide the dispatcher while submitting actions. We may do it again for the store’s registration:

  1. var createSubscriber = function (store) {
  2. return dispatcher.register(store);
  3. }

And instead of exporting the dispatcher we may export only these two functions createAction and createSubscriber. Here is how the final code looks like:

  1. var Dispatcher = function () {
  2. return {
  3. _stores: [],
  4. register: function (store) {
  5. if (!store || !store.update) {
  6. throw new Error(
  7. 'You should provide a store that has an `update` method'
  8. );
  9. } else {
  10. var consumers = [];
  11. var change = function () {
  12. consumers.forEach(function (consumer) {
  13. consumer(store);
  14. });
  15. };
  16. var subscribe = function (consumer, noInit) {
  17. consumers.push(consumer);
  18. !noInit ? consumer(store) : null;
  19. };
  20. this._stores.push({ store: store, change: change });
  21. return subscribe;
  22. }
  23. return false;
  24. },
  25. dispatch: function (action) {
  26. if (this._stores.length > 0) {
  27. this._stores.forEach(function (entry) {
  28. entry.store.update(action, entry.change);
  29. });
  30. }
  31. }
  32. }
  33. };
  34. module.exports = {
  35. create: function () {
  36. var dispatcher = Dispatcher();
  37. return {
  38. createAction: function (type) {
  39. if (!type) {
  40. throw new Error('Please, provide action\'s type.');
  41. } else {
  42. return function (payload) {
  43. return dispatcher.dispatch({
  44. type: type,
  45. payload: payload
  46. });
  47. }
  48. }
  49. },
  50. createSubscriber: function (store) {
  51. return dispatcher.register(store);
  52. }
  53. }
  54. }
  55. };

If we add the support of AMD, CommonJS and global usage we end up with 66 lines of code, 1.7KB plain or 795 bytes after minification JavaScript.

Wrapping up

We have a module that provides two helpers for building a Flux project. Let’s write a simple counter app that doesn’t involve React so we see the pattern in action.

The markup

We’ll need some UI to interact with it so:

  1. <div id="counter">
  2. <span></span>
  3. <button>increase</button>
  4. <button>decrease</button>
  5. </div>

The span will be used for displaying the current value of our counter. The buttons will change that value.

The view

  1. const View = function (subscribeToStore, increase, decrease) {
  2. var value = null;
  3. var el = document.querySelector('#counter');
  4. var display = el.querySelector('span');
  5. var [ increaseBtn, decreaseBtn ] =
  6. Array.from(el.querySelectorAll('button'));
  7. var render = () => display.innerHTML = value;
  8. var updateState = (store) => value = store.getValue();
  9. subscribeToStore([updateState, render]);
  10. increaseBtn.addEventListener('click', increase);
  11. decreaseBtn.addEventListener('click', decrease);
  12. };

It accepts a store subscriber function and two action function for increasing and decreasing the value. The first few lines of the view are just fetching the DOM elements.

After that we define a render function which puts the value inside the span tag. updateState will be called every time when the store changes. So, we pass these two functions to subscribeToStore because we want to get the view updated and we want to get an initial rendering. Remember how our consumers are called at least once by default.

The last bit is calling the action functions when we press the buttons.

The store

Every action has a type. It’s a good practice to create constants for these types so we don’t deal with raw strings.

  1. const INCREASE = 'INCREASE';
  2. const DECREASE = 'DECREASE';

Very often we have only one instance of every store. For the sake of simplicity we’ll create ours as a singleton.

  1. const CounterStore = {
  2. _data: { value: 0 },
  3. getValue: function () {
  4. return this._data.value;
  5. },
  6. update: function (action, change) {
  7. if (action.type === INCREASE) {
  8. this._data.value += 1;
  9. } else if (action.type === DECREASE) {
  10. this._data.value -= 1;
  11. }
  12. change();
  13. }
  14. };

_data is the internal state of the store. update is the well known method that our dispatcher calls. We process the action inside and say change() when we are done. getValue is a public method used by the view so it reaches the needed info. In our case this is just the value of the counter.

Wiring all the pieces

So, we have the store waiting for actions from the dispatcher. We have the view defined. Let’s create the store subscriber, the actions and run everything.

  1. const { createAction, createSubscriber } = Fluxiny.create();
  2. const counterStoreSubscriber = createSubscriber(CounterStore);
  3. const actions = {
  4. increase: createAction(INCREASE),
  5. decrease: createAction(DECREASE)
  6. };
  7. View(counterStoreSubscriber, actions.increase, actions.decrease);

And that’s it. Our view is subscribed to the store and it renders by default because one of our consumers is actually the render method.

A live demo

A live demo could be seen in the following JSBin http://jsbin.com/koxidu/embed?js,output. If that’s not enough and it seems too simple for you please checkout the example in Fluxiny repository https://github.com/krasimir/fluxiny/tree/master/example. It uses React as a view layer.

The Flux implementation discussed in this section is available here github.com/krasimir/fluxiny. Feel free to use it in a browser directly or as a npm dependency.