Files
ShoeGaze/Sources/LyrebirdShoeGazeUI/RigLibraryBootstrap.swift
T
josh 9a8e5461c0 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>
2026-06-27 19:50:38 -07:00

55 lines
2.3 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, !shoeGaze.boards.isEmpty else { return }
let entry: RigEntry = shoeGaze.captureEntry(name: seedName)
do {
try store.save(entry)
UserDefaults.standard.set(
entry.id.uuidString,
forKey: ShoeGazeBuilderView.currentRigDefaultsKey
)
} catch {
print("[RigLibraryBootstrap] seeding library failed: \(error)")
}
}
}