updux/types/index.d.ts

100 lines
2.2 KiB
TypeScript
Raw Normal View History

2021-10-12 18:49:08 +00:00
type Dict<T> = Record<string, T>;
2021-10-12 17:11:32 +00:00
2021-10-12 22:13:59 +00:00
type Mutation<TState = unknown> = (
payload: unknown,
action: Record<string, unknown>
) => (state: TState) => TState;
2021-10-14 16:30:06 +00:00
export * from './actions';
2021-10-12 22:13:59 +00:00
2021-10-14 16:30:06 +00:00
/**
* Configuration object typically passed to the constructor of the class Updux.
*/
export interface UpduxConfig<TState = unknown> {
2021-10-12 18:49:08 +00:00
/**
2021-10-14 16:30:06 +00:00
* Local initial state.
* @default {}
2021-10-12 18:49:08 +00:00
*/
2021-10-14 16:30:06 +00:00
initial?: TState;
2021-10-13 17:54:17 +00:00
/**
2021-10-14 16:30:06 +00:00
* Subduxes to be merged to this dux.
2021-10-13 17:54:17 +00:00
*/
2021-10-14 16:30:06 +00:00
subduxes?: Dict<Updux | UpduxConfig>;
/**
* Local actions.
*/
actions?: Record<string, any>;
/**
* Local selectors.
*/
selectors?: Record<string, Function>;
/**
* Local mutations
*/
mutations?: Record<string, Function>;
/**
* Selectors to apply to the mapped subduxes. Only
* applicable if the dux is a mapping dux.
*/
mappedSelectors?: Record<string, Function>;
/**
* Local effects.
*/
effects?: Record<string, Function>;
/**
* Local reactions.
*/
reactions?: Record<string, Function>;
/**
* If true, enables mapped reactions. Additionally, it can be
* a reaction function, which will treated as a regular
* reaction for the mapped dux.
*/
mappedReaction?: Function | boolean;
}
export class Updux<TState = unknown> {
constructor(config: Partial<UpduxConfig<TState>>);
get initial(): TState;
get selectors(): unknown;
/**
* Sets the local mutation for the given action.
*
* The action can be specified via its type
* or its generator.
*
* Returns the same mutation function.
*/
setMutation<TMutation extends Mutation>(
2021-10-13 17:54:17 +00:00
actionType: string,
2021-10-14 16:30:06 +00:00
mutation: TMutation
): TMutation;
setMutation<TMutation extends Mutation>(
action: ActionGenerator,
mutation: TMutation
): TMutation;
/**
* Registers the action for the dux.
* If no payload function is provided, whatever is
* given as an argument to the action generator will
* be set as-is in the action's payload.
*/
setAction(actionType: string, payloadFunc?: Function);
/**
* Registers a selector for the dux.
*/
setSelector(name: string, selector: Selector);
2021-10-12 17:11:32 +00:00
}
2021-10-14 16:30:06 +00:00