rxloop
TypeScript icon, indicating that this package has built-in type declarations

0.6.0 • Public • Published

rxLoop

rxLoop = Redux + redux-observable.

Predictable state container for JavaScript apps based on RxJS, like Redux with redux-observable middleware.

  1. Using RxJS instead of Redux.
  2. Easy study, only four apis: app.model、app.dispatch、app.getState、app.stream.
  3. Cancel async actions easyly.

Installation

$ npm i rxloop

Hello rxloop

import { Observable } from 'rxjs';
import { mergeMap, map } from 'rxjs/operators';
import rxLoop from 'rxloop';
 
const counterModel = {
  name: 'counter',
  state: {
    counter: 0,
  },
  reducers: {
    increment(state) {
      return {
        ...state,
        counter: state.counter + 1
      };
    },
  },
  epics: {
    getData(action$) {
      return action$.pipe(
        mergeMap(() => {
          return Observable.fromPromise(
            // Promise
            api().catch((error) => {
              return { error };
            }),
          );
        }),
        map((data) => {
          return {
            type: 'increment',
          };
        }),
      );
    }
  }
};
 
const app = rxLoop();
app.model(counterModel);
 
app.stream('counter').subscribe((state) => {
  // this.setState(state);
});
 
// sync update
app.dispatch({
  type: 'counter/increment',
});
 
// async update
app.dispatch({
  type: 'counter/getData',
});

Documentation

rxloop

Examples

examples

Releases

releases

License

MIT

Package Sidebar

Install

npm i rxloop

Weekly Downloads

1

Version

0.6.0

License

MIT

Unpacked Size

304 kB

Total Files

9

Last publish

Collaborators

  • wxnet