forked from wix/react-native-navigation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRNNBridgeManager.m
104 lines (77 loc) · 3.35 KB
/
RNNBridgeManager.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#import "RNNBridgeManager.h"
#import <React/RCTBridge.h>
#import <React/RCTUIManager.h>
#import "RNNEventEmitter.h"
#import "RNNSplashScreen.h"
#import "RNNBridgeModule.h"
#import "RNNRootViewCreator.h"
#import "RNNReactRootViewCreator.h"
@interface RNNBridgeManager() <RCTBridgeDelegate>
@property (nonatomic, strong, readwrite) RCTBridge *bridge;
@property (nonatomic, strong, readwrite) RNNStore *store;
@end
@implementation RNNBridgeManager {
NSURL* _jsCodeLocation;
NSDictionary* _launchOptions;
id<RNNBridgeManagerDelegate> _delegate;
RCTBridge* _bridge;
UIWindow* _mainWindow;
RNNStore* _store;
RNNCommandsHandler* _commandsHandler;
}
- (instancetype)initWithJsCodeLocation:(NSURL *)jsCodeLocation launchOptions:(NSDictionary *)launchOptions bridgeManagerDelegate:(id<RNNBridgeManagerDelegate>)delegate mainWindow:(UIWindow *)mainWindow {
if (self = [super init]) {
_mainWindow = mainWindow;
_jsCodeLocation = jsCodeLocation;
_launchOptions = launchOptions;
_delegate = delegate;
_store = [RNNStore new];
_bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:_launchOptions];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(onJavaScriptLoaded)
name:RCTJavaScriptDidLoadNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(onJavaScriptWillLoad)
name:RCTJavaScriptWillStartLoadingNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(onBridgeWillReload)
name:RCTBridgeWillReloadNotification
object:nil];
}
return self;
}
- (void)registerExternalComponent:(NSString *)name callback:(RNNExternalViewCreator)callback {
[_store registerExternalComponent:name callback:callback];
}
- (NSArray *)extraModulesFromDelegate {
if ([_delegate respondsToSelector:@selector(extraModulesForBridge:)]) {
return [_delegate extraModulesForBridge:_bridge];
}
return nil;
}
# pragma mark - RCTBridgeDelegate
- (NSURL *)sourceURLForBridge:(RCTBridge *)bridge {
return _jsCodeLocation;
}
- (NSArray<id<RCTBridgeModule>> *)extraModulesForBridge:(RCTBridge *)bridge {
RNNEventEmitter *eventEmitter = [[RNNEventEmitter alloc] init];
id<RNNRootViewCreator> rootViewCreator = [[RNNReactRootViewCreator alloc] initWithBridge:bridge];
RNNControllerFactory *controllerFactory = [[RNNControllerFactory alloc] initWithRootViewCreator:rootViewCreator eventEmitter:eventEmitter andBridge:bridge];
_commandsHandler = [[RNNCommandsHandler alloc] initWithStore:_store controllerFactory:controllerFactory eventEmitter:eventEmitter stackManager:[RNNNavigationStackManager new] modalManager:[RNNModalManager new] overlayManager:[RNNOverlayManager new] mainWindow:_mainWindow];
RNNBridgeModule *bridgeModule = [[RNNBridgeModule alloc] initWithCommandsHandler:_commandsHandler];
return [@[bridgeModule,eventEmitter] arrayByAddingObjectsFromArray:[self extraModulesFromDelegate]];
}
# pragma mark - JavaScript & Bridge Notifications
- (void)onJavaScriptWillLoad {
[_store clean];
}
- (void)onJavaScriptLoaded {
[_store setReadyToReceiveCommands:true];
[[_bridge moduleForClass:[RNNEventEmitter class]] sendOnAppLaunched];
}
- (void)onBridgeWillReload {
UIApplication.sharedApplication.delegate.window.rootViewController = nil;
}
@end