Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/ios-updates-startup-finished-launch.md
Original file line number Diff line number Diff line change
@@ -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.
24 changes: 19 additions & 5 deletions packages/react-native-brownfield/ios/Expo/ExpoUpdatesStartup.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,35 @@ 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
}
updatesCallbacks.append(completion)
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

Expand All @@ -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()
}
}
}
Expand Down
46 changes: 43 additions & 3 deletions packages/react-native-brownfield/tests/ios/run-expo-startup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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")
Expand All @@ -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)
}
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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" \
Expand Down