Skip to content

Commit ae47850

Browse files
author
Riccardo
authored
fix: improve TM explanation (#3364)
1 parent f042dca commit ae47850

File tree

4 files changed

+59
-24
lines changed

4 files changed

+59
-24
lines changed

docs/the-new-architecture/backward-compatibility-turbomodules.md

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,9 @@ This changes do three main things:
178178

179179
The second step is to instruct Xcode to avoid compiling all the lines using the New Architecture types and files when we are building an app with the Old Architecture.
180180

181-
The file to change is the module implementation file, which is usually a `<your-module>.mm` file. That file is structured as follows:
181+
There are two files to change. The module implementation file, which is usually a `<your-module>.mm` file, and the module header, which is usually a `<your-module>.h` file.
182+
183+
That implementation file is structured as follows:
182184

183185
- Some `#import` statements, among which there is a `<GeneratedSpec>.h` file.
184186
- The module implementation, using the various `RCT_EXPORT_xxx` and `RCT_REMAP_xxx` macros.
@@ -205,7 +207,27 @@ The **goal** is to make sure that the `Turbo Native Module` still builds with th
205207
@end
206208
```
207209

208-
This snippet uses the same `RCT_NEW_ARCH_ENABLED` flag used in the previous [section](#dependencies-ios). When this flag is not set, Xcode skips the lines within the `#ifdef` during compilation and it does not include them into the compiled binary.
210+
A similar thing needs to be done for the header file. Add the following lines at the bottom of your module header. You need to first import the header and then, if the New Architecture is enabled, make it conform to the Spec protocol.
211+
212+
```diff
213+
#import <React/RCTBridgeModule.h>
214+
+ #ifdef RCT_NEW_ARCH_ENABLED
215+
+ #import <YourModuleSpec/YourModuleSpec.h>
216+
+ #endif
217+
218+
@interface YourModule: NSObject <RCTBridgeModule>
219+
220+
@end
221+
222+
+ #ifdef RCT_NEW_ARCH_ENABLED
223+
+ @interface YourModule () <YourModuleSpec>
224+
225+
+ @end
226+
+ #endif
227+
228+
```
229+
230+
This snippets uses the same `RCT_NEW_ARCH_ENABLED` flag used in the previous [section](#dependencies-ios). When this flag is not set, Xcode skips the lines within the `#ifdef` during compilation and it does not include them into the compiled binary.
209231

210232
### Android
211233

docs/the-new-architecture/pillars-turbomodule.md

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -427,11 +427,11 @@ Now add the Native code for your Turbo Native Module. Create two files in the `R
427427
##### RTNCalculator.h
428428
429429
```objc title="RTNCalculator.h"
430-
#import <React/RCTBridgeModule.h>
430+
#import <RTNCalculatorSpec/RTNCalculatorSpec.h>
431431

432432
NS_ASSUME_NONNULL_BEGIN
433433

434-
@interface RTNCalculator : NSObject <RCTBridgeModule>
434+
@interface RTNCalculator : NSObject <NativeCalculatorSpec>
435435

436436
@end
437437

@@ -448,13 +448,9 @@ This file defines the interface for the `RTNCalculator` module. Here, we can add
448448

449449
@implementation RTNCalculator
450450

451-
RCT_EXPORT_MODULE(RTNCalculator)
451+
RCT_EXPORT_MODULE()
452452

453-
RCT_REMAP_METHOD(add, addA:(NSInteger)a
454-
andB:(NSInteger)b
455-
withResolver:(RCTPromiseResolveBlock) resolve
456-
withRejecter:(RCTPromiseRejectBlock) reject)
457-
{
453+
- (void)add:(double)a b:(double)b resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject {
458454
NSNumber *result = [[NSNumber alloc] initWithInteger:a+b];
459455
resolve(result);
460456
}
@@ -470,7 +466,7 @@ RCT_REMAP_METHOD(add, addA:(NSInteger)a
470466
471467
The most important call is to the `RCT_EXPORT_MODULE`, which is required to export the module so that React Native can load the Turbo Native Module.
472468
473-
Then the `RCT_REMAP_METHOD` macro is used to expose the `add` method.
469+
Then the `add` method, whose signature must match the one specified by the Codegen in the `RTNCalculatorSpec.h`.
474470
475471
Finally, the `getTurboModule` method gets an instance of the Turbo Native Module so that the JavaScript side can invoke its methods. The function is defined in (and requested by) the `RTNCalculatorSpec.h` file that was generated earlier by Codegen.
476472

website/versioned_docs/version-0.70/the-new-architecture/backward-compatibility-turbomodules.md

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,9 @@ This changes do three main things:
163163

164164
The second step is to instruct Xcode to avoid compiling all the lines using the New Architecture types and files when we are building an app with the Old Architecture.
165165

166-
The file to change is the module implementation file, which is usually a `<your-module>.mm` file. That file is structured as follow:
166+
There are two files to change. The module implementation file, which is usually a `<your-module>.mm` file, and the module header, which is usually a `<your-module>.h` file.
167+
168+
That implementation file is structured as follows:
167169

168170
- Some `#import` statements, among which there is a `<GeneratedSpec>.h` file.
169171
- The module implementation, using the various `RCT_EXPORT_xxx` and `RCT_REMAP_xxx` macros.
@@ -190,7 +192,27 @@ The **goal** is to make sure that the `Turbo Native Module` still builds with th
190192
@end
191193
```
192194

193-
This snippet uses the same `RCT_NEW_ARCH_ENABLED` flag used in the previous [section](#dependencies-ios). When this flag is not set, Xcode skips the lines within the `#ifdef` during compilation and it does not include them into the compiled binary.
195+
A similar thing needs to be done for the header file. Add the following lines at the bottom of your module header. You need to first import the header and then, if the New Architecture is enabled, make it conform to the Spec protocol.
196+
197+
```diff
198+
#import <React/RCTBridgeModule.h>
199+
+ #ifdef RCT_NEW_ARCH_ENABLED
200+
+ #import <YourModuleSpec/YourModuleSpec.h>
201+
+ #endif
202+
203+
@interface YourModule: NSObject <RCTBridgeModule>
204+
205+
@end
206+
207+
+ #ifdef RCT_NEW_ARCH_ENABLED
208+
+ @interface YourModule () <YourModuleSpec>
209+
210+
+ @end
211+
+ #endif
212+
213+
```
214+
215+
This snippets uses the same `RCT_NEW_ARCH_ENABLED` flag used in the previous [section](#dependencies-ios). When this flag is not set, Xcode skips the lines within the `#ifdef` during compilation and it does not include them into the compiled binary.
194216

195217
### Android
196218

website/versioned_docs/version-0.70/the-new-architecture/pillars-turbomodule.md

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,6 @@ require "json"
182182

183183
package = JSON.parse(File.read(File.join(__dir__, "package.json")))
184184

185-
folly_version = '2021.07.22.00'
186185
folly_compiler_flags = '-DFOLLY_NO_CONFIG -DFOLLY_MOBILE=1 -DFOLLY_USE_LIBCPP=1 -Wno-comma -Wno-shorten-64-to-32'
187186

188187
Pod::Spec.new do |s|
@@ -208,7 +207,7 @@ Pod::Spec.new do |s|
208207
}
209208

210209
s.dependency "React-Codegen"
211-
s.dependency "RCT-Folly", folly_version
210+
s.dependency "RCT-Folly"
212211
s.dependency "RCTRequired"
213212
s.dependency "RCTTypeSafety"
214213
s.dependency "ReactCommon/turbomodule/core"
@@ -415,11 +414,11 @@ Now add the Native code for your Turbo Native Module. Create two files in the `R
415414
##### RTNCalculator.h
416415
417416
```objc title="RTNCalculator.h"
418-
#import <React/RCTBridgeModule.h>
417+
#import <RTNCalculatorSpec/RTNCalculatorSpec.h>
419418

420419
NS_ASSUME_NONNULL_BEGIN
421420

422-
@interface RTNCalculator : NSObject <RCTBridgeModule>
421+
@interface RTNCalculator : NSObject <NativeCalculatorSpec>
423422

424423
@end
425424

@@ -436,13 +435,9 @@ This file defines the interface for the `RTNCalculator` module. Here, we can add
436435

437436
@implementation RTNCalculator
438437

439-
RCT_EXPORT_MODULE(RTNCalculator)
438+
RCT_EXPORT_MODULE()
440439

441-
RCT_REMAP_METHOD(add, addA:(NSInteger)a
442-
andB:(NSInteger)b
443-
withResolver:(RCTPromiseResolveBlock) resolve
444-
withRejecter:(RCTPromiseRejectBlock) reject)
445-
{
440+
- (void)add:(double)a b:(double)b resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject {
446441
NSNumber *result = [[NSNumber alloc] initWithInteger:a+b];
447442
resolve(result);
448443
}
@@ -458,7 +453,7 @@ RCT_REMAP_METHOD(add, addA:(NSInteger)a
458453
459454
The most important call is to the `RCT_EXPORT_MODULE`, which is required to export the module so that React Native can load the Turbo Native Module.
460455
461-
Then the `RCT_REMAP_METHOD` macro is used to expose the `add` method.
456+
Then the `add` method, whose signature must match the one specified by the Codegen in the `RTNCalculatorSpec.h`.
462457
463458
Finally, the `getTurboModule` method gets an instance of the Turbo Native Module so that the JavaScript side can invoke its methods. The function is defined in (and requested by) the `RTNCalculatorSpec.h` file that was generated earlier by Codegen.
464459

0 commit comments

Comments
 (0)