restores Helder in all its glory
This commit is contained in:
@@ -0,0 +1,86 @@
|
||||
import Foundation
|
||||
import HelderCore
|
||||
import HelderServices
|
||||
|
||||
extension WorkspaceModel {
|
||||
/// ⌘R: reloads git, the tree and the open file from disk (disk wins over unsaved edits), and flashes the columns.
|
||||
public func refresh() async {
|
||||
refreshTick += 1
|
||||
let editor = editor
|
||||
async let reread: Void = run([.git, .tree])
|
||||
async let active: Void = editor?.reloadActiveFromDisk() ?? ()
|
||||
_ = await (reread, active)
|
||||
}
|
||||
|
||||
/// Reacts to a disk change from the watcher: project and git events gather into one debounced refresh.
|
||||
public func handle(_ event: WatchEvent) async {
|
||||
switch event {
|
||||
case .project: scheduleRefresh([.git, .tree, .symbols, .openFiles, .note])
|
||||
case .git: scheduleRefresh(.git)
|
||||
case .config: await reloadConfig()
|
||||
}
|
||||
}
|
||||
|
||||
/// Waits until every scheduled refresh, symbol load, command and project switch has finished; tests use it to settle.
|
||||
public func settle() async {
|
||||
while true {
|
||||
if let task = lifecycleTask {
|
||||
await task.value
|
||||
} else if let task = refreshTask {
|
||||
await task.value
|
||||
} else if let task = symbolsTask {
|
||||
await task.value
|
||||
if symbolsTask == task { symbolsTask = nil }
|
||||
} else if let task = work.values.first {
|
||||
await task.value
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
await editor?.settle()
|
||||
}
|
||||
|
||||
func scheduleRefresh(_ scope: RefreshScope) {
|
||||
pendingRefresh.formUnion(scope)
|
||||
guard refreshTask == nil, !pendingRefresh.isEmpty else { return }
|
||||
let id = UUID()
|
||||
refreshID = id
|
||||
refreshTask = Task { [weak self] in
|
||||
guard let self else { return }
|
||||
try? await Task.sleep(for: self.timings.refreshDebounce)
|
||||
guard !Task.isCancelled, self.refreshID == id else { return }
|
||||
let scope = self.pendingRefresh
|
||||
self.pendingRefresh = []
|
||||
await self.run(scope)
|
||||
guard self.refreshID == id else { return }
|
||||
self.refreshTask = nil
|
||||
self.scheduleRefresh([])
|
||||
}
|
||||
}
|
||||
|
||||
/// Reads the stores once up front, so a pass that outlives a project switch only touches the old project.
|
||||
func run(_ scope: RefreshScope) async {
|
||||
let sourceControl = sourceControl, explorer = explorer, editor = editor, note = note
|
||||
let current = generation
|
||||
if scope.contains(.symbols) {
|
||||
await services?.symbols.invalidate()
|
||||
if generation == current { scheduleSymbolLoad(after: timings.symbolsAfterChange) }
|
||||
}
|
||||
async let git: Void = scope.contains(.git) ? (sourceControl?.refresh() ?? ()) : ()
|
||||
async let tree: Void = scope.contains(.tree) ? (explorer?.reload() ?? ()) : ()
|
||||
async let files: Void = scope.contains(.openFiles) ? (editor?.filesChanged(nil) ?? ()) : ()
|
||||
async let notes: Void = scope.contains(.note) ? (note?.reloadIfClean() ?? ()) : ()
|
||||
_ = await (git, tree, files, notes)
|
||||
}
|
||||
|
||||
/// Restarts the countdown on every change, so a burst of writes loads the class list once.
|
||||
func scheduleSymbolLoad(after delay: Duration) {
|
||||
symbolsTask?.cancel()
|
||||
let editor = editor
|
||||
symbolsTask = Task {
|
||||
try? await Task.sleep(for: delay)
|
||||
guard !Task.isCancelled else { return }
|
||||
await editor?.loadKnownSymbols()
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user