@@ -32,13 +32,6 @@ class ReactNativeBrownfieldDelegate: RCTDefaultReactNativeFactoryDelegate {
32
32
private var onBundleLoaded : ( ( ) -> Void ) ?
33
33
private var delegate = ReactNativeBrownfieldDelegate ( )
34
34
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
-
42
35
/**
43
36
* Path to JavaScript root.
44
37
* Default value: "index"
@@ -76,12 +69,18 @@ class ReactNativeBrownfieldDelegate: RCTDefaultReactNativeFactoryDelegate {
76
69
* Default value: nil
77
70
*/
78
71
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
+ }
85
84
86
85
/**
87
86
* Starts React Native with default parameters.
@@ -95,9 +94,11 @@ class ReactNativeBrownfieldDelegate: RCTDefaultReactNativeFactoryDelegate {
95
94
initialProps: [ AnyHashable : Any ] ? ,
96
95
launchOptions: [ AnyHashable : Any ] ? = nil
97
96
) -> UIView ? {
98
- checkFactoryInitialized ( launchOptions: launchOptions)
99
-
100
- return reactNativeFactory? . rootViewFactory. view (
97
+ let resolvedFactory = factory
98
+
99
+ let rootViewFactory = resolvedFactory. rootViewFactory
100
+
101
+ return rootViewFactory. view (
101
102
withModuleName: moduleName,
102
103
initialProperties: initialProps,
103
104
launchOptions: launchOptions
@@ -120,27 +121,31 @@ class ReactNativeBrownfieldDelegate: RCTDefaultReactNativeFactoryDelegate {
120
121
* @param launchOptions Launch options, typically passed from AppDelegate.
121
122
*/
122
123
@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
125
127
126
128
if let onBundleLoaded {
127
129
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
+ }
142
145
}
143
146
}
147
+
148
+ hasStartedReactNative = true
144
149
}
145
150
146
151
/**
@@ -152,13 +157,17 @@ class ReactNativeBrownfieldDelegate: RCTDefaultReactNativeFactoryDelegate {
152
157
return
153
158
}
154
159
155
- guard let factory = reactNativeFactory else { return }
160
+ guard let factory = reactNativeFactory else {
161
+ hasStartedReactNative = false
162
+ return
163
+ }
156
164
157
165
factory. bridge? . invalidate ( )
158
166
159
167
NotificationCenter . default. removeObserver ( self )
160
168
onBundleLoaded = nil
161
169
170
+ hasStartedReactNative = false
162
171
reactNativeFactory = nil
163
172
}
164
173
0 commit comments