0.1.10 • Published 5 years ago

reducer-types v0.1.10

Weekly downloads
1
License
ISC
Repository
-
Last release
5 years ago

Work in progress

This library is still not released and its API is still unstable. It's not recommended to use yet. Currently consolidating the API.

A library to write reducer-based state in a concise, readable and efficient way.

Why

State managers based on reducers and actions (like redux) are very powerful, but writing action and reducers for each part of the state can be very annoying and error prone. Usually, it's possible to find common patterns in different reducers, like:

  • Setting a new value
  • Pushing a new value in an array
  • Adding a new key-value pair in an object
  • ... many more!

reducer-types offers a different approach for this: define common actions only once, and instantiate many reducers which can handle the same set of actions. In this approach, a set of common actions defines a type: for example, The "array" type might handle the "push" and the "pop" actions, and you can use those actions without repeating the code for every single array in the state.

Features

  • Allows to define a reducer, and many actions to interact with it, simply calling a function
  • Zero dependencies
  • Extremely easy to integrate
  • Create new actions by grouping many actions into one. Execute the reducer only once!
  • Pluggable to existing reducer-based states (e.g. existing Redux code)
  • Allows to create dynamically pluggable modules easily

Concepts

  • Action: an object describing an operation on the state. It's serializable and it conforms to the FSA format.
  • Action creator: a function returning an action.
  • Action handler: (reducer) a function that takes the old state and the action as arguments, and returns a new state.
  • Type: a set of action creators and action handlers.
  • Type set: a set of types which are part of the system, and used to build reducers and action creators.
  • Type instance: an object describing the type and the initial state of an instance.
  • Model: a description of a state. It's a tree, and its leaves are type instances.

Creating a model

reducer-types offers a basic configuration to help getting started. This can be found in reducer-types/basic-types.

const { type } = require('reducer-types/basic-types');

// a very simple model, sescribing a number
const myNumberModel = type.number(0);

// a model describing a collection
const myCollectionModel = {
  byId: type.object({}),
  ids: type.array([]),
};

// extending a model
const myExtendedCollectionModel = {
  ...myCollectionModel,
  selectedId: type.string(null),
};

// combining models
const myStateModel = {
  someCounter: myNumberModel,
  someCollection: myExtendedCollectionModel,
};

This model describes an object

Todo

  • Custom action type for composed action
  • Splitting functions in different files, placing private ones in a subfolder
  • model distribution of types (separate npm packages exporting types)
  • passing data/functions to the action handlers
  • refactor types to be optimizable
  • unit test type setters
  • add string.replace
  • create datetime basic type
  • create a runtime typechecked types collection
  • fix issue: When creating a single-type module, with no mountpoint (root), and composing the action, due to its id being '', the action is not included in the composed action
0.1.10

5 years ago

0.1.9

5 years ago

0.1.8

5 years ago

0.1.7

5 years ago

0.1.6

5 years ago

0.1.5

5 years ago

0.1.3

5 years ago

0.1.2

5 years ago

0.1.0

5 years ago