feat(mobile): add command member to Invoke class (#6323)

This commit is contained in:
Lucas Fernandes Nogueira
2023-02-20 08:09:30 -08:00
committed by GitHub
parent 6fca895695
commit 037d488dee
5 changed files with 28 additions and 25 deletions

View File

@@ -8,6 +8,7 @@ import app.tauri.Logger
class Invoke(
val id: Long,
val command: String,
private val sendResponse: (success: PluginResult?, error: PluginResult?) -> Unit,
val data: JSObject) {

View File

@@ -123,9 +123,9 @@ class PluginHandle(private val manager: PluginManager, val name: String, private
InvalidPluginMethodException::class,
IllegalAccessException::class
)
fun invoke(methodName: String, invoke: Invoke) {
val methodMeta = pluginMethods[methodName]
?: throw InvalidPluginMethodException("No method " + methodName + " found for plugin " + instance.javaClass.name)
fun invoke(invoke: Invoke) {
val methodMeta = pluginMethods[invoke.command]
?: throw InvalidPluginMethodException("No command " + invoke.command + " found for plugin " + instance.javaClass.name)
methodMeta.method.invoke(instance, invoke)
}

View File

@@ -78,8 +78,8 @@ class PluginManager(val activity: AppCompatActivity) {
}
@JniMethod
fun postIpcMessage(webView: WebView, pluginId: String, methodName: String, data: JSObject, callback: Long, error: Long) {
val invoke = Invoke(callback, { successResult, errorResult ->
fun postIpcMessage(webView: WebView, pluginId: String, command: String, data: JSObject, callback: Long, error: Long) {
val invoke = Invoke(callback, command, { successResult, errorResult ->
val (fn, result) = if (errorResult == null) Pair(callback, successResult) else Pair(
error,
errorResult
@@ -87,22 +87,22 @@ class PluginManager(val activity: AppCompatActivity) {
webView.evaluateJavascript("window['_$fn']($result)", null)
}, data)
dispatchPluginMessage(invoke, pluginId, methodName)
dispatchPluginMessage(invoke, pluginId)
}
@JniMethod
fun runPluginMethod(id: Int, pluginId: String, methodName: String, data: JSObject) {
val invoke = Invoke(id.toLong(), { successResult, errorResult ->
fun runPluginMethod(id: Int, pluginId: String, command: String, data: JSObject) {
val invoke = Invoke(id.toLong(), command, { successResult, errorResult ->
handlePluginResponse(id, successResult?.toString(), errorResult?.toString())
}, data)
dispatchPluginMessage(invoke, pluginId, methodName)
dispatchPluginMessage(invoke, pluginId)
}
private fun dispatchPluginMessage(invoke: Invoke, pluginId: String, methodName: String) {
private fun dispatchPluginMessage(invoke: Invoke, pluginId: String) {
Logger.verbose(
Logger.tags("Plugin"),
"Tauri plugin: pluginId: $pluginId, methodName: $methodName"
"Tauri plugin: pluginId: $pluginId, command: $invoke.command"
)
try {
@@ -110,7 +110,7 @@ class PluginManager(val activity: AppCompatActivity) {
if (plugin == null) {
invoke.reject("Plugin $pluginId not initialized")
} else {
plugins[pluginId]?.invoke(methodName, invoke)
plugins[pluginId]?.invoke(invoke)
}
} catch (e: Exception) {
invoke.reject(e.toString())

View File

@@ -14,12 +14,14 @@ import UIKit
return ISO8601DateFormatter()
}()
var sendResponse: (JsonValue?, JsonValue?) -> Void
public var command: String
public var data: JSObject
var sendResponse: (JsonValue?, JsonValue?) -> Void
public init(sendResponse: @escaping (JsonValue?, JsonValue?) -> Void, data: JSObject?) {
self.sendResponse = sendResponse
public init(command: String, sendResponse: @escaping (JsonValue?, JsonValue?) -> Void, data: JSObject?) {
self.command = command
self.data = data ?? [:]
self.sendResponse = sendResponse
}
public func resolve() {

View File

@@ -55,10 +55,10 @@ public class PluginManager {
plugins[name] = handle
}
func invoke(name: String, methodName: String, invoke: Invoke) {
func invoke(name: String, invoke: Invoke) {
if let plugin = plugins[name] {
ipcDispatchQueue.async {
let selectorWithThrows = Selector(("\(methodName):error:"))
let selectorWithThrows = Selector(("\(invoke.command):error:"))
if plugin.instance.responds(to: selectorWithThrows) {
var error: NSError? = nil
withUnsafeMutablePointer(to: &error) {
@@ -70,11 +70,11 @@ public class PluginManager {
let _ = toRust(error) // TODO app is crashing without this memory leak (when an error is thrown)
}
} else {
let selector = Selector(("\(methodName):"))
let selector = Selector(("\(invoke.command):"))
if plugin.instance.responds(to: selector) {
plugin.instance.perform(selector, with: invoke)
} else {
invoke.reject("No method \(methodName) found for plugin \(name)")
invoke.reject("No command \(invoke.command) found for plugin \(name)")
}
}
}
@@ -105,8 +105,8 @@ func onWebviewCreated(webview: WKWebView, viewController: UIViewController) {
}
@_cdecl("post_ipc_message")
func postIpcMessage(webview: WKWebView, name: UnsafePointer<SRString>, methodName: UnsafePointer<SRString>, data: NSDictionary, callback: UInt, error: UInt) {
let invoke = Invoke(sendResponse: { (successResult: JsonValue?, errorResult: JsonValue?) -> Void in
func postIpcMessage(webview: WKWebView, name: UnsafePointer<SRString>, command: UnsafePointer<SRString>, data: NSDictionary, callback: UInt, error: UInt) {
let invoke = Invoke(command: command.pointee.to_string(), sendResponse: { (successResult: JsonValue?, errorResult: JsonValue?) -> Void in
let (fn, payload) = errorResult == nil ? (callback, successResult) : (error, errorResult)
var payloadJson: String
do {
@@ -116,18 +116,18 @@ func postIpcMessage(webview: WKWebView, name: UnsafePointer<SRString>, methodNam
}
webview.evaluateJavaScript("window['_\(fn)'](\(payloadJson))")
}, data: JSTypes.coerceDictionaryToJSObject(data, formattingDatesAsStrings: true))
PluginManager.shared.invoke(name: name.pointee.to_string(), methodName: methodName.pointee.to_string(), invoke: invoke)
PluginManager.shared.invoke(name: name.pointee.to_string(), invoke: invoke)
}
@_cdecl("run_plugin_method")
func runPluginMethod(
id: Int,
name: UnsafePointer<SRString>,
methodName: UnsafePointer<SRString>,
command: UnsafePointer<SRString>,
data: NSDictionary,
callback: @escaping @convention(c) (Int, Bool, UnsafePointer<CChar>?) -> Void
) {
let invoke = Invoke(sendResponse: { (successResult: JsonValue?, errorResult: JsonValue?) -> Void in
let invoke = Invoke(command: command.pointee.to_string(), sendResponse: { (successResult: JsonValue?, errorResult: JsonValue?) -> Void in
let (success, payload) = errorResult == nil ? (true, successResult) : (false, errorResult)
var payloadJson: String = ""
do {
@@ -137,5 +137,5 @@ func runPluginMethod(
}
callback(id, success, payloadJson.cString(using: String.Encoding.utf8))
}, data: JSTypes.coerceDictionaryToJSObject(data, formattingDatesAsStrings: true))
PluginManager.shared.invoke(name: name.pointee.to_string(), methodName: methodName.pointee.to_string(), invoke: invoke)
PluginManager.shared.invoke(name: name.pointee.to_string(), invoke: invoke)
}