Skip to content

Commit a84d375

Browse files
committed
chore: Simplify native API
1 parent aabd2d0 commit a84d375

18 files changed

Lines changed: 68 additions & 1356 deletions

api/src/main/kotlin/io/github/revenge/bridge/JavaScriptBridge.kt

Lines changed: 0 additions & 27 deletions
This file was deleted.

api/src/main/kotlin/io/github/revenge/bridge/impl/BridgeHost.kt

Lines changed: 0 additions & 17 deletions
This file was deleted.

api/src/main/kotlin/io/github/revenge/plugins/MethodCallback.kt

Lines changed: 0 additions & 17 deletions
This file was deleted.
Lines changed: 24 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,35 @@
11
package io.github.revenge.plugins
22

33
import InternalApi
4-
import android.content.Context
54
import android.content.pm.ApplicationInfo
6-
import io.github.revenge.plugins.impl.PluginsHost
7-
import java.util.*
8-
import java.util.concurrent.locks.ReentrantReadWriteLock
9-
import kotlin.concurrent.read
10-
import kotlin.concurrent.write
5+
import kotlin.properties.ReadOnlyProperty
116

12-
@OptIn(InternalApi::class)
13-
abstract class Plugin(val manifest: PluginManifest) {
14-
private val flagsLock = ReentrantReadWriteLock()
15-
private val _flags: MutableSet<PluginFlags> = mutableSetOf()
7+
typealias Callback = Plugin.(args: ArrayList<Any>) -> Any
168

17-
/**
18-
* The current flags set for the plugin.
19-
*
20-
* The returned set is a copy and modifying it does not affect the plugin's flags.
21-
* To update the flags, assign a new set to this property.
22-
*
23-
* Example:
24-
*
25-
* ```kotlin
26-
* plugin.flags += + PluginFlags.RELOAD_REQUIRED
27-
* ```
28-
*
29-
* See [PluginFlags] for all possible flags.
30-
*/
31-
var flags: Set<PluginFlags>
32-
get() = flagsLock.read { Collections.unmodifiableSet(_flags) }
33-
set(value) = flagsLock.write {
34-
val oldValue = _flags.toSet()
35-
if (oldValue != value) {
36-
_flags.clear()
37-
_flags.addAll(value)
38-
PluginsHost.notifyFlagsUpdate(this)
39-
}
40-
}
9+
class PluginBuilder() {
10+
private val methods: MutableMap<String, Callback> = mutableMapOf()
11+
private var init: ((ApplicationInfo, ClassLoader) -> Unit)? = null
4112

42-
/**
43-
* Runs when the plugin is loaded, before [start].
44-
*/
45-
open fun init(applicationInfo: ApplicationInfo, classLoader: ClassLoader) {}
13+
operator fun String.invoke(callback: Callback) = apply { methods[this] = callback }
4614

47-
/**
48-
* Runs when the plugin is able to access [Context].
49-
*/
50-
open fun start(context: Context) {}
15+
fun init(block: (ApplicationInfo, ClassLoader) -> Unit) = apply { init = block }
5116

52-
/**
53-
* Runs when the plugin is being stopped.
54-
*/
55-
open fun stop(context: Context) {}
17+
fun build(jsCaller: (methodName: String, args: Any) -> Any) = Plugin(init, methods, jsCaller)
18+
}
19+
20+
fun plugin(block: PluginBuilder.() -> Unit) = PluginBuilder().apply(block)
5621

57-
/**
58-
* Returns a map of method names to their corresponding bridge method callbacks.
59-
* These methods will be exposed to the JavaScript side of the application.
60-
*
61-
* This method is called immediately after [init]. If you need [Context], return a lambda that captures it from [start].
62-
*
63-
* See [MethodCallback] for possible return types.
64-
*/
65-
open fun getMethods(
66-
applicationInfo: ApplicationInfo,
67-
classLoader: ClassLoader
68-
): Map<String, MethodCallback> = mapOf()
22+
23+
class Plugin internal constructor(
24+
@OptIn(InternalApi::class) val init: ((ApplicationInfo, ClassLoader) -> Unit)?,
25+
@OptIn(InternalApi::class) val methods: Map<String, Callback>,
26+
val jsCaller: (methodName: String, args: Any) -> Any,
27+
) {
28+
operator fun String.invoke(vararg args: Any) = jsCaller(this, args.toList())
6929
}
30+
31+
val p = plugin {
32+
"name" {
33+
"jsFunc"("args", 1)
34+
}
35+
}

api/src/main/kotlin/io/github/revenge/plugins/PluginDependency.kt

Lines changed: 0 additions & 12 deletions
This file was deleted.

api/src/main/kotlin/io/github/revenge/plugins/PluginFlags.kt

Lines changed: 0 additions & 25 deletions
This file was deleted.

api/src/main/kotlin/io/github/revenge/plugins/PluginManifest.kt

Lines changed: 0 additions & 13 deletions
This file was deleted.

api/src/main/kotlin/io/github/revenge/plugins/PluginProvider.kt

Lines changed: 0 additions & 13 deletions
This file was deleted.

api/src/main/kotlin/io/github/revenge/plugins/PluginView.kt

Lines changed: 0 additions & 11 deletions
This file was deleted.

api/src/main/kotlin/io/github/revenge/plugins/impl/PluginsHost.kt

Lines changed: 0 additions & 41 deletions
This file was deleted.

0 commit comments

Comments
 (0)