Giter Site home page Giter Site logo

rx-redux's Introduction

npm version

rx-redux

A reimplementation of redux using RxJS.

Why?

Reactive by default, this makes difference.

If you just want to use redux with RxJS and don't care about API compatibility, see redux-core.

Features

  • All of the redux APIs implemented.
  • Additionally, store provides 2 rx objects you can utilize:
    • dispatcher$ is a Subject that you can pass actions in.
    • state$ is an Observable, a stream of states.
  • And a helper function connectAction to stream actions to store (see example below).

What does it look like?

import {createStore, combineReducers, applyMiddleware, connectAction} from 'rx-redux'
import thunkMiddleware from 'redux-thunk'
import * as reducers from './reducers'
import { render, getActionStream } from './view'

const action$ = getActionStream();

const newCreateStore = applyMiddleware(thunkMiddleware)(createStore);
const reducer = combineReducers(reducers);
const store = newCreateStore(reducer);

// stream states to view
store.state$.subscribe(state => render(state));

// stream actions to dispatcher
action$.subscribe(action => store.dispatcher$.onNext(action));
// or you can write this way
// connectAction(action$, store);

Best practice to make your app all the way reactive

Don't do async in Middleware, create RxMiddleware instead.

This will ease the pain to build universal apps.

RxMiddleware

Which wraps action stream, look like this:

import Rx from 'rx';

export default function thunkMiddleware(getState) {
  return action => {
    if(typeof action === 'function') {
      return Rx.Observable.just(action(getState));
    }

    // Don't know how to handle this thing, pass to next rx-middleware
    return Rx.Observable.just(action);
  };
}

How to design RxMiddleware

  • Get action, return Observable.
  • Must return Observable.
    • If you don't want to return an action (eg. if counter is not odd), return a Rx.Observable.empty().

See a basic RxMiddleware example

WIP

  • Figure out how to test a Rx project (No experience before).
  • Work with Hot Module Replacement.
  • Work with redux-devtools.
  • More examples.

Thanks

  • @xgrommx for submitting pull requests and suggestions.

Feel free to ask questions or submit pull requests!

Inspiration

  • redux, learn a lot through the source code.
  • Cycle.js for the cool MVI flow.

License

The MIT License (MIT)

rx-redux's People

Contributors

xgrommx avatar jas-chen avatar

Watchers

James Cloos avatar  avatar

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.