diff --git a/.changeset/ios-updates-startup-finished-launch.md b/.changeset/ios-updates-startup-finished-launch.md new file mode 100644 index 000000000..f8650bf3d --- /dev/null +++ b/.changeset/ios-updates-startup-finished-launch.md @@ -0,0 +1,5 @@ +--- +'@callstack/react-native-brownfield': patch +--- + +Fix the iOS Expo Updates startup gate waiting forever when the host app started expo-updates itself and its launch had already finished (most visibly an emergency launch with no embedded bundle), or when the launch finished while the gate was registering its delegate. diff --git a/packages/react-native-brownfield/ios/Expo/ExpoUpdatesStartup.swift b/packages/react-native-brownfield/ios/Expo/ExpoUpdatesStartup.swift index d8374f80f..c7c5d49c8 100644 --- a/packages/react-native-brownfield/ios/Expo/ExpoUpdatesStartup.swift +++ b/packages/react-native-brownfield/ios/Expo/ExpoUpdatesStartup.swift @@ -17,7 +17,7 @@ final class ExpoUpdatesStartup: NSObject { let controller = AppController.sharedInstance guard controller.isActiveController else { completion(); return } // expo-updates 57 traps in launchAssetUrl() until start() sets its startup procedure. - if updatesReady || (controller.isStarted && controller.launchAssetUrl() != nil) { + if updatesReady || (controller.isStarted && Self.hasFinishedLaunching(controller)) { completion() return } @@ -25,10 +25,27 @@ final class ExpoUpdatesStartup: NSObject { controller.delegate = self if !controller.isStarted { controller.start() + } else if Self.hasFinishedLaunching(controller) { + releaseCallbacks() } } func cancelPendingRequests() { updatesCallbacks.removeAll() } + + private func releaseCallbacks() { + updatesReady = true + let callbacks = updatesCallbacks + updatesCallbacks.removeAll() + callbacks.forEach { $0() } + } + + private static func hasFinishedLaunching(_ controller: InternalAppControllerInterface) -> Bool { + if controller.launchAssetUrl() != nil { + return true + } + let constants = controller.getConstantsForModule().toModuleConstantsMap() + return (constants["launchDuration"] ?? nil) != nil + } } #endif @@ -37,13 +54,10 @@ extension ExpoUpdatesStartup: AppControllerDelegate { func appController(_ appController: any AppControllerInterface, didStartWithSuccess success: Bool) { DispatchQueue.main.async { [weak self] in guard let self else { return } - self.updatesReady = true if !success { NSLog("%@", "Expo Updates failed to select a launch asset; using the bundle resolver fallback.") } - let callbacks = self.updatesCallbacks - self.updatesCallbacks.removeAll() - callbacks.forEach { $0() } + self.releaseCallbacks() } } } diff --git a/packages/react-native-brownfield/tests/ios/run-expo-startup.sh b/packages/react-native-brownfield/tests/ios/run-expo-startup.sh index a9ab7c107..b74b6302d 100644 --- a/packages/react-native-brownfield/tests/ios/run-expo-startup.sh +++ b/packages/react-native-brownfield/tests/ios/run-expo-startup.sh @@ -10,7 +10,15 @@ public protocol AppControllerInterface {} public protocol AppControllerDelegate: AnyObject { func appController(_ controller: any AppControllerInterface, didStartWithSuccess success: Bool) } -public final class AppController: AppControllerInterface { +public protocol InternalAppControllerInterface: AppControllerInterface { + func launchAssetUrl() -> URL? + func getConstantsForModule() -> UpdatesModuleConstants +} +public struct UpdatesModuleConstants { + let launchDuration: Double? + public func toModuleConstantsMap() -> [String: Any?] { ["launchDuration": launchDuration] } +} +public final class AppController: InternalAppControllerInterface { public static var initialized = false public static var controller = AppController() public static var sharedInstance: AppController { @@ -22,9 +30,20 @@ public final class AppController: AppControllerInterface { public var isStarted = false public var starts = 0 public var asset: URL? - public weak var delegate: (any AppControllerDelegate)? + public var launched = false + public var beforeDelegateAssignment: (() -> Void)? + public weak var delegate: (any AppControllerDelegate)? { + willSet { + let hook = beforeDelegateAssignment + beforeDelegateAssignment = nil + hook?() + } + } public init() {} public func launchAssetUrl() -> URL? { asset } + public func getConstantsForModule() -> UpdatesModuleConstants { + UpdatesModuleConstants(launchDuration: launched ? 1 : nil) + } public func start() { precondition(isActiveController, "Cannot start a dev launcher controller") precondition(!isStarted, "Cannot start twice") @@ -33,6 +52,7 @@ public final class AppController: AppControllerInterface { } public func finish(_ success: Bool = true) { if success { asset = URL(fileURLWithPath: "/update.jsbundle") } + launched = true delegate?.appController(self, didStartWithSuccess: success) } } @@ -82,6 +102,26 @@ AppController.controller.finish(false) drain() failed.prepareReactNative { fallback += 1 } precondition(fallback == 2 && AppController.controller.starts == 1) +reset() +AppController.initializeWithoutStarting() +AppController.controller.start() +AppController.controller.finish(false) +let alreadyFailed = ExpoUpdatesStartup() +var afterFailure = 0 +alreadyFailed.prepareReactNative { afterFailure += 1 } +drain() +precondition(afterFailure == 1 && AppController.controller.starts == 1) +for success in [true, false] { + reset() + AppController.initializeWithoutStarting() + AppController.controller.start() + AppController.controller.beforeDelegateAssignment = { AppController.controller.finish(success) } + let raced = ExpoUpdatesStartup() + var afterRace = 0 + raced.prepareReactNative { afterRace += 1 } + drain() + precondition(afterRace == 1 && AppController.controller.starts == 1) +} // Verify the shared preload path waits, preserves launch options and honors cancellation. final class JSBundleTimings {} final class JSBundleLoadObserver { @@ -120,7 +160,7 @@ cancelled.preloadState.reset() AppController.controller.finish() drain() precondition(BrownfieldReactHostPreloader.loads == 1) -print("PASS: initialization, inactive controllers, concurrent waiters, remount, stop/restart, failure") +print("PASS: initialization, inactive controllers, concurrent waiters, remount, stop/restart, failure, launch finished before the request, launch finished before the delegate") SWIFT xcrun swiftc -module-cache-path "$test_dir/cache" -emit-library -emit-module -module-name EXUpdates \ "$test_dir/EXUpdates.swift" -o "$test_dir/libEXUpdates.dylib" \