0.3.1 • Published 5 years ago

redux-agent v0.3.1

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

Redux Agent

In UI development, React lets you describe the interface and have the machine worry about drawing it. A simple and powerful model which unfortunately stops at visual I/O. Non-visual I/O such as network requests is usually done in thunks/sagas/epics (scattering logic across middlewares and reducers) or in UI components (coupling them to remote APIs).

Redux Agent extends React's model to non-visual I/O: describe a network request, a storage operation, a websocket message, ... and let the machine worry about performing it. Logic stays in the reducer, components stay lightweight, and it's easy to see what state triggers which effect.

Redux Agent doesn't introduce middlewares, doesn't modify existing APIs, and doesn't involve exotic concepts; it has only one basic abstraction (the "task") and is UI agnostic. Sending an HTTP request is as simple as:

import { addTask } from 'redux-agent'

const reducer = (state, action) => {
  switch(action.type) {
    case 'FETCH_TODO':
      return addTask({
        type: 'http',
        method: 'get',
        url: 'https://jsonplaceholder.typicode.com/todos/1',
        actions: {
          success: 'FETCH_TODO_SUCCESS',
          failure: 'FETCH_TODO_FAILURE'
        }
      })
    case 'FETCH_TODO_SUCCESS':
      return {
        ...state,
        items: [ ...state.items, action.payload ]
      }

Try it

See Redux Agent in action in one of these interactive examples:

Q&A

"I thought that the reducer should stay free from side effects?"

Yes. In fact, addTask in the example above doesn't perform any effect, it only stores a task description in the state which is later used by the runtime to perform the effect, just like you normally store data in the state which is later used to render the UI.

"What if I want to not just add a task but also modify the state in other ways?"

addTask simply returns a new state that includes the desired task. You can further derive new states as usual:

    case 'FETCH_TODO':
-     return addTask(state, { /* ... */ })
+     const s1 = addTask(state, { /* ... */ })
+     return { ...s1, isLoading: true }

Or make it even easier on the eyes with Redux Agent's built-in support for Immer.

"Is this similar to Elm?"

Quite. But whereas Elm's reducer returns the new state plus commands, Redux Agent considers active tasks an integral part of the application state and therefore keeps them in the state tree.

"Is this similar to redux-loop?"

Yes and no. Yes, since both make the reducer the single source of logic. No, since redux-loop changes the reducer's API so it can return state plus commands, whereas Redux Agent uses vanilla Redux APIs.

Documentation

License

MIT

Credits

reduceReducers by Tim Cheung. Icon by Setyo Ari Wibowo.

0.3.1

5 years ago

0.3.0

5 years ago

0.3.0-alpha.3

5 years ago

0.3.0-alpha.2

5 years ago

0.3.0-alpha.1

5 years ago

0.3.0-alpha.0

5 years ago

0.2.9

5 years ago

0.2.8

5 years ago

0.2.7

5 years ago

0.2.6

5 years ago

0.2.5

5 years ago

0.2.4

5 years ago

0.2.3

5 years ago

0.2.2

5 years ago

0.2.1

5 years ago

0.2.0

5 years ago

0.1.1

5 years ago

0.2.0-alpha.0

5 years ago

0.0.0

5 years ago