RCTTouchEvent.m 2.9 KB

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