11 KiB
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
- _middlewareEntries
- actions
- asDux
- createStore
- initial
- middleware
- mutations
- reducer
- selectors
- subduxUpreducer
- upreducer
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
: Mutation‹S›): Mutation‹S›
Parameters:
Name | Type |
---|---|
mutation |
Mutation‹S› |
subduxes
• subduxes: Dictionary‹Dux›
Accessors
_middlewareEntries
• get _middlewareEntries(): any[]
Returns: any[]
actions
• get actions(): DuxActions‹A, 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: DuxActions‹A, 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): Store‹S› & object
- (
-
initial: = this.initial
-
middleware(): function
-
(
api
: UpduxMiddlewareAPI‹S, 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): Store‹S› & object
Parameters:
Name | Type |
---|---|
initial? |
S |
injectEnhancer? |
Function |
initial
• get initial(): AggDuxState‹S, C›
Returns: AggDuxState‹S, C›
middleware
• get middleware(): UpduxMiddleware‹AggDuxState‹S, C›, DuxSelectors‹AggDuxState‹S, 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: UpduxMiddleware‹AggDuxState‹S, C›, DuxSelectors‹AggDuxState‹S, C›, X, C››
mutations
• get mutations(): Dictionary‹Mutation‹S››
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:
Name | Type |
---|---|
state |
S | undefined |
action |
Action |
selectors
• get selectors(): DuxSelectors‹AggDuxState‹S, 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: DuxSelectors‹AggDuxState‹S, 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(): Upreducer‹S›
Returns: Upreducer‹S›
Methods
addAction
▸ addAction(theaction
: string, transform?
: any): ActionCreator‹string, 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: ActionCreator‹string, any›
▸ addAction(theaction
: string | ActionCreator‹any›, transform?
: undefined): ActionCreator‹string, any›
Parameters:
Name | Type |
---|---|
theaction |
string | ActionCreator‹any› |
transform? |
undefined |
Returns: ActionCreator‹string, any›
addEffect
▸ addEffect<AC>(creator
: AC, middleware
: UpduxMiddleware‹AggDuxState‹S, C›, DuxSelectors‹AggDuxState‹S, C›, X, C›, ReturnType‹AC››, isGenerator?
: undefined | false | true): any
Type parameters:
▪ AC: ActionCreator
Parameters:
Name | Type |
---|---|
creator |
AC |
middleware |
UpduxMiddleware‹AggDuxState‹S, C›, DuxSelectors‹AggDuxState‹S, C›, X, C›, ReturnType‹AC›› |
isGenerator? |
undefined | false | true |
Returns: any
▸ addEffect(creator
: string, middleware
: UpduxMiddleware‹AggDuxState‹S, C›, DuxSelectors‹AggDuxState‹S, C›, X, C››, isGenerator?
: undefined | false | true): any
Parameters:
Name | Type |
---|---|
creator |
string |
middleware |
UpduxMiddleware‹AggDuxState‹S, C›, DuxSelectors‹AggDuxState‹S, C›, X, C›› |
isGenerator? |
undefined | false | true |
Returns: any
addMutation
▸ addMutation<A>(creator
: A, mutation
: Mutation‹S, ActionType‹A››, 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 |
Mutation‹S, ActionType‹A›› | - |
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
: Mutation‹S, any›, isSink?
: undefined | false | true): any
Type parameters:
▪ A: ActionCreator
Parameters:
Name | Type |
---|---|
creator |
string |
mutation |
Mutation‹S, any› |
isSink? |
undefined | false | true |
Returns: any
addSelector
▸ addSelector(name
: string, selector
: Selector): void
Parameters:
Name | Type |
---|---|
name |
string |
selector |
Selector |
Returns: void