Skip to content

Commit aab1339

Browse files
author
Nazar Sydiaha
committed
chore: refactor factory initializer
1 parent 07451b6 commit aab1339

File tree

2 files changed

+35
-38
lines changed

2 files changed

+35
-38
lines changed

example/swift/App.swift

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,25 @@ struct MyApp: App {
1919
struct ContentView: View {
2020
var body: some View {
2121
NavigationView {
22-
VStack {
23-
Text("React Native Brownfield App")
24-
.font(.title)
25-
.bold()
26-
.padding()
27-
.multilineTextAlignment(.center)
28-
29-
NavigationLink("Push React Native Screen") {
30-
ReactNativeView(moduleName: "ReactNative")
31-
.navigationBarHidden(true)
32-
}
33-
34-
Button("Stop React Native") {
35-
ReactNativeBrownfield.shared.stopReactNative()
36-
}
37-
.buttonStyle(PlainButtonStyle())
38-
.padding(.top)
39-
.foregroundColor(.red)
40-
}
22+
VStack {
23+
Text("React Native Brownfield App")
24+
.font(.title)
25+
.bold()
26+
.padding()
27+
.multilineTextAlignment(.center)
28+
29+
NavigationLink("Push React Native Screen") {
30+
ReactNativeView(moduleName: "ReactNative")
31+
.navigationBarHidden(true)
32+
}
33+
34+
Button("Stop React Native") {
35+
ReactNativeBrownfield.shared.stopReactNative()
36+
}
37+
.buttonStyle(PlainButtonStyle())
38+
.padding(.top)
39+
.foregroundColor(.red)
40+
}
4141
}
4242
}
4343
}

ios/ReactNativeBrownfield.swift

Lines changed: 16 additions & 19 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,15 +69,16 @@ 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-
* Always proxies the currently active React Native factory so restarting
82-
* React Native yields a fresh root view factory instance.
83-
*/
84-
private var rootViewFactory: RCTRootViewFactory? {
85-
reactNativeFactory?.rootViewFactory
72+
private var factory: RCTReactNativeFactory {
73+
if let existingFactory = reactNativeFactory {
74+
return existingFactory
75+
}
76+
77+
delegate.dependencyProvider = RCTAppDependencyProvider()
78+
let createdFactory = RCTReactNativeFactory(delegate: delegate)
79+
reactNativeFactory = createdFactory
80+
return createdFactory
8681
}
87-
8882
/**
8983
* Starts React Native with default parameters.
9084
*/
@@ -97,9 +91,11 @@ class ReactNativeBrownfieldDelegate: RCTDefaultReactNativeFactoryDelegate {
9791
initialProps: [AnyHashable: Any]?,
9892
launchOptions: [AnyHashable: Any]? = nil
9993
) -> UIView? {
100-
checkFactoryInitialized(launchOptions: launchOptions)
101-
102-
return rootViewFactory?.view(
94+
let resolvedFactory = factory
95+
96+
let rootViewFactory = resolvedFactory.rootViewFactory
97+
98+
return rootViewFactory.view(
10399
withModuleName: moduleName,
104100
initialProperties: initialProps,
105101
launchOptions: launchOptions
@@ -123,7 +119,8 @@ class ReactNativeBrownfieldDelegate: RCTDefaultReactNativeFactoryDelegate {
123119
*/
124120
@objc public func startReactNative(onBundleLoaded: (() -> Void)?, launchOptions: [AnyHashable: Any]?) {
125121
guard reactNativeFactory == nil else { return }
126-
checkFactoryInitialized(launchOptions: launchOptions)
122+
_ = launchOptions
123+
_ = factory
127124

128125
if let onBundleLoaded {
129126
self.onBundleLoaded = onBundleLoaded

0 commit comments

Comments
 (0)