File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -58,20 +58,25 @@ $env:PSModulePath = ($uniquePaths.ToArray()) -join $sep
5858
5959# ── 第 3 层:OnIdle 事件延迟全量加载(空闲时静默加载完整 psutils 模块) ──
6060# 将 wrapper.ps1 的加载也合并到此处
61- $script :__PsutilsManifestPath = $moduleManifest
62- $script :__WrapperScriptPath = Join-Path (Split-Path - Parent $PSScriptRoot ) ' wrapper.ps1'
63- Register-EngineEvent - SourceIdentifier PowerShell.OnIdle - MaxTriggerCount 1 - Action {
61+ # 注意:Action 脚本块运行在独立作用域,不能访问 $script: 变量,
62+ # 必须通过 -MessageData 传递数据,在 Action 中用 $Event.MessageData 获取
63+ $onIdleData = @ {
64+ ManifestPath = $moduleManifest
65+ WrapperPath = Join-Path (Split-Path - Parent $PSScriptRoot ) ' wrapper.ps1'
66+ }
67+ Register-EngineEvent - SourceIdentifier PowerShell.OnIdle - MaxTriggerCount 1 - MessageData $onIdleData - Action {
68+ $data = $Event.MessageData
6469 try {
6570 # 全量加载 psutils 模块(覆盖单独加载的子模块,补全其余子模块)
66- Import-Module $script :__PsutilsManifestPath - Force - Global - ErrorAction Stop
71+ Import-Module $data .ManifestPath - Force - Global - ErrorAction Stop
6772 }
6873 catch {
6974 Write-Warning " [profile/loadModule.ps1] OnIdle psutils 全量加载失败: $ ( $_.Exception.Message ) "
7075 }
7176 try {
7277 # 延迟加载 wrapper.ps1(yaz, Add-CondaEnv 等函数)
73- if (Test-Path $script :__WrapperScriptPath ) {
74- . $script :__WrapperScriptPath
78+ if (Test-Path $data .WrapperPath ) {
79+ . $data .WrapperPath
7580 }
7681 }
7782 catch {
You can’t perform that action at this time.
0 commit comments