react-native-navigation的迁移库

RNNBridgeManager.m 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #import "RNNBridgeManager.h"
  2. #import <React/RCTBridge.h>
  3. #import <React/RCTUIManager.h>
  4. #import "RNNEventEmitter.h"
  5. #import "RNNSplashScreen.h"
  6. #import "RNNBridgeModule.h"
  7. #import "RNNRootViewCreator.h"
  8. #import "RNNReactRootViewCreator.h"
  9. @interface RNNBridgeManager() <RCTBridgeDelegate>
  10. @property (nonatomic, strong, readwrite) RCTBridge *bridge;
  11. @end
  12. @implementation RNNBridgeManager {
  13. NSURL* _jsCodeLocation;
  14. NSDictionary* _launchOptions;
  15. id<RNNBridgeManagerDelegate> _delegate;
  16. RCTBridge* _bridge;
  17. RNNStore* _store;
  18. RNNCommandsHandler* _commandsHandler;
  19. }
  20. - (instancetype)initWithJsCodeLocation:(NSURL *)jsCodeLocation launchOptions:(NSDictionary *)launchOptions bridgeManagerDelegate:(id<RNNBridgeManagerDelegate>)delegate {
  21. if (self = [super init]) {
  22. _jsCodeLocation = jsCodeLocation;
  23. _launchOptions = launchOptions;
  24. _delegate = delegate;
  25. _store = [RNNStore new];
  26. _bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:_launchOptions];
  27. [[NSNotificationCenter defaultCenter] addObserver:self
  28. selector:@selector(onJavaScriptLoaded)
  29. name:RCTJavaScriptDidLoadNotification
  30. object:nil];
  31. [[NSNotificationCenter defaultCenter] addObserver:self
  32. selector:@selector(onJavaScriptWillLoad)
  33. name:RCTJavaScriptWillStartLoadingNotification
  34. object:nil];
  35. [[NSNotificationCenter defaultCenter] addObserver:self
  36. selector:@selector(onBridgeWillReload)
  37. name:RCTBridgeWillReloadNotification
  38. object:nil];
  39. }
  40. return self;
  41. }
  42. - (void)registerExternalComponent:(NSString *)name callback:(RNNExternalViewCreator)callback {
  43. [_store registerExternalComponent:name callback:callback];
  44. }
  45. - (NSArray *)extraModulesFromDelegate {
  46. if ([_delegate respondsToSelector:@selector(extraModulesForBridge:)]) {
  47. return [_delegate extraModulesForBridge:_bridge];
  48. }
  49. return nil;
  50. }
  51. # pragma mark - RCTBridgeDelegate
  52. - (NSURL *)sourceURLForBridge:(RCTBridge *)bridge {
  53. return _jsCodeLocation;
  54. }
  55. - (NSArray<id<RCTBridgeModule>> *)extraModulesForBridge:(RCTBridge *)bridge {
  56. RNNEventEmitter *eventEmitter = [[RNNEventEmitter alloc] init];
  57. id<RNNRootViewCreator> rootViewCreator = [[RNNReactRootViewCreator alloc] initWithBridge:bridge];
  58. RNNControllerFactory *controllerFactory = [[RNNControllerFactory alloc] initWithRootViewCreator:rootViewCreator store:_store eventEmitter:eventEmitter andBridge:bridge];
  59. _commandsHandler = [[RNNCommandsHandler alloc] initWithStore:_store controllerFactory:controllerFactory eventEmitter:eventEmitter];
  60. RNNBridgeModule *bridgeModule = [[RNNBridgeModule alloc] initWithCommandsHandler:_commandsHandler];
  61. return [@[bridgeModule,eventEmitter] arrayByAddingObjectsFromArray:[self extraModulesFromDelegate]];
  62. }
  63. # pragma mark - JavaScript & Bridge Notifications
  64. - (void)onJavaScriptWillLoad {
  65. [_store clean];
  66. }
  67. - (void)onJavaScriptLoaded {
  68. [_store setReadyToReceiveCommands:true];
  69. [[_bridge moduleForClass:[RNNEventEmitter class]] sendOnAppLaunched];
  70. }
  71. - (void)onBridgeWillReload {
  72. UIApplication.sharedApplication.delegate.window.rootViewController = nil;
  73. }
  74. @end