项目作者: kahlil

项目描述 :
UI state management with RxJS.
高级语言: JavaScript
项目地址: git://github.com/kahlil/flow-state.git
创建时间: 2016-06-01T13:07:05Z
项目社区:https://github.com/kahlil/flow-state

开源协议:MIT License

下载


FlowState 🌊

Dead simple Redux and redux-observable library built with RxJS streams. This is an easy way to introduce a stream-based unidirectional dataflow into your app.

Highlights

  • Inspired by Redux and redux-observable
  • The state is delivered with streams
  • Reducers are just functions, no switch-case
  • Isolate side effects that map back to actions

Install

npm install @kahlil/flow-state

Usage

  1. import { createFlowState } from '@kahlil/flow-state';
  2. const flowState = createFlowState();

Dispatch Actions

In your components dispatch actions by passing the action constant and optionally
an action payload. The payload can be any value.

  1. flowState.dispatch({ type: 'SOME_ACTION', payload: { some: 'state' } });

Create A State Stream Based On Reducers

In a file that you could call Store,
create and expose state streams for your components
by passing the respective reducers.

Here is also the place where you can combine state streams if they
depend on one another.

  1. // A collection of reducers.
  2. const itemListReducers = {
  3. deleteItem: (action, state) => state
  4. .filter(item => state.filter(item.id !== action.payload.id)),
  5. addItem: (action, state) => [...state, action.payload],
  6. // ...
  7. };
  8. itemListState$ = flowState.createState$(itemListReducers, initialState);

In your component you can now subscribe to the component state stream:

  1. itemListState$.subscribe(state => console.log(state));

Trigger Side Effects

You can trigger your side effects similar to redux-observable
by listening to the actions stream, triggering your side effect
and return a new action.

Each side effect is a function and has to be passed to
flowState.runSideEffects.

The action that the result of each side effect maps to

  1. // An imaginary API.
  2. const serverApi = new serverApi();
  3. const action$ = flowState.getAction$();
  4. const sideEffect1 = action$ => action$
  5. .filter(action => action.type === 'DELETE_ITEM')
  6. .switchMap(action => serverApi.deleteItem(action.payload))
  7. .map(response => ({ type: 'RECEIVE_ITEMS', payload: response }))
  8. .catch(response => ({ type: 'DELETE_ITEM_ERROR', payload: response.error });
  9. const sideEffect1 = action$ => action$
  10. .filter(action => action.type === 'ADD_ITEM')
  11. .switchMap(action => serverApi.addItem(action.payload))
  12. .map(response => ({ type: 'RECEIVE_ITEMS', payload: response }))
  13. .catch(response => ({ type: 'ADD_ITEM_ERROR', payload: response.error });
  14. flowState.runSideEffects(sideEffect1, sideEffect2);

License

MIT © Kahlil Lechelt