react-native-navigation的迁移库

ReactNativeNavigation.m 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #import "ReactNativeNavigation.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 ReactNativeNavigation() <RCTBridgeDelegate>
  10. @end
  11. @implementation ReactNativeNavigation {
  12. NSURL* _jsCodeLocation;
  13. NSDictionary* _launchOptions;
  14. RNNStore* _store;
  15. RCTBridge* _bridge;
  16. }
  17. # pragma mark - public API
  18. +(void)bootstrap:(NSURL *)jsCodeLocation launchOptions:(NSDictionary *)launchOptions {
  19. [[ReactNativeNavigation sharedInstance] bootstrap:jsCodeLocation launchOptions:launchOptions];
  20. }
  21. # pragma mark - instance
  22. +(instancetype) sharedInstance {
  23. static ReactNativeNavigation *instance = nil;
  24. static dispatch_once_t onceToken = 0;
  25. dispatch_once(&onceToken,^{
  26. if (instance == nil) {
  27. instance = [[ReactNativeNavigation alloc] init];
  28. }
  29. });
  30. return instance;
  31. }
  32. -(void)bootstrap:(NSURL *)jsCodeLocation launchOptions:(NSDictionary *)launchOptions {
  33. _jsCodeLocation = jsCodeLocation;
  34. _launchOptions = launchOptions;
  35. _store = [RNNStore new];
  36. [RNNSplashScreen show];
  37. [self registerForJsEvents];
  38. [self createBridgeLoadJsAndThenInitDependencyGraph];
  39. }
  40. # pragma mark - RCTBridgeDelegate
  41. -(NSURL *)sourceURLForBridge:(RCTBridge *)bridge {
  42. return _jsCodeLocation;
  43. }
  44. /**
  45. * here we initialize all of our dependency graph
  46. */
  47. -(NSArray<id<RCTBridgeModule>> *)extraModulesForBridge:(RCTBridge *)bridge {
  48. RNNEventEmitter *eventEmitter = [[RNNEventEmitter alloc] init];
  49. id<RNNRootViewCreator> rootViewCreator = [[RNNReactRootViewCreator alloc] initWithBridge:bridge];
  50. RNNControllerFactory *controllerFactory = [[RNNControllerFactory alloc] initWithRootViewCreator:rootViewCreator store:_store eventEmitter:eventEmitter];
  51. RNNCommandsHandler *commandsHandler = [[RNNCommandsHandler alloc] initWithStore:_store controllerFactory:controllerFactory];
  52. RNNBridgeModule *bridgeModule = [[RNNBridgeModule alloc] initWithCommandsHandler:commandsHandler];
  53. return @[bridgeModule,eventEmitter];
  54. }
  55. # pragma mark - js events
  56. -(void)onJavaScriptWillLoad {
  57. [self resetRootViewControllerOnlyOnJSDevReload];
  58. [_store clean];
  59. }
  60. -(void)onJavaScriptLoaded {
  61. [_store setReadyToReceiveCommands:true];
  62. [[_bridge moduleForClass:[RNNEventEmitter class]] sendOnAppLaunched];
  63. }
  64. # pragma mark - private
  65. -(void)createBridgeLoadJsAndThenInitDependencyGraph {
  66. _bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:_launchOptions];
  67. }
  68. -(void)registerForJsEvents {
  69. [[NSNotificationCenter defaultCenter] addObserver:self
  70. selector:@selector(onJavaScriptLoaded)
  71. name:RCTJavaScriptDidLoadNotification
  72. object:nil];
  73. [[NSNotificationCenter defaultCenter] addObserver:self
  74. selector:@selector(onJavaScriptWillLoad)
  75. name:RCTJavaScriptWillStartLoadingNotification
  76. object:nil];
  77. }
  78. -(void)resetRootViewControllerOnlyOnJSDevReload {
  79. #ifdef DEBUG
  80. if(![UIApplication.sharedApplication.delegate.window.rootViewController isKindOfClass:[RNNSplashScreen class]]) {
  81. UIApplication.sharedApplication.delegate.window.rootViewController = nil;
  82. }
  83. #endif
  84. }
  85. @end