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
+9 -9
View File
@@ -55,9 +55,8 @@ final class AppDelegate: NSObject, NSApplicationDelegate {
private let shoeGaze: ShoeGaze
private let window: LyrebirdWindow
/// The rig library and the entry to highlight at launch; built in `init`.
/// The rig library; constructed in `init`, resolved + loaded asynchronously after launch.
private let library: RigLibraryStore
private let initialRigID: UUID?
/// Record toggle item, retained so `validateMenuItem(_:)` can flip its title + enabled state.
private var recordMenuItem: NSMenuItem?
@@ -88,14 +87,12 @@ final class AppDelegate: NSObject, NSApplicationDelegate {
)
// Stored `let`s must be set before `super.init()`. Restore the autosaved working rig (or
// seed a board), then bootstrap the rig library from it — all operating on `shoeGaze`, not
// on `self` methods (which aren't available pre-`super.init`).
// seed a board) — operating on `shoeGaze`, not on `self` methods (unavailable pre-super).
if !RigStore.restore(into: shoeGaze) {
AppDelegate.seedInitialBoard(into: shoeGaze)
}
let bootstrap: RigLibraryBootstrap.Result = RigLibraryBootstrap.make(for: shoeGaze)
self.library = bootstrap.store
self.initialRigID = bootstrap.initialRigID
// The library constructs instantly; it resolves iCloud + loads asynchronously after launch.
self.library = RigLibraryBootstrap.makeStore()
super.init()
}
@@ -120,16 +117,19 @@ final class AppDelegate: NSObject, NSApplicationDelegate {
let session: ShoeGaze = shoeGaze
let driver: CoreAudioDriver = coreDriver
let rigLibrary: RigLibraryStore = library
let startupRigID: UUID? = initialRigID
window.open {
ShoeGazeBuilderView(
shoeGaze: session,
library: rigLibrary,
initialRigID: startupRigID,
hardwareSampleRate: { driver.deviceSampleRate }
)
}
anchorWindowLeft()
// Resolve iCloud + load the library off the main thread, then seed from the working rig if
// it is still empty (first run).
let rigSession: ShoeGaze = shoeGaze
Task { await RigLibraryBootstrap.start(rigLibrary, seedFrom: rigSession) }
}
/// Anchors the window `leftMargin` points from the left of the visible screen and sizes it to
@@ -27,12 +27,12 @@ struct ShoeGazeBuilderiOSApp: App {
ShoeGazeBuilderView(
shoeGaze: model.shoeGaze,
library: model.library,
initialRigID: model.initialRigID,
hardwareSampleRate: { model.hardwareSampleRate },
onEnableLiveInput: { model.enableLiveInput() }
)
.navigationTitle("ShoeGaze Builder")
.navigationBarTitleDisplayMode(.inline)
.task { await model.startLibrary() }
}
.onDisappear { model.stop() }
}
@@ -56,14 +56,10 @@ final class ShoeGazeBuilderiOSAppModel {
/// The sample rate the audio session actually committed to (for the config mismatch warning).
var hardwareSampleRate: Double? { iosDriver.sessionSampleRate }
/// The library entry to highlight as current on this launch (and after an engine rebuild),
/// resolved live from the persisted id so a mid-session selection survives a rebuild.
var initialRigID: UUID? {
guard let stored: String = UserDefaults.standard.string(
forKey: ShoeGazeBuilderView.currentRigDefaultsKey),
let id: UUID = UUID(uuidString: stored),
library.entries.contains(where: { $0.id == id }) else { return nil }
return id
/// Resolves iCloud + loads the library off the main thread, then seeds it from the working rig
/// on first run. Call once from the app's `.task`. Safe to call again (no-op once non-empty).
func startLibrary() async {
await RigLibraryBootstrap.start(library, seedFrom: shoeGaze)
}
// MARK: - Init
@@ -84,8 +80,9 @@ final class ShoeGazeBuilderiOSAppModel {
ShoeGazeBuilderiOSAppModel.seedFallbackBoard(into: shoeGaze)
}
// Bootstrap the rig library (iCloud-or-local), seeding it from the working rig on first run.
self.library = RigLibraryBootstrap.make(for: shoeGaze).store
// Construct the library instantly; it resolves iCloud + loads asynchronously via
// startLibrary() from the app's .task.
self.library = RigLibraryBootstrap.makeStore()
}
/// Persists the current rig. Call when the app moves to the background.