r-magnet
Version:
A state management library for React
110 lines (109 loc) • 5.21 kB
TypeScript
export interface Action {
type: any;
}
export interface AnyAction extends Action {
[extraProps: string]: any;
}
export declare type Reducer<S = any, A extends Action = AnyAction> = (state: S, action: A) => S;
export declare type ReducerMetods<State> = {
[K: string]: Reducer<State, PayloadAction<any>>;
};
export declare type ValidateReducers<S, ACR extends ReducerMetods<S>> = ACR & {
[T in keyof ACR]: ACR[T] extends {
reducer(s: S, action?: infer A): any;
} ? {
prepare(...a: never[]): Omit<A, 'type'>;
} : {};
};
export declare type EffectHandlers = {
[K: string]: EffectHandler<PayloadAction<any>>;
};
export declare type ValidateEffects<ACR extends EffectHandlers> = ACR & {
[T in keyof ACR]: ACR[T] extends {
handler(action?: infer A, put?: (key: string, data: any) => void, getState?: () => any, dispatch?: (action: AnyAction) => void): void;
} ? {
prepare(...a: never[]): Omit<A, 'type'>;
} : {};
};
export declare type PayloadAction<P = void, T extends string = string> = {
payload: P;
type: T;
};
export interface ActionCreatorWithoutPayload<T extends string = string> {
/**
* Calling this {@link redux#ActionCreator} will
* return a {@link PayloadAction} of type `T` with a payload of `undefined`
*/
(): PayloadAction<undefined, T>;
}
export interface ActionCreatorWithPayload<P, T extends string = string> {
/**
* Calling this {@link redux#ActionCreator} with an argument will
* return a {@link PayloadAction} of type `T` with a payload of `P`
*/
(payload: P): PayloadAction<P, T>;
}
declare type ActionCreatorForReducer<R> = R extends (state: any, action: infer Action) => any ? Action extends {
payload: infer P;
} ? ActionCreatorWithPayload<P> : ActionCreatorWithoutPayload : ActionCreatorWithoutPayload;
export declare type ReducerActions<Reducers extends ReducerMetods<any>> = {
[Type in keyof Reducers]: ActionCreatorForReducer<Reducers[Type]>;
};
export declare type EfffectOptioons = {
[key: string]: EffectHandler;
};
export interface SliceOptions<State = unknown, R extends ReducerMetods<State> = ReducerMetods<State>, M extends EffectHandlers = EffectHandlers, Name extends string = string> {
name: Name;
initialState: State;
reducers: ValidateReducers<State, R>;
effects?: ValidateEffects<M>;
}
export interface Slice<State = unknown, R extends ReducerMetods<State> = ReducerMetods<State>, M extends EffectHandlers = EffectHandlers, Name extends string = string> {
name: Name;
actions: ReducerActions<R> & EffectActions<M>;
reducer: Reducer<State, AnyAction>;
}
export declare type EffectHandler<A extends Action = AnyAction> = (action: A, put: (key: string, data: any) => void, getState: () => any, dispatch: (action: AnyAction) => void) => void;
export declare type EffectHandler2<A extends Action = AnyAction> = (action: A, getState: () => any, dispatch: (action: AnyAction) => void) => void;
declare type ActionCreatorForEffect<R> = R extends (action: infer Action, put: (key: string, data: any) => void, getState: () => any, dispatch: (action: AnyAction) => void) => any ? Action extends {
payload: infer P;
} ? ActionCreatorWithPayload<P> : ActionCreatorWithoutPayload : ActionCreatorWithoutPayload;
export declare type EffectActions<Reducers extends EffectHandlers> = {
[Type in keyof Reducers]: ActionCreatorForEffect<Reducers[Type]>;
};
export declare type ReducersMapObject<S = any, A extends Action = Action> = {
[K in keyof S]: Reducer<S[K], A>;
};
declare const $CombinedState: unique symbol;
interface EmptyObject {
readonly [$CombinedState]?: undefined;
}
export declare type CombinedState<S> = EmptyObject & S;
export declare type ActionFn = (...args: any[]) => Action;
export declare type ActionParam = ActionFn | ActionFn[];
export interface TypedUseSelectorByActionsHook<TState> {
<TSelected>(actions: ActionParam, selector: (state: TState) => TSelected, equalityFn?: (left: TSelected, right: TSelected) => boolean): TSelected;
}
export declare type Store<T extends Object = any> = {
dispatch(action: AnyAction): void;
getState: () => T;
createAction<P = void, T extends string = string>(type: string): ActionCreatorWithoutPayload<T> | ActionCreatorWithPayload<P, T>;
createReducer<State, CR extends ReducerMetods<State>, M extends EffectHandlers, Name extends string = string>(options: SliceOptions<State, CR, M, Name>): Slice<State, CR, M, Name>;
on(...actions: ActionFn[]): {
debounce(milliseconds: number): {
effect(handlerFn: EffectHandler2): {
unsubscribe: () => void;
};
};
effect(handlerFn: EffectHandler2): {
unsubscribe: () => void;
};
};
createEffect(actions: ActionParam, handlerFn: EffectHandler2): {
unsubscribe: () => void;
};
subscribe(fn: (state: any, action: AnyAction) => void): () => void;
addReducer<T>(name: string, reduce: (state: T, action: AnyAction) => T, initialState: T): void;
[key: string]: any;
};
export {};