Open source CapCut alternative.
TypeScript
fix(app): heal signed-out notch when a second instance services sign-in
David Le1da2364036ad
diff --git a/apps/Donkey/Sources/Donkey/DonkeyAppDelegate.swift b/apps/Donkey/Sources/Donkey/DonkeyAppDelegate.swiftindex bfbad1c3..be7180ac 100644--- a/apps/Donkey/Sources/Donkey/DonkeyAppDelegate.swift+++ b/apps/Donkey/Sources/Donkey/DonkeyAppDelegate.swift@@ -75,6 +75,16 @@ final class DonkeyAppDelegate: NSObject, NSApplicationDelegate {}}+ func applicationDidBecomeActive(_ notification: Notification) {+ // The donkey:// sign-in callback can be serviced by a different app instance than the one rendering+ // the notch (LaunchServices cold-launching a second copy of this bundle id, which shares this+ // UserDefaults domain). That copy persists the session and exits, leaving this instance's in-memory+ // phase signed-out and the notch stuck on the login CTA. Clicking "Open Donkey" activates us, so+ // reconciling from the durable session here flips the phase — and the $phase observer flips the+ // notch — without a relaunch. A no-op when already signed in or when no session is on disk.+ authCoordinator?.reconcileWithPersistedSession()+ }+func applicationShouldHandleReopen(_ sender: NSApplication,hasVisibleWindows flag: Booldiff --git a/apps/Donkey/Sources/Donkey/DonkeyAuthCoordinator.swift b/apps/Donkey/Sources/Donkey/DonkeyAuthCoordinator.swiftindex 34df7123..7a4d66a8 100644--- a/apps/Donkey/Sources/Donkey/DonkeyAuthCoordinator.swift+++ b/apps/Donkey/Sources/Donkey/DonkeyAuthCoordinator.swift@@ -222,6 +222,19 @@ final class DonkeyAuthCoordinator: ObservableObject {authenticationCompleted?(session)}+ /// Re-reads the durable session and promotes a signed-out phase to signed-in when one exists on disk.+ /// The sign-in callback can be handled by a different app instance than the one rendering the notch —+ /// LaunchServices can cold-launch a second copy of this bundle id (which shares this UserDefaults+ /// domain) to service the donkey:// URL. That copy mints the session on disk and flips its own phase,+ /// while this instance's in-memory phase stays signed-out, leaving the notch stuck on the login CTA+ /// until relaunch. Reconciling from the durable store on every activation heals it without a relaunch.+ /// An in-flight sign-in, a live session, or a failed attempt is left untouched.+ func reconcileWithPersistedSession() {+ guard case .signedOut = phase,+ let session = stateStore.loadSession() else { return }+ phase = .signedIn(session)+ }+func clearFailedState() {guard case .failed = phase else { return }phase = stateStore.loadSession().map(DonkeyAuthPhase.signedIn) ?? .signedOutdiff --git a/apps/Donkey/Tests/DonkeyRuntimeTests/DonkeyAuthCoordinatorReconcileTests.swift b/apps/Donkey/Tests/DonkeyRuntimeTests/DonkeyAuthCoordinatorReconcileTests.swiftnew file mode 100644index 00000000..ca0c9a6d--- /dev/null+++ b/apps/Donkey/Tests/DonkeyRuntimeTests/DonkeyAuthCoordinatorReconcileTests.swift@@ -0,0 +1,83 @@+@testable import Donkey+import Foundation+import Testing++/// The notch's login state is driven by the in-memory `phase`, which syncs with the durable session only at+/// init. When the donkey:// callback is serviced by a different app instance sharing this UserDefaults domain,+/// the session lands on disk while this instance stays signed-out. `reconcileWithPersistedSession()` heals that+/// on the next activation by promoting signed-out → signed-in from the store, without clobbering any other phase.+@MainActor+@Suite+struct DonkeyAuthCoordinatorReconcileTests {+ @Test+ func reconcilePromotesSignedOutToSignedInWhenSessionAppearsOnDisk() {+ let store = MutableAuthStateStore()+ let coordinator = makeCoordinator(store: store)+ #expect(coordinator.isAuthenticated == false)++ // Another instance mints the session in the shared store after this coordinator already came up+ // signed-out.+ store.session = Self.session++ coordinator.reconcileWithPersistedSession()++ #expect(coordinator.isAuthenticated)+ }++ @Test+ func reconcileIsNoOpWhenNoSessionOnDisk() {+ let coordinator = makeCoordinator(store: MutableAuthStateStore())++ coordinator.reconcileWithPersistedSession()++ #expect(coordinator.isAuthenticated == false)+ }++ @Test+ func reconcileDoesNotClobberLiveSessionWhenStoreIsCleared() {+ let store = MutableAuthStateStore()+ store.session = Self.session+ let coordinator = makeCoordinator(store: store)+ #expect(coordinator.isAuthenticated)++ // A stale read or a cross-instance clear must not sign a live session out on activation.+ store.session = nil+ coordinator.reconcileWithPersistedSession()++ #expect(coordinator.isAuthenticated)+ }++ // MARK: - Helpers++ private static let session = DonkeyAuthSession(+ id: "session-id",+ provider: "google",+ authenticatedAt: Date(timeIntervalSince1970: 0)+ )++ private func makeCoordinator(store: MutableAuthStateStore) -> DonkeyAuthCoordinator {+ DonkeyAuthCoordinator(+ configuration: DonkeyAuthConfiguration(+ webBaseURL: URL(string: "https://reconcile-test.example")!,+ callbackScheme: "donkey"+ ),+ stateStore: store+ )+ }+}++/// In-memory auth state whose session can be mutated between calls to simulate another instance writing the+/// shared store.+private final class MutableAuthStateStore: DonkeyAuthStateStoring, @unchecked Sendable {+ var session: DonkeyAuthSession?+ var hasEverSignedIn = false++ func loadSession() -> DonkeyAuthSession? { session }+ func saveSession(_ session: DonkeyAuthSession) { self.session = session }+ func clearSession() { session = nil }+ func loadPendingState() -> String? { nil }+ func savePendingState(_ state: String) {}+ func clearPendingState() {}+ func loadHasEverSignedIn() -> Bool { hasEverSignedIn }+ func markHasEverSignedIn() { hasEverSignedIn = true }+}diff --git a/scripts/package-donkey-app.sh b/scripts/package-donkey-app.shindex 3b2ff803..8078df85 100755--- a/scripts/package-donkey-app.sh+++ b/scripts/package-donkey-app.sh@@ -406,6 +406,8 @@ cat > "$CONTENTS_DIR/Info.plist" <<PLIST<string>$APP_BUILD</string><key>LSMinimumSystemVersion</key><string>14.0</string>+ <key>LSMultipleInstancesProhibited</key>+ <true/><key>DonkeyWebBaseURL</key><string>$WEB_BASE_URL</string><key>DonkeyAuthCallbackScheme</key>diff --git a/scripts/run-donkey-dev.sh b/scripts/run-donkey-dev.shindex ed5283d2..9b9ce785 100755--- a/scripts/run-donkey-dev.sh+++ b/scripts/run-donkey-dev.sh@@ -405,6 +405,8 @@ write_info_plist() {<string>1</string><key>LSMinimumSystemVersion</key><string>14.0</string>+ <key>LSMultipleInstancesProhibited</key>+ <true/><key>DonkeyWebBaseURL</key><string>$escaped_web_base_url</string><key>DonkeyAuthCallbackScheme</key>