Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 6c5ac80

Browse files
RSNarafacebook-github-bot
authored andcommittedMay 14, 2021
Bridgeless Mode: Migrate modules away from invokeJS
Summary: This diff removes all synthesize invokeJS = _invokeJS calls, and instead funnels them through synthesize callableJSModules = _callableJSModules. Now, all these NativeModules shouldn't have different branching in bridgeless mode vs bridge mode. Changelog: [Internal] Reviewed By: fkgozali Differential Revision: D28395445 fbshipit-source-id: 41a58d54c60be55e6bf5031e5417728f5eb6285c
1 parent 22ba277 commit 6c5ac80

File tree

8 files changed

+69
-88
lines changed

8 files changed

+69
-88
lines changed
 

‎React/Base/RCTEventDispatcherProtocol.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
#import <UIKit/UIKit.h>
99

1010
#import <React/RCTBridge.h>
11-
#import <React/RCTJSInvokerModule.h>
1211

1312
/**
1413
* The threshold at which text inputs will start warning that the JS thread
@@ -80,7 +79,7 @@ typedef NS_ENUM(NSInteger, RCTTextEventType) {
8079
* This class wraps the -[RCTBridge enqueueJSCall:args:] method, and
8180
* provides some convenience methods for generating event calls.
8281
*/
83-
@protocol RCTEventDispatcherProtocol <RCTBridgeModule, RCTJSDispatcherModule, RCTJSInvokerModule>
82+
@protocol RCTEventDispatcherProtocol <RCTBridgeModule, RCTJSDispatcherModule>
8483

