react-native-navigation的迁移库

RNNBridgeManager.m 3.8KB

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