Files
ShoeGaze/Sources/LyrebirdShoeGazeUI/RigLibraryBootstrap.swift
T
josh 2c970aa344 refactor(rigs): address external-review findings
- Recall router state in applyState: the lightweight (no-rebuild) recall path
  now positionally applies router branch pedal params and the splitter/merger
  blend, instead of silently dropping them for routed rigs. Test added.
- Simplify first-run seeding: RigLibraryStore.save persists asynchronously
  (best-effort, logged not thrown), so the previous per-entry rollback could
  never fire for a real disk failure. Replaced with an honest best-effort
  save + working-rig fallback, and a comment stating the semantics.
- Move FactoryRigBuild to its own file (one public type per file).
- Document that a router's output master gain is intentionally not persisted,
  matching BoardState's treatment of board master gain.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-08-09 13:32:58 -07:00

85 lines
3.9 KiB
Swift

// RigLibraryBootstrap.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Foundation
import LyrebirdShoeGaze
/// Setup of the rig library for an app shell, split so nothing blocks the launch path.
///
/// ``makeStore()`` returns an auto-resolving store instantly (safe to construct in a synchronous
/// initializer and hand to the UI). ``start(_:seedFrom:seedName:)`` then does the blocking work —
/// resolving the iCloud-or-local location and the initial load — off the main thread, and seeds the
/// library from the current working rig **only after** the real (possibly iCloud) contents are
/// known, so a first launch can't create a duplicate "Default" rig on every synced device.
@MainActor
public enum RigLibraryBootstrap {
/// Creates an auto-resolving store. Construct this synchronously at launch; call
/// ``start(_:seedFrom:seedName:)`` afterwards (e.g. from a SwiftUI `.task` or a `Task`).
public static func makeStore() -> RigLibraryStore {
RigLibraryStore()
}
/// Resolves + loads `store` off the main thread, then seeds it from `shoeGaze` if it is still
/// empty after loading (first run). Idempotent enough to call once at launch.
///
/// - Parameters:
/// - store: The store from ``makeStore()``.
/// - shoeGaze: The session whose current rig seeds the library when it is first empty.
/// - seedName: Name for the seeded first rig. Defaults to `"Default"`.
public static func start(
_ store: RigLibraryStore,
seedFrom shoeGaze: ShoeGaze,
seedName: String = "Default"
) async {
await store.start()
// Seed only after the (possibly iCloud) library has loaded: an empty library here means a
// genuine first run, not a not-yet-synced one.
guard store.entries.isEmpty else { return }
// First run: populate the library with the shipped starter rigs. These are built in
// throwaway offline sessions, so they are independent of the current working graph. The
// working session is intentionally left as-is (no `currentRigID`) — the user picks a starter
// rig from the sidebar to load it, rather than the highlight pointing at a rig the working
// graph has not actually loaded.
// Seed the shipped starter rigs. `store.save` updates the in-memory library synchronously
// (so the session sees every rig immediately) and persists to disk asynchronously — a disk
// failure is logged by the store, not thrown here, so there is no per-entry rollback to do.
// `save` throws only on a synchronous encode failure of this deterministic rig data, which
// should not happen at runtime; if the build or an encode somehow fails, fall back to
// seeding the working rig so the library is never left empty on first run.
do {
let entries: [RigEntry] = try FactoryRigLibrary.makeEntries()
for entry: RigEntry in entries {
try store.save(entry)
}
} catch {
print("[RigLibraryBootstrap] seeding factory rigs failed: \(error)")
seedWorkingRig(store, seedFrom: shoeGaze, seedName: seedName)
}
}
/// Seeds the working session's current rig as a single named library entry.
///
/// Last-resort fallback used only when the factory rigs could not be built; keeps the library
/// from being left empty on first run.
private static func seedWorkingRig(
_ store: RigLibraryStore,
seedFrom shoeGaze: ShoeGaze,
seedName: String
) {
guard !shoeGaze.boards.isEmpty else { return }
let entry: RigEntry = shoeGaze.captureEntry(name: seedName)
do {
try store.save(entry)
shoeGaze.currentRigID = entry.id
} catch {
print("[RigLibraryBootstrap] fallback seeding failed: \(error)")
}
}
}