-
Notifications
You must be signed in to change notification settings - Fork 26
feat: add method to deallocate reactNativeFactory instance for iOS #137
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -32,6 +32,13 @@ class ReactNativeBrownfieldDelegate: RCTDefaultReactNativeFactoryDelegate { | |||||
private var onBundleLoaded: (() -> Void)? | ||||||
private var delegate = ReactNativeBrownfieldDelegate() | ||||||
|
||||||
private func checkFactoryInitialized(launchOptions: [AnyHashable: Any]? = nil) { | ||||||
if reactNativeFactory == nil { | ||||||
delegate.dependencyProvider = RCTAppDependencyProvider() | ||||||
self.reactNativeFactory = RCTReactNativeFactory(delegate: delegate) | ||||||
} | ||||||
} | ||||||
|
||||||
/** | ||||||
* Path to JavaScript root. | ||||||
* Default value: "index" | ||||||
|
@@ -88,7 +95,9 @@ class ReactNativeBrownfieldDelegate: RCTDefaultReactNativeFactoryDelegate { | |||||
initialProps: [AnyHashable: Any]?, | ||||||
launchOptions: [AnyHashable: Any]? = nil | ||||||
) -> UIView? { | ||||||
reactNativeFactory?.rootViewFactory.view( | ||||||
checkFactoryInitialized(launchOptions: launchOptions) | ||||||
|
||||||
return reactNativeFactory?.rootViewFactory.view( | ||||||
withModuleName: moduleName, | ||||||
initialProperties: initialProps, | ||||||
launchOptions: launchOptions | ||||||
|
@@ -112,9 +121,7 @@ class ReactNativeBrownfieldDelegate: RCTDefaultReactNativeFactoryDelegate { | |||||
*/ | ||||||
@objc public func startReactNative(onBundleLoaded: (() -> Void)?, launchOptions: [AnyHashable: Any]?) { | ||||||
guard reactNativeFactory == nil else { return } | ||||||
|
||||||
delegate.dependencyProvider = RCTAppDependencyProvider() | ||||||
self.reactNativeFactory = RCTReactNativeFactory(delegate: delegate) | ||||||
checkFactoryInitialized(launchOptions: launchOptions) | ||||||
|
||||||
if let onBundleLoaded { | ||||||
self.onBundleLoaded = onBundleLoaded | ||||||
|
@@ -136,6 +143,25 @@ class ReactNativeBrownfieldDelegate: RCTDefaultReactNativeFactoryDelegate { | |||||
} | ||||||
} | ||||||
|
||||||
/** | ||||||
* Stops React Native and releases the underlying factory instance. | ||||||
*/ | ||||||
@objc public func stopReactNative() { | ||||||
if !Thread.isMainThread { | ||||||
DispatchQueue.main.async { [weak self] in self?.stopReactNative() } | ||||||
return | ||||||
} | ||||||
|
||||||
guard let factory = reactNativeFactory else { return } | ||||||
|
guard let factory = reactNativeFactory else { return } | |
guard let reactNativeFactory else { return } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you test the scenario of what happens when react native view is still shown while calling this? Let's make sure it doesn't crash the app.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe there is a way to detect current react native surfaces in the app and show a warning if the user tries to do this with views still on screen
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we create w getter on the factory that initizalizes it if it's nil? It should remove the need to call this function