react-native-navigation的迁移库

RNNBridgeManager.m 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. - (void)registerExternalComponent:(NSString *)name callback:(RNNExternalViewCreator)callback {
  41. [_store registerExternalComponent:name callback:callback];
  42. }
  43. # pragma mark - RCTBridgeDelegate
  44. - (NSURL *)sourceURLForBridge:(RCTBridge *)bridge {
  45. return _jsCodeLocation;
  46. }
  47. - (NSArray<id<RCTBridgeModule>> *)extraModulesForBridge:(RCTBridge *)bridge {
  48. RNNEventEmitter *eventEmitter = [[RNNEventEmitter alloc] init];
  49. id<RNNRootViewCreator> rootViewCreator = [[RNNReactRootViewCreator alloc] initWithBridge:bridge];
  50. RNNControllerFactory *controllerFactory = [[RNNControllerFactory alloc] initWithRootViewCreator:rootViewCreator store:_store eventEmitter:eventEmitter andBridge:bridge];
  51. _commandsHandler = [[RNNCommandsHandler alloc] initWithStore:_store controllerFactory:controllerFactory eventEmitter:eventEmitter];
  52. RNNBridgeModule *bridgeModule = [[RNNBridgeModule alloc] initWithCommandsHandler:_commandsHandler];
  53. return @[bridgeModule,eventEmitter];
  54. }
  55. # pragma mark - JavaScript & Bridge Notifications
  56. - (void)onJavaScriptWillLoad {
  57. [_store clean];
  58. }
  59. - (void)onJavaScriptLoaded {
  60. [_store setReadyToReceiveCommands:true];
  61. [[_bridge moduleForClass:[RNNEventEmitter class]] sendOnAppLaunched];
  62. }
  63. - (void)onBridgeWillReload {
  64. UIApplication.sharedApplication.delegate.window.rootViewController = nil;
  65. }
  66. @end