2016-02-03 13:22:16 +00:00
/ * *
* 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"
2017-01-12 20:06:53 +00:00
2016-02-03 13:22:18 +00:00
# import "RCTAssert.h"
2016-02-03 13:22:16 +00:00
@ implementation RCTTouchEvent
{
NSArray < NSDictionary * > * _reactTouches ;
NSArray < NSNumber * > * _changedIndexes ;
2016-04-01 13:53:10 +00:00
uint16_t _coalescingKey ;
2016-02-03 13:22:16 +00:00
}
@ synthesize eventName = _eventName ;
@ synthesize viewTag = _viewTag ;
- ( instancetype ) initWithEventName : ( NSString * ) eventName
2016-09-16 23:12:17 +00:00
reactTag : ( NSNumber * ) reactTag
2016-02-03 13:22:16 +00:00
reactTouches : ( NSArray < NSDictionary * > * ) reactTouches
changedIndexes : ( NSArray < NSNumber * > * ) changedIndexes
2016-04-01 13:53:10 +00:00
coalescingKey : ( uint16_t ) coalescingKey
2016-02-03 13:22:16 +00:00
{
if ( self = [ super init ] ) {
2016-09-16 23:12:17 +00:00
_viewTag = reactTag ;
2016-02-03 13:22:16 +00:00
_eventName = eventName ;
_reactTouches = reactTouches ;
_changedIndexes = changedIndexes ;
2016-04-01 13:53:10 +00:00
_coalescingKey = coalescingKey ;
2016-02-03 13:22:16 +00:00
}
return self ;
}
RCT_NOT _IMPLEMENTED ( - ( instancetype ) init )
# pragma mark - RCTEvent
- ( BOOL ) canCoalesce
{
2016-02-03 13:22:18 +00:00
return [ _eventName isEqual : @ "touchMove" ] ;
2016-02-03 13:22:16 +00:00
}
2016-02-03 13:22:18 +00:00
// We coalesce only move events , while holding some assumptions that seem reasonable but there are no explicit guarantees about them .
2016-02-03 13:22:16 +00:00
- ( id < RCTEvent > ) coalesceWithEvent : ( id < RCTEvent > ) newEvent
{
2016-02-03 13:22:18 +00:00
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 ;
2016-02-03 13:22:16 +00:00
}
+ ( NSString * ) moduleDotMethod
{
return @ "RCTEventEmitter.receiveTouches" ;
}
- ( NSArray * ) arguments
{
return @ [ RCTNormalizeInputEventName ( _eventName ) , _reactTouches , _changedIndexes ] ;
}
2016-04-01 13:53:10 +00:00
- ( uint16_t ) coalescingKey
{
return _coalescingKey ;
}
2016-05-24 12:05:51 +00:00
- ( NSString * ) description
{
return [ NSString stringWithFormat : @ "<%@: %p; name = %@; coalescing key = %hu>" , [ self class ] , self , _eventName , _coalescingKey ] ;
}
2016-02-03 13:22:16 +00:00
@ end