Make rig saves non-blocking (background disk I/O)

Save/delete/eraseAll now update the in-memory entries on the main actor
immediately and run the file write/delete — including NSFileCoordinator —
on a serial background queue, removing the UI stall the synchronous
coordinated write caused (especially on the iCloud path).

- Encoding stays synchronous so encode errors still surface to the caller;
  only the disk write is deferred (logs on failure).
- File-system helpers and coordinated I/O are now nonisolated static so the
  background queue can call them without touching actor state.
- Add flushPendingWrites() to drain the queue; called by tests asserting
  on-disk state and by both app shells at termination / background.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-06-27 19:40:02 -07:00
parent e8e134499d
commit 849240e6e2
5 changed files with 123 additions and 75 deletions
@@ -40,6 +40,7 @@ struct RigLibraryStoreTests {
// Sorted by name, case-insensitive.
#expect(store.entries.map(\.name) == ["Ambient", "Crunch"])
store.flushPendingWrites()
// A fresh store over the same directory sees the persisted files.
let reopened: RigLibraryStore = RigLibraryStore(directory: dir, isICloud: false)
reopened.start()
@@ -59,6 +60,7 @@ struct RigLibraryStoreTests {
#expect(store.entries.count == 1)
#expect(store.entries.first?.name == "Renamed")
store.flushPendingWrites()
let files: [URL] = try FileManager.default.contentsOfDirectory(
at: dir, includingPropertiesForKeys: nil
).filter { $0.pathExtension == "json" }
@@ -74,6 +76,7 @@ struct RigLibraryStoreTests {
let renamed: RigEntry = try store.rename(entry, to: "After")
#expect(renamed.id == entry.id)
#expect(store.entries.map(\.name) == ["After"])
store.flushPendingWrites()
}
@Test func duplicateCreatesIndependentEntry() throws {
@@ -85,6 +88,7 @@ struct RigLibraryStoreTests {
let copy: RigEntry = try store.duplicate(entry, newName: "Base 2")
#expect(copy.id != entry.id)
#expect(store.entries.count == 2)
store.flushPendingWrites()
}
@Test func deleteRemovesFileAndEntry() throws {
@@ -96,6 +100,7 @@ struct RigLibraryStoreTests {
try store.delete(entry)
#expect(store.entries.isEmpty)
store.flushPendingWrites()
let files: [URL] = try FileManager.default.contentsOfDirectory(
at: dir, includingPropertiesForKeys: nil
).filter { $0.pathExtension == "json" }
@@ -112,6 +117,7 @@ struct RigLibraryStoreTests {
try store.eraseAll()
#expect(store.entries.isEmpty)
store.flushPendingWrites()
let reopened: RigLibraryStore = RigLibraryStore(directory: dir, isICloud: false)
reopened.start()
#expect(reopened.entries.isEmpty)
@@ -131,6 +137,8 @@ struct RigLibraryStoreTests {
let added: Int = try other.importArchive(from: archiveData)
#expect(added == 2)
#expect(other.entries.map(\.name) == ["One", "Two"])
store.flushPendingWrites()
other.flushPendingWrites()
}
@Test func importColludingIdGetsFreshId() throws {
@@ -146,6 +154,7 @@ struct RigLibraryStoreTests {
let added: Int = try store.importArchive(from: data)
#expect(added == 1)
#expect(store.entries.count == 2, "colliding id should be imported as a new entry")
store.flushPendingWrites()
}
@Test func importLegacySingleRigFile() throws {
@@ -158,5 +167,6 @@ struct RigLibraryStoreTests {
let added: Int = try store.importArchive(from: data, legacyRigName: "Recovered")
#expect(added == 1)
#expect(store.entries.first?.name == "Recovered")
store.flushPendingWrites()
}
}