RCTReloadCommand.m 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. /**
  2. * Copyright (c) Facebook, Inc. and its 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. #import "RCTReloadCommand.h"
  8. #import "RCTAssert.h"
  9. #import "RCTKeyCommands.h"
  10. /** main queue only */
  11. static NSHashTable<id<RCTReloadListener>> *listeners;
  12. void RCTRegisterReloadCommandListener(id<RCTReloadListener> listener)
  13. {
  14. RCTAssertMainQueue(); // because registerKeyCommandWithInput: must be called on the main thread
  15. static dispatch_once_t onceToken;
  16. dispatch_once(&onceToken, ^{
  17. listeners = [NSHashTable weakObjectsHashTable];
  18. [[RCTKeyCommands sharedInstance] registerKeyCommandWithInput:@"r"
  19. modifierFlags:UIKeyModifierCommand
  20. action:
  21. ^(__unused UIKeyCommand *command) {
  22. RCTTriggerReloadCommandListeners();
  23. }];
  24. });
  25. [listeners addObject:listener];
  26. }
  27. void RCTTriggerReloadCommandListeners(void)
  28. {
  29. RCTAssertMainQueue();
  30. // Copy to protect against mutation-during-enumeration.
  31. // If listeners hasn't been initialized yet we get nil, which works just fine.
  32. NSArray<id<RCTReloadListener>> *copiedListeners = [listeners allObjects];
  33. for (id<RCTReloadListener> l in copiedListeners) {
  34. [l didReceiveReloadCommand];
  35. }
  36. }