RCTEventDispatcher.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 <UIKit/UIKit.h>
  8. #import <React/RCTBridge.h>
  9. typedef NS_ENUM(NSInteger, RCTTextEventType) {
  10. RCTTextEventTypeFocus,
  11. RCTTextEventTypeBlur,
  12. RCTTextEventTypeChange,
  13. RCTTextEventTypeSubmit,
  14. RCTTextEventTypeEnd,
  15. RCTTextEventTypeKeyPress
  16. };
  17. /**
  18. * The threshold at which text inputs will start warning that the JS thread
  19. * has fallen behind (resulting in poor input performance, missed keys, etc.)
  20. */
  21. RCT_EXTERN const NSInteger RCTTextUpdateLagWarningThreshold;
  22. /**
  23. * Takes an input event name and normalizes it to the form that is required
  24. * by the events system (currently that means starting with the "top" prefix,
  25. * but that's an implementation detail that may change in future).
  26. */
  27. RCT_EXTERN NSString *RCTNormalizeInputEventName(NSString *eventName);
  28. @protocol RCTEvent <NSObject>
  29. @required
  30. @property (nonatomic, strong, readonly) NSNumber *viewTag;
  31. @property (nonatomic, copy, readonly) NSString *eventName;
  32. - (BOOL)canCoalesce;
  33. /** used directly for doing a JS call */
  34. + (NSString *)moduleDotMethod;
  35. /** must contain only JSON compatible values */
  36. - (NSArray *)arguments;
  37. @optional
  38. /**
  39. * Coalescing related methods must only be implemented if canCoalesce
  40. * returns YES.
  41. */
  42. @property (nonatomic, assign, readonly) uint16_t coalescingKey;
  43. - (id<RCTEvent>)coalesceWithEvent:(id<RCTEvent>)newEvent;
  44. @end
  45. /**
  46. * This protocol allows observing events dispatched by RCTEventDispatcher.
  47. */
  48. @protocol RCTEventDispatcherObserver <NSObject>
  49. /**
  50. * Called before dispatching an event, on the same thread the event was
  51. * dispatched from.
  52. */
  53. - (void)eventDispatcherWillDispatchEvent:(id<RCTEvent>)event;
  54. @end
  55. /**
  56. * This class wraps the -[RCTBridge enqueueJSCall:args:] method, and
  57. * provides some convenience methods for generating event calls.
  58. */
  59. @interface RCTEventDispatcher : NSObject <RCTBridgeModule>
  60. /**
  61. * Deprecated, do not use.
  62. */
  63. - (void)sendAppEventWithName:(NSString *)name body:(id)body __deprecated_msg("Subclass RCTEventEmitter instead");
  64. /**
  65. * Deprecated, do not use.
  66. */
  67. - (void)sendDeviceEventWithName:(NSString *)name body:(id)body __deprecated_msg("Subclass RCTEventEmitter instead");
  68. /**
  69. * Send a text input/focus event. For internal use only.
  70. */
  71. - (void)sendTextEventWithType:(RCTTextEventType)type
  72. reactTag:(NSNumber *)reactTag
  73. text:(NSString *)text
  74. key:(NSString *)key
  75. eventCount:(NSInteger)eventCount;
  76. /**
  77. * Send a pre-prepared event object.
  78. *
  79. * Events are sent to JS as soon as the thread is free to process them.
  80. * If an event can be coalesced and there is another compatible event waiting, the coalescing will happen immediately.
  81. */
  82. - (void)sendEvent:(id<RCTEvent>)event;
  83. /**
  84. * Add an event dispatcher observer.
  85. */
  86. - (void)addDispatchObserver:(id<RCTEventDispatcherObserver>)observer;
  87. /**
  88. * Remove an event dispatcher observer.
  89. */
  90. - (void)removeDispatchObserver:(id<RCTEventDispatcherObserver>)observer;
  91. @end
  92. @interface RCTBridge (RCTEventDispatcher)
  93. - (RCTEventDispatcher *)eventDispatcher;
  94. @end