Skip to content

Commit c11f9c0

Browse files
author
Nazar Sydiaha
committed
chore: refactor factory initializer
1 parent 9264071 commit c11f9c0

File tree

1 file changed

+42
-33
lines changed

1 file changed

+42
-33
lines changed

ios/ReactNativeBrownfield.swift

Lines changed: 42 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,6 @@ class ReactNativeBrownfieldDelegate: RCTDefaultReactNativeFactoryDelegate {
3232
private var onBundleLoaded: (() -> Void)?
3333
private var delegate = ReactNativeBrownfieldDelegate()
3434

35-
private func checkFactoryInitialized(launchOptions: [AnyHashable: Any]? = nil) {
36-
if reactNativeFactory == nil {
37-
delegate.dependencyProvider = RCTAppDependencyProvider()
38-
self.reactNativeFactory = RCTReactNativeFactory(delegate: delegate)
39-
}
40-
}
41-
4235
/**
4336
* Path to JavaScript root.
4437
* Default value: "index"
@@ -76,12 +69,18 @@ class ReactNativeBrownfieldDelegate: RCTDefaultReactNativeFactoryDelegate {
7669
* Default value: nil
7770
*/
7871
private var reactNativeFactory: RCTReactNativeFactory? = nil
79-
/**
80-
* Root view factory used to create React Native views.
81-
*/
82-
lazy private var rootViewFactory: RCTRootViewFactory? = {
83-
return reactNativeFactory?.rootViewFactory
84-
}()
72+
private var hasStartedReactNative = false
73+
74+
private var factory: RCTReactNativeFactory {
75+
if let existingFactory = reactNativeFactory {
76+
return existingFactory
77+
}
78+
79+
delegate.dependencyProvider = RCTAppDependencyProvider()
80+
let createdFactory = RCTReactNativeFactory(delegate: delegate)
81+
reactNativeFactory = createdFactory
82+
return createdFactory
83+
}
8584

8685
/**
8786
* Starts React Native with default parameters.
@@ -95,9 +94,11 @@ class ReactNativeBrownfieldDelegate: RCTDefaultReactNativeFactoryDelegate {
9594
initialProps: [AnyHashable: Any]?,
9695
launchOptions: [AnyHashable: Any]? = nil
9796
) -> UIView? {
98-
checkFactoryInitialized(launchOptions: launchOptions)
99-
100-
return reactNativeFactory?.rootViewFactory.view(
97+
let resolvedFactory = factory
98+
99+
guard let rootViewFactory = resolvedFactory.rootViewFactory else { return nil }
100+
101+
return rootViewFactory.view(
101102
withModuleName: moduleName,
102103
initialProperties: initialProps,
103104
launchOptions: launchOptions
@@ -120,27 +121,31 @@ class ReactNativeBrownfieldDelegate: RCTDefaultReactNativeFactoryDelegate {
120121
* @param launchOptions Launch options, typically passed from AppDelegate.
121122
*/
122123
@objc public func startReactNative(onBundleLoaded: (() -> Void)?, launchOptions: [AnyHashable: Any]?) {
123-
guard reactNativeFactory == nil else { return }
124-
checkFactoryInitialized(launchOptions: launchOptions)
124+
guard !hasStartedReactNative else { return }
125+
_ = launchOptions
126+
_ = factory
125127

126128
if let onBundleLoaded {
127129
self.onBundleLoaded = onBundleLoaded
128-
if RCTIsNewArchEnabled() {
129-
NotificationCenter.default.addObserver(
130-
self,
131-
selector: #selector(jsLoaded),
132-
name: NSNotification.Name("RCTInstanceDidLoadBundle"),
133-
object: nil
134-
)
135-
} else {
136-
NotificationCenter.default.addObserver(
137-
self,
138-
selector: #selector(jsLoaded),
139-
name: NSNotification.Name("RCTJavaScriptDidLoadNotification"),
140-
object: nil
141-
)
130+
let notificationName: Notification.Name = RCTIsNewArchEnabled()
131+
? Notification.Name("RCTInstanceDidLoadBundle")
132+
: Notification.Name("RCTJavaScriptDidLoadNotification")
133+
134+
NotificationCenter.default.addObserver(
135+
self,
136+
selector: #selector(jsLoaded),
137+
name: notificationName,
138+
object: nil
139+
)
140+
141+
if let bridge = reactNativeFactory?.bridge, !bridge.isLoading {
142+
DispatchQueue.main.async { [weak self] in
143+
self?.jsLoaded(Notification(name: notificationName))
144+
}
142145
}
143146
}
147+
148+
hasStartedReactNative = true
144149
}
145150

146151
/**
@@ -152,13 +157,17 @@ class ReactNativeBrownfieldDelegate: RCTDefaultReactNativeFactoryDelegate {
152157
return
153158
}
154159

155-
guard let factory = reactNativeFactory else { return }
160+
guard let factory = reactNativeFactory else {
161+
hasStartedReactNative = false
162+
return
163+
}
156164

157165
factory.bridge?.invalidate()
158166

159167
NotificationCenter.default.removeObserver(self)
160168
onBundleLoaded = nil
161169

170+
hasStartedReactNative = false
162171
reactNativeFactory = nil
163172
}
164173

0 commit comments

Comments
 (0)