初始化管道系统(Initialization Pipeline)是一个管理应用启动流程的状态机系统,将复杂的异步初始化逻辑分解为清晰的、可管理的节点,每个节点负责特定的初始化任务,支持错误处理、用户交互和状态追踪。
| 概念 | 专业术语 | 业内对应方案 |
|---|---|---|
| 初始化管道 | Initialization Pipeline | React Query的查询管道 |
| 启动序列 | Bootstrap Sequence | Vuex的初始化模块 |
| 状态机流转 | State Machine Transition | XState状态机 |
| 用户参与点 | User Interaction Point | 表单验证流程 |
src/features/initializationPipeline/types/index.ts - 管道相关类型定义src/features/initializationPipeline/constants/nodes.ts - 节点常量定义src/features/initializationPipeline/constants/transitions.ts - 状态流转规则src/features/initializationPipeline/state/initializationSlice.ts - Redux slicesrc/features/initializationPipeline/state/pipelineManager.ts - 管道管理器src/features/initializationPipeline/components/PipelineProgress.tsx - 进度显示组件src/features/initializationPipeline/components/NodeStatusIndicator.tsx - 节点状态指示器src/features/initializationPipeline/components/ErrorHandler.tsx - 错误处理组件src/features/initializationPipeline/services/NodeExecutor.ts - 节点执行器src/features/initializationPipeline/services/PipelineService.ts - 管道服务src/features/initializationPipeline/utils/nodeHelpers.ts - 节点辅助函数src/features/initializationPipeline/utils/pipelineLogger.ts - 管道日志工具src/app.tsx - 集成管道系统,替换现有的初始化逻辑src/states/store.ts - 注册新的 initialization slicesrc/features/initializationPipeline/index.ts - 统一导出sequenceDiagram
participant User as 用户
participant App as App.tsx
participant Pipeline as PipelineManager
participant NodeExec as NodeExecutor
participant Redux as Redux Store
participant UI as UI Components
participant Services as 外部服务
App->>Pipeline: startPipeline()
Pipeline->>Redux: dispatch(INIT_PIPELINE)
loop 每个节点
Pipeline->>NodeExec: executeNode(nodeId)
NodeExec->>Redux: updateNodeStatus(RUNNING)
NodeExec->>Services: 执行节点任务
alt 任务成功
Services-->>NodeExec: 返回成功结果
NodeExec->>Redux: updateNodeStatus(SUCCESS)
NodeExec->>Pipeline: onNodeSuccess()
else 任务失败
Services-->>NodeExec: 返回错误
NodeExec->>Redux: updateNodeStatus(ERROR)
NodeExec->>Pipeline: onNodeError()
alt 需要用户输入
Pipeline->>UI: 显示配置界面
UI->>User: 请求用户输入
User->>UI: 提供配置信息
UI->>Pipeline: 用户输入完成
Pipeline->>NodeExec: retryNode()
else 可重试
Pipeline->>UI: 显示重试按钮
User->>UI: 点击重试
UI->>Pipeline: retryNode()
end
end
Pipeline->>Pipeline: checkTransition()
Pipeline->>Redux: updateCurrentNode()
end
Pipeline->>Redux: dispatch(PIPELINE_COMPLETED)
Pipeline->>App: onPipelineComplete()
App->>UI: 显示主应用界面
flowchart TD
subgraph "用户交互层"
UI[UI 组件] --> Actions[Redux Actions]
end
subgraph "状态管理层"
Actions --> Store[Redux Store]
Store --> State[Pipeline State]
State --> Selectors[Selectors]
Selectors --> UI
end
subgraph "业务逻辑层"
Actions --> PipelineSvc[PipelineService]
PipelineSvc --> NodeExec[NodeExecutor]
NodeExec --> PlatformSvc[Platform Service]
NodeExec --> ConfigSvc[Config Service]
NodeExec --> ApiSvc[API Service]
NodeExec --> I18nSvc[I18n Service]
end
subgraph "数据持久层"
ConfigSvc --> Storage[Local Storage]
Storage --> ConfigSvc
end
subgraph "外部服务层"
ApiSvc --> Backend[后端 API]
Backend --> ApiSvc
end
PipelineSvc --> Store
NodeExec --> Store
// src/features/initializationPipeline/types/index.ts
/**
* 管道节点状态
*/
export enum PipelineNodeStatus {
PENDING = 'pending',
RUNNING = 'running',
SUCCESS = 'success',
ERROR = 'error',
WAITING_USER_INPUT = 'waiting_user_input',
SKIPPED = 'skipped'
}
/**
* 管道节点定义
*/
export interface PipelineNode {
id: string;
name: string;
description: string;
status: PipelineNodeStatus;
requiresUserInteraction: boolean;
canRetry: boolean;
errorMessage?: string;
data?: any;
startTime?: number;
endTime?: number;
}
/**
* 管道状态
*/
export interface PipelineState {
currentNodeId: string | null;
nodes: Record<string, PipelineNode>;
isCompleted: boolean;
isRunning: boolean;
globalError?: string;
startTime: number;
}
/**
* 节点执行结果
*/
export interface NodeExecutionResult {
success: boolean;
data?: any;
error?: Error;
requiresUserInput?: boolean;
}
/**
* 节点执行器接口
*/
export interface INodeExecutor {
execute(nodeId: string): Promise<NodeExecutionResult>;
retry(nodeId: string): Promise<NodeExecutionResult>;
cancel(nodeId: string): Promise<void>;
}
// src/features/initializationPipeline/constants/nodes.ts
export enum PipelineNodes {
PLATFORM_DETECTION = 'platform_detection',
SERVER_CONNECTION_CHECK = 'server_connection_check',
PRODUCT_INITIALIZATION = 'product_initialization',
I18N_LOADING = 'i18n_loading',
APP_READY = 'app_ready'
}
export const NODE_DEFINITIONS: Record<PipelineNodes, Omit<PipelineNode, 'status' | 'data'>> = {
[PipelineNodes.PLATFORM_DETECTION]: {
id: PipelineNodes.PLATFORM_DETECTION,
name: '平台检测',
description: '检测当前运行的平台环境',
requiresUserInteraction: false,
canRetry: false
},
[PipelineNodes.SERVER_CONNECTION_CHECK]: {
id: PipelineNodes.SERVER_CONNECTION_CHECK,
name: '服务器连接检查',
description: '检查服务器配置和连接状态',
requiresUserInteraction: true, // 可能需要用户配置
canRetry: true
},
[PipelineNodes.PRODUCT_INITIALIZATION]: {
id: PipelineNodes.PRODUCT_INITIALIZATION,
name: '产品初始化',
description: '获取产品信息和基础配置',
requiresUserInteraction: false,
canRetry: true
},
[PipelineNodes.I18N_LOADING]: {
id: PipelineNodes.I18N_LOADING,
name: '多语言加载',
description: '加载对应语言的多语言资源',
requiresUserInteraction: false,
canRetry: true
},
[PipelineNodes.APP_READY]: {
id: PipelineNodes.APP_READY,
name: '应用就绪',
description: '所有初始化完成,应用可以正常使用',
requiresUserInteraction: false,
canRetry: false
}
};
起点:应用启动(非用户操作触发)
触发条件:
自动执行:应用启动后自动开始初始化管道
flowchart TD
Start([应用启动]) --> Init[初始化管道管理器]
Init --> PlatformCheck[执行 PLATFORM_DETECTION]
PlatformCheck --> PlatformSuccess{检测成功?}
PlatformCheck --> PlatformError{检测失败?}
PlatformError -->|是| ShowError[显示错误并退出]
PlatformSuccess -->|是| ServerCheck[执行 SERVER_CONNECTION_CHECK]
ServerCheck --> ServerSuccess{连接正常?}
ServerCheck --> ServerError{连接失败?}
ServerError -->|是| ShowConfig[显示配置对话框]
ShowConfig --> UserInput[等待用户输入]
UserInput --> ValidateConfig[验证配置]
ValidateConfig --> ConfigValid{配置有效?}
ConfigValid -->|否| ShowConfig
ConfigValid -->|是| SaveConfig[保存配置]
SaveConfig --> ServerCheck
ServerSuccess -->|是| ProductInit[执行 PRODUCT_INITIALIZATION]
ProductInit --> ProductSuccess{初始化成功?}
ProductInit --> ProductError{初始化失败?}
ProductError -->|是| ShowError2[显示错误]
ShowError2 --> CanRetry{可以重试?}
CanRetry -->|是| ProductInit
CanRetry -->|否| ShowConfig
ProductSuccess -->|是| I18nLoad[执行 I18N_LOADING]
I18nLoad --> I18nSuccess{加载成功?}
I18nLoad --> I18nError{加载失败?}
I18nError -->|是| UseDefaultLang[使用默认语言]
UseDefaultLang --> AppReady[执行 APP_READY]
I18nSuccess -->|是| AppReady
AppReady --> Complete[管道完成]
Complete --> ShowMainUI[显示主应用界面]
ShowError --> End([应用退出])
ShowMainUI --> End2([应用正常运行])
首次启动(无配置)
已有配置(连接正常)
已有配置(连接失败)
网络错误
服务器错误
语言包加载失败
# 测试首次启动
npm run build:electron
npm run start:electron
# 验证:显示配置对话框
# 测试已有配置
# 修改本地配置文件
# 重启应用
# 验证:直接进入应用
# 测试动态配置
npm run dev:h5
# 验证:自动使用当前 host
# 测试 Cordova 打包
npm run build:android
# 验证:配置持久化正常
准备环境:
执行测试:
验证结果:
http://192.168.1.100:8080)准备环境:
执行测试:
验证结果:
准备环境:
执行测试:
sequenceDiagram
participant App as 应用
participant Pipeline as 管道管理器
participant Node as 节点执行器
participant Store as Redux Store
participant UI as 用户界面
App->>Pipeline: startPipeline()
Pipeline->>Store: 初始化管道状态
Store-->>Pipeline: 状态初始化完成
Pipeline->>Node: executeNode(PLATFORM_DETECTION)
Node->>Store: 更新节点状态为 RUNNING
Node->>Node: 执行平台检测逻辑
Node->>Store: 更新节点状态为 SUCCESS
Pipeline->>Node: executeNode(SERVER_CONNECTION_CHECK)
Node->>Store: 更新节点状态为 RUNNING
Node->>Node: 检查服务器连接
alt 连接失败
Node->>Store: 更新节点状态为 WAITING_USER_INPUT
Pipeline->>UI: 显示配置对话框
UI->>UI: 等待用户输入
UI->>Pipeline: 用户完成配置
Pipeline->>Node: retryNode()
Node->>Node: 重新检查连接
end
Node->>Store: 更新节点状态为 SUCCESS
Pipeline->>Node: executeNode(PRODUCT_INITIALIZATION)
Node->>Store: 更新节点状态为 RUNNING
Node->>Node: 获取产品信息
Node->>Store: 更新节点状态为 SUCCESS
Pipeline->>Node: executeNode(I18N_LOADING)
Node->>Store: 更新节点状态为 RUNNING
Node->>Node: 加载多语言资源
Node->>Store: 更新节点状态为 SUCCESS
Pipeline->>Node: executeNode(APP_READY)
Node->>Store: 更新节点状态为 SUCCESS
Pipeline->>Store: 更新管道状态为 COMPLETED
Pipeline->>App: onPipelineComplete()
App->>UI: 显示主应用界面
classDiagram
class PipelineManager {
+startPipeline(): Promise<void>
+executeNode(nodeId: string): Promise<void>
+retryNode(nodeId: string): Promise<void>
+onNodeComplete(nodeId: string, result): void
+checkTransition(): string | null
-currentNodeId: string
-nodes: Record<string, PipelineNode>
}
class NodeExecutor {
+execute(nodeId: string): Promise<NodeExecutionResult>
+retry(nodeId: string): Promise<NodeExecutionResult>
+cancel(nodeId: string): Promise<void>
+validateResult(result): boolean
}
class PipelineService {
+initializePipeline(): PipelineState
+updateNodeStatus(nodeId, status): void
+getNodeStatus(nodeId): PipelineNodeStatus
+isPipelineCompleted(): boolean
+getCurrentNode(): PipelineNode | null
}
class InitializationSlice {
+startPipeline(): AsyncThunk
+executeNode(nodeId): AsyncThunk
+retryNode(nodeId): AsyncThunk
+updateNodeStatus(nodeId, status): Action
+setCurrentNode(nodeId): Action
+completePipeline(): Action
}
class PipelineProgress {
+render(): JSX.Element
+getNodeIcon(status): string
+getNodeColor(status): string
+onRetryClick(nodeId): void
}
class NodeStatusIndicator {
+render(): JSX.Element
+getStatusText(status): string
+getStatusIcon(status): IconComponent
+showRetryButton(): boolean
}
class ErrorHandler {
+render(): JSX.Element
+getErrorMessage(error): string
+getRecoveryAction(error): Action
+onRetryClick(): void
+onConfigClick(): void
}
PipelineManager --> NodeExecutor
PipelineManager --> PipelineService
NodeExecutor --> PipelineService
InitializationSlice --> PipelineService
PipelineProgress --> InitializationSlice
NodeStatusIndicator --> InitializationSlice
ErrorHandler --> InitializationSlice
flowchart TD
subgraph "开始状态"
START([开始]) --> INIT[初始化管道管理器]
end
subgraph "节点执行循环"
INIT --> CHECK_CURRENT{获取当前节点}
CHECK_CURRENT -->|有节点| EXECUTE_NODE[执行节点]
CHECK_CURRENT -->|无节点| COMPLETED[管道完成]
EXECUTE_NODE --> UPDATE_STATUS[更新节点状态为 RUNNING]
UPDATE_STATUS --> RUN_LOGIC[执行节点逻辑]
RUN_LOGIC --> CHECK_RESULT{检查执行结果}
CHECK_RESULT -->|成功| MARK_SUCCESS[标记节点成功]
CHECK_RESULT -->|失败| MARK_ERROR[标记节点失败]
CHECK_RESULT -->|需要用户输入| MARK_WAITING[标记等待用户输入]
MARK_SUCCESS --> TRANSITION[检查状态流转]
MARK_ERROR --> HANDLE_ERROR[错误处理]
MARK_WAITING --> WAIT_USER[等待用户输入]
HANDLE_ERROR --> CAN_RETRY{可以重试?}
CAN_RETRY -->|是| SHOW_RETRY[显示重试选项]
CAN_RETRY -->|否| SHOW_ERROR[显示错误信息]
SHOW_RETRY --> USER_RETRY{用户选择重试?}
USER_RETRY -->|是| RETRY_NODE[重试节点]
USER_RETRY -->|否| PIPELINE_FAILED[管道失败]
SHOW_ERROR --> PIPELINE_FAILED
WAIT_USER --> USER_INPUT[用户提供输入]
USER_INPUT --> VALIDATE_INPUT[验证用户输入]
VALIDATE_INPUT --> INPUT_VALID{输入有效?}
INPUT_VALID -->|否| WAIT_USER
INPUT_VALID -->|是| RETRY_NODE
RETRY_NODE --> EXECUTE_NODE
TRANSITION --> NEXT_NODE[设置下一个节点]
NEXT_NODE --> CHECK_CURRENT
end
subgraph "结束状态"
COMPLETED --> FINISH([完成])
PIPELINE_FAILED --> FINISH_ERROR([失败])
end
try {
const result = await executeNodeLogic(nodeId);
return { success: true, data: result };
} catch (error) {
console.error(`节点 ${nodeId} 执行失败:`, error);
return {
success: false,
error: error.message,
canRetry: isRetryableError(error)
};
}
try {
const nextNodeId = checkTransition(currentNodeId, result);
updateCurrentNode(nextNodeId);
} catch (transitionError) {
console.error('状态流转失败:', transitionError);
setGlobalError('初始化流程异常,请重启应用');
}
try {
return <PipelineProgress nodes={nodes} />;
} catch (renderError) {
console.error('UI 渲染失败:', renderError);
return <ErrorFallback message="界面渲染失败" />;
}
immer 或类似库优化深层状态更新React.memo 优化组件性能初始化管道系统是一个企业级的应用启动管理方案,具有以下优势:
这个初始化管道系统是现代 React 应用的最佳实践,特别适用于需要复杂初始化流程的企业级应用。