updux/docs/typedoc/classes/updux.md

11 KiB
Raw Blame History

updux - v1.2.0 Globals Updux

Class: Updux <S, A, X, C>

Type parameters

S

A

X

C: UpduxConfig

Hierarchy

  • Updux

Index

Constructors

Properties

Accessors

Methods

Constructors

constructor

+ new Updux(config: C): Updux

Parameters:

Name Type Default Description
config C {} as C an UpduxConfig plain object

Returns: Updux

Properties

coduxes

coduxes: Dux[]


groomMutations

groomMutations: function

Type declaration:

▸ (mutation: MutationS): MutationS

Parameters:

Name Type
mutation MutationS

subduxes

subduxes: DictionaryDux

Accessors

_middlewareEntries

get _middlewareEntries(): any[]

Returns: any[]


actions

get actions(): DuxActionsA, C

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, that key won't be present in the produced action).

The same action creator can be included in multiple subduxes. However, if two different creators are included for the same action, an error will be thrown.

example

const actions = updux.actions;

Returns: DuxActionsA, C


asDux

get asDux(): object

Returns a ducks-like plain object holding the reducer from the Updux object and all its trimmings.

example

const {
    createStore,
    upreducer,
    subduxes,
    coduxes,
    middleware,
    actions,
    reducer,
    mutations,
    initial,
    selectors,
} = myUpdux.asDux;

Returns: object

  • actions: = this.actions

  • coduxes: object[] = this.coduxes

  • createStore(): function

    • (initial?: S, injectEnhancer?: Function): StoreS & object
  • initial: = this.initial

  • middleware(): function

    • (api: UpduxMiddlewareAPIS, X): function

      • (next: Function): function

        • (action: A): any
  • mutations(): object

  • reducer(): function

    • (state: S | undefined, action: Action): S
  • selectors: = this.selectors

  • subduxes(): object

  • upreducer(): function

    • (action: Action): function

      • (state: S): S

createStore

get createStore(): function

Returns a createStore function that takes two argument: initial and injectEnhancer. initial is a custom initial state for the store, and injectEnhancer is a function taking in the middleware built by the updux object and allowing you to wrap it in any enhancer you want.

example

const createStore = updux.createStore;

const store = createStore(initial);

Returns: function

▸ (initial?: S, injectEnhancer?: Function): StoreS & object

Parameters:

Name Type
initial? S
injectEnhancer? Function

initial

get initial(): AggDuxStateS, C

Returns: AggDuxStateS, C


middleware

get middleware(): UpduxMiddlewareAggDuxStateS, C, DuxSelectorsAggDuxStateS, C, X, C

Array of middlewares aggregating all the effects defined in the updux and its subduxes. Effects of the updux itself are done before the subduxes effects. Note that getState will always return the state of the local updux.

example

const middleware = updux.middleware;

Returns: UpduxMiddlewareAggDuxStateS, C, DuxSelectorsAggDuxStateS, C, X, C


mutations

get mutations(): DictionaryMutationS

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: DictionaryMutationS


reducer

get reducer(): function

A Redux reducer generated using the computed initial state and mutations.

Returns: function

▸ (state: S | undefined, action: Action): S

Parameters:

Name Type
state S | undefined
action Action

selectors

get selectors(): DuxSelectorsAggDuxStateS, C, X, C

A dictionary of the updux's selectors. Subduxes' selectors are included as well (with the mapping to the sub-state already taken care of you).

Returns: DuxSelectorsAggDuxStateS, C, X, C


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

▸ (action: Action): function

Parameters:

Name Type
action Action

▸ (state: S): S

Parameters:

Name Type
state S

upreducer

get upreducer(): UpreducerS

Returns: UpreducerS

Methods

addAction

addAction(theaction: string, transform?: any): ActionCreatorstring, any

Adds an action to the updux. It can take an already defined action creator, or any arguments that can be passed to actionCreator.

example

    const action = updux.addAction( name, ...creatorArgs );
    const action = updux.addAction( otherActionCreator );

example

import {actionCreator, Updux} from 'updux';

const updux = new Updux();

const foo = updux.addAction('foo');
const bar = updux.addAction( 'bar', (x) => ({stuff: x+1}) );

const baz = actionCreator( 'baz' );

foo({ a: 1});  // => { type: 'foo', payload: { a: 1 } }
bar(2);        // => { type: 'bar', payload: { stuff: 3 } }
baz();         // => { type: 'baz', payload: undefined }

Parameters:

Name Type
theaction string
transform? any

Returns: ActionCreatorstring, any

addAction(theaction: string | ActionCreatorany, transform?: undefined): ActionCreatorstring, any

Parameters:

Name Type
theaction string | ActionCreatorany
transform? undefined

Returns: ActionCreatorstring, any


addEffect

addEffect<AC>(creator: AC, middleware: UpduxMiddlewareAggDuxStateS, C, DuxSelectorsAggDuxStateS, C, X, C, ReturnTypeAC, isGenerator?: undefined | false | true): any

Type parameters:

AC: ActionCreator

Parameters:

Name Type
creator AC
middleware UpduxMiddlewareAggDuxStateS, C, DuxSelectorsAggDuxStateS, C, X, C, ReturnTypeAC
isGenerator? undefined | false | true

Returns: any

addEffect(creator: string, middleware: UpduxMiddlewareAggDuxStateS, C, DuxSelectorsAggDuxStateS, C, X, C, isGenerator?: undefined | false | true): any

Parameters:

Name Type
creator string
middleware UpduxMiddlewareAggDuxStateS, C, DuxSelectorsAggDuxStateS, C, X, C
isGenerator? undefined | false | true

Returns: any


addMutation

addMutation<A>(creator: A, mutation: MutationS, ActionTypeA, isSink?: undefined | false | true): any

Adds a mutation and its associated action to the updux.

remarks

If a local mutation was already associated to the action, it will be replaced by the new one.

example

updux.addMutation(
    action('ADD', payload<int>() ),
    inc => state => state + in
);

Type parameters:

A: ActionCreator

Parameters:

Name Type Description
creator A -
mutation MutationS, ActionTypeA -
isSink? undefined | false | true If true, disables the subduxes mutations for this action. To conditionally run the subduxes mutations, check out subduxUpreducer. Defaults to false.

Returns: any

addMutation<A>(creator: string, mutation: MutationS, any, isSink?: undefined | false | true): any

Type parameters:

A: ActionCreator

Parameters:

Name Type
creator string
mutation MutationS, any
isSink? undefined | false | true

Returns: any


addSelector

addSelector(name: string, selector: Selector): void

Parameters:

Name Type
name string
selector Selector

Returns: void