8584
/**
8685
* Deprecated, do not use.

‎React/CoreModules/RCTDevMenu.mm

+3-8
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
#import <React/RCTBundleURLProvider.h>
1313
#import <React/RCTDefines.h>
1414
#import <React/RCTDevSettings.h>
15-
#import <React/RCTJSInvokerModule.h>
1615
#import <React/RCTKeyCommands.h>
1716
#import <React/RCTLog.h>
1817
#import <React/RCTReloadCommand.h>
@@ -86,7 +85,7 @@ - (NSString *)title
8685

8786
typedef void (^RCTDevMenuAlertActionHandler)(UIAlertAction *action);
8887

89-
@interface RCTDevMenu () <RCTBridgeModule, RCTInvalidating, NativeDevMenuSpec, RCTJSInvokerModule>
88+
@interface RCTDevMenu () <RCTBridgeModule, RCTInvalidating, NativeDevMenuSpec>
9089

9190
@end
9291

@@ -97,7 +96,7 @@ @implementation RCTDevMenu {
9796

9897
@synthesize bridge = _bridge;
9998
@synthesize moduleRegistry = _moduleRegistry;
100-
@synthesize invokeJS = _invokeJS;
99+
@synthesize callableJSModules = _callableJSModules;
101100
@synthesize bundleManager = _bundleManager;
102101

103102
RCT_EXPORT_MODULE()
@@ -419,11 +418,7 @@ - (void)setDefaultJSBundle
419418
_presentedItems = items;
420419
[RCTPresentedViewController() presentViewController:_actionSheet animated:YES completion:nil];
421420

422-
if (_bridge) {
423-
[_bridge enqueueJSCall:@"RCTNativeAppEventEmitter" method:@"emit" args:@[ @"RCTDevMenuShown" ] completion:NULL];
424-
} else {
425-
_invokeJS(@"RCTNativeAppEventEmitter", @"emit", @[ @"RCTDevMenuShown" ]);
426-
}
421+
[_callableJSModules invokeModule:@"RCTNativeAppEventEmitter" method:@"emit" withArgs:@[ @"RCTDevMenuShown" ]];
427422
}
428423

429424
- (RCTDevMenuAlertActionHandler)alertActionHandlerForDevItem:(RCTDevMenuItem *__nullable)item

‎React/CoreModules/RCTDevSettings.mm

+12-22
Original file line numberDiff line numberDiff line change
@@ -350,16 +350,12 @@ - (void)_profilingSettingDidChange
350350
#pragma clang diagnostic push
351351
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
352352
if (enabled) {
353-
if (self.bridge) {
354-
[self.bridge enqueueJSCall:@"HMRClient" method:@"enable" args:@[] completion:NULL];
355-
} else if (self.invokeJS) {
356-
self.invokeJS(@"HMRClient", @"enable", @[]);
353+
if (self.callableJSModules) {
354+
[self.callableJSModules invokeModule:@"HMRClient" method:@"enable" withArgs:@[]];
357355
}
358356
} else {
359-
if (self.bridge) {
360-
[self.bridge enqueueJSCall:@"HMRClient" method:@"disable" args:@[] completion:NULL];
361-
} else if (self.invokeJS) {
362-
self.invokeJS(@"HMRClient", @"disable", @[]);
357+
if (self.callableJSModules) {
358+
[self.callableJSModules invokeModule:@"HMRClient" method:@"disable" withArgs:@[]];
363359
}
364360
}
365361
#pragma clang diagnostic pop
@@ -441,27 +437,21 @@ - (void)setupHMRClientWithBundleURL:(NSURL *)bundleURL
441437
NSString *const host = bundleURL.host;
442438
NSNumber *const port = bundleURL.port;
443439
BOOL isHotLoadingEnabled = self.isHotLoadingEnabled;
444-
if (self.bridge) {
445-
[self.bridge enqueueJSCall:@"HMRClient"
446-
method:@"setup"
447-
args:@[ @"ios", path, host, RCTNullIfNil(port), @(isHotLoadingEnabled) ]
448-
completion:NULL];
449-
} else {
450-
self.invokeJS(@"HMRClient", @"setup", @[ @"ios", path, host, RCTNullIfNil(port), @(isHotLoadingEnabled) ]);
440+
if (self.callableJSModules) {
441+
[self.callableJSModules invokeModule:@"HMRClient"
442+
method:@"setup"
443+
withArgs:@[ @"ios", path, host, RCTNullIfNil(port), @(isHotLoadingEnabled) ]];
451444
}
452445
}
453446
}
454447

455448
- (void)setupHMRClientWithAdditionalBundleURL:(NSURL *)bundleURL
456449
{
457450
if (bundleURL && !bundleURL.fileURL) { // isHotLoadingAvailable check
458-
if (self.bridge) {
459-
[self.bridge enqueueJSCall:@"HMRClient"
460-
method:@"registerBundle"
461-
args:@[ [bundleURL absoluteString] ]
462-
completion:NULL];
463-
} else {
464-
self.invokeJS(@"HMRClient", @"registerBundle", @[ [bundleURL absoluteString] ]);
451+
if (self.callableJSModules) {
452+
[self.callableJSModules invokeModule:@"HMRClient"
453+
method:@"registerBundle"
454+
withArgs:@[ [bundleURL absoluteString] ]];
465455
}
466456
}
467457
}

‎React/CoreModules/RCTEventDispatcher.mm

+11-26
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ @implementation RCTEventDispatcher {
4242

4343
@synthesize bridge = _bridge;
4444
@synthesize dispatchToJSThread = _dispatchToJSThread;
45-
@synthesize invokeJS = _invokeJS;
45+
@synthesize callableJSModules = _callableJSModules;
4646

4747
RCT_EXPORT_MODULE()
4848

@@ -59,26 +59,14 @@ - (void)setBridge:(RCTBridge *)bridge
5959

6060
- (void)sendAppEventWithName:(NSString *)name body:(id)body
6161
{
62-
if (_bridge) {
63-
[_bridge enqueueJSCall:@"RCTNativeAppEventEmitter"
64-
method:@"emit"
65-
args:body ? @[ name, body ] : @[ name ]
66-
completion:NULL];
67-
} else if (_invokeJS) {
68-
_invokeJS(@"RCTNativeAppEventEmitter", @"emit", body ? @[ name, body ] : @[ name ]);
69-
}
62+
[_callableJSModules invokeModule:@"RCTNativeAppEventEmitter"
63+
method:@"emit"
64+
withArgs:body ? @[ name, body ] : @[ name ]];
7065
}
7166

7267
- (void)sendDeviceEventWithName:(NSString *)name body:(id)body
7368
{
74-
if (_bridge) {
75-
[_bridge enqueueJSCall:@"RCTDeviceEventEmitter"
76-
method:@"emit"
77-
args:body ? @[ name, body ] : @[ name ]
78-
completion:NULL];
79-
} else if (_invokeJS) {
80-
_invokeJS(@"RCTDeviceEventEmitter", @"emit", body ? @[ name, body ] : @[ name ]);
81-
}
69+
[_callableJSModules invokeModule:@"RCTDeviceEventEmitter" method:@"emit" withArgs:body ? @[ name, body ] : @[ name ]];
8270
}
8371

8472
- (void)sendTextEventWithType:(RCTTextEventType)type
@@ -195,15 +183,12 @@ - (void)removeDispatchObserver:(id<RCTEventDispatcherObserver>)observer
195183

196184
- (void)dispatchEvent:(id<RCTEvent>)event
197185
{
198-
if (_bridge) {
199-
[_bridge enqueueJSCall:[[event class] moduleDotMethod] args:[event arguments]];
200-
} else if (_invokeJS) {
201-
NSString *moduleDotMethod = [[event class] moduleDotMethod];
202-
NSArray<NSString *> *const components = [moduleDotMethod componentsSeparatedByString:@"."];
203-
NSString *const moduleName = components[0];
204-
NSString *const methodName = components[1];
205-
_invokeJS(moduleName, methodName, [event arguments]);
206-
}
186+
NSString *moduleDotMethod = [[event class] moduleDotMethod];
187+
NSArray<NSString *> *const components = [moduleDotMethod componentsSeparatedByString:@"."];
188+
NSString *const moduleName = components[0];
189+
NSString *const methodName = components[1];
190+
191+
[_callableJSModules invokeModule:moduleName method:methodName withArgs:[event arguments]];
207192
}
208193

209194
- (dispatch_queue_t)methodQueue

‎React/CoreModules/RCTKeyboardObserver.mm

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ - (void)stopObserving
6060
#define IMPLEMENT_KEYBOARD_HANDLER(EVENT) \
6161
-(void)EVENT : (NSNotification *)notification \
6262
{ \
63-
if (!self.bridge && !self.invokeJS) { \
63+
if (!self.callableJSModules) { \
6464
return; \
6565
} \
6666
[self sendEventWithName:@ #EVENT body:RCTParseKeyboardNotification(notification)]; \

‎React/Modules/RCTEventEmitter.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,12 @@
66
*/
77

