react-native-navigation的迁移库

RNNEventEmitter.m 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. -(NSArray<NSString *> *)supportedEvents {
  13. return @[onAppLaunched, componentDidAppear, componentDidDisappear, onNavigationButtonPressed, navigationCommands];
  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)sendNavigationEvent:(RNNNavigationEvent *)navigationEvent {
  24. [self send:navigationCommands body:navigationEvent.body];
  25. }
  26. -(void)sendComponentDidAppear:(NSString *)componentId {
  27. [self send:componentDidAppear body:componentId];
  28. }
  29. -(void)sendComponentDidDisappear:(NSString *)componentId {
  30. [self send:componentDidDisappear body:componentId];
  31. }
  32. -(void)sendOnNavigationButtonPressed:(NSString *)componentId buttonId:(NSString*)buttonId {
  33. [self send:onNavigationButtonPressed body:@{@"componentId":componentId , @"buttonId": buttonId }];
  34. }
  35. - (void)addListener:(NSString *)eventName {
  36. [super addListener:eventName];
  37. if ([eventName isEqualToString:onAppLaunched]) {
  38. _appLaunchedListenerCount++;
  39. if (_appLaunchedEventDeferred) {
  40. _appLaunchedEventDeferred = FALSE;
  41. [self sendOnAppLaunched];
  42. }
  43. }
  44. }
  45. # pragma mark private
  46. -(void)send:(NSString *)eventName body:(id)body {
  47. [self sendEventWithName:eventName body:body];
  48. }
  49. @end