Socket
Socket
Sign inDemoInstall

redux-fluent

Package Overview
Dependencies
6
Maintainers
2
Versions
74
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    redux-fluent

Enjoy Redux - Less Boilerplate / Go Functional


Version published
Weekly downloads
7
decreased by-41.67%
Maintainers
2
Install size
71.8 kB
Created
Weekly downloads
 

Changelog

Source

0.26.1 (2019-12-20)

Bug Fixes

  • export typescript definitions (9373d20)

Readme

Source

Redux Fluent redux-fluent

Build Status npm version License Conventional Commits npm downloads Maintainability Test Coverage Codacy Badge Greenkeeper badge

Tiny and eloquent way of bringing redux to the next level (~3K, typings included).

Try it out on RunKit (coming soon)

Motivations and Design Goals

Redux is great, every recent web application has most likely been built on top of it, and we can really make it better!

  • Flux Standard Actions as a first class concept. FSA is a pretty common standard and we committed on following it to ensure interoperability.
  • λ Go Functional, Everything is a function and reducers are built by function composition rather than piling up if and switch-case statements: Let's introduce Redux Fluent Reducers.
  • Type Safety means great developer experience. That's what we had in mind while designing redux-fluent. Watch it yourself!
  • Reducers at scale, due to being handling multiple actions, reducers tend to grow and become difficult to maintain: Let's introduce Redux Fluent Action Handlers.
  • Less boilerplate, Flux architecture is usually verbose and some of their concepts, such as Action, Action Type and Action Creator could all be implemented in a single entity: Let's introduce Redux Fluent Actions.

Installation

yarn add redux-fluent redux flux-standard-action

Getting Started

/** todos.actions.js **/
import { createAction } from 'redux-fluent';

export const addTodo = createAction('todos | add');
/** todos.reducer.js **/
import { createReducer, ofType } from 'redux-fluent';
import * as actions from './todos.actions.js';

const addTodo = (state, { payload }) => state.concat(payload);

export const todos = createReducer('todos')
  .actions(
    ofType(actions.addTodo).map(addTodo),
    /* and so on */
  )
  .default(() => []);
/** application.js **/
import { createStore } from 'redux';
import { combineReducers } from 'redux-fluent';
import * as actions from './todos.actions.js';
import { todos } from './todos.reducer.js';

const rootReducer = combineReducers(todos, ...);
const store = createStore(rootReducer);
console.log(store.getState()); // { todos: [] }

store.dispatch(actions.addTodo({ title: 'Listen for some music' }));
console.log(store.getState()); // { todos: [{ title: 'Listen for some music' }] }

Documentation

createReducer()

createReducer is a function combinator whose purpose is to output a redux reducer by combining action handlers and getDefaultState together.

import { createReducer } from 'redux-fluent';

const reducer = createReducer(name)
  .actions(
    ...handlers: (state, action, config?) => state,
  )
  .default(
    getDefaultState: (state: void, action, config?) => state,
  );

createAction()

createAction is a factory function whose purpose is to output an action creator responsible of shaping your actions. The resulting function holds a field type giving you access to the action type.

import { createAction } from 'redux-fluent';

createAction(
  type: string,
  payloadCreator?: (rawPayload, rawMeta, type) => payload,
  metaCreator?: (rawPayload, rawMeta, type) => meta,
);

/**
* Action Creator
*
* @param {*|Error} payload
* @param {*} meta
* @returns RFA<'todos | add', *|Error, *> - Flux Standard Action
*/
const addTodo = createAction('todos | add');
console.log(addTodo.type); // 'todos | add'
console.log(addTodo({ id: 1, title: 'have a break' })); // { type: 'todos | add', payload: { id: 1, title: 'have a break' } }

ofType()

As we just said, a reducer is nothing more than a function combinator, it does not contain any business logic. The job of actually mutating the state is left to the Action Handlers. By embracing the Single Responsibility Principle, we can build simple, easy to test, dedicated functions that do only one thing.

import { ofType } from 'redux-fluent';

ofType(action: ReduxFluentActionCreator).map((state: any, action: RFA) => state);
ofType(type: string).map((state: any, action: AnyAction) => state);
ofType(action: { type: string }).map((state: any, action: AnyAction) => state);

// write your action handlers by only focusing on the actual task,
// let `ofType` take care of the orchestration (i.e. intercepting actions).
ofType('todos | add').map(
  (state, action) => state.concat(action.payload),
);

withConfig()

Redux Fluent also comes with some additional features to help you write code at scale. Functions that only rely on their arguments are easy to test and reuse, so you can provide an additional argument config: any to enable dependency injection (multiple config are shallowly merged).

import * as R from 'ramda';
import { withConfig, createReducer, ofType } from 'redux-fluent';

withConfig(config: any);

const todos = createReducer('todos')
  .actions(
    ofType('todos | doSomething').map(
      (state, action, { doSomething }) => doSomething(state, action),
    ),
    ofType('todos | doSomethingElse').map(
      (state, action, { doSomethingElse }) => doSomethingElse(state, action),
    ),
  )
  .default(() => []);

export default R.pipe(
  withConfig({ doSomething: R.tap(console.log) }),
  withConfig({ doSomethingElse: R.tap(console.log) }),
)(todos);

combineReducers()

This api is not strictly needed, you can still use redux#combineReducers. With Redux Fluent, by the way, both namespace and reducer are defined in the same place so that you don't need to take care of the shape when combining them.

import { createStore } from 'redux';
import { combineReducers } from 'redux-fluent';
import { todosReducer, notesReducer, otherReducer } from './reducers';

combineReducers(...reducers: ReduxFluentReducer[]);

const rootReducer = combineReducers(todosReducer, notesReducer, otherReducer, ...);
const store = createStore(rootReducer);

createReducersMapObject()

This api lets you combine your reducers with redux#combineReducers, and can be useful when mixing redux fluent reducers with any other reducer.

import { createStore, combineReducers } from 'redux';
import { createReducersMapObject } from 'redux-fluent';
import { todosReducer, notesReducer, otherReducer } from './reducers';

createReducersMapObject(...reducers: ReduxFluentReducer[]);

const reducersMapObject = createReducersMapObject(todosReducer, notesReducer, otherReducer, ...);
const rootReducer = combineReducers({
  ...reducersMapObject,
  anyOtherReducer: (state, action) => 'any other state',
});
const store = createStore(rootReducer);

Typescript Definitions

Redux Fluent

Keywords

FAQs

Last updated on 09 Aug 2019

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc