react-native-navigation的迁移库

RNNBridgeManager.m 3.1KB

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