@@ -32,6 +32,13 @@ 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
+
35
42
/**
36
43
* Path to JavaScript root.
37
44
* Default value: "index"
@@ -88,7 +95,9 @@ class ReactNativeBrownfieldDelegate: RCTDefaultReactNativeFactoryDelegate {
88
95
initialProps: [ AnyHashable : Any ] ? ,
89
96
launchOptions: [ AnyHashable : Any ] ? = nil
90
97
) -> UIView ? {
91
- reactNativeFactory? . rootViewFactory. view (
98
+ checkFactoryInitialized ( launchOptions: launchOptions)
99
+
100
+ return reactNativeFactory? . rootViewFactory. view (
92
101
withModuleName: moduleName,
93
102
initialProperties: initialProps,
94
103
launchOptions: launchOptions
@@ -112,9 +121,7 @@ class ReactNativeBrownfieldDelegate: RCTDefaultReactNativeFactoryDelegate {
112
121
*/
113
122
@objc public func startReactNative( onBundleLoaded: ( ( ) -> Void ) ? , launchOptions: [ AnyHashable : Any ] ? ) {
114
123
guard reactNativeFactory == nil else { return }
115
-
116
- delegate. dependencyProvider = RCTAppDependencyProvider ( )
117
- self . reactNativeFactory = RCTReactNativeFactory ( delegate: delegate)
124
+ checkFactoryInitialized ( launchOptions: launchOptions)
118
125
119
126
if let onBundleLoaded {
120
127
self . onBundleLoaded = onBundleLoaded
@@ -136,6 +143,25 @@ class ReactNativeBrownfieldDelegate: RCTDefaultReactNativeFactoryDelegate {
136
143
}
137
144
}
138
145
146
+ /**
147
+ * Stops React Native and releases the underlying factory instance.
148
+ */
149
+ @objc public func stopReactNative( ) {
150
+ if !Thread. isMainThread {
151
+ DispatchQueue . main. async { [ weak self] in self ? . stopReactNative ( ) }
152
+ return
153
+ }
154
+
155
+ guard let factory = reactNativeFactory else { return }
156
+
157
+ factory. bridge? . invalidate ( )
158
+
159
+ NotificationCenter . default. removeObserver ( self )
160
+ onBundleLoaded = nil
161
+
162
+ reactNativeFactory = nil
163
+ }
164
+
139
165
@objc private func jsLoaded( _ notification: Notification ) {
140
166
onBundleLoaded ? ( )
141
167
onBundleLoaded = nil
0 commit comments