83 lines
3.9 KiB
TypeScript
83 lines
3.9 KiB
TypeScript
import * as rtk from '@reduxjs/toolkit';
|
|
import { Action, AnyAction, EnhancedStore, PayloadAction } from '@reduxjs/toolkit';
|
|
import { AugmentedMiddlewareAPI, DuxActions, DuxConfig, DuxReaction, DuxSelectors, DuxState, Mutation } from './types.js';
|
|
import { EffectMiddleware } from './effects.js';
|
|
type CreateStoreOptions<D> = Partial<{
|
|
preloadedState: DuxState<D>;
|
|
}>;
|
|
interface ActionCreator<T extends string, P, A extends Array<any>> {
|
|
type: T;
|
|
match: (action: Action<string>) => action is PayloadAction<P, T, never, never>;
|
|
(...args: A): PayloadAction<P, T, never, never>;
|
|
}
|
|
/**
|
|
* @description main Updux class
|
|
*/
|
|
export default class Updux<D extends DuxConfig> {
|
|
#private;
|
|
private readonly duxConfig;
|
|
constructor(duxConfig: D);
|
|
/**
|
|
* @description Initial state of the dux.
|
|
*/
|
|
get initialState(): DuxState<D>;
|
|
get subduxes(): {};
|
|
get actions(): DuxActions<D>;
|
|
createStore(options?: CreateStoreOptions<D>): EnhancedStore<DuxState<D>> & AugmentedMiddlewareAPI<D>;
|
|
addAction(action: ActionCreator<any, any, any>): void;
|
|
addSelector<R, N extends string>(name: N, selector: (state: DuxState<D>) => R): Updux<D & {
|
|
selectors: Record<N, (state: DuxState<D>) => R>;
|
|
}>;
|
|
addMutation<A extends keyof DuxActions<D>>(actionType: A, mutation: Mutation<DuxActions<D>[A] extends (...args: any[]) => infer R ? R : AnyAction, DuxState<D>>, terminal?: boolean): Updux<D>;
|
|
addMutation<AC extends rtk.ActionCreatorWithPreparedPayload<any, any, string, never, never>>(actionCreator: AC, mutation: Mutation<ReturnType<AC>, DuxState<D>>, terminal?: boolean): Updux<D & {
|
|
actions: Record<AC extends {
|
|
type: infer T;
|
|
} ? T : never, AC>;
|
|
}>;
|
|
addMutation<A extends string, P, X extends Array<any>>(actionCreator: ActionCreator<A, P, X>, mutation: Mutation<ReturnType<ActionCreator<A, P, X>>, DuxState<D>>, terminal?: boolean): Updux<D & {
|
|
actions: Record<A, ActionCreator<A, P, X>>;
|
|
}>;
|
|
addMutation(matcher: (action: AnyAction) => boolean, mutation: Mutation<AnyAction, DuxState<D>>, terminal?: boolean): Updux<D>;
|
|
setDefaultMutation(mutation: Mutation<any, DuxState<D>>, terminal?: boolean): Updux<D>;
|
|
get reducer(): any;
|
|
get selectors(): DuxSelectors<D>;
|
|
addEffect<A extends keyof DuxActions<D>>(actionType: A, effect: EffectMiddleware<D, DuxActions<D>[A] extends (...args: any[]) => infer R ? R : AnyAction>): Updux<D>;
|
|
addEffect<AC extends rtk.ActionCreatorWithPreparedPayload<any, any, string, never, never>>(actionCreator: AC, effect: EffectMiddleware<D, ReturnType<AC>>): Updux<D & {
|
|
actions: Record<AC extends {
|
|
type: infer T;
|
|
} ? T : never, AC>;
|
|
}>;
|
|
addEffect(guardFunc: (action: AnyAction) => boolean, effect: EffectMiddleware<D>): Updux<D>;
|
|
addEffect(effect: EffectMiddleware<D>): Updux<D>;
|
|
get effects(): any[];
|
|
get upreducer(): (action: AnyAction) => (state?: DuxState<D>) => DuxState<D>;
|
|
addReaction(reaction: DuxReaction<D>): this;
|
|
get reactions(): any;
|
|
get defaultMutation(): {
|
|
terminal: boolean;
|
|
mutation: Mutation<any, (D extends {
|
|
initialState: any;
|
|
} ? D["initialState"] : {}) & (D extends {
|
|
subduxes: any;
|
|
} ? import("./types.js").SubduxesState<D> : unknown) extends infer T ? T extends (D extends {
|
|
initialState: any;
|
|
} ? D["initialState"] : {}) & (D extends {
|
|
subduxes: any;
|
|
} ? import("./types.js").SubduxesState<D> : unknown) ? T extends {
|
|
[key: string]: any;
|
|
} ? { [key in keyof T]: T[key]; } : T : never : never>;
|
|
};
|
|
/**
|
|
* @description Returns an object holding the Updux reducer and all its
|
|
* paraphernalia.
|
|
*/
|
|
get asDux(): {
|
|
initialState: DuxState<D>;
|
|
actions: DuxActions<D>;
|
|
reducer: (state: DuxState<D>, action: AnyAction) => DuxState<D>;
|
|
effects: EffectMiddleware<D>[];
|
|
reactions: DuxReaction<D>[];
|
|
};
|
|
}
|
|
export {};
|