react-native-navigation的迁移库

RNNEventEmitter.m 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 onNavigationButtonPressed = @"RNN.navigationButtonPressed";
  11. static NSString* const navigationCommands = @"RNN.navigationCommands";
  12. static NSString* const componentLifecycle = @"RNN.componentLifecycle";
  13. -(NSArray<NSString *> *)supportedEvents {
  14. return @[onAppLaunched, componentDidAppear, componentDidDisappear, onNavigationButtonPressed, navigationCommands, componentLifecycle];
  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)sendNavigationEvent:(RNNNavigationEvent *)navigationEvent {
  25. [self send:navigationCommands body:navigationEvent.body];
  26. }
  27. -(void)sendLifecycleEvent:(RNNComponentLifecycleEvent *)navigationEvent {
  28. [self send:componentLifecycle body:navigationEvent.body];
  29. }
  30. -(void)sendComponentDidAppear:(NSString *)componentId {
  31. [self send:componentDidAppear body:componentId];
  32. }
  33. -(void)sendComponentDidDisappear:(NSString *)componentId {
  34. [self send:componentDidDisappear body:componentId];
  35. }
  36. -(void)sendOnNavigationButtonPressed:(NSString *)componentId buttonId:(NSString*)buttonId {
  37. [self send:onNavigationButtonPressed body:@{@"componentId":componentId , @"buttonId": buttonId }];
  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. [self sendEventWithName:eventName body:body];
  52. }
  53. @end