Skip to content

Commit aabd2d0

Browse files
committed
feat: native to JS bridge
1 parent de9bc07 commit aabd2d0

5 files changed

Lines changed: 255 additions & 52 deletions

File tree

api/build.gradle.kts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,8 @@ android {
2020
repositories {
2121
google()
2222
mavenCentral()
23+
}
24+
25+
dependencies {
26+
compileOnly("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.10.2")
2327
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package io.github.revenge.bridge
2+
3+
import InternalApi
4+
import io.github.revenge.bridge.impl.BridgeHost
5+
import io.github.revenge.plugins.MethodArgs
6+
7+
@Suppress("UNUSED")
8+
@OptIn(InternalApi::class)
9+
/**
10+
* Bridge for plugins to communicate with the JavaScript side of the application.
11+
*
12+
* In order for a method to be exposed, you must register the method using the `modules` API.
13+
* Return values must be serializable by React Native, otherwise may result in unexpected behavior.
14+
*
15+
* ```js
16+
* import { registerJSMethod } from '@revenge-mod/modules/native'
17+
*
18+
* registerJSMethod('meaningOfLife', (arg1, arg2) => {
19+
* console.log(arg1 + arg2)
20+
* return 42
21+
* })
22+
* ```
23+
*/
24+
object JavaScriptBridge {
25+
fun callJSMethod(methodName: String, args: MethodArgs) =
26+
BridgeHost.callJSMethod!!.invoke(methodName, args)
27+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package io.github.revenge.bridge.impl
2+
3+
import InternalApi
4+
import io.github.revenge.plugins.MethodArgs
5+
import kotlinx.coroutines.CompletableDeferred
6+
7+
@Suppress("UNUSED")
8+
@InternalApi
9+
/**
10+
* Host for the bridge system to communicate with the application. For example, a Xposed module.
11+
*/
12+
object BridgeHost {
13+
/**
14+
* Callback for when a plugin calls a JS method.
15+
*/
16+
var callJSMethod: ((methodName: String, args: MethodArgs) -> CompletableDeferred<Any?>)? = null
17+
}
Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
package io.github.revenge.plugins
22

33
/**
4-
* See for possible return types:
5-
* https://github.com/facebook/react-native/blob/c23e84ae9/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridge/Arguments.kt#L19
4+
* You may return a [Unit] and it will be converted to `null`.
65
*
7-
* You may return a [Unit] and the resulting value will be `null`.
6+
* See [NativeObject] for more information.
87
*/
9-
typealias MethodCallback = (args: MethodArgs) -> Any?
8+
typealias MethodCallback = (args: MethodArgs) -> NativeObject
109

11-
typealias MethodArgs = ArrayList<Any?>
10+
typealias MethodArgs = ArrayList<NativeObject>
11+
12+
/**
13+
* Represents a React Native JNI serializable object. These objects can be passed to JavaScript as values.
14+
*
15+
* See: https://github.com/facebook/react-native/blob/7dcedf1/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridge/Arguments.kt#L19
16+
*/
17+
typealias NativeObject = Any?

0 commit comments

Comments
 (0)