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:
@@ -7,61 +7,48 @@
|
||||
import Foundation
|
||||
import LyrebirdShoeGaze
|
||||
|
||||
/// One-call setup of the rig library for an app shell.
|
||||
/// Setup of the rig library for an app shell, split so nothing blocks the launch path.
|
||||
///
|
||||
/// Resolves the (iCloud-or-local) library location, constructs and starts a ``RigLibraryStore``,
|
||||
/// seeds it with the current working rig the first time it is empty (so the listing is never blank
|
||||
/// and the user's existing setup becomes their first saved rig), and reports which entry should be
|
||||
/// highlighted as the current rig on this launch.
|
||||
/// ``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 {
|
||||
|
||||
/// The result of bootstrapping: a started store and the rig to highlight at launch.
|
||||
public struct Result {
|
||||
public let store: RigLibraryStore
|
||||
public let initialRigID: UUID?
|
||||
/// 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()
|
||||
}
|
||||
|
||||
/// Builds and starts the rig library for `shoeGaze`.
|
||||
///
|
||||
/// - Important: Resolving the iCloud container performs blocking I/O. Call this once at app
|
||||
/// startup; it is acceptable there but should not be called repeatedly.
|
||||
/// 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"`.
|
||||
/// - Returns: The started store and the entry id to seed the builder's current-rig highlight.
|
||||
public static func make(
|
||||
for shoeGaze: ShoeGaze,
|
||||
public static func start(
|
||||
_ store: RigLibraryStore,
|
||||
seedFrom shoeGaze: ShoeGaze,
|
||||
seedName: String = "Default"
|
||||
) -> Result {
|
||||
let resolved: RigLibraryLocation.Resolved = RigLibraryLocation.resolve()
|
||||
let store: RigLibraryStore = RigLibraryStore(resolved: resolved)
|
||||
store.start()
|
||||
) async {
|
||||
await store.start()
|
||||
|
||||
let defaults: UserDefaults = UserDefaults.standard
|
||||
let key: String = ShoeGazeBuilderView.currentRigDefaultsKey
|
||||
|
||||
// First run with an empty library: capture the current working rig as the first entry and
|
||||
// remember it as current.
|
||||
if store.entries.isEmpty, !shoeGaze.boards.isEmpty {
|
||||
let entry: RigEntry = shoeGaze.captureEntry(name: seedName)
|
||||
do {
|
||||
try store.save(entry)
|
||||
defaults.set(entry.id.uuidString, forKey: key)
|
||||
} catch {
|
||||
print("[RigLibraryBootstrap] seeding library failed: \(error)")
|
||||
}
|
||||
// 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)")
|
||||
}
|
||||
|
||||
// Resolve the highlight only if the remembered id still exists in the library.
|
||||
let initial: UUID? = {
|
||||
guard let stored: String = defaults.string(forKey: key),
|
||||
let id: UUID = UUID(uuidString: stored),
|
||||
store.entries.contains(where: { $0.id == id }) else { return nil }
|
||||
return id
|
||||
}()
|
||||
|
||||
return Result(store: store, initialRigID: initial)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user