mirror of
https://github.com/status-im/react-native.git
synced 2025-01-10 01:25:39 +00:00
060664fd3d
Summary: public The `bridge.modules` dictionary provides access to all native modules, but this API requires that every module is initialized in advance so that any module can be accessed. This diff introduces a better API that will allow modules to be initialized lazily as they are needed, and deprecates `bridge.modules` (modules that use it will still work, but should be rewritten to use `bridge.moduleClasses` or `-[bridge moduleForName/Class:` instead. The rules are now as follows: * Any module that overrides `init` or `setBridge:` will be initialized on the main thread when the bridge is created * Any module that implements `constantsToExport:` will be initialized later when the config is exported (the module itself will be initialized on a background queue, but `constantsToExport:` will still be called on the main thread. * All other modules will be initialized lazily when a method is first called on them. These rules may seem slightly arcane, but they have the advantage of not violating any assumptions that may have been made by existing code - any module written under the original assumption that it would be initialized synchronously on the main thread when the bridge is created should still function exactly the same, but modules that avoid overriding `init` or `setBridge:` will now be loaded lazily. I've rewritten most of the standard modules to take advantage of this new lazy loading, with the following results: Out of the 65 modules included in UIExplorer: * 16 are initialized on the main thread when the bridge is created * A further 8 are initialized when the config is exported to JS * The remaining 41 will be initialized lazily on-demand Reviewed By: jspahrsummers Differential Revision: D2677695 fb-gh-sync-id: 507ae7e9fd6b563e89292c7371767c978e928f33
244 lines
5.3 KiB
Objective-C
244 lines
5.3 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 "RCTEventDispatcher.h"
|
|
|
|
#import "RCTAssert.h"
|
|
#import "RCTBridge.h"
|
|
#import "RCTUtils.h"
|
|
|
|
const NSInteger RCTTextUpdateLagWarningThreshold = 3;
|
|
|
|
NSString *RCTNormalizeInputEventName(NSString *eventName)
|
|
{
|
|
if ([eventName hasPrefix:@"on"]) {
|
|
eventName = [eventName stringByReplacingCharactersInRange:(NSRange){0, 2} withString:@"top"];
|
|
} else if (![eventName hasPrefix:@"top"]) {
|
|
eventName = [[@"top" stringByAppendingString:[eventName substringToIndex:1].uppercaseString]
|
|
stringByAppendingString:[eventName substringFromIndex:1]];
|
|
}
|
|
return eventName;
|
|
}
|
|
|
|
static NSNumber *RCTGetEventID(id<RCTEvent> event)
|
|
{
|
|
return @(
|
|
event.viewTag.intValue |
|
|
(((uint64_t)event.eventName.hash & 0xFFFF) << 32) |
|
|
(((uint64_t)event.coalescingKey) << 48)
|
|
);
|
|
}
|
|
|
|
@implementation RCTBaseEvent
|
|
|
|
@synthesize viewTag = _viewTag;
|
|
@synthesize eventName = _eventName;
|
|
@synthesize body = _body;
|
|
|
|
- (instancetype)initWithViewTag:(NSNumber *)viewTag
|
|
eventName:(NSString *)eventName
|
|
body:(NSDictionary *)body
|
|
{
|
|
if (RCT_DEBUG) {
|
|
RCTAssertParam(eventName);
|
|
}
|
|
|
|
if ((self = [super init])) {
|
|
_viewTag = viewTag;
|
|
_eventName = eventName;
|
|
_body = body;
|
|
}
|
|
return self;
|
|
}
|
|
|
|
RCT_NOT_IMPLEMENTED(- (instancetype)init)
|
|
|
|
- (uint16_t)coalescingKey
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
- (BOOL)canCoalesce
|
|
{
|
|
return YES;
|
|
}
|
|
|
|
- (id<RCTEvent>)coalesceWithEvent:(id<RCTEvent>)newEvent
|
|
{
|
|
return newEvent;
|
|
}
|
|
|
|
+ (NSString *)moduleDotMethod
|
|
{
|
|
return nil;
|
|
}
|
|
|
|
@end
|
|
|
|
@interface RCTEventDispatcher() <RCTFrameUpdateObserver>
|
|
|
|
@end
|
|
|
|
@implementation RCTEventDispatcher
|
|
{
|
|
NSMutableDictionary *_eventQueue;
|
|
NSLock *_eventQueueLock;
|
|
}
|
|
|
|
@synthesize bridge = _bridge;
|
|
@synthesize paused = _paused;
|
|
@synthesize pauseCallback = _pauseCallback;
|
|
|
|
RCT_EXPORT_MODULE()
|
|
|
|
- (void)setBridge:(RCTBridge *)bridge
|
|
{
|
|
_bridge = bridge;
|
|
_paused = YES;
|
|
_eventQueue = [NSMutableDictionary new];
|
|
_eventQueueLock = [NSLock new];
|
|
}
|
|
|
|
- (void)setPaused:(BOOL)paused
|
|
{
|
|
if (_paused != paused) {
|
|
_paused = paused;
|
|
if (_pauseCallback) {
|
|
_pauseCallback();
|
|
}
|
|
}
|
|
}
|
|
|
|
- (void)sendAppEventWithName:(NSString *)name body:(id)body
|
|
{
|
|
[_bridge enqueueJSCall:@"RCTNativeAppEventEmitter.emit"
|
|
args:body ? @[name, body] : @[name]];
|
|
}
|
|
|
|
- (void)sendDeviceEventWithName:(NSString *)name body:(id)body
|
|
{
|
|
[_bridge enqueueJSCall:@"RCTDeviceEventEmitter.emit"
|
|
args:body ? @[name, body] : @[name]];
|
|
}
|
|
|
|
- (void)sendInputEventWithName:(NSString *)name body:(NSDictionary *)body
|
|
{
|
|
if (RCT_DEBUG) {
|
|
RCTAssert([body[@"target"] isKindOfClass:[NSNumber class]],
|
|
@"Event body dictionary must include a 'target' property containing a React tag");
|
|
}
|
|
|
|
name = RCTNormalizeInputEventName(name);
|
|
[_bridge enqueueJSCall:@"RCTEventEmitter.receiveEvent"
|
|
args:body ? @[body[@"target"], name, body] : @[body[@"target"], name]];
|
|
}
|
|
|
|
- (void)sendTextEventWithType:(RCTTextEventType)type
|
|
reactTag:(NSNumber *)reactTag
|
|
text:(NSString *)text
|
|
key:(NSString *)key
|
|
eventCount:(NSInteger)eventCount
|
|
{
|
|
static NSString *events[] = {
|
|
@"focus",
|
|
@"blur",
|
|
@"change",
|
|
@"submitEditing",
|
|
@"endEditing",
|
|
@"keyPress"
|
|
};
|
|
|
|
NSMutableDictionary *body = [[NSMutableDictionary alloc] initWithDictionary:@{
|
|
@"eventCount": @(eventCount),
|
|
@"target": reactTag
|
|
}];
|
|
|
|
if (text) {
|
|
body[@"text"] = text;
|
|
}
|
|
|
|
if (key) {
|
|
if (key.length == 0) {
|
|
key = @"Backspace"; // backspace
|
|
} else {
|
|
switch ([key characterAtIndex:0]) {
|
|
case '\t':
|
|
key = @"Tab";
|
|
break;
|
|
case '\n':
|
|
key = @"Enter";
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
body[@"key"] = key;
|
|
}
|
|
|
|
[self sendInputEventWithName:events[type] body:body];
|
|
}
|
|
|
|
- (void)sendEvent:(id<RCTEvent>)event
|
|
{
|
|
if (!event.canCoalesce) {
|
|
[self dispatchEvent:event];
|
|
return;
|
|
}
|
|
|
|
[_eventQueueLock lock];
|
|
|
|
NSNumber *eventID = RCTGetEventID(event);
|
|
id<RCTEvent> previousEvent = _eventQueue[eventID];
|
|
|
|
if (previousEvent) {
|
|
event = [previousEvent coalesceWithEvent:event];
|
|
}
|
|
|
|
_eventQueue[eventID] = event;
|
|
self.paused = NO;
|
|
|
|
[_eventQueueLock unlock];
|
|
}
|
|
|
|
- (void)dispatchEvent:(id<RCTEvent>)event
|
|
{
|
|
NSMutableArray<id /* any JSON value */> *arguments = [NSMutableArray new];
|
|
|
|
if (event.viewTag) {
|
|
[arguments addObject:event.viewTag];
|
|
}
|
|
|
|
[arguments addObject:RCTNormalizeInputEventName(event.eventName)];
|
|
|
|
if (event.body) {
|
|
[arguments addObject:event.body];
|
|
}
|
|
|
|
[_bridge enqueueJSCall:[[event class] moduleDotMethod] args:arguments];
|
|
}
|
|
|
|
- (dispatch_queue_t)methodQueue
|
|
{
|
|
return RCTJSThread;
|
|
}
|
|
|
|
- (void)didUpdateFrame:(__unused RCTFrameUpdate *)update
|
|
{
|
|
[_eventQueueLock lock];
|
|
NSDictionary *eventQueue = _eventQueue;
|
|
_eventQueue = [NSMutableDictionary new];
|
|
self.paused = YES;
|
|
[_eventQueueLock unlock];
|
|
|
|
for (id<RCTEvent> event in eventQueue.allValues) {
|
|
[self dispatchEvent:event];
|
|
}
|
|
}
|
|
|
|
@end
|