react-native-navigation的迁移库

RNNBridgeManager.m 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. #import "RNNReactComponentRegistry.h"
  10. @interface RNNBridgeManager() <RCTBridgeDelegate>
  11. @property (nonatomic, strong, readwrite) RCTBridge *bridge;
  12. @property (nonatomic, strong, readwrite) RNNStore *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. RNNStore* _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 = [RNNStore 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)registerExternalComponent:(NSString *)name callback:(RNNExternalViewCreator)callback {
  48. [_store registerExternalComponent:name callback:callback];
  49. }
  50. - (NSArray *)extraModulesFromDelegate {
  51. if ([_delegate respondsToSelector:@selector(extraModulesForBridge:)]) {
  52. return [_delegate extraModulesForBridge:_bridge];
  53. }
  54. return nil;
  55. }
  56. # pragma mark - RCTBridgeDelegate
  57. - (NSURL *)sourceURLForBridge:(RCTBridge *)bridge {
  58. return _jsCodeLocation;
  59. }
  60. - (NSArray<id<RCTBridgeModule>> *)extraModulesForBridge:(RCTBridge *)bridge {
  61. RNNEventEmitter *eventEmitter = [[RNNEventEmitter alloc] init];
  62. id<RNNRootViewCreator> rootViewCreator = [[RNNReactRootViewCreator alloc] initWithBridge:bridge];
  63. _componentRegistry = [[RNNReactComponentRegistry alloc] initWithCreator:rootViewCreator];
  64. RNNControllerFactory *controllerFactory = [[RNNControllerFactory alloc] initWithRootViewCreator:rootViewCreator eventEmitter:eventEmitter store:_store componentRegistry:_componentRegistry andBridge:bridge];
  65. _commandsHandler = [[RNNCommandsHandler alloc] initWithStore:_store controllerFactory:controllerFactory eventEmitter:eventEmitter stackManager:[RNNNavigationStackManager new] modalManager:[RNNModalManager new] overlayManager:[RNNOverlayManager new] mainWindow:_mainWindow];
  66. RNNBridgeModule *bridgeModule = [[RNNBridgeModule alloc] initWithCommandsHandler:_commandsHandler];
  67. return [@[bridgeModule,eventEmitter] arrayByAddingObjectsFromArray:[self extraModulesFromDelegate]];
  68. }
  69. # pragma mark - JavaScript & Bridge Notifications
  70. - (void)onJavaScriptWillLoad {
  71. [_store clean];
  72. [_componentRegistry clean];
  73. }
  74. - (void)onJavaScriptLoaded {
  75. [_store setReadyToReceiveCommands:true];
  76. [[_bridge moduleForClass:[RNNEventEmitter class]] sendOnAppLaunched];
  77. }
  78. - (void)onBridgeWillReload {
  79. UIApplication.sharedApplication.delegate.window.rootViewController = nil;
  80. }
  81. @end