diff --git a/functions/private/Start-WinUtilInstallAppRendering.ps1 b/functions/private/Start-WinUtilInstallAppRendering.ps1 index 3274f821..b15ef7aa 100644 --- a/functions/private/Start-WinUtilInstallAppRendering.ps1 +++ b/functions/private/Start-WinUtilInstallAppRendering.ps1 @@ -1,3 +1,18 @@ +function Invoke-WinUtilInstallAppRenderBatch { + param( + [Parameter(Mandatory = $true)] + $CategoryBatch + ) + + foreach ($appKey in $CategoryBatch.AppKeys) { + $sync.$appKey = Initialize-InstallAppEntry -TargetElement $CategoryBatch.TargetElement -AppKey $appKey + } + + if ($sync.currentTab -eq "Install" -and $sync.SearchBar -and -not [string]::IsNullOrWhiteSpace($sync.SearchBar.Text)) { + Find-AppsByNameOrDescription -SearchString $sync.SearchBar.Text + } +} + function Start-WinUtilInstallAppRendering { if ($null -eq $sync.InstallAppRenderQueue) { return @@ -5,31 +20,22 @@ function Start-WinUtilInstallAppRendering { $sync.InstallAppEntriesRendered = $false - $renderCategory = { - param($CategoryBatch) - - foreach ($appKey in $CategoryBatch.AppKeys) { - $sync.$appKey = Initialize-InstallAppEntry -TargetElement $CategoryBatch.TargetElement -AppKey $appKey - } - - if ($sync.currentTab -eq "Install" -and $sync.SearchBar -and -not [string]::IsNullOrWhiteSpace($sync.SearchBar.Text)) { - Find-AppsByNameOrDescription -SearchString $sync.SearchBar.Text - } - } - if ($sync.Form -and $sync.Form.Dispatcher -and ("System.Windows.Threading.DispatcherTimer" -as [type])) { $timer = New-Object System.Windows.Threading.DispatcherTimer $timer.Interval = [TimeSpan]::FromMilliseconds(1) $timer.Add_Tick({ - $timer.Stop() + param($sender) + + $dispatcherTimer = [System.Windows.Threading.DispatcherTimer]$sender + $dispatcherTimer.Stop() if ($sync.InstallAppRenderQueue.Count -gt 0) { $categoryBatch = $sync.InstallAppRenderQueue.Dequeue() - & $renderCategory $categoryBatch + Invoke-WinUtilInstallAppRenderBatch -CategoryBatch $categoryBatch } if ($sync.InstallAppRenderQueue.Count -gt 0) { - $timer.Start() + $dispatcherTimer.Start() return } @@ -43,7 +49,7 @@ function Start-WinUtilInstallAppRendering { while ($sync.InstallAppRenderQueue.Count -gt 0) { $categoryBatch = $sync.InstallAppRenderQueue.Dequeue() - & $renderCategory $categoryBatch + Invoke-WinUtilInstallAppRenderBatch -CategoryBatch $categoryBatch } $sync.InstallAppEntriesRendered = $true diff --git a/pester/install-rendering.Tests.ps1 b/pester/install-rendering.Tests.ps1 index 4c479a7b..0b4a5430 100644 --- a/pester/install-rendering.Tests.ps1 +++ b/pester/install-rendering.Tests.ps1 @@ -23,6 +23,16 @@ Describe "Install app rendering startup contract" { $renderScript | Should -Match 'Install app entries rendered' } + It "does not rely on local timer variables after the dispatcher tick fires" { + $renderScript = Get-Content -Path (Join-Path $script:repoRoot "functions\private\Start-WinUtilInstallAppRendering.ps1") -Raw + + $renderScript | Should -Match 'param\(\$sender\)' + $renderScript | Should -Match '\$dispatcherTimer = \[System\.Windows\.Threading\.DispatcherTimer\]\$sender' + $renderScript | Should -Not -Match '\$timer\.Stop\(\)' + $renderScript | Should -Match '\$dispatcherTimer\.Start\(\)' + $renderScript | Should -Not -Match '& \$renderCategory' + } + It "keeps app-entry metadata lookup independent from the old caller scope" { $entryScript = Get-Content -Path (Join-Path $script:repoRoot "functions\private\Initialize-InstallAppEntry.ps1") -Raw