Skip to content

Predictable state container for JavaScript apps

License

MIT, Unknown licenses found

Licenses found

MIT
LICENSE.md
Unknown
LICENSE-logo.md
Notifications You must be signed in to change notification settings

fourlabsldn/future-redux

 
 

Repository files navigation

Future Redux

Redux is a predictable state container for JavaScript apps.

This is a fork of Redux with a single difference: The return value of reducers.

In Redux your reducer returns a state object. This is very straight forward, but makes dealing with asynchronous updates quite tricky (there are more than 60 different libraries tackling this problem).

In Future Redux your update function returns an array containing at most two items:

  • The first is your state object
  • The second one is optional and is a Future object, which is similar to a Promise.

The resolve function of the future is equivalent to store.dispatch. You can use it like this:

// This is the future implementation we will use.
// You can use any implementation with a `fork` method.
import { Future } from "ramda-fantasy";

function myReducer(state, action) {
  switch(action.type) {
    case "FETCH_CONTENT";
      return [
        state,
        new Future((reject, resolve) => {
          fetch("www.content.com/endpoint")
            .then(r => r.json())
            .then(content =>
              resolve({ type : "CONTENT_ARRIVED", value: content})
            )
        })
      ];

    case "CONTENT_ARRIVED":
      return [action.value]
  }
}

That's it. This way you can have your reducers return the description of a side effect without losing its purity and without having to hook up other libraries for that.

License

MIT

About

Predictable state container for JavaScript apps

Resources

License

MIT, Unknown licenses found

Licenses found

MIT
LICENSE.md
Unknown
LICENSE-logo.md

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 87.9%
  • TypeScript 11.6%
  • CSS 0.5%