2019-10-16 20:03:47 +03:00
|
|
|
/*
|
2018-09-12 01:27:47 +03:00
|
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
2016-12-08 07:04:33 +03:00
|
|
|
*
|
2018-02-17 05:24:55 +03:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2016-12-08 07:04:33 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
#import "RCTReloadCommand.h"
|
|
|
|
|
2017-10-05 23:17:07 +03:00
|
|
|
#import "RCTAssert.h"
|
2016-12-08 07:04:33 +03:00
|
|
|
#import "RCTKeyCommands.h"
|
2019-10-30 22:20:29 +03:00
|
|
|
#import "RCTUtils.h"
|
2016-12-08 07:04:33 +03:00
|
|
|
|
2017-10-05 23:17:07 +03:00
|
|
|
static NSHashTable<id<RCTReloadListener>> *listeners;
|
2019-11-09 02:30:05 +03:00
|
|
|
static NSLock *listenersLock;
|
2019-11-19 03:15:05 +03:00
|
|
|
static NSURL *bundleURL;
|
2017-10-05 23:17:07 +03:00
|
|
|
|
2019-10-30 22:20:29 +03:00
|
|
|
NSString *const RCTTriggerReloadCommandNotification = @"RCTTriggerReloadCommandNotification";
|
|
|
|
NSString *const RCTTriggerReloadCommandReasonKey = @"reason";
|
2019-11-19 03:15:05 +03:00
|
|
|
NSString *const RCTTriggerReloadCommandBundleURLKey = @"bundleURL";
|
2019-10-30 22:20:29 +03:00
|
|
|
|
2016-12-08 07:04:33 +03:00
|
|
|
void RCTRegisterReloadCommandListener(id<RCTReloadListener> listener)
|
|
|
|
{
|
2019-11-09 02:30:05 +03:00
|
|
|
if (!listenersLock) {
|
|
|
|
listenersLock = [NSLock new];
|
|
|
|
}
|
|
|
|
[listenersLock lock];
|
|
|
|
if (!listeners) {
|
|
|
|
listeners = [NSHashTable weakObjectsHashTable];
|
|
|
|
}
|
|
|
|
#if RCT_DEV
|
2017-10-05 23:17:07 +03:00
|
|
|
RCTAssertMainQueue(); // because registerKeyCommandWithInput: must be called on the main thread
|
2016-12-08 07:04:33 +03:00
|
|
|
static dispatch_once_t onceToken;
|
|
|
|
dispatch_once(&onceToken, ^{
|
|
|
|
[[RCTKeyCommands sharedInstance] registerKeyCommandWithInput:@"r"
|
|
|
|
modifierFlags:UIKeyModifierCommand
|
|
|
|
action:
|
|
|
|
^(__unused UIKeyCommand *command) {
|
2019-11-09 02:30:05 +03:00
|
|
|
RCTTriggerReloadCommandListeners(@"Command + R");
|
|
|
|
}];
|
2016-12-08 07:04:33 +03:00
|
|
|
});
|
2019-11-09 02:30:05 +03:00
|
|
|
#endif
|
2017-10-05 23:17:07 +03:00
|
|
|
[listeners addObject:listener];
|
2019-11-09 02:30:05 +03:00
|
|
|
[listenersLock unlock];
|
2017-10-05 23:17:07 +03:00
|
|
|
}
|
2016-12-08 07:04:33 +03:00
|
|
|
|
2019-10-30 22:20:29 +03:00
|
|
|
void RCTTriggerReloadCommandListeners(NSString *reason)
|
2017-10-05 23:17:07 +03:00
|
|
|
{
|
2019-11-09 02:30:05 +03:00
|
|
|
[listenersLock lock];
|
2019-10-30 22:20:29 +03:00
|
|
|
[[NSNotificationCenter defaultCenter] postNotificationName:RCTTriggerReloadCommandNotification
|
|
|
|
object:nil
|
2019-11-19 03:15:05 +03:00
|
|
|
userInfo:@{RCTTriggerReloadCommandReasonKey: RCTNullIfNil(reason),
|
|
|
|
RCTTriggerReloadCommandBundleURLKey: RCTNullIfNil(bundleURL)
|
|
|
|
}];
|
2019-10-30 22:20:29 +03:00
|
|
|
|
2019-11-09 02:30:05 +03:00
|
|
|
for (id<RCTReloadListener> l in [listeners allObjects]) {
|
2017-10-05 23:17:07 +03:00
|
|
|
[l didReceiveReloadCommand];
|
2016-12-08 07:04:33 +03:00
|
|
|
}
|
2019-11-09 02:30:05 +03:00
|
|
|
[listenersLock unlock];
|
2016-12-08 07:04:33 +03:00
|
|
|
}
|
2019-11-19 03:15:05 +03:00
|
|
|
|
|
|
|
void RCTReloadCommandSetBundleURL(NSURL *URL)
|
|
|
|
{
|
|
|
|
bundleURL = URL;
|
|
|
|
}
|