react-native-navigation的迁移库

RNNBridgeManager.m 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. RNNStore* _store;
  19. RNNStore* _overlayStore;
  20. RNNCommandsHandler* _commandsHandler;
  21. }
  22. - (instancetype)initWithJsCodeLocation:(NSURL *)jsCodeLocation launchOptions:(NSDictionary *)launchOptions bridgeManagerDelegate:(id<RNNBridgeManagerDelegate>)delegate {
  23. if (self = [super init]) {
  24. _jsCodeLocation = jsCodeLocation;
  25. _launchOptions = launchOptions;
  26. _delegate = delegate;
  27. _store = [RNNStore new];
  28. _overlayStore = [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 overlayStore:_overlayStore controllerFactory:controllerFactory eventEmitter:eventEmitter stackManager:[RNNNavigationStackManager new] modalManager:[RNNModalManager new] overlayManager:[RNNOverlayManager new]];
  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. [_overlayStore clean];
  70. }
  71. - (void)onJavaScriptLoaded {
  72. [_store setReadyToReceiveCommands:true];
  73. [_overlayStore setReadyToReceiveCommands:true];
  74. [[_bridge moduleForClass:[RNNEventEmitter class]] sendOnAppLaunched];
  75. }
  76. - (void)onBridgeWillReload {
  77. UIApplication.sharedApplication.delegate.window.rootViewController = nil;
  78. }
  79. @end