Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .jules/bolt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 2024-06-18 - Offload Synchronous Operations from Main Actor
**Learning:** In Swift Concurrency, `Task { ... }` created from within a `@MainActor` inherits that actor's context. To prevent synchronous, blocking operations (like `DiskInfo.current()`, `process.waitUntilExit()`, and `pipe.readDataToEndOfFile()`) from blocking the main thread and freezing the UI, they must be executed outside the actor's context.
**Action:** Use `Task.detached { ... }` which does not inherit the actor context, and `await` its `.value` property to safely return the result without blocking the `@MainActor`.
17 changes: 11 additions & 6 deletions Sources/Cacheout/ViewModels/CacheoutViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,8 @@ class CacheoutViewModel: ObservableObject {
func scan() async {
isScanning = true
isNodeModulesScanning = true
diskInfo = DiskInfo.current()
// ⚡ Bolt: Offload blocking I/O to background thread to prevent UI freeze during scan startup
diskInfo = await Task.detached { DiskInfo.current() }.value

// Scan caches and node_modules in parallel
async let cacheResults = scanner.scanAll(CacheCategory.allCategories)
Expand Down Expand Up @@ -241,10 +242,13 @@ class CacheoutViewModel: ObservableObject {
]

do {
try process.run()
process.waitUntilExit()
let data = pipe.fileHandleForReading.readDataToEndOfFile()
let output = String(data: data, encoding: .utf8) ?? ""
// ⚡ Bolt: Offload synchronous process execution to prevent MainActor blocking (~3-5s UI freeze prevention)
let output = try await Task.detached {
try process.run()
process.waitUntilExit()
let data = pipe.fileHandleForReading.readDataToEndOfFile()
return String(data: data, encoding: .utf8) ?? ""
}.value

if process.terminationStatus == 0 {
// Extract "Total reclaimed space:" line
Expand All @@ -270,7 +274,8 @@ class CacheoutViewModel: ObservableObject {
}

// Refresh disk info after prune
diskInfo = DiskInfo.current()
// ⚡ Bolt: Offload blocking I/O to background thread to prevent UI freeze
diskInfo = await Task.detached { DiskInfo.current() }.value
}

func clean() async {
Expand Down