Skip to content

Support TurboModule #239

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"parser": "babel-eslint",
"plugins": ["prettier", "jest"],
"parser": "@babel/eslint-parser",
"plugins": ["ft-flow", "prettier", "jest"],
"env": {
"es6": true,
"node": true,
Expand Down
94 changes: 94 additions & 0 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,20 @@ buildscript {
}
}

def isNewArchitectureEnabled() {
return rootProject.hasProperty('newArchEnabled') && rootProject.getProperty('newArchEnabled') == 'true'
}

apply plugin: 'com.android.library'

def safeExtGet(prop, fallback) {
rootProject.ext.has(prop) ? rootProject.ext.get(prop) : fallback
}

if (isNewArchitectureEnabled()) {
apply plugin: 'com.facebook.react'
}

android {
compileSdkVersion safeExtGet('compileSdkVersion', 31)

Expand All @@ -31,10 +39,23 @@ android {
defaultConfig {
minSdkVersion safeExtGet('minSdkVersion', 16)
targetSdkVersion safeExtGet('targetSdkVersion', 31)
buildConfigField 'boolean', 'IS_NEW_ARCHITECTURE_ENABLED', isNewArchitectureEnabled().toString()
}
lintOptions{
abortOnError false
}
sourceSets {
main {
if (isNewArchitectureEnabled()) {
java.srcDirs += [
"src/turbo",
"${project.buildDir}/generated/source/codegen/java"
]
} else {
java.srcDirs += ["src/legacy"]
}
}
}
}

repositories {
Expand All @@ -45,9 +66,82 @@ repositories {
google()
mavenLocal()
mavenCentral()

def found = false
def defaultDir = null
def androidSourcesName = 'React Native sources'

if (rootProject.ext.has('reactNativeAndroidRoot')) {
defaultDir = rootProject.ext.get('reactNativeAndroidRoot')
} else {
defaultDir = new File(
projectDir,
'/../../../node_modules/react-native/android'
)
}

if (defaultDir.exists()) {
maven {
url defaultDir.toString()
name androidSourcesName
}

logger.info(":${project.name}:reactNativeAndroidRoot ${defaultDir.canonicalPath}")
found = true
} else {
def parentDir = rootProject.projectDir

1.upto(5, {
if (found) return true
parentDir = parentDir.parentFile

def androidSourcesDir = new File(
parentDir,
'node_modules/react-native'
)

def androidPrebuiltBinaryDir = new File(
parentDir,
'node_modules/react-native/android'
)

if (androidPrebuiltBinaryDir.exists()) {
maven {
url androidPrebuiltBinaryDir.toString()
name androidSourcesName
}

logger.info(":${project.name}:reactNativeAndroidRoot ${androidPrebuiltBinaryDir.canonicalPath}")
found = true
} else if (androidSourcesDir.exists()) {
maven {
url androidSourcesDir.toString()
name androidSourcesName
}

logger.info(":${project.name}:reactNativeAndroidRoot ${androidSourcesDir.canonicalPath}")
found = true
}
})
}

if (!found) {
throw new GradleException(
"${project.name}: unable to locate React Native android sources. " +
"Ensure you have you installed React Native as a dependency in your project and try again."
)
}
}

dependencies {
//noinspection GradleDynamicVersion
implementation 'com.facebook.react:react-native:+'
}

if (isNewArchitectureEnabled()) {
react {
jsRootDir = file("../src/")
libraryName = "UdpSockets"
codegenJavaPackageName = "com.tradle.react"
}
}
37 changes: 37 additions & 0 deletions android/src/legacy/UdpSocketsSpec.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.tradle.react;

import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.Callback;
import javax.annotation.Nullable;

public abstract class UdpSocketsSpec extends ReactContextBaseJavaModule {
UdpSocketsSpec(ReactApplicationContext context) {
super(context);
}

public abstract void createSocket(double cId, ReadableMap options);
public abstract void bind(
double cId,
double port,
@Nullable String address,
@Nullable ReadableMap options,
Callback callback
);
public abstract void addMembership(double cId, String multicastAddress);
public abstract void dropMembership(double cId, String multicastAddress);
public abstract void send(
double cId,
String base64String,
double port,
String address,
Callback callback
);
public abstract void close(double cId, Callback callback);
public abstract void setBroadcast(double cId, boolean flag, Callback callback);

// RCTEventEmitter
public abstract void addListener(String eventName);
public abstract void removeListeners(double count);
g
58 changes: 33 additions & 25 deletions android/src/main/java/com/tradle/react/UdpSockets.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.WritableMap;
Expand All @@ -28,9 +27,9 @@
/**
* The NativeModule in charge of storing active {@link UdpSocketClient}s, and acting as an api layer.
*/
public final class UdpSockets extends ReactContextBaseJavaModule
public final class UdpSockets extends UdpSocketsSpec
implements UdpSocketClient.OnDataReceivedListener, UdpSocketClient.OnRuntimeExceptionListener {
private static final String TAG = "UdpSockets";
public static final String TAG = "UdpSockets";
private static final int N_THREADS = 2;

private WifiManager.MulticastLock mMulticastLock;
Expand Down Expand Up @@ -68,8 +67,8 @@ public void run() {
/**
* Private method to retrieve clients.
*/
private UdpSocketClient findClient(final Integer cId, final Callback callback) {
final UdpSocketClient client = mClients.get(cId);
private UdpSocketClient findClient(final double cId, final Callback callback) {
final UdpSocketClient client = mClients.get((int)cId);
if (client == null) {
if (callback == null) {
FLog.e(TAG, "missing callback parameter.");
Expand All @@ -84,26 +83,23 @@ private UdpSocketClient findClient(final Integer cId, final Callback callback) {
/**
* Creates a {@link UdpSocketClient} with the given ID, and options
*/
@Override
@ReactMethod
public void createSocket(final Integer cId, final ReadableMap options) {
if (cId == null) {
FLog.e(TAG, "createSocket called with nil id parameter.");
return;
}

UdpSocketClient client = mClients.get(cId);
public void createSocket(final double cId, final ReadableMap options) {
UdpSocketClient client = mClients.get((int)cId);
if (client != null) {
FLog.e(TAG, "createSocket called twice with the same id.");
return;
}
mClients.put(cId, new UdpSocketClient(this, this));
mClients.put((int)cId, new UdpSocketClient(this, this));
}

/**
* Binds to a given port and address, and begins listening for data.
*/
@Override
@ReactMethod
public void bind(final Integer cId, final Integer port, final @Nullable String address, final @Nullable ReadableMap options,
public void bind(final double cId, final double port, final @Nullable String address, final @Nullable ReadableMap options,
final Callback callback) {
executorService.execute(new Thread(new Runnable() {
@Override
Expand All @@ -114,11 +110,11 @@ public void run() {
}

try {
client.bind(port, address);
client.bind((int)port, address);

WritableMap result = Arguments.createMap();
result.putString("address", address);
result.putInt("port", port);
result.putInt("port", (int)port);

callback.invoke(null, result);
} catch (Exception e) {
Expand All @@ -132,9 +128,10 @@ public void run() {
/**
* Joins a multi-cast group
*/
@Override
@SuppressWarnings("unused")
@ReactMethod
public void addMembership(final Integer cId, final String multicastAddress) {
public void addMembership(final double cId, final String multicastAddress) {
executorService.execute(new Thread(new Runnable() {
@Override
public void run() {
Expand Down Expand Up @@ -180,8 +177,9 @@ public void run() {
/**
* Leaves a multi-cast group
*/
@Override
@ReactMethod
public void dropMembership(final Integer cId, final String multicastAddress) {
public void dropMembership(final double cId, final String multicastAddress) {
executorService.execute(new Thread(new Runnable() {
@Override
public void run() {
Expand All @@ -207,9 +205,10 @@ public void run() {
/**
* Sends udp data via the {@link UdpSocketClient}
*/
@Override
@ReactMethod
public void send(final Integer cId, final String base64String,
final Integer port, final String address, final Callback callback) {
public void send(final double cId, final String base64String,
final double port, final String address, final Callback callback) {
executorService.execute(new Thread(new Runnable() {
@Override
public void run() {
Expand All @@ -219,7 +218,7 @@ public void run() {
}

try {
client.send(base64String, port, address, callback);
client.send(base64String, (int)port, address, callback);
} catch (Exception exception) {
callback.invoke((UdpErrorUtil.getError(UdpErrorCodes.sendError.name(), exception.getMessage())));
}
Expand All @@ -230,8 +229,9 @@ public void run() {
/**
* Closes a specific client's socket, and removes it from the list of known clients.
*/
@Override
@ReactMethod
public void close(final Integer cId, final Callback callback) {
public void close(final double cId, final Callback callback) {
executorService.execute(new Thread(new Runnable() {
@Override
public void run() {
Expand All @@ -246,16 +246,17 @@ public void run() {
}
client.close();
callback.invoke();
mClients.remove(cId);
mClients.remove((int)cId);
}
}));
}

/**
* Sets the broadcast flag for a given client.
*/
@Override
@ReactMethod
public void setBroadcast(final Integer cId, final Boolean flag, final Callback callback) {
public void setBroadcast(final double cId, final boolean flag, final Callback callback) {
executorService.execute(new Thread(new Runnable() {
@Override
public void run() {
Expand All @@ -274,6 +275,12 @@ public void run() {
}));
}

@Override
public void addListener(String eventName) {}

@Override
public void removeListeners(double count) {}

/**
* Notifies the javascript layer upon data receipt.
*/
Expand All @@ -297,6 +304,7 @@ public void run() {
}

WritableMap eventParams = Arguments.createMap();
eventParams.putInt("id", clientID);
eventParams.putString("data", data);
eventParams.putString("address", host);
eventParams.putInt("port", port);
Expand All @@ -306,7 +314,7 @@ public void run() {
ReactContext reactContext = UdpSockets.this.getReactApplicationContext();
reactContext
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit("udp-" + clientID + "-data", eventParams);
.emit("UdpSocketMessage", eventParams);
}
}));
}
Expand Down
Loading