RCTEvent protocol changes (3/7)

Summary:
I want to use the `RCTEvent` protocol for touch events as well. That's why I'm removing not very well defined `body` property and replacing it with `arguments` method, which will return an array that will be passed directly to the js call.
I think this makes sense because there is no unified arguments format for all events and and the called  js method (`moduleDotMethod`) is already event specific.
This way touch events and scroll events can result in calling a completely different js function with a completely different arguments (what they indeed currently do).

public
___
//This diff is part of a larger stack. For high level overview what's going on jump to D2884593.//

Reviewed By: nicklockwood

Differential Revision: D2884590

fb-gh-sync-id: 2c1885c3414e255d8572c0fbbbfe62a23d94dd06
This commit is contained in:
Martin Kralik 2016-02-03 05:22:14 -08:00 committed by facebook-github-bot-4
parent 3e89c3ea3b
commit 7f2b72528e
4 changed files with 20 additions and 21 deletions

View File

@ -29,7 +29,6 @@
@synthesize viewTag = _viewTag; @synthesize viewTag = _viewTag;
@synthesize eventName = _eventName; @synthesize eventName = _eventName;
@synthesize body = _body;
- (instancetype)initWithViewTag:(NSNumber *)viewTag eventName:(NSString *)eventName body:(NSDictionary<NSString *, id> *)body - (instancetype)initWithViewTag:(NSNumber *)viewTag eventName:(NSString *)eventName body:(NSDictionary<NSString *, id> *)body
{ {
@ -52,6 +51,11 @@
return @"RCTDeviceEventEmitter.emit"; return @"RCTDeviceEventEmitter.emit";
} }
- (NSArray *)arguments
{
return @[_eventName, _body];
}
@end @end
@interface RCTEventDispatcherTests : XCTestCase @interface RCTEventDispatcherTests : XCTestCase
@ -89,7 +93,7 @@
- (void)testLegacyEventsAreImmediatelyDispatched - (void)testLegacyEventsAreImmediatelyDispatched
{ {
[[_bridge expect] enqueueJSCall:_JSMethod [[_bridge expect] enqueueJSCall:_JSMethod
args:@[_eventName, _body]]; args:[_testEvent arguments]];
[_eventDispatcher sendDeviceEventWithName:_eventName body:_body]; [_eventDispatcher sendDeviceEventWithName:_eventName body:_body];
@ -100,7 +104,7 @@
{ {
_testEvent.canCoalesce = NO; _testEvent.canCoalesce = NO;
[[_bridge expect] enqueueJSCall:_JSMethod [[_bridge expect] enqueueJSCall:_JSMethod
args:@[_eventName, _body]]; args:[_testEvent arguments]];
[_eventDispatcher sendEvent:_testEvent]; [_eventDispatcher sendEvent:_testEvent];
@ -112,7 +116,7 @@
[_eventDispatcher sendEvent:_testEvent]; [_eventDispatcher sendEvent:_testEvent];
[[_bridge expect] enqueueJSCall:@"RCTDeviceEventEmitter.emit" [[_bridge expect] enqueueJSCall:@"RCTDeviceEventEmitter.emit"
args:@[_eventName, _body]]; args:[_testEvent arguments]];
[(id<RCTFrameUpdateObserver>)_eventDispatcher didUpdateFrame:nil]; [(id<RCTFrameUpdateObserver>)_eventDispatcher didUpdateFrame:nil];
@ -129,7 +133,7 @@
[_eventDispatcher sendEvent:_testEvent]; [_eventDispatcher sendEvent:_testEvent];
[[_bridge expect] enqueueJSCall:@"RCTDeviceEventEmitter.emit" [[_bridge expect] enqueueJSCall:@"RCTDeviceEventEmitter.emit"
args:@[_eventName, _body]]; args:[_testEvent arguments]];
[(id<RCTFrameUpdateObserver>)_eventDispatcher didUpdateFrame:nil]; [(id<RCTFrameUpdateObserver>)_eventDispatcher didUpdateFrame:nil];
@ -147,10 +151,10 @@
[_eventDispatcher sendEvent:_testEvent]; [_eventDispatcher sendEvent:_testEvent];
[[_bridge expect] enqueueJSCall:@"RCTDeviceEventEmitter.emit" [[_bridge expect] enqueueJSCall:@"RCTDeviceEventEmitter.emit"
args:@[firstEventName, _body]]; args:[firstEvent arguments]];
[[_bridge expect] enqueueJSCall:@"RCTDeviceEventEmitter.emit" [[_bridge expect] enqueueJSCall:@"RCTDeviceEventEmitter.emit"
args:@[_eventName, _body]]; args:[_testEvent arguments]];
[(id<RCTFrameUpdateObserver>)_eventDispatcher didUpdateFrame:nil]; [(id<RCTFrameUpdateObserver>)_eventDispatcher didUpdateFrame:nil];

View File

@ -48,12 +48,14 @@ RCT_EXTERN NSString *RCTNormalizeInputEventName(NSString *eventName);
@property (nonatomic, strong, readonly) NSNumber *viewTag; @property (nonatomic, strong, readonly) NSNumber *viewTag;
@property (nonatomic, copy, readonly) NSString *eventName; @property (nonatomic, copy, readonly) NSString *eventName;
@property (nonatomic, copy, readonly) NSDictionary *body;
- (BOOL)canCoalesce; - (BOOL)canCoalesce;
- (id<RCTEvent>)coalesceWithEvent:(id<RCTEvent>)newEvent; - (id<RCTEvent>)coalesceWithEvent:(id<RCTEvent>)newEvent;
// used directly for doing a JS call
+ (NSString *)moduleDotMethod; + (NSString *)moduleDotMethod;
// must contain only JSON compatible values
- (NSArray *)arguments;
@end @end

View File

@ -159,19 +159,7 @@ RCT_EXPORT_MODULE()
- (void)dispatchEvent:(id<RCTEvent>)event - (void)dispatchEvent:(id<RCTEvent>)event
{ {
NSMutableArray<id /* any JSON value */> *arguments = [NSMutableArray new]; [_bridge enqueueJSCall:[[event class] moduleDotMethod] args:[event arguments]];
if (event.viewTag) {
[arguments addObject:event.viewTag];
}
[arguments addObject:RCTNormalizeInputEventName(event.eventName)];
if (event.body) {
[arguments addObject:event.body];
}
[_bridge enqueueJSCall:[[event class] moduleDotMethod] args:arguments];
} }
- (dispatch_queue_t)methodQueue - (dispatch_queue_t)methodQueue

View File

@ -129,6 +129,11 @@ RCT_NOT_IMPLEMENTED(- (instancetype)init)
return @"RCTEventEmitter.receiveEvent"; return @"RCTEventEmitter.receiveEvent";
} }
- (NSArray *)arguments
{
return @[self.viewTag, RCTNormalizeInputEventName(self.eventName), [self body]];
}
@end @end
/** /**