react-native-navigation的迁移库

RNN.m 2.7KB

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