Git Product home page Git Product logo

rereduce's Introduction

Rereduce

Simple reducer library for Redux. It's like Reselect but for reducers.

By using aggressive memoization, reducers can depend on each others in an efficient way, without having to query Redux store.

It works fine with time-travel debugging and server-side rendering, because reducers remains totally stateless pure functions.

It permits to replace the imperative waitFor of original Flux implementation by a purely functional approach.

API

The API is a first draft and may change in the future. Don't hesitate to discuss what the API should look like here: slorber#1

createReducer( [reducerDependencies], reducerFunction)

[reducerDependencies] is an optional object that looks like {key1: reducer1, key2: reducer2}

If dependencies are provided, reducerFunction will be called with a 3rd argument with the state of the dependency reducers.

Simple Reducers (without dependencies)

Rereduce only add basic memoization on the original reducer. You don't need to use rereduce for those reducers at all. But if you plan to make other reducers depend on your simple reducer, wrapping it with rereduce will be more CPU and memory efficient.

Reducers with dependencies

  • reducerFunction will be called with a 3rd argument with the state of the dependency reducers.
  • The reducer always return an object with 2 attributes value and __dependencies
  • value is the computed value of your reducing function.
  • __dependencies is an attribute that you could simply ignore (don't worry, it does not consume as much additional memory as you may naively think).
  • __dependencies is required to keep the reducer stateless.
  • You should take care of the __dependencies attribute if you need efficient Redux store serialization/deserialization.

Example

import {  createReducer  } from 'rereduce'

const firstReducer =  createReducer((state = 0, action) => {
  switch (action.type) {
    case 'increment': return state + 1
    case 'decrement': return state - 1
    default: return state
  }
})

const secondReducer = createReducer({ firstReducer },
  (state = 0,action,{ firstReducer }) => firstReducer + 1
)

const rootReducer = combineReducers({ firstReducer, secondReducer })

const initialState = undefined
const stateInitialized = rootReducer(initialState,{ type: 'init' })
const stateIncremented = rootReducer(stateInitialized,{ type: 'increment' })

assert.equal(stateInitialized.firstReducer, 0)
assert.equal(stateInitialized.secondReducer.value, 1)

assert.equal(stateIncremented.firstReducer, 1)
assert.equal(stateIncremented.secondReducer.value, 2)

rereduce's People

Contributors

slorber avatar aikoven avatar yarabarla avatar javascriptjedi avatar

Watchers

James Cloos avatar Dobrin Ganev avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.