1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | #import "RCTTouchEvent.h"
|
9 |
|
10 | #import "RCTAssert.h"
|
11 |
|
12 | @implementation RCTTouchEvent
|
13 | {
|
14 | NSArray<NSDictionary *> *_reactTouches;
|
15 | NSArray<NSNumber *> *_changedIndexes;
|
16 | uint16_t _coalescingKey;
|
17 | }
|
18 |
|
19 | @synthesize eventName = _eventName;
|
20 | @synthesize viewTag = _viewTag;
|
21 |
|
22 | - (instancetype)initWithEventName:(NSString *)eventName
|
23 | reactTag:(NSNumber *)reactTag
|
24 | reactTouches:(NSArray<NSDictionary *> *)reactTouches
|
25 | changedIndexes:(NSArray<NSNumber *> *)changedIndexes
|
26 | coalescingKey:(uint16_t)coalescingKey
|
27 | {
|
28 | if (self = [super init]) {
|
29 | _viewTag = reactTag;
|
30 | _eventName = eventName;
|
31 | _reactTouches = reactTouches;
|
32 | _changedIndexes = changedIndexes;
|
33 | _coalescingKey = coalescingKey;
|
34 | }
|
35 | return self;
|
36 | }
|
37 |
|
38 | RCT_NOT_IMPLEMENTED(- (instancetype)init)
|
39 |
|
40 | #pragma mark - RCTEvent
|
41 |
|
42 | - (BOOL)canCoalesce
|
43 | {
|
44 | return [_eventName isEqual:@"touchMove"];
|
45 | }
|
46 |
|
47 | // We coalesce only move events, while holding some assumptions that seem reasonable but there are no explicit guarantees about them.
|
48 | - (id<RCTEvent>)coalesceWithEvent:(id<RCTEvent>)newEvent
|
49 | {
|
50 | RCTAssert([newEvent isKindOfClass:[RCTTouchEvent class]], @"Touch event cannot be coalesced with any other type of event, such as provided %@", newEvent);
|
51 | RCTTouchEvent *newTouchEvent = (RCTTouchEvent *)newEvent;
|
52 | RCTAssert([_reactTouches count] == [newTouchEvent->_reactTouches count], @"Touch events have different number of touches. %@ %@", self, newEvent);
|
53 |
|
54 | BOOL newEventIsMoreRecent = NO;
|
55 | BOOL oldEventIsMoreRecent = NO;
|
56 | NSInteger count = _reactTouches.count;
|
57 | for (int i = 0; i<count; i++) {
|
58 | NSDictionary *touch = _reactTouches[i];
|
59 | NSDictionary *newTouch = newTouchEvent->_reactTouches[i];
|
60 | RCTAssert([touch[@"identifier"] isEqual:newTouch[@"identifier"]], @"Touch events doesn't have touches in the same order. %@ %@", touch, newTouch);
|
61 | if ([touch[@"timestamp"] doubleValue] > [newTouch[@"timestamp"] doubleValue]) {
|
62 | oldEventIsMoreRecent = YES;
|
63 | } else {
|
64 | newEventIsMoreRecent = YES;
|
65 | }
|
66 | }
|
67 | RCTAssert(!(oldEventIsMoreRecent && newEventIsMoreRecent), @"Neither touch event is exclusively more recent than the other one. %@ %@", _reactTouches, newTouchEvent->_reactTouches);
|
68 | return newEventIsMoreRecent ? newEvent : self;
|
69 | }
|
70 |
|
71 | + (NSString *)moduleDotMethod
|
72 | {
|
73 | return @"RCTEventEmitter.receiveTouches";
|
74 | }
|
75 |
|
76 | - (NSArray *)arguments
|
77 | {
|
78 | return @[RCTNormalizeInputEventName(_eventName), _reactTouches, _changedIndexes];
|
79 | }
|
80 |
|
81 | - (uint16_t)coalescingKey
|
82 | {
|
83 | return _coalescingKey;
|
84 | }
|
85 |
|
86 | - (NSString *)description
|
87 | {
|
88 | return [NSString stringWithFormat:@"<%@: %p; name = %@; coalescing key = %hu>", [self class], self, _eventName, _coalescingKey];
|
89 | }
|
90 |
|
91 | @end
|