Skip to content
Merged
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
18 changes: 15 additions & 3 deletions Sources/PureMouseAppSupport/ConfigStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ public final class ConfigStore {
undo?.clear()
return
}
guard data != diskBytes else { return }
// Identical bytes are a no-op, unless an error is showing: the file may have recovered.
guard data != diskBytes || configProblem != nil else { return }
// Someone else changed the file: what the steps would put back no longer fits it.
undo?.clear()
// Edited by hand while a change of ours waits: save ours on top of the new file now.
Expand Down Expand Up @@ -277,15 +278,26 @@ public final class ConfigStore {
/// watch the directory; some editors write in place, which leaves the directory untouched, so
/// watch the file too, following each new one. `reloadConfig` compares bytes, so a save seen
/// by both costs one read.
///
/// While the directory does not exist, the nearest existing ancestor is watched instead; each
/// event there rearms the watches, so they move down as the directories appear and settle on
/// the real directory (event-driven, no polling).
public func startWatching() {
directoryWatch?.cancel()
directoryWatch = nil
let directory = locations.config.resolvingSymlinksInPath().deletingLastPathComponent()
directoryWatch = watch(directory.path, [.write, .delete, .rename]) { store, events in
if !events.isDisjoint(with: [.delete, .rename]) { store.startWatching() }
var watched = directory
while watched.path != "/", !FileManager.default.fileExists(atPath: watched.path) {
watched = watched.deletingLastPathComponent()
}
let waiting = watched.path != directory.path
directoryWatch = watch(watched.path, [.write, .delete, .rename]) { store, events in
if waiting || !events.isDisjoint(with: [.delete, .rename]) { store.startWatching() }
store.configChanged()
}
watchConfigFile()
// The file may have appeared between the last event and the new watch.
if !waiting, directoryWatch != nil, fileWatch != nil { reloadConfig() }
}

private func watchConfigFile() {
Expand Down
35 changes: 35 additions & 0 deletions Tests/PureMouseAppSupportTests/ConfigStoreTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,38 @@ private func loadedStore(_ sandbox: Sandbox, clock: Clock? = nil) -> ConfigStore
#expect(flushes == 1)
#expect(!sandbox.exists(sandbox.config))
}

@MainActor @Test func aReadFailureClearsOnceTheIdenticalFileIsBack() throws {
let sandbox = try Sandbox()
let text = #"{"dpi": 800}"#
try sandbox.write(text)
let store = loadedStore(sandbox)
#expect(!store.readOnly)
try FileManager.default.removeItem(at: sandbox.config)
try FileManager.default.createDirectory(at: sandbox.config, withIntermediateDirectories: false)
store.reloadConfig()
#expect(store.readOnly)
#expect(store.configProblem != nil)
try FileManager.default.removeItem(at: sandbox.config)
try sandbox.write(text)
store.reloadConfig()
#expect(!store.readOnly)
#expect(store.configProblem == nil)
}

@MainActor @Test func watchingAMissingDirectoryFollowsItsCreation() async throws {
let sandbox = try Sandbox()
let missing = sandbox.directory.appending(path: "a").appending(path: "b")
let config = missing.appending(path: "config.json")
let locations = Locations(config: config, log: sandbox.locations.log, daemon: nil,
companion: sandbox.locations.companion)
let store = ConfigStore(locations: locations, clock: ManualClock().clock)
store.reloadConfig()
store.startWatching()
try FileManager.default.createDirectory(at: missing, withIntermediateDirectories: true)
try sandbox.write(#"{"dpi":800}"#, to: config)
for _ in 0..<100 where store.config.int(["dpi"]) != 800 {
try await Task.sleep(for: .milliseconds(50))
}
#expect(store.config.int(["dpi"]) == 800)
}
5 changes: 5 additions & 0 deletions docs/companion.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,11 @@ so the file's own descriptor goes stale). It reloads when the file's bytes chang
about 150 ms, exactly as for SIGHUP. Hand edits therefore apply on save. A failed load keeps the
previous config and reports `config.error`.

The menu app and Settings (`ConfigStore`) watch the same directory, plus the file itself for
in-place editors. While the directory does not exist yet, they watch its nearest existing ancestor
and move the watch down as directories appear, so a config created later is still seen (events
only, no polling). After a read error, the same bytes back on disk clear the error.

Slider writes from the menu are debounced to about 300 ms.

## Versioning
Expand Down
Loading