react-native-navigation的迁移库

RNNBridgeManager.m 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. RCTBridge* _bridge;
  16. RNNStore* _store;
  17. RNNCommandsHandler* _commandsHandler;
  18. }
  19. - (instancetype)initWithJsCodeLocation:(NSURL *)jsCodeLocation launchOptions:(NSDictionary *)launchOptions {
  20. if (self = [super init]) {
  21. _jsCodeLocation = jsCodeLocation;
  22. _launchOptions = launchOptions;
  23. _store = [RNNStore new];
  24. _bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:_launchOptions];
  25. [[NSNotificationCenter defaultCenter] addObserver:self
  26. selector:@selector(onJavaScriptLoaded)
  27. name:RCTJavaScriptDidLoadNotification
  28. object:nil];
  29. [[NSNotificationCenter defaultCenter] addObserver:self
  30. selector:@selector(onJavaScriptWillLoad)
  31. name:RCTJavaScriptWillStartLoadingNotification
  32. object:nil];
  33. [[NSNotificationCenter defaultCenter] addObserver:self
  34. selector:@selector(onBridgeWillReload)
  35. name:RCTBridgeWillReloadNotification
  36. object:nil];
  37. }
  38. return self;
  39. }
  40. # pragma mark - RCTBridgeDelegate
  41. - (NSURL *)sourceURLForBridge:(RCTBridge *)bridge {
  42. return _jsCodeLocation;
  43. }
  44. - (NSArray<id<RCTBridgeModule>> *)extraModulesForBridge:(RCTBridge *)bridge {
  45. RNNEventEmitter *eventEmitter = [[RNNEventEmitter alloc] init];
  46. id<RNNRootViewCreator> rootViewCreator = [[RNNReactRootViewCreator alloc] initWithBridge:bridge];
  47. RNNControllerFactory *controllerFactory = [[RNNControllerFactory alloc] initWithRootViewCreator:rootViewCreator store:_store eventEmitter:eventEmitter andBridge:bridge];
  48. _commandsHandler = [[RNNCommandsHandler alloc] initWithStore:_store controllerFactory:controllerFactory];
  49. RNNBridgeModule *bridgeModule = [[RNNBridgeModule alloc] initWithCommandsHandler:_commandsHandler];
  50. return @[bridgeModule,eventEmitter];
  51. }
  52. # pragma mark - JavaScript & Bridge Notifications
  53. - (void)onJavaScriptWillLoad {
  54. [_store clean];
  55. }
  56. - (void)onJavaScriptLoaded {
  57. [_store setReadyToReceiveCommands:true];
  58. [[_bridge moduleForClass:[RNNEventEmitter class]] sendOnAppLaunched];
  59. }
  60. - (void)onBridgeWillReload {
  61. UIApplication.sharedApplication.delegate.window.rootViewController = nil;
  62. }
  63. @end