mirror of
https://github.com/status-im/react-native.git
synced 2025-01-09 09:12:02 +00:00
2e8eb652e1
Summary: This is a final diff in the stack, which makes us not send a bazillion events to js after it's been busy while an user dragged his finger on a screen (resulting in ~two events per frame). I made "touchMove" event to be coalescable, which makes us not send anything while dragging (since this makes both scroll events and touch move events coalesced and not being send until either next js frame or the next different touch event occurs). This change is far from perfect. The event name is a hard coded string and the coalescing works with some (reasonable) assumptions on internal structure of these touch events. Which may or may not be really true. It would be great if someone could comment on these. I'm thinking about making the touches more strongly typed. Any thoughts on this? 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: D2884595 fb-gh-sync-id: f3c2f13430679e2bf52e0c7a3689650b3acae42f
78 lines
2.5 KiB
Objective-C
78 lines
2.5 KiB
Objective-C
/**
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
* All rights reserved.
|
|
*
|
|
* This source code is licensed under the BSD-style license found in the
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
*/
|
|
|
|
#import "RCTTouchEvent.h"
|
|
#import "RCTAssert.h"
|
|
|
|
@implementation RCTTouchEvent
|
|
{
|
|
NSArray<NSDictionary *> *_reactTouches;
|
|
NSArray<NSNumber *> *_changedIndexes;
|
|
}
|
|
|
|
@synthesize eventName = _eventName;
|
|
@synthesize viewTag = _viewTag;
|
|
|
|
- (instancetype)initWithEventName:(NSString *)eventName
|
|
reactTouches:(NSArray<NSDictionary *> *)reactTouches
|
|
changedIndexes:(NSArray<NSNumber *> *)changedIndexes
|
|
{
|
|
if (self = [super init]) {
|
|
_eventName = eventName;
|
|
_reactTouches = reactTouches;
|
|
_changedIndexes = changedIndexes;
|
|
}
|
|
return self;
|
|
}
|
|
|
|
RCT_NOT_IMPLEMENTED(- (instancetype)init)
|
|
|
|
#pragma mark - RCTEvent
|
|
|
|
- (BOOL)canCoalesce
|
|
{
|
|
return [_eventName isEqual:@"touchMove"];
|
|
}
|
|
|
|
// We coalesce only move events, while holding some assumptions that seem reasonable but there are no explicit guarantees about them.
|
|
- (id<RCTEvent>)coalesceWithEvent:(id<RCTEvent>)newEvent
|
|
{
|
|
RCTAssert([newEvent isKindOfClass:[RCTTouchEvent class]], @"Touch event cannot be coalesced with any other type of event, such as provided %@", newEvent);
|
|
RCTTouchEvent *newTouchEvent = (RCTTouchEvent *)newEvent;
|
|
RCTAssert([_reactTouches count] == [newTouchEvent->_reactTouches count], @"Touch events have different number of touches. %@ %@", self, newEvent);
|
|
|
|
BOOL newEventIsMoreRecent = NO;
|
|
BOOL oldEventIsMoreRecent = NO;
|
|
NSInteger count = _reactTouches.count;
|
|
for (int i = 0; i<count; i++) {
|
|
NSDictionary *touch = _reactTouches[i];
|
|
NSDictionary *newTouch = newTouchEvent->_reactTouches[i];
|
|
RCTAssert([touch[@"identifier"] isEqual:newTouch[@"identifier"]], @"Touch events doesn't have touches in the same order. %@ %@", touch, newTouch);
|
|
if ([touch[@"timestamp"] doubleValue] > [newTouch[@"timestamp"] doubleValue]) {
|
|
oldEventIsMoreRecent = YES;
|
|
} else {
|
|
newEventIsMoreRecent = YES;
|
|
}
|
|
}
|
|
RCTAssert(!(oldEventIsMoreRecent && newEventIsMoreRecent), @"Neither touch event is exclusively more recent than the other one. %@ %@", _reactTouches, newTouchEvent->_reactTouches);
|
|
return newEventIsMoreRecent ? newEvent : self;
|
|
}
|
|
|
|
+ (NSString *)moduleDotMethod
|
|
{
|
|
return @"RCTEventEmitter.receiveTouches";
|
|
}
|
|
|
|
- (NSArray *)arguments
|
|
{
|
|
return @[RCTNormalizeInputEventName(_eventName), _reactTouches, _changedIndexes];
|
|
}
|
|
|
|
@end
|