88
#import <React/RCTBridge.h>
9-
#import <React/RCTJSInvokerModule.h>
109

1110
/**
1211
* RCTEventEmitter is an abstract base class to be used for modules that emit
1312
* events to be observed by JS.
1413
*/
15-
@interface RCTEventEmitter : NSObject <RCTBridgeModule, RCTJSInvokerModule, RCTInvalidating>
14+
@interface RCTEventEmitter : NSObject <RCTBridgeModule, RCTInvalidating>
1615

1716
@property (nonatomic, weak) RCTBridge *bridge;
1817
@property (nonatomic, weak) RCTModuleRegistry *moduleRegistry;

‎React/Modules/RCTEventEmitter.m

+8-11
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ @implementation RCTEventEmitter {
1515
BOOL _observationDisabled;
1616
}
1717

18-
@synthesize invokeJS = _invokeJS;
18+
@synthesize callableJSModules = _callableJSModules;
1919

2020
+ (NSString *)moduleName
2121
{
@@ -48,10 +48,10 @@ - (instancetype)initWithDisabledObservation
4848
- (void)sendEventWithName:(NSString *)eventName body:(id)body
4949
{
5050
RCTAssert(
51-
_bridge != nil || _invokeJS != nil,
51+
_callableJSModules != nil,
5252
@"Error when sending event: %@ with body: %@. "
53-
"Bridge is not set. This is probably because you've "
54-
"explicitly synthesized the bridge in %@, even though it's inherited "
53+
"RCTCallableJSModules is not set. This is probably because you've "
54+
"explicitly synthesized the RCTCallableJSModules in %@, even though it's inherited "
5555
"from RCTEventEmitter.",
5656
eventName,
5757
body,
@@ -67,13 +67,10 @@ - (void)sendEventWithName:(NSString *)eventName body:(id)body
6767

6868
BOOL shouldEmitEvent = (_observationDisabled || _listenerCount > 0);
6969

70-
if (shouldEmitEvent && _bridge) {
71-
[_bridge enqueueJSCall:@"RCTDeviceEventEmitter"
72-
method:@"emit"
73-
args:body ? @[ eventName, body ] : @[ eventName ]
74-
completion:NULL];
75-
} else if (shouldEmitEvent && _invokeJS) {
76-
_invokeJS(@"RCTDeviceEventEmitter", @"emit", body ? @[ eventName, body ] : @[ eventName ]);
70+
if (shouldEmitEvent && _callableJSModules) {
71+
[_callableJSModules invokeModule:@"RCTDeviceEventEmitter"
72+
method:@"emit"
73+
withArgs:body ? @[ eventName, body ] : @[ eventName ]];
7774
} else {
7875
RCTLogWarn(@"Sending `%@` with no listeners registered.", eventName);
7976
}

‎packages/rn-tester/RNTesterUnitTests/RCTEventDispatcherTests.m

+32-16
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ @implementation RCTEventDispatcherTests
7575
{
7676
id _bridge;
7777
RCTEventDispatcher *_eventDispatcher;
78+
RCTCallableJSModules *_callableJSModules;
7879

7980
NSString *_eventName;
8081
NSDictionary<NSString *, id> *_body;
@@ -89,8 +90,12 @@ - (void)setUp
8990

9091
_bridge = [OCMockObject mockForClass:[RCTDummyBridge class]];
9192

93+
_callableJSModules = [RCTCallableJSModules new];
94+
[_callableJSModules setBridge:_bridge];
95+
9296
_eventDispatcher = [RCTEventDispatcher new];
9397
[_eventDispatcher setValue:_bridge forKey:@"bridge"];
98+
[_eventDispatcher setValue:_callableJSModules forKey:@"callableJSModules"];
9499

95100
_eventName = RCTNormalizeInputEventName(@"sampleEvent");
96101
_body = @{ @"foo": @"bar" };
@@ -161,8 +166,8 @@ - (void)testRunningTheDispatchedBlockResultInANewOneBeingEnqueued
161166
[_bridge verify];
162167

163168
// eventsEmittingBlock would be called when js is no longer busy, which will result in emitting events
164-
[[_bridge expect] enqueueJSCall:[[_testEvent class] moduleDotMethod]
165-
args:[_testEvent arguments]];
169+
[self _expectBridgeJSCall:[[_testEvent class] moduleDotMethod]
170+
args:[_testEvent arguments]];
166171
eventsEmittingBlock();
167172
[_bridge verify];
168173

@@ -179,8 +184,8 @@ - (void)testBasicCoalescingReturnsLastEvent
179184
eventsEmittingBlock = block;
180185
return YES;
181186
}] queue:RCTJSThread];
182-
[[_bridge expect] enqueueJSCall:[[_testEvent class] moduleDotMethod]
183-
args:[_testEvent arguments]];
187+
[self _expectBridgeJSCall:[[_testEvent class] moduleDotMethod]
188+
args:[_testEvent arguments]];
184189

185190
RCTTestEvent *ignoredEvent = [[RCTTestEvent alloc] initWithViewTag:nil
186191
eventName:_eventName
@@ -206,10 +211,10 @@ - (void)testDifferentEventTypesDontCoalesce
206211
eventsEmittingBlock = block;
207212
return YES;
208213
}] queue:RCTJSThread];
209-
[[_bridge expect] enqueueJSCall:[[_testEvent class] moduleDotMethod]
210-
args:[firstEvent arguments]];
211-
[[_bridge expect] enqueueJSCall:[[_testEvent class] moduleDotMethod]
212-
args:[_testEvent arguments]];
214+
[self _expectBridgeJSCall:[[_testEvent class] moduleDotMethod]
215+
args:[firstEvent arguments]];
216+
[self _expectBridgeJSCall:[[_testEvent class] moduleDotMethod]
217+
args:[_testEvent arguments]];
213218

