react-native-navigation的迁移库

RNNEventEmitter.m 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #import "RNNEventEmitter.h"
  2. @implementation RNNEventEmitter {
  3. NSInteger _appLaunchedListenerCount;
  4. BOOL _appLaunchedEventDeferred;
  5. }
  6. RCT_EXPORT_MODULE();
  7. static NSString* const onAppLaunched = @"RNN.appLaunched";
  8. static NSString* const componentDidAppear = @"RNN.componentDidAppear";
  9. static NSString* const componentDidDisappear = @"RNN.componentDidDisappear";
  10. static NSString* const commandComplete = @"RNN.commandCompleted";
  11. static NSString* const navigationEvent = @"RNN.nativeEvent";
  12. -(NSArray<NSString *> *)supportedEvents {
  13. return @[onAppLaunched, componentDidAppear, componentDidDisappear, commandComplete, navigationEvent];
  14. }
  15. # pragma mark public
  16. -(void)sendOnAppLaunched {
  17. if (_appLaunchedListenerCount > 0) {
  18. [self send:onAppLaunched body:nil];
  19. } else {
  20. _appLaunchedEventDeferred = TRUE;
  21. }
  22. }
  23. -(void)sendComponentDidAppear:(NSString *)componentId componentName:(NSString *)componentName {
  24. [self send:componentDidAppear body:@{@"componentId":componentId, @"componentName": componentName}];
  25. }
  26. -(void)sendComponentDidDisappear:(NSString *)componentId componentName:(NSString *)componentName{
  27. [self send:componentDidDisappear body:@{@"componentId":componentId, @"componentName": componentName}];
  28. }
  29. -(void)sendOnNavigationButtonPressed:(NSString *)componentId buttonId:(NSString*)buttonId {
  30. [self send:navigationEvent body:@{@"name": @"buttonPressed", @"params": @{@"componentId": componentId , @"buttonId": buttonId}}];
  31. }
  32. -(void)sendOnNavigationCommand:(NSString *)commandName params:(NSDictionary*)params {
  33. [self send:navigationEvent body:@{@"name":commandName , @"params": params}];
  34. }
  35. -(void)sendOnNavigationEvent:(NSString *)commandName params:(NSDictionary*)params {
  36. [self send:navigationEvent body:@{@"name":commandName , @"params": params}];
  37. }
  38. -(void)sendOnSearchBarUpdated:(NSString *)componentId
  39. text:(NSString*)text
  40. isFocused:(BOOL)isFocused {
  41. [self send:navigationEvent body:@{@"name": @"searchBarUpdated",
  42. @"params": @{
  43. @"componentId": componentId,
  44. @"text": text,
  45. @"isFocused": @(isFocused)}}];
  46. }
  47. - (void)addListener:(NSString *)eventName {
  48. [super addListener:eventName];
  49. if ([eventName isEqualToString:onAppLaunched]) {
  50. _appLaunchedListenerCount++;
  51. if (_appLaunchedEventDeferred) {
  52. _appLaunchedEventDeferred = FALSE;
  53. [self sendOnAppLaunched];
  54. }
  55. }
  56. }
  57. # pragma mark private
  58. -(void)send:(NSString *)eventName body:(id)body {
  59. if ([eventName isEqualToString:componentDidDisappear] && self.bridge == nil) {
  60. return;
  61. }
  62. [self sendEventWithName:eventName body:body];
  63. }
  64. @end