UNPKG

module-reaction

Version:

modulized redux store management framework, based on react-redux

91 lines (90 loc) 5.25 kB
import React from 'react'; import { Store } from 'redux'; export declare const MODULE_COMMON = "reaction_module_common"; export interface KV { [k: string]: any; } export interface ModuleStore extends KV { module: string; } export interface ModuleAction<PAYLOAD_TYPE = any, MODULE_STORE = ModuleStore, PROCEED_RESULT = KV> { /** the relative module this action will modify */ module: string; /** the action's name, by default will be 'moduleAction'*/ name?: string; /** the max time(by seconds) allowed the process functiion execute, if timeout, the process will be cancel and return a blank {} */ maxProcessSeconds?: number; /** the business logic processor, normally, you can fetch apis, do sth complex, etc. * when finished your logic, please return the data to modify via k-v like, actually, you can * only modify the specific moduleStore's props which you indicates by the 'module' property * eg. there's a moduleStore holding the user info: * const userStore: ModuleStore = {module:'user', username: '', level: 0}; * then there's an action to fetch userInfo: * const getUserInfoAction: ModuleAction = { * module: 'user', * process: async (payload: KV, moduleStore: any) => { * const res = await someFetchMethod(payload.username, payload.password); * // when got the user info by server, return infos you wanna modify * return { level: res.level} * } * } * * PS: in process function, you can call 'getGlobalState ro getModuleState or getModuleProp' to get * global/other module's store prop */ process?: (payload: PAYLOAD_TYPE, moduleStore: MODULE_STORE) => Promise<PROCEED_RESULT>; } interface ReactionDb { store: Store; showLoading: (loadingTag?: string) => void; hideLoading: (loadingTag?: string) => void; defaultMaxProcessSeconds: number; } export declare const reaction: ReactionDb; /** * execute an action * specially ,if the first param was given a moduleName string, meanwile the payload is k-v data, * the freamework will simply merge the payload to the moduleStore of the moduleName * @param moduleAction action instance or moduleName * @param payload this data will be passed to action's process method,typically , it's a k-v data, eg: {a: 1, b: 'xx'}. if you give a simple metadata type (such as string, number, boolean...), the moduleAction must has a process method to deal with it * @param loading whether call showloading when execute this action */ export declare function doAction<P = KV>(moduleAction: ModuleAction<any, any, any> | string, payload?: P, loadingTag?: string | 'none'): void; export declare function doFunction(fn: () => Promise<any>, payload?: KV): void; /** * insert an action at the certain pos closely after the current * action's process be finished * @access this method can only be called inside a action's process!! */ export declare function plusAction<P = KV>(moduleAction: ModuleAction<any, any, any> | string, payload?: P, loadingTag?: string | 'none'): void; /** * the decorator used to inject moduleStore's props to Compnent * you can use this decorator several times to inject different moduleStore's props, eg. mapProp(moduleA, 'a','b') mapProp(moduleB, 'c','d') * while, you can not use it twice or more when inject one same moduleStore. in other words, mapProp(moduleA, 'a','b') mapProp(moduleA, 'c', 'd') will course error, instead, use mapProp(moduleA, 'a','b','c','d') to inject all props you need in one call * * @param moduleStore the moduleStore or the moduleName you wanna inject from. * here receive two types of para * 1. string, indicates the moduleName, make sure you have called the 'regStore' mannaully before * 2. moduleStore instance, then in this mapProp function, will call 'regStore' automaticly by using the [copy] of the moduleStore given only if moduleStore.module not registered before!!(so one module will not be registered twice or more) * @param props these propNames of the moduleStore you wanna inject, * here're two syntax sugars: * 1.if you want to inject all the props of the moduleStore, you can bypass the props param(feel free to let this param blank) * 2.you can rename props by use a ':', eg. mapProp(moduleA, 'a','b:bbb'), then in the Component, this.props.bbb refers to the moduleA.b 's value * */ export declare function mapProp(module: ModuleStore | string, ...props: string[]): Function; /** * reg a moduleStore to global redux's store mannally * note: when you call this method mannally, it will replace the original data if there's already registed a moduleStore with the same module name * @param moduleStore the moduleStore to reg */ export declare function regStore(moduleStore: ModuleStore): void; /** * enable redux_devtools */ export declare function enableDevtools(): void; export declare function getGlobalState(useClone?: boolean): any; export declare function getModuleState<T>(moduleName: string, useClone?: boolean): T | any; export declare function getModuleProp(moduleName: string, propName: string, useClone?: boolean): any; export declare const Provider: React.FC; export {};