1/*
2 * Copyright (c) Meta Platforms, Inc. and 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
8#import "ABI48_0_0RCTReloadCommand.h"
9
10#import "ABI48_0_0RCTAssert.h"
11#import "ABI48_0_0RCTKeyCommands.h"
12#import "ABI48_0_0RCTUtils.h"
13
14static NSHashTable<id<ABI48_0_0RCTReloadListener>> *listeners;
15static NSLock *listenersLock;
16static NSURL *bundleURL;
17
18NSString *const ABI48_0_0RCTTriggerReloadCommandNotification = @"ABI48_0_0RCTTriggerReloadCommandNotification";
19NSString *const ABI48_0_0RCTTriggerReloadCommandReasonKey = @"reason";
20NSString *const ABI48_0_0RCTTriggerReloadCommandBundleURLKey = @"bundleURL";
21
22void ABI48_0_0RCTRegisterReloadCommandListener(id<ABI48_0_0RCTReloadListener> listener)
23{
24  if (!listenersLock) {
25    listenersLock = [NSLock new];
26  }
27  [listenersLock lock];
28  if (!listeners) {
29    listeners = [NSHashTable weakObjectsHashTable];
30  }
31#if ABI48_0_0RCT_DEV
32  ABI48_0_0RCTAssertMainQueue(); // because registerKeyCommandWithInput: must be called on the main thread
33  static dispatch_once_t onceToken;
34  dispatch_once(&onceToken, ^{
35    [[ABI48_0_0RCTKeyCommands sharedInstance] registerKeyCommandWithInput:@"r"
36                                                   modifierFlags:UIKeyModifierCommand
37                                                          action:^(__unused UIKeyCommand *command) {
38                                                            ABI48_0_0RCTTriggerReloadCommandListeners(@"Command + R");
39                                                          }];
40  });
41#endif
42  [listeners addObject:listener];
43  [listenersLock unlock];
44}
45
46void ABI48_0_0RCTTriggerReloadCommandListeners(NSString *reason)
47{
48  [listenersLock lock];
49  [[NSNotificationCenter defaultCenter] postNotificationName:ABI48_0_0RCTTriggerReloadCommandNotification
50                                                      object:nil
51                                                    userInfo:@{
52                                                      ABI48_0_0RCTTriggerReloadCommandReasonKey : ABI48_0_0RCTNullIfNil(reason),
53                                                      ABI48_0_0RCTTriggerReloadCommandBundleURLKey : ABI48_0_0RCTNullIfNil(bundleURL)
54                                                    }];
55
56  for (id<ABI48_0_0RCTReloadListener> l in [listeners allObjects]) {
57    [l didReceiveReloadCommand];
58  }
59  [listenersLock unlock];
60}
61
62void ABI48_0_0RCTReloadCommandSetBundleURL(NSURL *URL)
63{
64  bundleURL = URL;
65}
66