20 KiB
updux - v1.2.0
Index
Classes
Type aliases
- Action
- ActionPair
- ActionPayloadGenerator
- ActionsOf
- CoduxesOf
- Dictionary
- Dux
- DuxActions
- DuxActionsCoduxes
- DuxActionsSubduxes
- DuxSelectors
- DuxState
- DuxStateCoduxes
- DuxStateGlobSub
- DuxStateSubduxes
- Effect
- GenericActions
- ItemsOf
- LocalDuxState
- MaybePayload
- MaybeReturnType
- Merge
- Mutation
- MutationEntry
- MwGen
- Next
- RebaseSelector
- Selector
- SelectorsOf
- StateOf
- StoreWithDispatchActions
- SubMutations
- Submws
- UnionToIntersection
- UpduxActions
- UpduxConfig
- UpduxLocalActions
- UpduxMiddleware
- Upreducer
Variables
Functions
- MiddlewareFor
- buildActions
- buildCreateStore
- buildInitial
- buildMiddleware
- buildMutations
- buildSelectors
- buildUpreducer
- coduxes
- composeMutations
- composeMw
- dux
- effectToMw
- sliceMw
- subMiddleware
- subSelectors
Type aliases
Action
Ƭ Action: object & MaybePayload‹P›
ActionPair
Ƭ ActionPair: [string, ActionCreator]
ActionPayloadGenerator
Ƭ ActionPayloadGenerator: function
Type declaration:
▸ (...args
: any[]): any
Parameters:
Name | Type |
---|---|
...args |
any[] |
ActionsOf
Ƭ ActionsOf: U extends Updux ? U["actions"] : object
CoduxesOf
Ƭ CoduxesOf: U extends Updux<any, any, any, infer S> ? S : []
Dictionary
Ƭ Dictionary: object
Type declaration:
-
**key**: *string*
Dux
Ƭ Dux: object
Type declaration:
-
actions: A
-
coduxes: Dux[]
-
initial: AggDuxState‹S, C›
-
subduxes: Dictionary‹Dux›
DuxActions
Ƭ DuxActions:
DuxActionsCoduxes
Ƭ DuxActionsCoduxes: C extends Array ? UnionToIntersection<ActionsOf> : object
DuxActionsSubduxes
Ƭ DuxActionsSubduxes: C extends object ? ActionsOf<C[keyof C]> : unknown
DuxSelectors
Ƭ DuxSelectors: unknown extends X ? object : X
DuxState
Ƭ DuxState: D extends object ? S : unknown
DuxStateCoduxes
Ƭ DuxStateCoduxes: C extends Array ? UnionToIntersection<StateOf> : unknown
DuxStateGlobSub
Ƭ DuxStateGlobSub: S extends object ? StateOf : unknown
DuxStateSubduxes
Ƭ DuxStateSubduxes: C extends object ? object : C extends object ? object : unknown
Effect
Ƭ Effect: [string, UpduxMiddleware | MwGen, undefined | false | true]
GenericActions
Ƭ GenericActions: Dictionary‹ActionCreator‹string, function››
ItemsOf
Ƭ ItemsOf: C extends object ? C[keyof C] : unknown
LocalDuxState
Ƭ LocalDuxState: S extends never[] ? unknown[] : S
MaybePayload
Ƭ MaybePayload: P extends object | string | boolean | number ? object : object
MaybeReturnType
Ƭ MaybeReturnType: X extends function ? ReturnType : unknown
Merge
Ƭ Merge: UnionToIntersection‹T[keyof T]›
Mutation
Ƭ Mutation: function
Type declaration:
▸ (payload
: A["payload"], action
: A): function
Parameters:
Name | Type |
---|---|
payload |
A["payload"] |
action |
A |
▸ (state
: S): S
Parameters:
Name | Type |
---|---|
state |
S |
MutationEntry
Ƭ MutationEntry: [ActionCreator | string, Mutation‹any, Action‹string, any››, undefined | false | true]
MwGen
Ƭ MwGen: function
Type declaration:
▸ (): UpduxMiddleware
Next
Ƭ Next: function
Type declaration:
▸ (action
: Action): any
Parameters:
Name | Type |
---|---|
action |
Action |
RebaseSelector
Ƭ RebaseSelector: object
Type declaration:
Selector
Ƭ Selector: function
Type declaration:
▸ (state
: S): unknown
Parameters:
Name | Type |
---|---|
state |
S |
SelectorsOf
Ƭ SelectorsOf: C extends object ? S : unknown
StateOf
Ƭ StateOf: D extends object ? I : unknown
StoreWithDispatchActions
Ƭ StoreWithDispatchActions: Store‹S› & object
SubMutations
Ƭ SubMutations: object
Type declaration:
-
**slice**: *string*
Submws
Ƭ Submws: Dictionary‹UpduxMiddleware›
UnionToIntersection
Ƭ UnionToIntersection: U extends any ? function : never extends function ? I : never
UpduxActions
Ƭ UpduxActions: U extends Updux ? UnionToIntersection<UpduxLocalActions | ActionsOf<CoduxesOf[keyof CoduxesOf]>> : object
UpduxConfig
Ƭ UpduxConfig: Partial‹object›
Configuration object given to Updux's constructor.
arguments
initial
Default initial state of the reducer. If applicable, is merged with the subduxes initial states, with the parent having precedence.
If not provided, defaults to an empty object.
actions
Actions used by the updux.
import { dux } from 'updux';
import { action, payload } from 'ts-action';
const bar = action('BAR', payload<int>());
const foo = action('FOO');
const myDux = dux({
actions: {
bar
},
mutations: [
[ foo, () => state => state ]
]
});
myDux.actions.foo({ x: 1, y: 2 }); // => { type: foo, x:1, y:2 }
myDux.actions.bar(2); // => { type: bar, payload: 2 }
New actions used directly in mutations and effects will be added to the
dux actions -- that is, they will be accessible via dux.actions
-- but will
not appear as part of its Typescript type.
selectors
Dictionary of selectors for the current updux. The updux also inherit its subduxes' selectors.
The selectors are available via the class' getter.
mutations
mutations: [
[ action, mutation, isSink ],
...
]
or
mutations: {
action: mutation,
...
}
List of mutations for assign to the dux. If you want Typescript goodness, you
probably want to use addMutation()
instead.
In its generic array-of-array form, each mutation tuple contains: the action, the mutation, and boolean indicating if this is a sink mutation.
The action can be an action creator function or a string. If it's a string, it's considered to be the
action type and a generic action( actionName, payload() )
creator will be
generated for it. If an action is not already defined in the actions
parameter, it'll be automatically added.
The pseudo-action type *
can be used to match any action not explicitly matched by other mutations.
const todosUpdux = updux({
mutations: {
add: todo => state => [ ...state, todo ],
done: done_id => u.map( u.if( ({id} => id === done_id), {done: true} ) ),
'*' (payload,action) => state => {
console.warn( "unexpected action ", action.type );
return state;
},
}
});
The signature of the mutations is (payload,action) => state => newState
.
It is designed to play well with Updeep
(and Immer). This way, instead of doing
mutation: {
renameTodo: newName => state => { ...state, name: newName }
}
we can do
mutation: {
renameTodo: newName => u({ name: newName })
}
The final argument is the optional boolean isSink
. If it is true, it'll
prevent subduxes' mutations on the same action. It defaults to false
.
The object version of the argument can be used as a shortcut when all actions
are strings. In that case, isSink
is false
for all mutations.
groomMutations
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);
}
});
subduxes
Object mapping slices of the state to sub-upduxes. In addition to creating sub-reducers for those slices, it'll make the parend updux inherit all the actions and middleware from its subduxes.
For example, if in plain Redux you would do
import { combineReducers } from 'redux';
import todosReducer from './todos';
import statisticsReducer from './statistics';
const rootReducer = combineReducers({
todos: todosReducer,
stats: statisticsReducer,
});
then with Updux you'd do
import { updux } from 'updux';
import todos from './todos';
import statistics from './statistics';
const rootUpdux = updux({
subduxes: {
todos,
statistics
}
});
effects
Array of arrays or plain object defining asynchronous actions and side-effects triggered by actions.
The effects themselves are Redux middleware, with the dispatch
property of the first argument augmented with all the available actions.
updux({
effects: {
fetch: ({dispatch}) => next => async (action) => {
next(action);
let result = await fetch(action.payload.url).then( result => result.json() );
dispatch.fetchSuccess(result);
}
}
});
example
import Updux from 'updux';
import { actions, payload } from 'ts-action';
import u from 'updeep';
const todoUpdux = new Updux({
initial: {
done: false,
note: "",
},
actions: {
finish: action('FINISH', payload()),
edit: action('EDIT', payload()),
},
mutations: [
[ edit, note => u({note}) ]
],
selectors: {
getNote: state => state.note
},
groomMutations: mutation => transform(mutation),
subduxes: {
foo
},
effects: {
finish: () => next => action => {
console.log( "Woo! one more bites the dust" );
}
}
})
UpduxLocalActions
Ƭ UpduxLocalActions: S extends Updux<any, null> ? object : S extends Updux<any, infer A> ? A : object
UpduxMiddleware
Ƭ UpduxMiddleware: function
Type declaration:
▸ (api
: UpduxMiddlewareAPI‹S, X›): function
Parameters:
Name | Type |
---|---|
api |
UpduxMiddlewareAPI‹S, X› |
▸ (next
: Function): function
Parameters:
Name | Type |
---|---|
next |
Function |
▸ (action
: A): any
Parameters:
Name | Type |
---|---|
action |
A |
Upreducer
Ƭ Upreducer: function
Type declaration:
▸ (action
: Action): function
Parameters:
Name | Type |
---|---|
action |
Action |
▸ (state
: S): S
Parameters:
Name | Type |
---|---|
state |
S |
Variables
Const
subEffects
• subEffects: Effect = [ '*', subMiddleware ] as any
Const
updux
• updux: Updux‹unknown, null, unknown, object› = new Updux({ subduxes: { foo: dux({ initial: "banana" }) } })
Functions
Const
MiddlewareFor
▸ MiddlewareFor(type
: any, mw
: Middleware): Middleware
Parameters:
Name | Type |
---|---|
type |
any |
mw |
Middleware |
Returns: Middleware
buildActions
▸ buildActions(actions
: ActionPair[]): Dictionary‹ActionCreator‹string, function››
Parameters:
Name | Type | Default |
---|---|---|
actions |
ActionPair[] | [] |
Returns: Dictionary‹ActionCreator‹string, function››
buildCreateStore
▸ buildCreateStore<S, A>(reducer
: Reducer‹S›, middleware
: Middleware, actions
: A): function
Type parameters:
▪ S
▪ A
Parameters:
Name | Type | Default |
---|---|---|
reducer |
Reducer‹S› | - |
middleware |
Middleware | - |
actions |
A | {} as A |
Returns: function
▸ (initial?
: S, injectEnhancer?
: Function): Store‹S› & object
Parameters:
Name | Type |
---|---|
initial? |
S |
injectEnhancer? |
Function |
buildInitial
▸ buildInitial(initial
: any, coduxes
: any, subduxes
: any): any
Parameters:
Name | Type | Default |
---|---|---|
initial |
any | - |
coduxes |
any | [] |
subduxes |
any | {} |
Returns: any
buildMiddleware
▸ buildMiddleware<S>(local
: UpduxMiddleware[], co
: UpduxMiddleware[], sub
: Submws): UpduxMiddleware‹S›
Type parameters:
▪ S
Parameters:
Name | Type | Default |
---|---|---|
local |
UpduxMiddleware[] | [] |
co |
UpduxMiddleware[] | [] |
sub |
Submws | {} |
Returns: UpduxMiddleware‹S›
buildMutations
▸ buildMutations(mutations
: Dictionary‹Mutation | [Mutation, boolean | undefined]›, subduxes
: object, coduxes
: Upreducer[]): object
Parameters:
Name | Type | Default |
---|---|---|
mutations |
Dictionary‹Mutation | [Mutation, boolean | undefined]› | {} |
subduxes |
object | {} |
coduxes |
Upreducer[] | [] |
Returns: object
buildSelectors
▸ buildSelectors(localSelectors
: Dictionary‹Selector›, coduxes
: Dictionary‹Selector›[], subduxes
: Dictionary‹Selector›): object
Parameters:
Name | Type | Default |
---|---|---|
localSelectors |
Dictionary‹Selector› | {} |
coduxes |
Dictionary‹Selector›[] | [] |
subduxes |
Dictionary‹Selector› | {} |
Returns: object
buildUpreducer
▸ buildUpreducer<S>(initial
: S, mutations
: Dictionary‹Mutation‹S››): Upreducer‹S›
Type parameters:
▪ S
Parameters:
Name | Type |
---|---|
initial |
S |
mutations |
Dictionary‹Mutation‹S›› |
Returns: Upreducer‹S›
Const
coduxes
▸ coduxes<C, U>(...coduxes
: U): object
Type parameters:
▪ C: Dux
▪ U: [C]
Parameters:
Name | Type |
---|---|
...coduxes |
U |
Returns: object
- coduxes: U
Const
composeMutations
▸ composeMutations(mutations
: Mutation[]): function | (Anonymous function)
Parameters:
Name | Type |
---|---|
mutations |
Mutation[] |
Returns: function | (Anonymous function)
Const
composeMw
▸ composeMw(mws
: UpduxMiddleware[]): (Anonymous function)
Parameters:
Name | Type |
---|---|
mws |
UpduxMiddleware[] |
Returns: (Anonymous function)
Const
dux
▸ dux<S, A, X, C>(config
: C): object
Type parameters:
▪ S
▪ A
▪ X
▪ C: UpduxConfig
Parameters:
Name | Type |
---|---|
config |
C |
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
- (
-
Const
effectToMw
▸ effectToMw(effect
: Effect, actions
: Dictionary‹ActionCreator›, selectors
: Dictionary‹Selector›): subMiddleware | augmented
Parameters:
Name | Type |
---|---|
effect |
Effect |
actions |
Dictionary‹ActionCreator› |
selectors |
Dictionary‹Selector› |
Returns: subMiddleware | augmented
sliceMw
▸ sliceMw(slice
: string, mw
: UpduxMiddleware): UpduxMiddleware
Parameters:
Name | Type |
---|---|
slice |
string |
mw |
UpduxMiddleware |
Returns: UpduxMiddleware
Const
subMiddleware
▸ subMiddleware(): (Anonymous function)
Returns: (Anonymous function)
subSelectors
▸ subSelectors(__namedParameters
: [string, Function]): [string, Selector][]
Parameters:
Name | Type |
---|---|
__namedParameters |
[string, Function] |
Returns: [string, Selector][]