8.0.3 • Published 7 years ago

oddstream v8.0.3

Weekly downloads
3
License
MIT
Repository
github
Last release
7 years ago

oddstream

Greenkeeper badge

One Direction Data Stream.

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

Install

npm install oddstream

Usage

import { createOddstream } from 'oddstream';

const oddstream = createOddstream();

Dispatch Actions

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

oddstream.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.

// A collection of reducers.
const itemListReducers = {
  deleteItem: (action, state) => state
    .filter(item => state.filter(item.id !== action.payload.id)),
    
  addItem: (action, state) => [...state, action.payload],
  // ...
};

itemListState$ = oddstream.createState$(itemListReducers, initialState);

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

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 oddstream.runSideEffects.

The action that the result of each side effect maps to

// An imaginary API.
const serverApi = new serverApi();

const sideEffect1 = action$ => action$
  .filter(action => action.type === 'DELETE_ITEM')
  .switchMap(action => serverApi.deleteItem(action.payload))
  .map(response => ({ type: 'RECEIVE_ITEMS', payload: response }))
  .catch(response => ({ type: 'DELETE_ITEM_ERROR', payload: response.error });

const sideEffect1 = action$ => action$
  .filter(action => action.type === 'ADD_ITEM')
  .switchMap(action => serverApi.addItem(action.payload))
  .map(response => ({ type: 'RECEIVE_ITEMS', payload: response }))
  .catch(response => ({ type: 'ADD_ITEM_ERROR', payload: response.error });

oddstream.runSideEffects(sideEffect1, sideEffect2);

License

MIT © Kahlil Lechelt

8.0.3

7 years ago

8.0.2

7 years ago

8.0.1

7 years ago

8.0.0

7 years ago

7.0.0

7 years ago

6.0.0

7 years ago

5.0.3

7 years ago

5.0.2

7 years ago

5.0.1

7 years ago

5.0.0

7 years ago

4.1.6

7 years ago

4.1.5

7 years ago

4.1.4

7 years ago

4.1.3

7 years ago

4.1.2

7 years ago

4.1.1

7 years ago

4.1.0

7 years ago

4.0.7

7 years ago

4.0.6

7 years ago

4.0.5

7 years ago

4.0.4

7 years ago

4.0.3

7 years ago

4.0.2

7 years ago

3.0.0

8 years ago

2.1.0

8 years ago

2.0.0

8 years ago

1.0.5

8 years ago

1.0.4

8 years ago

1.0.3

8 years ago

1.0.2

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago