react-native-navigation的迁移库

RNNEventEmitter.m 2.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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)addListener:(NSString *)eventName {
  39. [super addListener:eventName];
  40. if ([eventName isEqualToString:onAppLaunched]) {
  41. _appLaunchedListenerCount++;
  42. if (_appLaunchedEventDeferred) {
  43. _appLaunchedEventDeferred = FALSE;
  44. [self sendOnAppLaunched];
  45. }
  46. }
  47. }
  48. # pragma mark private
  49. -(void)send:(NSString *)eventName body:(id)body {
  50. if ([eventName isEqualToString:componentDidDisappear] && self.bridge == nil) {
  51. return;
  52. }
  53. [self sendEventWithName:eventName body:body];
  54. }
  55. @end