Make rig library loads/resolve fully off-main

Move the remaining synchronous main-thread I/O off the main actor:

- RigLibraryStore now constructs instantly with a local placeholder and
  exposes an awaitable async start(): it resolves the iCloud-or-local
  location off-main (the blocking ubiquity lookup), loads entries off-main
  via the shared ioQueue (so a reload can't drop a just-saved rig), then
  sets isLoaded. reload() is async; iCloud metadata updates re-read via it.
- Seeding moves into RigLibraryBootstrap.start(_:seedFrom:), which seeds a
  "Default" rig only AFTER the (possibly iCloud) library has loaded and is
  still empty — preventing a duplicate "Default" on every synced device on
  first launch.
- Sidebar shows a Loading state until isLoaded; the builder restores the
  persisted current-rig highlight when isLoaded flips true (initialRigID
  param dropped).
- Shells construct the store synchronously and start it asynchronously
  (macOS via Task in didFinishLaunching; iOS via .task). Codec + file
  helpers are nonisolated static so background work needs no actor hop.
- Tests await store.start().

Full suite: 206 passing.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-06-27 19:50:38 -07:00
parent 849240e6e2
commit 9a8e5461c0
9 changed files with 175 additions and 133 deletions
@@ -19,10 +19,10 @@ struct RigLibraryStoreTests {
return url
}
private func makeStore() -> (RigLibraryStore, URL) {
private func makeStore() async -> (RigLibraryStore, URL) {
let dir: URL = makeTempDirectory()
let store: RigLibraryStore = RigLibraryStore(directory: dir, isICloud: false)
store.start()
await store.start()
return (store, dir)
}
@@ -30,8 +30,8 @@ struct RigLibraryStoreTests {
RigEntry(name: name, state: ShoeGazeState(boards: [BoardState(label: name, segments: [])]))
}
@Test func saveAddsEntryAndReloadsAcrossInstances() throws {
let (store, dir): (RigLibraryStore, URL) = makeStore()
@Test func saveAddsEntryAndReloadsAcrossInstances() async throws {
let (store, dir): (RigLibraryStore, URL) = await makeStore()
defer { try? FileManager.default.removeItem(at: dir) }
try store.save(makeEntry("Ambient"))
@@ -43,13 +43,13 @@ struct RigLibraryStoreTests {
store.flushPendingWrites()
// A fresh store over the same directory sees the persisted files.
let reopened: RigLibraryStore = RigLibraryStore(directory: dir, isICloud: false)
reopened.start()
await reopened.start()
#expect(reopened.entries.count == 2)
#expect(reopened.entries.map(\.name) == ["Ambient", "Crunch"])
}
@Test func saveSameIdReplacesInPlace() throws {
let (store, dir): (RigLibraryStore, URL) = makeStore()
@Test func saveSameIdReplacesInPlace() async throws {
let (store, dir): (RigLibraryStore, URL) = await makeStore()
defer { try? FileManager.default.removeItem(at: dir) }
let entry: RigEntry = makeEntry("Original")
@@ -67,8 +67,8 @@ struct RigLibraryStoreTests {
#expect(files.count == 1, "rename must not leave the old-name file behind")
}
@Test func renameKeepsIdChangesName() throws {
let (store, dir): (RigLibraryStore, URL) = makeStore()
@Test func renameKeepsIdChangesName() async throws {
let (store, dir): (RigLibraryStore, URL) = await makeStore()
defer { try? FileManager.default.removeItem(at: dir) }
let entry: RigEntry = makeEntry("Before")
@@ -79,8 +79,8 @@ struct RigLibraryStoreTests {
store.flushPendingWrites()
}
@Test func duplicateCreatesIndependentEntry() throws {
let (store, dir): (RigLibraryStore, URL) = makeStore()
@Test func duplicateCreatesIndependentEntry() async throws {
let (store, dir): (RigLibraryStore, URL) = await makeStore()
defer { try? FileManager.default.removeItem(at: dir) }
let entry: RigEntry = makeEntry("Base")
@@ -91,8 +91,8 @@ struct RigLibraryStoreTests {
store.flushPendingWrites()
}
@Test func deleteRemovesFileAndEntry() throws {
let (store, dir): (RigLibraryStore, URL) = makeStore()
@Test func deleteRemovesFileAndEntry() async throws {
let (store, dir): (RigLibraryStore, URL) = await makeStore()
defer { try? FileManager.default.removeItem(at: dir) }
let entry: RigEntry = makeEntry("Doomed")
@@ -107,8 +107,8 @@ struct RigLibraryStoreTests {
#expect(files.isEmpty)
}
@Test func eraseAllClearsLibrary() throws {
let (store, dir): (RigLibraryStore, URL) = makeStore()
@Test func eraseAllClearsLibrary() async throws {
let (store, dir): (RigLibraryStore, URL) = await makeStore()
defer { try? FileManager.default.removeItem(at: dir) }
try store.save(makeEntry("A"))
@@ -119,12 +119,12 @@ struct RigLibraryStoreTests {
store.flushPendingWrites()
let reopened: RigLibraryStore = RigLibraryStore(directory: dir, isICloud: false)
reopened.start()
await reopened.start()
#expect(reopened.entries.isEmpty)
}
@Test func exportThenImportRoundTrips() throws {
let (store, dir): (RigLibraryStore, URL) = makeStore()
@Test func exportThenImportRoundTrips() async throws {
let (store, dir): (RigLibraryStore, URL) = await makeStore()
defer { try? FileManager.default.removeItem(at: dir) }
try store.save(makeEntry("One"))
@@ -132,7 +132,7 @@ struct RigLibraryStoreTests {
let archiveData: Data = try store.exportArchiveData()
// Import into a fresh, empty store.
let (other, otherDir): (RigLibraryStore, URL) = makeStore()
let (other, otherDir): (RigLibraryStore, URL) = await makeStore()
defer { try? FileManager.default.removeItem(at: otherDir) }
let added: Int = try other.importArchive(from: archiveData)
#expect(added == 2)
@@ -141,8 +141,8 @@ struct RigLibraryStoreTests {
other.flushPendingWrites()
}
@Test func importColludingIdGetsFreshId() throws {
let (store, dir): (RigLibraryStore, URL) = makeStore()
@Test func importColludingIdGetsFreshId() async throws {
let (store, dir): (RigLibraryStore, URL) = await makeStore()
defer { try? FileManager.default.removeItem(at: dir) }
let entry: RigEntry = makeEntry("Shared")
@@ -157,8 +157,8 @@ struct RigLibraryStoreTests {
store.flushPendingWrites()
}
@Test func importLegacySingleRigFile() throws {
let (store, dir): (RigLibraryStore, URL) = makeStore()
@Test func importLegacySingleRigFile() async throws {
let (store, dir): (RigLibraryStore, URL) = await makeStore()
defer { try? FileManager.default.removeItem(at: dir) }
// A legacy file is a bare ShoeGazeState (no `rigs` array).