react-native-navigation的迁移库

RNNBridgeManager.m 4.1KB

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