react-native-navigation的迁移库

RNNBridgeManager.m 3.4KB

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