Skip to content

Commit

Permalink
Introduce RCTInitializing
Browse files Browse the repository at this point in the history
Summary:
## Motivation
NativeModules need a way to execute custom initialization code. The current way to achieve this is by overriding the setBridge: method. This doesn't work in bridgeless mode, because in bridgeless mode, the TurboModule system doesn't (and shouldn't) invoke the setBridge: method.

## Changes
This diff introduces a new protocol RCTInitializing that NativeModules can conform to. Once they conform to this protocol, they can implement the `- (void)initialize;` method. Both the NativeModule and TurboModule infra will execute this method towards the end of NativeModule setup.

## Questions
Why don't we just override the NativeModule init method?
1. **Backwards compatibility:** The legacy NativeModule infra requires that [modules that override their init method](https://www.internalfb.com/code/fbsource/[8098212b4e3733f0b469f11f3e61c5249a47f9f5]/xplat/js/react-native-github/React/Base/RCTModuleData.mm?lines=82-83) [must be initialized on the main thread](https://www.internalfb.com/code/fbsource/[8098212b4e3733f0b469f11f3e61c5249a47f9f5]/xplat/js/react-native-github/React/Base/RCTModuleData.mm?lines=85). We ported this over [to the TurboModule system](https://www.internalfb.com/code/fbsource/[d58a07e9ae90951d382224c28cb78c32c7cda80d]/xplat/js/react-native-github/ReactCommon/react/nativemodule/core/platform/ios/RCTTurboModuleManager.mm?lines=684%2C686). *We cannot require all NativeModules that need custom initialization to be initialized on the main thread.*
2. **More flexibility:** A custom `- (void)initialize` hook can be invoked by the NativeModule infra after the NativeModule is prepared (i.e: the bridge, module registry, view registry, etc. is attached). The same isn't true of an overriden init method, which has to be invoked when the object is created. This means that a custom `- (void)initialize` hook can use the bridge/method queue, etc., but an overriden init method cannot.

What about Android?
- After this diff, NativeModules can implement the `initialize` method to initialize themselves, and the `invalidate` method to invalidate themselves. This unifies initialization/invalidation across both iOS and Android, because on Android, TurboModules can also implement the same methods:

https://www.internalfb.com/code/fbsource/[d58a07e9ae90951d382224c28cb78c32c7cda80d]/xplat/js/react-native-github/ReactAndroid/src/main/java/com/facebook/react/turbomodule/core/interfaces/TurboModule.java?lines=11-17

Changelog: [iOS][Added] - Introduce RCTInitializing to allow NativeModules to initialize themselves

Reviewed By: fkgozali

Differential Revision: D28435078

fbshipit-source-id: 1a4a95e30f0a8b32b0844e9b9beee274afcbb577
  • Loading branch information
RSNara authored and facebook-github-bot committed May 21, 2021
1 parent 883e0d5 commit 9b45df1
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions React/Base/RCTInitializing.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

#import <Foundation/Foundation.h>

@protocol RCTInitializing <NSObject>

- (void)initialize;

@end

0 comments on commit 9b45df1

Please sign in to comment.