Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Updux<S>

Updux is a way to minimize and simplify the boilerplate associated with the creation of a Redux store. It takes a shorthand configuration object, and generates the appropriate reducer, actions, middleware, etc. In true Redux-like fashion, upduxes can be made of sub-upduxes (subduxes for short) for different slices of the root state.

Type parameters

  • S

    Store's state type. Defaults to any.

Hierarchy

  • Updux

Index

Constructors

constructor

  • Parameters

    Returns Updux

Properties

groomMutations

groomMutations: function

Function that can be provided to alter all local mutations of the updux (the mutations of subduxes are left untouched).

Can be used, for example, for Immer integration:

import Updux from 'updux';
import { produce } from 'Immer';

const updux = new Updux({
    initial: { counter: 0 },
    groomMutations: mutation => (...args) => produce( mutation(...args) ),
    mutations: {
        add: (inc=1) => draft => draft.counter += inc
    }
});

Or perhaps for debugging:

import Updux from 'updux';

const updux = new Updux({
    initial: { counter: 0 },
    groomMutations: mutation => (...args) => state => {
        console.log( "got action ", args[1] );
        return mutation(...args)(state);
    }
});

Type declaration

initial

initial: S

Default initial state of the reducer. If applicable, merges the initial states of config and subduxes, with config having precedence.

If nothing was provided, defaults to an empty object.

subduxes

subduxes: Dictionary<Updux>

Accessors

actions

  • Action creators for all actions defined or used in the actions, mutations, effects and subduxes of the updux config.

    Non-custom action creators defined in actions have the signature (payload={},meta={}) => ({type, payload,meta}) (with the extra sugar that if meta or payload are not specified, the key is not present in the produced action).

    If the same action appears in multiple locations, the precedence order determining which one will prevail is

    actions generated from mutations/effects < non-custom subduxes actions <
    custom subduxes actions < custom actions

    Returns Dictionary<ActionCreator>

asDux

  • get asDux(): Dux<S>
  • Returns a ducks-like plain object holding the reducer from the Updux object and all its trimmings.

    Returns Dux<S>

createStore

  • get createStore(): function
  • Same as doing

    import { createStore, applyMiddleware } from 'redux';
    
    const { initial, reducer, middleware, actions } = updox(...);
    
    const store = createStore( initial, reducer, applyMiddleware(middleware) );
    
    for ( let type in actions ) {
        store.dispatch[type] = (...args) => {
            store.dispatch(actions[type](...args))
        };
    }

    So that later on you can do

    store.dispatch.addTodo(...);
    
    // still work
    store.dispatch( actions.addTodo(...) );

    Returns function

middleware

  • A middleware aggregating all the effects defined in the updux and its subduxes. Effects of the updux itself are done before the subduxes effects.

    Returns Middleware<__type, S, UpduxDispatch>

mutations

  • Merge of the updux and subduxes mutations. If an action triggers mutations in both the main updux and its subduxes, the subduxes mutations will be performed first.

    Returns Dictionary<Mutation<S>>

reducer

  • get reducer(): function
  • A Redux reducer generated using the computed initial state and mutations.

    Returns function

      • (state: S | undefined, action: Action): S
      • Parameters

        • state: S | undefined
        • action: Action

        Returns S

subduxUpreducer

  • get subduxUpreducer(): function
  • Returns the upreducer made of the merge of all sudbuxes reducers, without the local mutations. Useful, for example, for sink mutations.

    example
    import todo from './todo'; // updux for a single todo
    import Updux from 'updux';
    import u from 'updeep';
    
    const todos = new Updux({ initial: [], subduxes: { '*': todo } });
    todos.addMutation(
        todo.actions.done,
        ({todo_id},action) => u.map( u.if( u.is('id',todo_id) ), todos.subduxUpreducer(action) )
        true
    );

    Returns function

      • Parameters

        Returns function

          • (state: S): S
          • Parameters

            • state: S

            Returns S

upreducer

  • Returns Upreducer<S>

Methods

addMutation

  • addMutation<A>(creator: A, mutation: Mutation<S, A extends (...args: any[]) => infer R ? R : never>, isSink?: undefined | false | true): void
  • Adds a mutation and its associated action to the updux. If a local mutation was already associated to the action, it will be replaced by the new one.

    example
    updux.addMutation( add, inc => state => state + inc );

    Type parameters

    Parameters

    • creator: A
    • mutation: Mutation<S, A extends (...args: any[]) => infer R ? R : never>
    • Optional isSink: undefined | false | true

      If true, disables the subduxes mutations for this action. To conditionally run the subduxes mutations, check out subduxUpreducer.

    Returns void

Generated using TypeDoc