mirror of
https://github.com/status-im/react-native.git
synced 2025-01-10 17:45:59 +00:00
1d3db4c5dc
Summary:Previously, (mostly touch and scroll) event handling on iOS worked in a hybrid way: * All incoming coalesce-able events would be pooled and retrieved by js thread in the beginning of its frame (all of this happens on js thread) * Any non-coalesce-able event would be immediately dispatched on a js thread (triggered from main thread), and if there would be pooled coalesce-able events they would be immediately dispatched at first too. This behavior has a subtle race condition, where two events are produced (on MT) in one order and received in js in different order. See https://github.com/facebook/react-native/issues/5246#issuecomment-198326673 for further explanation of this case. The new event handling is (afaik) what Android already does. When an event comes we add it into a pool of events and dispatch a block on js thread to inform js there are events to be processed. We keep track of whether we did so, so there is at most one of these blocks waiting to be processed. When the block is executed js will process all events that are in pool at that time (NOT at time of enqueuing the block). This creates a single way of processing events and makes it impossible to process them in different order in js. The tricky part was making sure we don't coalesce events across gestures/different scrolls. Before this was achieved by knowing that gestures and scrolls start/end with non-coalesce-able event, so the pool never contained events that shouldn't be coalesced together. That "assumption" doesn't hold now. I've re-added `coalescingKey` and made touch and scroll events use it to prevent coalescing events of the same type that should remain separate in previous diffs (see dependencies). On top of it it decreases latency in events processing in case where we get only coalesce-able events. Previously these would be processed at begging of the next js frame, even when js would be free and could process them sooner. This delay is done, since they would get processed as soon as the enqueued block would run. To illustrate this improvement let's look at these two systraces. Before: https://cloud.githubusercontent.com/assets/713625/14021417/47b35b7a-f1d3-11e5-93dd-4363edfa1923.png After: https://cloud.githubusercontent.com/assets/713625/14021415/4798582a-f1d3-11e5-8715-425596e0781c.png Reviewed By: javache Differential Revision: D3092867 fb-gh-sync-id: 29071780f00fcddb0b1886a46caabdb3da1d5d84 fbshipit-source-id: 29071780f00fcddb0b1886a46caabdb3da1d5d84
106 lines
3.1 KiB
Objective-C
106 lines
3.1 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 <UIKit/UIKit.h>
|
|
|
|
#import "RCTBridge.h"
|
|
|
|
typedef NS_ENUM(NSInteger, RCTTextEventType) {
|
|
RCTTextEventTypeFocus,
|
|
RCTTextEventTypeBlur,
|
|
RCTTextEventTypeChange,
|
|
RCTTextEventTypeSubmit,
|
|
RCTTextEventTypeEnd,
|
|
RCTTextEventTypeKeyPress
|
|
};
|
|
|
|
typedef NS_ENUM(NSInteger, RCTScrollEventType) {
|
|
RCTScrollEventTypeStart,
|
|
RCTScrollEventTypeMove,
|
|
RCTScrollEventTypeEnd,
|
|
RCTScrollEventTypeStartDeceleration,
|
|
RCTScrollEventTypeEndDeceleration,
|
|
RCTScrollEventTypeEndAnimation,
|
|
};
|
|
|
|
/**
|
|
* The threshold at which text inputs will start warning that the JS thread
|
|
* has fallen behind (resulting in poor input performance, missed keys, etc.)
|
|
*/
|
|
RCT_EXTERN const NSInteger RCTTextUpdateLagWarningThreshold;
|
|
|
|
/**
|
|
* Takes an input event name and normalizes it to the form that is required
|
|
* by the events system (currently that means starting with the "top" prefix,
|
|
* but that's an implementation detail that may change in future).
|
|
*/
|
|
RCT_EXTERN NSString *RCTNormalizeInputEventName(NSString *eventName);
|
|
|
|
@protocol RCTEvent <NSObject>
|
|
|
|
@required
|
|
|
|
@property (nonatomic, strong, readonly) NSNumber *viewTag;
|
|
@property (nonatomic, copy, readonly) NSString *eventName;
|
|
@property (nonatomic, assign, readonly) uint16_t coalescingKey;
|
|
|
|
- (BOOL)canCoalesce;
|
|
- (id<RCTEvent>)coalesceWithEvent:(id<RCTEvent>)newEvent;
|
|
|
|
// used directly for doing a JS call
|
|
+ (NSString *)moduleDotMethod;
|
|
// must contain only JSON compatible values
|
|
- (NSArray *)arguments;
|
|
|
|
@end
|
|
|
|
|
|
/**
|
|
* This class wraps the -[RCTBridge enqueueJSCall:args:] method, and
|
|
* provides some convenience methods for generating event calls.
|
|
*/
|
|
@interface RCTEventDispatcher : NSObject <RCTBridgeModule>
|
|
|
|
/**
|
|
* Send an application-specific event that does not relate to a specific
|
|
* view, e.g. a navigation or data update notification.
|
|
*/
|
|
- (void)sendAppEventWithName:(NSString *)name body:(id)body;
|
|
|
|
/**
|
|
* Send a device or iOS event that does not relate to a specific view,
|
|
* e.g.rotation, location, keyboard show/hide, background/awake, etc.
|
|
*/
|
|
- (void)sendDeviceEventWithName:(NSString *)name body:(id)body;
|
|
|
|
/**
|
|
* Send a user input event. The body dictionary must contain a "target"
|
|
* parameter, representing the React tag of the view sending the event
|
|
*/
|
|
- (void)sendInputEventWithName:(NSString *)name body:(NSDictionary *)body;
|
|
|
|
/**
|
|
* Send a text input/focus event.
|
|
*/
|
|
- (void)sendTextEventWithType:(RCTTextEventType)type
|
|
reactTag:(NSNumber *)reactTag
|
|
text:(NSString *)text
|
|
key:(NSString *)key
|
|
eventCount:(NSInteger)eventCount;
|
|
|
|
/**
|
|
* Send a pre-prepared event object.
|
|
*
|
|
* Events are sent to JS as soon as the thread is free to process them.
|
|
* If an event can be coalesced and there is another compatible event waiting, the coalescing will happen immediately.
|
|
*/
|
|
- (void)sendEvent:(id<RCTEvent>)event;
|
|
|
|
@end
|