refactor🎨: (阅读代码):redux.ts增加注释

master
yetao 2 weeks ago
parent 8c3f169824
commit 21adc57d48

@ -1,70 +1,201 @@
// 导入 React 库,用于构建组件
import * as React from 'react';
// 导入 react-redux 库中的 connect 函数,用于连接 React 组件和 Redux 存储
import { connect as realConnect } from 'react-redux';
// 导入 redux 库中的 combineReducers、createStore 和 Dispatch 函数,用于管理 Redux 状态
import { combineReducers, createStore, Dispatch } from 'redux';
// 从 './data' 模块中导入所有内容,可能包括 reducer、action 等
import * as data from './data';
// 从 './view' 模块中导入所有内容,可能包括 reducer、action 等
import * as view from './view';
// 从 './vscode' 模块中导入 API 对象,用于与 VSCode 环境交互
import { API } from './vscode';
// 使用 redux 的 combineReducers 函数将多个 reducer 合并为一个
const reducers = combineReducers({
// 引入 data 模块的 reducer用于处理数据相关的状态
data: data.reducer,
// 引入 view 模块的 reducer用于处理视图相关的状态
view: view.reducer,
});
/**
*
* @interface State
* @property {data.IState} data -
* @property {view.IState} view -
*/
export interface State {
// 数据相关的状态
data: data.IState;
// 视图相关的状态
view: view.IState;
}
/**
* action
* @type Action
* @property {data.actions.Action} data - action
* @property {view.actions.Action} view - action
*/
export type Action = data.actions.Action | view.actions.Action;
/**
*
* @const DEFAULT_STATE
* @type {State}
* @property {data.DEFAULT_STATE} data -
* @property {view.DEFAULT_STATE} view -
*/
export const DEFAULT_STATE: State = {
// 数据相关的默认状态
data: data.DEFAULT_STATE,
// 视图相关的默认状态
view: view.DEFAULT_STATE,
};
/**
* Redux store
* @const STORE
* @type {Store<State, Action>}
* @param {Reducer<State, Action>} reducers - reducer
* @param {State} preloadedState - API 使
* @param {Enhancer} enhancer - store enhancer store
*/
export const STORE = createStore(reducers, API.getState() || DEFAULT_STATE, undefined);
/**
* data store
* @function initStore
* @param {Store<State, Action>} store - store
*/
data.initStore(STORE);
/**
* view store
* @function initStore
* @param {Store<State, Action>} store - store
*/
view.initStore(STORE);
// 保存原始的 dispatch 方法,以便在重写的 dispatch 方法中调用
const oldDispatch = STORE.dispatch.bind(STORE);
// 重写 STORE 的 dispatch 方法
STORE.dispatch = (action) => {
// 打印日志,显示当前正在分发的 action
console.log('STORE.dispatch', action);
// 调用原始的 dispatch 方法,分发 action
oldDispatch(action);
// 返回 action以便在调用 dispatch 方法后可以继续处理 action
return action;
};
// 订阅 STORE 的变化,当 STORE 发生变化时,调用 API.setState 方法将当前状态同步到 VSCode 的 API 中
STORE.subscribe(() => API.setState(STORE.getState()));
// 当导航事件发生时,向 VSCode 发送一个消息,消息类型为 'navigated',内容包含当前的视图状态
API.postMessage({ type: 'navigated', view: STORE.getState().view.view });
// Makes debugging easier (and this is inside our WebView context anyway)
// 将 STORE 挂载到全局 window 对象上,以便在其他地方访问
(window as any).STORE = STORE;
/**
*
* @typeparam C -
* @returns
*/
type GetComponentProps<C> = C extends React.ComponentClass<infer P, any> ? P : (C extends React.FunctionComponent<infer P2> ? P2 : {});
/**
*
* @typeparam C -
* @returns
*/
type GetComponentState<C> = C extends React.ComponentClass<any, infer S> ? S : {};
/**
* T K
* @typeparam T -
* @typeparam K -
* @returns T K
*/
type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
/**
*
* @typeparam C -
* @typeparam P -
* @returns P
*/
type OwnProps<C extends (React.ComponentClass<any> | React.FunctionComponent<any>), P> = Omit<GetComponentProps<C>, keyof P>;
/**
* ConnectReturn connect
* @interface ConnectReturn
* @typeparam C -
*/
interface ConnectReturn<C extends (React.ComponentClass<any> | React.FunctionComponent<any>)> {
/**
* stateToProps
* @template TStateProps, TState
* @param { (state: TState, ownProps: OwnProps<C, TStateProps>) => TStateProps } stateToProps - Redux props
* @returns { React.ComponentClass<Omit<GetComponentProps<C>, keyof TStateProps>, GetComponentState<C>> } - props stateToProps TStateProps
*/
<TStateProps, TState = State>(
stateToProps: (state: TState, ownProps: OwnProps<C, TStateProps>) => TStateProps
): React.ComponentClass<Omit<GetComponentProps<C>, keyof TStateProps>, GetComponentState<C>>;
/**
* stateToProps dispatchToProps
* @template TStateProps, TDispatchProps, TState
* @param { (state: TState, ownProps: Omit<GetComponentProps<C>, keyof (TStateProps & TDispatchProps)>) => TStateProps } stateToProps - Redux props
* @param { (dispatch: Dispatch<Action>, ownProps: Omit<GetComponentProps<C>, keyof TStateProps & TDispatchProps>) => TDispatchProps } dispatchToProps - Redux dispatch props
* @returns { React.ComponentClass<Omit<GetComponentProps<C>, keyof (TStateProps & TDispatchProps)>, GetComponentState<C>> } - props stateToProps dispatchToProps TStateProps TDispatchProps
*/
<TStateProps, TDispatchProps, TState = State>(
stateToProps: (state: TState, ownProps: Omit<GetComponentProps<C>, keyof (TStateProps & TDispatchProps)>) => TStateProps,
dispatchToProps: (dispatch: Dispatch<Action>, ownProps: Omit<GetComponentProps<C>, keyof TStateProps & TDispatchProps>) => TDispatchProps,
): React.ComponentClass<Omit<GetComponentProps<C>, keyof (TStateProps & TDispatchProps)>, GetComponentState<C>>;
}
/**
* React Redux
* @function connect
* @template TComponent -
* @param {TComponent} component -
* @returns {ConnectReturn<TComponent>} - Redux
* @remarks
* Redux
* stateToProps dispatchToPropsstateToProps Redux props
* dispatchToProps Redux dispatch props
* stateToProps dispatchToProps 使 Redux dispatch
*/
export function connect<TComponent extends (React.ComponentClass<any> | React.FunctionComponent<any>)>(component: TComponent): ConnectReturn<TComponent> {
return (stateToProps: any, dispatchToProps?: any) => realConnect(stateToProps, dispatchToProps)(component) as any;
}
export function pickProperties<T, K extends keyof T>(obj: T, ...keys: K[]): Pick<T, K> {
/**
*
* @template T -
* @template K - T
* @param {T} obj -
* @param {...K} keys -
* @returns {Pick<T, K>} -
* @remarks
*
* undefined
*/
export function pickProperties<T, K extends keyof T>(obj: T,...keys: K[]): Pick<T, K> {
// 创建一个新的对象,用于存储从原对象中提取的属性
const res: Pick<T, K> = {} as any;
// 遍历传入的键列表
for (const key of keys) {
// 将原对象中对应键的值赋给新对象中的对应键
res[key] = obj[key];
}
// 返回新对象,其中包含了从原对象中提取的指定属性
return res;
}
}
Loading…
Cancel
Save