react-native-navigation的迁移库

RNNEventEmitter.m 2.4KB

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