react-native-navigation的迁移库

ReactNativeNavigation.m 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. RCTBridge* _bridge;
  15. RNNStore* _store;
  16. RNNCommandsHandler* _commandsHandler;
  17. }
  18. # pragma mark - public API
  19. +(void)bootstrap:(NSURL *)jsCodeLocation launchOptions:(NSDictionary *)launchOptions {
  20. [[ReactNativeNavigation sharedInstance] bootstrap:jsCodeLocation launchOptions:launchOptions];
  21. }
  22. # pragma mark - instance
  23. +(instancetype) sharedInstance {
  24. static ReactNativeNavigation *instance = nil;
  25. static dispatch_once_t onceToken = 0;
  26. dispatch_once(&onceToken,^{
  27. if (instance == nil) {
  28. instance = [[ReactNativeNavigation alloc] init];
  29. }
  30. });
  31. return instance;
  32. }
  33. -(void)bootstrap:(NSURL *)jsCodeLocation launchOptions:(NSDictionary *)launchOptions {
  34. _jsCodeLocation = jsCodeLocation;
  35. _launchOptions = launchOptions;
  36. _store = [RNNStore new];
  37. [RNNSplashScreen show];
  38. [self registerForJsEvents];
  39. [self createBridgeLoadJsAndThenInitDependencyGraph];
  40. }
  41. # pragma mark - RCTBridgeDelegate
  42. -(NSURL *)sourceURLForBridge:(RCTBridge *)bridge {
  43. return _jsCodeLocation;
  44. }
  45. /**
  46. * here we initialize all of our dependency graph
  47. */
  48. -(NSArray<id<RCTBridgeModule>> *)extraModulesForBridge:(RCTBridge *)bridge {
  49. RNNEventEmitter *eventEmitter = [[RNNEventEmitter alloc] init];
  50. id<RNNRootViewCreator> rootViewCreator = [[RNNReactRootViewCreator alloc] initWithBridge:bridge];
  51. RNNControllerFactory *controllerFactory = [[RNNControllerFactory alloc] initWithRootViewCreator:rootViewCreator store:_store eventEmitter:eventEmitter];
  52. _commandsHandler = [[RNNCommandsHandler alloc] initWithStore:_store controllerFactory:controllerFactory];
  53. RNNBridgeModule *bridgeModule = [[RNNBridgeModule alloc] initWithCommandsHandler:_commandsHandler];
  54. return @[bridgeModule,eventEmitter];
  55. }
  56. # pragma mark - js events
  57. -(void)onJavaScriptWillLoad {
  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. @end