Skip to content

Commit

Permalink
Added explicit init to observer modules
Browse files Browse the repository at this point in the history
Summary:
Modules which call JS methods directly, or use `sendDeviceEventWithName:`, can trigger effects in JS without ever being referenced from the JS code. This breaks some assumptions in my earlier diff about when modules can be lazily loaded.

Pending a better solution, I've put explicit `init` methods in these modules to ensure they are eagerly initialized (the downside to this is that they'll still be initialized even if they are never used).

Reviewed By: javache

Differential Revision: D3258232

fb-gh-sync-id: f925bc2e5339c1fbfcc244d4613062c5ab848fc2
fbshipit-source-id: f925bc2e5339c1fbfcc244d4613062c5ab848fc2
  • Loading branch information
nicklockwood authored and Facebook Github Bot 7 committed May 4, 2016
1 parent a9a90aa commit e72163f
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 3 deletions.
7 changes: 7 additions & 0 deletions Libraries/LinkingIOS/RCTLinkingManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ @implementation RCTLinkingManager

RCT_EXPORT_MODULE()

- (instancetype)init
{
// We're only overriding this to ensure the module gets created at startup
// TODO (t11106126): Remove once we have more declarative control over module setup.
return [super init];
}

- (void)setBridge:(RCTBridge *)bridge
{
_bridge = bridge;
Expand Down
10 changes: 10 additions & 0 deletions Libraries/PushNotificationIOS/RCTPushNotificationManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,20 @@ - (void)dealloc
[[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (instancetype)init
{
// We're only overriding this to ensure the module gets created at startup
// TODO (t11106126): Remove once we have more declarative control over module setup.
return [super init];
}

- (void)setBridge:(RCTBridge *)bridge
{
_bridge = bridge;

// TODO: if we add an explicit "startObserving" method, we can take this out
// of the application startup path

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleLocalNotificationReceived:)
name:RCTLocalNotificationReceived
Expand Down
2 changes: 1 addition & 1 deletion React/Base/RCTBatchedBridge.m
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ - (void)initModulesWithDispatchGroup:(dispatch_group_t)dispatchGroup

// The executor is a bridge module, but we want it to be instantiated before
// any other module has access to the bridge, in case they need the JS thread.
// TODO: once we have more fine-grained control of init (D3175632) we can
// TODO: once we have more fine-grained control of init (t11106126) we can
// probably just replace this with [self moduleForClass:self.executorClass]
if (!_javaScriptExecutor) {
id<RCTJavaScriptExecutor> executorModule = [self.executorClass new];
Expand Down
7 changes: 7 additions & 0 deletions React/Base/RCTKeyboardObserver.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ @implementation RCTKeyboardObserver

RCT_EXPORT_MODULE()

- (instancetype)init
{
// We're only overriding this to ensure the module gets created at startup
// TODO (t11106126): Remove once we have more declarative control over module setup.
return [super init];
}

- (void)setBridge:(RCTBridge *)bridge
{
_bridge = bridge;
Expand Down
7 changes: 7 additions & 0 deletions React/Base/RCTPerformanceLogger.m
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,13 @@ @implementation RCTPerformanceLogger

@synthesize bridge = _bridge;

- (instancetype)init
{
// We're only overriding this to ensure the module gets created at startup
// TODO (t11106126): Remove once we have more declarative control over module setup.
return [super init];
}

- (void)setBridge:(RCTBridge *)bridge
{
_bridge = bridge;
Expand Down
2 changes: 1 addition & 1 deletion React/Modules/RCTDevLoadingView.m
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ - (void)dealloc
- (instancetype)init
{
// We're only overriding this to ensure the module gets created at startup
// TODO (D3175632): Remove once we have more declarative control over module setup.
// TODO (t11106126): Remove once we have more declarative control over module setup.
return [super init];
}

Expand Down
10 changes: 10 additions & 0 deletions React/Modules/RCTStatusBarManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,20 @@ static BOOL RCTViewControllerBasedStatusBarAppearance()

@synthesize bridge = _bridge;

- (instancetype)init
{
// We're only overriding this to ensure the module gets created at startup
// TODO (t11106126): Remove once we have more declarative control over module setup.
return [super init];
}

- (void)setBridge:(RCTBridge *)bridge
{
_bridge = bridge;

// TODO: if we add an explicit "startObserving" method, we can take this out
// of the application startup path

NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self selector:@selector(applicationDidChangeStatusBarFrame:) name:UIApplicationDidChangeStatusBarFrameNotification object:nil];
[nc addObserver:self selector:@selector(applicationWillChangeStatusBarFrame:) name:UIApplicationWillChangeStatusBarFrameNotification object:nil];
Expand Down
2 changes: 1 addition & 1 deletion React/Profiler/RCTPerfMonitor.m
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ @implementation RCTPerfMonitor {
- (instancetype)init
{
// We're only overriding this to ensure the module gets created at startup
// TODO (D3175632): Remove once we have more declarative control over module setup.
// TODO (t11106126): Remove once we have more declarative control over module setup.
return [super init];
}

Expand Down

0 comments on commit e72163f

Please sign in to comment.