-
Notifications
You must be signed in to change notification settings - Fork 95
/
Copy pathReactInstance.swift
240 lines (205 loc) · 7.19 KB
/
ReactInstance.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// This source code is licensed under the MIT license found in the
// LICENSE file in the root directory of this source tree.
//
import Foundation
final class ReactInstance: NSObject, RCTBridgeDelegate {
public static let scanForQRCodeNotification =
NSNotification.Name("ReactInstance.scanForQRCodeNotification")
static func jsBundleURL() -> URL? {
RCTBundleURLProvider.sharedSettings().jsBundleURL(
forBundleRoot: "index",
fallbackResource: nil
)
}
var remoteBundleURL: URL? {
didSet {
initReact(onDidInitialize: { /* noop */ })
}
}
private(set) var bridge: RCTBridge?
override init() {
#if DEBUG
remoteBundleURL = ReactInstance.jsBundleURL()
#endif
super.init()
// Turbo Modules is incompatible with remote JS debugging
RCTEnableTurboModule(false)
RCTSetFatalHandler { (error: Error?) in
guard let error = error else {
print("Unknown error")
return
}
guard let nsError = error as NSError? else {
print(error.localizedDescription)
return
}
let message = RCTFormatError(
nsError.localizedDescription,
nsError.userInfo[RCTJSStackTraceKey] as? [[String: Any]],
9001
)
print(message ?? nsError.localizedDescription)
}
NotificationCenter.default.addObserver(
self,
selector: #selector(onJavaScriptLoading(_:)),
name: .RCTJavaScriptWillStartLoading,
object: nil
)
NotificationCenter.default.addObserver(
self,
selector: #selector(onJavaScriptLoaded(_:)),
name: .RCTJavaScriptDidLoad,
object: nil
)
#if os(iOS)
NotificationCenter.default.addObserver(
self,
selector: #selector(onRemoteBundleURLReceived(_:)),
name: .didReceiveRemoteBundleURL,
object: nil
)
#endif
#if USE_FLIPPER
if let flipper = FlipperClient.shared() {
flipper.add(FlipperKitLayoutPlugin(
rootNode: UIApplication.shared,
with: SKDescriptorMapper(defaults: ())
))
flipper.add(FKUserDefaultsPlugin(suiteName: nil))
flipper.add(FlipperKitReactPlugin())
flipper.add(FlipperKitNetworkPlugin(networkAdapter: SKIOSNetworkAdapter()))
flipper.start()
}
#endif
}
init(forTestingPurposesOnly: Bool) {
assert(forTestingPurposesOnly)
}
func initReact(onDidInitialize: @escaping () -> Void) {
if bridge == nil {
NotificationCenter.default.post(
name: .ReactTestAppWillInitializeReactNative,
object: nil
)
}
if let bridge = bridge {
if remoteBundleURL == nil {
// When loading the embedded bundle, we must disable remote
// debugging to prevent the bridge from getting stuck in
// -[RCTWebSocketExecutor executeApplicationScript:sourceURL:onComplete:]
RCTDevSettings().isDebuggingRemotely = false
}
RTATriggerReloadCommand(bridge, "ReactTestApp")
} else if let bridge = RCTBridge(delegate: self, launchOptions: nil) {
self.bridge = bridge
NotificationCenter.default.post(
name: .ReactTestAppDidInitializeReactNative,
object: bridge
)
onDidInitialize()
} else {
assertionFailure("Failed to instantiate RCTBridge")
}
}
// MARK: - RCTBridgeDelegate details
func sourceURL(for _: RCTBridge?) -> URL? {
if let remoteBundleURL = remoteBundleURL {
return remoteBundleURL
}
#if os(iOS)
let possibleEntryFiles = [
"index.ios",
"main.ios",
"index.mobile",
"main.mobile",
"index.native",
"main.native",
"index",
"main",
]
#elseif os(macOS)
let possibleEntryFiles = [
"index.macos",
"main.macos",
"index.native",
"main.native",
"index",
"main",
]
#endif
let jsBundleURL = possibleEntryFiles
.lazy
.map {
Bundle.main.url(
forResource: $0,
withExtension: "jsbundle"
)
}
.first(where: { $0 != nil })
return jsBundleURL ?? ReactInstance.jsBundleURL()
}
func extraModules(for _: RCTBridge!) -> [RCTBridgeModule] {
[]
}
// MARK: - Private
@objc private func onJavaScriptLoaded(_ notification: Notification) {
guard let bridge = notification.userInfo?["bridge"] as? RCTBridge,
let currentBundleURL = bridge.bundleURL
else {
return
}
RCTExecuteOnMainQueue { [weak self] in
guard let devMenu = bridge.devMenu else {
return
}
devMenu.add(RCTDevMenuItem.buttonItem(
titleBlock: {
currentBundleURL.isFileURL
? "Load From Dev Server"
: "Load Embedded JS Bundle"
},
handler: {
guard let strongSelf = self else {
return
}
if currentBundleURL.isFileURL {
strongSelf.remoteBundleURL = ReactInstance.jsBundleURL()
} else {
strongSelf.remoteBundleURL = nil
}
}
))
#if os(iOS) && !targetEnvironment(simulator)
devMenu.add(RCTDevMenuItem.buttonItem(withTitle: "Scan QR Code") {
NotificationCenter.default.post(
name: ReactInstance.scanForQRCodeNotification,
object: self
)
})
#endif
}
}
@objc private func onJavaScriptLoading(_ notification: Notification) {
guard self.bridge != nil else {
// This is a cold boot. The bridge will be set in initReact(onDidInitialize:).
return
}
let bridge = notification.userInfo?["bridge"] as? RCTBridge
if bridge != self.bridge {
self.bridge = bridge
}
}
@objc private func onRemoteBundleURLReceived(_ notification: Notification) {
guard let value = notification.userInfo?["value"] as? String,
var urlComponents = URLComponents(string: value)
else {
return
}
urlComponents.queryItems = [URLQueryItem(name: "platform", value: "ios")]
remoteBundleURL = urlComponents.url
}
}