mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
7a1d96e8aa
Summary: In D17880909 I migrated `[bridge reload]` to RCTReloadCommand. One thing I didn't consider too much is the plethora of things which trigger a reload, and the `RCTAssertMainQueue()` constraint. I'd rather use locking (to protect listeners list) than requiring reloads be called from main thread. Changelog: [iOS][Fixed] Fix potential assert firing in RCTReloadCommand Reviewed By: rodrigos-facebook Differential Revision: D18353156 fbshipit-source-id: d20b851fc5fe6c3518dfa3db8f1fc075cf7edee9
57 lines
1.8 KiB
Objective-C
57 lines
1.8 KiB
Objective-C
/*
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*/
|
|
|
|
#import "RCTReloadCommand.h"
|
|
|
|
#import "RCTAssert.h"
|
|
#import "RCTKeyCommands.h"
|
|
#import "RCTUtils.h"
|
|
|
|
static NSHashTable<id<RCTReloadListener>> *listeners;
|
|
static NSLock *listenersLock;
|
|
|
|
NSString *const RCTTriggerReloadCommandNotification = @"RCTTriggerReloadCommandNotification";
|
|
NSString *const RCTTriggerReloadCommandReasonKey = @"reason";
|
|
|
|
void RCTRegisterReloadCommandListener(id<RCTReloadListener> listener)
|
|
{
|
|
if (!listenersLock) {
|
|
listenersLock = [NSLock new];
|
|
}
|
|
[listenersLock lock];
|
|
if (!listeners) {
|
|
listeners = [NSHashTable weakObjectsHashTable];
|
|
}
|
|
#if RCT_DEV
|
|
RCTAssertMainQueue(); // because registerKeyCommandWithInput: must be called on the main thread
|
|
static dispatch_once_t onceToken;
|
|
dispatch_once(&onceToken, ^{
|
|
[[RCTKeyCommands sharedInstance] registerKeyCommandWithInput:@"r"
|
|
modifierFlags:UIKeyModifierCommand
|
|
action:
|
|
^(__unused UIKeyCommand *command) {
|
|
RCTTriggerReloadCommandListeners(@"Command + R");
|
|
}];
|
|
});
|
|
#endif
|
|
[listeners addObject:listener];
|
|
[listenersLock unlock];
|
|
}
|
|
|
|
void RCTTriggerReloadCommandListeners(NSString *reason)
|
|
{
|
|
[listenersLock lock];
|
|
[[NSNotificationCenter defaultCenter] postNotificationName:RCTTriggerReloadCommandNotification
|
|
object:nil
|
|
userInfo:@{RCTTriggerReloadCommandReasonKey: RCTNullIfNil(reason)} ];
|
|
|
|
for (id<RCTReloadListener> l in [listeners allObjects]) {
|
|
[l didReceiveReloadCommand];
|
|
}
|
|
[listenersLock unlock];
|
|
}
|