Skip to content

Commit

Permalink
feat: update turbo modules template to use a synchronous API
Browse files Browse the repository at this point in the history
  • Loading branch information
MateWW authored and satya164 committed Jun 30, 2022
1 parent bbcde94 commit 1393060
Show file tree
Hide file tree
Showing 16 changed files with 73 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ android.iml
#
example/ios/Pods

# Ruby
example/vendor/

# node.js
#
node_modules/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@ To run the example app on iOS:
```sh
yarn example ios
```

<% if (!project.native) { -%>
To run the example app on Web:

```sh
yarn example web
```
<% } -%>

Make sure your code passes TypeScript and ESLint. Run the following to verify:

```sh
Expand Down
10 changes: 9 additions & 1 deletion packages/create-react-native-library/templates/common/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
## Installation

```sh
npm install <%- project.slug -%>
npm install <%- project.slug %>
```

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

<<%- project.name -%>View color="tomato" />
```
<% } else if (project.architecture == 'turbo') { -%>
```js
import { multiply } from "<%- project.slug -%>";

// ...

const result = multiply(3, 7);
```
<% } else { -%>
```js
import { multiply } from "<%- project.slug -%>";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ export default function App() {
</View>
);
}
<% } else if (project.architecture == 'turbo') { -%>
const result = multiply(3, 7);

export default function App() {
return (
<View style={styles.container}>
<Text>Result: {result}</Text>
</View>
);
}
<% } else { -%>
export default function App() {
const [result, setResult] = React.useState<number | undefined>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"android": "react-native run-android",
"ios": "react-native run-ios",
"start": "react-native start",
"pods": "RCT_NEW_ARCH_ENABLED=<%- project.turbomodule ? 1 : 0 -%> pod-install --quiet",
"pods": "pod-install --quiet",
"postinstall": "patch-package"
},
"dependencies": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ require_relative '../node_modules/@react-native-community/cli-platform-ios/nativ
platform :ios, '11.0'
install! 'cocoapods', :deterministic_uuids => false

ENV['RCT_NEW_ARCH_ENABLED'] = '<%- project.turbomodule ? 1 : 0 -%>'

target '<%- project.name -%>Example' do
config = use_native_modules!

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public String getName() {
return <%- project.name -%>ModuleImpl.NAME;
}

// Example method
// See https://reactnative.dev/docs/native-modules-android
@ReactMethod
public void multiply(double a, double b, Promise promise) {
<%- project.name -%>ModuleImpl.multiply(a, b, promise);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ public String getName() {
return <%- project.name -%>ModuleImpl.NAME;
}

// Example method
// See https://reactnative.dev/docs/native-modules-android
@Override
@ReactMethod
public void multiply(double a, double b, Promise promise) {
<%- project.name -%>ModuleImpl.multiply(a, b, promise);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ public String getName() {

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

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import type { TurboModule } from 'react-native';
import { TurboModuleRegistry } from 'react-native';

export interface Spec extends TurboModule {
multiply(a: number, b: number): Promise<number>;
}

export default TurboModuleRegistry.getEnforcing<Spec>('<%- project.name -%>');

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import type { TurboModule } from 'react-native';
import { TurboModuleRegistry } from 'react-native';

export interface Spec extends TurboModule {
multiply(a: number, b: number): number;
}

export default TurboModuleRegistry.getEnforcing<Spec>('<%- project.name -%>');
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const <%- project.name -%> = require('./Native<%- project.name -%>').default

export function multiply(a: number, b: number): Promise<number> {
export function multiply(a: number, b: number): number {
return <%- project.name -%>.multiply(a, b);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,26 @@ @implementation <%- project.name -%>

// Example method
// See // https://reactnative.dev/docs/native-modules-ios
<% if (project.architecture == 'turbo') { -%>
RCT_REMAP_BLOCKING_SYNCHRONOUS_METHOD(multiply,
NSNumber *,
multiplyWithA:(double)a withB:(double)b)
{
NSNumber *result = @(a * b);

return result;
}
<% } else { -%>
RCT_REMAP_METHOD(multiply,
multiplyWithA:(nonnull NSNumber*)a withB:(nonnull NSNumber*)b
multiplyWithA:(double)a withB:(double)b
withResolver:(RCTPromiseResolveBlock)resolve
withRejecter:(RCTPromiseRejectBlock)reject)
{
NSNumber *result = @([a floatValue] * [b floatValue]);
NSNumber *result = @(a * b);

resolve(result);
}
<% } -%>

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

0 comments on commit 1393060

Please sign in to comment.