Skip to content

Commit 1393060

Browse files
MateWWsatya164
authored andcommitted
feat: update turbo modules template to use a synchronous API
1 parent bbcde94 commit 1393060

File tree

16 files changed

+73
-26
lines changed

16 files changed

+73
-26
lines changed

packages/create-react-native-library/templates/common/$.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ android.iml
4444
#
4545
example/ios/Pods
4646

47+
# Ruby
48+
example/vendor/
49+
4750
# node.js
4851
#
4952
node_modules/

packages/create-react-native-library/templates/common/CONTRIBUTING.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,15 @@ To run the example app on iOS:
3131
```sh
3232
yarn example ios
3333
```
34+
3435
<% if (!project.native) { -%>
3536
To run the example app on Web:
3637

3738
```sh
3839
yarn example web
3940
```
4041
<% } -%>
42+
4143
Make sure your code passes TypeScript and ESLint. Run the following to verify:
4244

4345
```sh

packages/create-react-native-library/templates/common/README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
## Installation
66

77
```sh
8-
npm install <%- project.slug -%>
8+
npm install <%- project.slug %>
99
```
1010

1111
## Usage
@@ -18,6 +18,14 @@ import { <%- project.name -%>View } from "<%- project.slug -%>";
1818

1919
<<%- project.name -%>View color="tomato" />
2020
```
21+
<% } else if (project.architecture == 'turbo') { -%>
22+
```js
23+
import { multiply } from "<%- project.slug -%>";
24+
25+
// ...
26+
27+
const result = multiply(3, 7);
28+
```
2129
<% } else { -%>
2230
```js
2331
import { multiply } from "<%- project.slug -%>";

packages/create-react-native-library/templates/common/example/src/App.tsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@ export default function App() {
1616
</View>
1717
);
1818
}
19+
<% } else if (project.architecture == 'turbo') { -%>
20+
const result = multiply(3, 7);
21+
22+
export default function App() {
23+
return (
24+
<View style={styles.container}>
25+
<Text>Result: {result}</Text>
26+
</View>
27+
);
28+
}
1929
<% } else { -%>
2030
export default function App() {
2131
const [result, setResult] = React.useState<number | undefined>();

packages/create-react-native-library/templates/example/example/$package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"android": "react-native run-android",
88
"ios": "react-native run-ios",
99
"start": "react-native start",
10-
"pods": "RCT_NEW_ARCH_ENABLED=<%- project.turbomodule ? 1 : 0 -%> pod-install --quiet",
10+
"pods": "pod-install --quiet",
1111
"postinstall": "patch-package"
1212
},
1313
"dependencies": {

packages/create-react-native-library/templates/example/example/ios/Podfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ require_relative '../node_modules/@react-native-community/cli-platform-ios/nativ
44
platform :ios, '11.0'
55
install! 'cocoapods', :deterministic_uuids => false
66

7+
ENV['RCT_NEW_ARCH_ENABLED'] = '<%- project.turbomodule ? 1 : 0 -%>'
8+
79
target '<%- project.name -%>Example' do
810
config = use_native_modules!
911

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>IDEDidComputeMac32BitWarning</key>
6+
<true/>
7+
</dict>
8+
</plist>

packages/create-react-native-library/templates/java-library-mixed/android/src/legacy/{%- project.name %}Module.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ public String getName() {
1919
return <%- project.name -%>ModuleImpl.NAME;
2020
}
2121

22+
// Example method
23+
// See https://reactnative.dev/docs/native-modules-android
2224
@ReactMethod
2325
public void multiply(double a, double b, Promise promise) {
2426
<%- project.name -%>ModuleImpl.multiply(a, b, promise);

packages/create-react-native-library/templates/java-library-mixed/android/src/turbo/{%- project.name %}Module.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ public String getName() {
1818
return <%- project.name -%>ModuleImpl.NAME;
1919
}
2020

21+
// Example method
22+
// See https://reactnative.dev/docs/native-modules-android
23+
@Override
2124
@ReactMethod
2225
public void multiply(double a, double b, Promise promise) {
2326
<%- project.name -%>ModuleImpl.multiply(a, b, promise);

packages/create-react-native-library/templates/java-library-turbo/android/src/main/java/com/{%- project.package %}/{%- project.name %}Module.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ public String getName() {
2323

2424
// Example method
2525
// See https://reactnative.dev/docs/native-modules-android
26-
@ReactMethod
27-
public void multiply(double a, double b, Promise promise) {
28-
promise.resolve(a * b);
26+
@Override
27+
public double multiply(double a, double b) {
28+
return a * b;
2929
}
3030
}

packages/create-react-native-library/templates/native-library-mixed/src/Native{%- project.name %}.js

Lines changed: 0 additions & 9 deletions
This file was deleted.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import type { TurboModule } from 'react-native';
2+
import { TurboModuleRegistry } from 'react-native';
3+
4+
export interface Spec extends TurboModule {
5+
multiply(a: number, b: number): Promise<number>;
6+
}
7+
8+
export default TurboModuleRegistry.getEnforcing<Spec>('<%- project.name -%>');

packages/create-react-native-library/templates/native-library-turbo/src/Native{%- project.name %}.js

Lines changed: 0 additions & 9 deletions
This file was deleted.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import type { TurboModule } from 'react-native';
2+
import { TurboModuleRegistry } from 'react-native';
3+
4+
export interface Spec extends TurboModule {
5+
multiply(a: number, b: number): number;
6+
}
7+
8+
export default TurboModuleRegistry.getEnforcing<Spec>('<%- project.name -%>');
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const <%- project.name -%> = require('./Native<%- project.name -%>').default
22

3-
export function multiply(a: number, b: number): Promise<number> {
3+
export function multiply(a: number, b: number): number {
44
return <%- project.name -%>.multiply(a, b);
55
}

packages/create-react-native-library/templates/objc-library/ios/{%- project.name %}.mm

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,26 @@ @implementation <%- project.name -%>
1010

1111
// Example method
1212
// See // https://reactnative.dev/docs/native-modules-ios
13+
<% if (project.architecture == 'turbo') { -%>
14+
RCT_REMAP_BLOCKING_SYNCHRONOUS_METHOD(multiply,
15+
NSNumber *,
16+
multiplyWithA:(double)a withB:(double)b)
17+
{
18+
NSNumber *result = @(a * b);
19+
20+
return result;
21+
}
22+
<% } else { -%>
1323
RCT_REMAP_METHOD(multiply,
14-
multiplyWithA:(nonnull NSNumber*)a withB:(nonnull NSNumber*)b
24+
multiplyWithA:(double)a withB:(double)b
1525
withResolver:(RCTPromiseResolveBlock)resolve
1626
withRejecter:(RCTPromiseRejectBlock)reject)
1727
{
18-
NSNumber *result = @([a floatValue] * [b floatValue]);
28+
NSNumber *result = @(a * b);
1929

2030
resolve(result);
2131
}
32+
<% } -%>
2233

2334
// Don't compile this code when we build for the old architecture.
2435
#ifdef RCT_NEW_ARCH_ENABLED

0 commit comments

Comments
 (0)