214219

215220
[_eventDispatcher sendEvent:firstEvent];
@@ -235,10 +240,10 @@ - (void)testDifferentViewTagsDontCoalesce
235240
eventsEmittingBlock = block;
236241
return YES;
237242
}] queue:RCTJSThread];
238-
[[_bridge expect] enqueueJSCall:[[firstEvent class] moduleDotMethod]
239-
args:[firstEvent arguments]];
240-
[[_bridge expect] enqueueJSCall:[[secondEvent class] moduleDotMethod]
241-
args:[secondEvent arguments]];
243+
[self _expectBridgeJSCall:[[firstEvent class] moduleDotMethod]
244+
args:[firstEvent arguments]];
245+
[self _expectBridgeJSCall:[[secondEvent class] moduleDotMethod]
246+
args:[secondEvent arguments]];
242247

243248

244249
[_eventDispatcher sendEvent:firstEvent];
@@ -265,10 +270,10 @@ - (void)testSameEventTypesWithDifferentCoalesceKeysDontCoalesce
265270
eventsEmittingBlock = block;
266271
return YES;
267272
}] queue:RCTJSThread];
268-
[[_bridge expect] enqueueJSCall:[[_testEvent class] moduleDotMethod]
269-
args:[firstEvent arguments]];
270-
[[_bridge expect] enqueueJSCall:[[_testEvent class] moduleDotMethod]
271-
args:[secondEvent arguments]];
273+
[self _expectBridgeJSCall:[[_testEvent class] moduleDotMethod]
274+
args:[firstEvent arguments]];
275+
[self _expectBridgeJSCall:[[_testEvent class] moduleDotMethod]
276+
args:[secondEvent arguments]];
272277

273278

274279
[_eventDispatcher sendEvent:firstEvent];
@@ -284,4 +289,15 @@ - (void)testSameEventTypesWithDifferentCoalesceKeysDontCoalesce
284289
[_bridge verify];
285290
}
286291

292+
-(void)_expectBridgeJSCall:(NSString *)moduleDotMethod args:(NSArray *)args
293+
{
294+
NSArray<NSString *> *const components = [moduleDotMethod componentsSeparatedByString:@"."];
295+
NSString *const moduleName = components[0];
296+
NSString *const methodName = components[1];
297+
[[_bridge expect] enqueueJSCall:moduleName
298+
method:methodName
299+
args:args
300+
completion:NULL];
301+
}
302+
287303
@end

0 commit comments

Comments
 (0)
Please sign in to comment.