Coder Social home page Coder Social logo

redux_future's Introduction

redux_future

Build Status codecov

A Redux Middleware for handling Dart Futures as actions, with support for loading & optimistic payloads.

The futureMiddleware can be attached to the Redux Store upon construction.

Once attached, you can store.dispatch a Future or FutureAction, and the futureMiddleware will intercept it.

  • If the Future / FutureAction completes successfully, a FutureFulfilledAction will be dispatched with the result of the future.
  • If the Future / FutureAction fails, a FutureRejectedAction will be dispatched containing the error that was returned.

Examples

main() {
  // First, create a reducer that knows how to handle the Future Actions:
  // `FutureFulfilledAction` and `FutureRejectedAction`.
  String exampleReducer(String state, action) {
    if (action is String) {
      return action;
    } else if (action is FutureFulfilledAction<String>) {
      return action.result;
    } else if (action is FutureRejectedAction<Exception>) {
      return action.error.toString();
    }
  
    return state;
  }
  
  // Next, create a Store that includes `futureMiddleware`. It will
  // intercept all `FutureAction`s that are dispatched.
  final store = new Store(
    exampleReducer,
    middleware: [futureMiddleware],
  );
  
  // Next, dispatch some actions!
  
  // In this example, once the Future completes, a `FutureFulfilledAction`
  // will be dispatched with "Hi" as the result. The `exampleReducer` will
  // take the result of this action and update the state of the Store!
  store.dispatch(new Future.value("Hi"));
  
  // In this example, the initialAction String "Fetching" will be
  // immediately dispatched. After the future completes, the
  // "Search Results" will be dispatched.
  store.dispatch(new FutureAction(
    new Future.value("Search Results"),
    initialAction: "Fetching"));
  
  // In this example, the future will complete with an error. When that
  // happens, a `FutureRejectedAction` will be dispatched to your store,
  // and the state will be updated by the `exampleReducer`.
  store.dispatch(new Future.error("Oh no!"));
}

Contributors

redux_future's People

Contributors

brianegan avatar kezhuw avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

redux_future's Issues

Dependencies

Depends on redux 2.0.0 not be able to used on 3.0 version.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.