[v3] Add CallByID method to runtime.

This commit is contained in:
Lea Anthony
2023-08-27 16:59:18 +10:00
parent cdf48e0589
commit 21790dc3ee
20 changed files with 423 additions and 533 deletions
+13 -4
View File
@@ -10,11 +10,13 @@ The electron alternative for Go
/* jshint esversion: 9 */
import {newRuntimeCaller} from "./runtime";
import {newRuntimeCaller, newRuntimeCallerWithID, objectNames} from "./runtime";
import { nanoid } from 'nanoid/non-secure';
let call = newRuntimeCaller("call");
let call = newRuntimeCallerWithID(objectNames.Call);
let CallBinding = 0;
let callResponses = new Map();
@@ -61,7 +63,14 @@ function callBinding(type, options) {
}
export function Call(options) {
return callBinding("Call", options);
return callBinding(CallBinding, options);
}
export function CallByID(methodID, ...args) {
return callBinding(CallBinding, {
methodID: methodID,
args: args,
});
}
/**
@@ -72,7 +81,7 @@ export function Call(options) {
* @returns {Promise<any>} - promise that resolves with the result
*/
export function Plugin(pluginName, methodName, ...args) {
return callBinding("Call", {
return callBinding(CallBinding, {
packageName: "wails-plugins",
structName: pluginName,
methodName: methodName,
+2 -1
View File
@@ -14,7 +14,7 @@ import * as Clipboard from './clipboard';
import * as Application from './application';
import * as Screens from './screens';
import * as System from './system';
import {Plugin, Call, callErrorCallback, callCallback} from "./calls";
import {Plugin, Call, callErrorCallback, callCallback, CallByID} from "./calls";
import {newWindow} from "./window";
import {dispatchWailsEvent, Emit, Off, OffAll, On, Once, OnMultiple} from "./events";
import {dialogCallback, dialogErrorCallback, Error, Info, OpenFile, Question, SaveFile, Warning,} from "./dialogs";
@@ -57,6 +57,7 @@ export function newRuntime(windowName) {
System,
Screens,
Call,
CallByID,
Plugin,
WML: {
Reload: reloadWML,
-8
View File
@@ -36,10 +36,6 @@ function runtimeCall(method, windowName, args) {
fetchOptions.headers["x-wails-window-name"] = windowName;
}
if (args) {
if (args['wails-method-id']) {
fetchOptions.headers["x-wails-method-id"] = args['wails-method-id'];
delete args['wails-method-id'];
}
url.searchParams.append("args", JSON.stringify(args));
}
return new Promise((resolve, reject) => {
@@ -77,10 +73,6 @@ function runtimeCallWithID(objectID, method, windowName, args) {
fetchOptions.headers["x-wails-window-name"] = windowName;
}
if (args) {
if (args['wails-method-id']) {
fetchOptions.headers["x-wails-method-id"] = args['wails-method-id'];
delete args['wails-method-id'];
}
url.searchParams.append("args", JSON.stringify(args));
}
return new Promise((resolve, reject) => {
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
+2
View File
@@ -11,6 +11,7 @@ import (
)
type CallOptions struct {
MethodID uint32 `json:"methodID"`
PackageName string `json:"packageName"`
StructName string `json:"structName"`
MethodName string `json:"methodName"`
@@ -147,6 +148,7 @@ func (b *Bindings) AddPlugins(plugins map[string]Plugin) error {
b.boundMethods[packageName][structName] = make(map[string]*BoundMethod)
}
b.boundMethods[packageName][structName][methodName] = method
b.boundByID[method.ID] = method
globalApplication.info("Added plugin method: "+structName+"."+methodName, "id", method.ID)
}
}
+33 -50
View File
@@ -2,12 +2,10 @@ package application
import (
"fmt"
jsoniter "github.com/json-iterator/go"
"log/slog"
"net/http"
"strconv"
"strings"
jsoniter "github.com/json-iterator/go"
)
// TODO maybe we could use a new struct that has the targetWindow as an attribute so we could get rid of passing the targetWindow
@@ -70,52 +68,37 @@ func (m *MessageProcessor) HandleRuntimeCall(rw http.ResponseWriter, r *http.Req
m.HandleRuntimeCallWithIDs(rw, r)
return
}
// Read "method" from query string
method := r.URL.Query().Get("method")
if method == "" {
m.httpError(rw, "No method specified")
return
}
splitMethod := strings.Split(method, ".")
if len(splitMethod) != 2 {
m.httpError(rw, "Invalid method format")
return
}
// Get the object
object = splitMethod[0]
// Get the method
method = splitMethod[1]
params := QueryParams(r.URL.Query())
targetWindow := m.getTargetWindow(r)
if targetWindow == nil {
m.httpError(rw, "No valid window found")
return
}
switch object {
//case "window":
// m.processWindowMethod(method, rw, r, targetWindow, params)
//case "clipboard":
// m.processClipboardMethod(method, rw, r, targetWindow, params)
//case "dialog":
// m.processDialogMethod(method, rw, r, targetWindow, params)
//case "events":
// m.processEventsMethod(method, rw, r, targetWindow, params)
//case "application":
// m.processApplicationMethod(method, rw, r, targetWindow, params)
//case "contextmenu":
// m.processContextMenuMethod(method, rw, r, targetWindow, params)
//case "screens":
// m.processScreensMethod(method, rw, r, targetWindow, params)
case "call":
m.processCallMethod(method, rw, r, targetWindow, params)
//case "system":
// m.processSystemMethod(method, rw, r, targetWindow, params)
default:
m.httpError(rw, "Unknown runtime call: %s", object)
}
//// Read "method" from query string
//method := r.URL.Query().Get("method")
//if method == "" {
// m.httpError(rw, "No method specified")
// return
//}
//splitMethod := strings.Split(method, ".")
//if len(splitMethod) != 2 {
// m.httpError(rw, "Invalid method format")
// return
//}
//// Get the object
//object = splitMethod[0]
//// Get the method
//method = splitMethod[1]
//
//params := QueryParams(r.URL.Query())
//
//targetWindow := m.getTargetWindow(r)
//if targetWindow == nil {
// m.httpError(rw, "No valid window found")
// return
//}
//
//switch object {
//case "call":
// m.processCallMethod(method, rw, r, targetWindow, params)
//default:
// m.httpError(rw, "Unknown runtime call: %s", object)
//}
}
func (m *MessageProcessor) HandleRuntimeCallWithIDs(rw http.ResponseWriter, r *http.Request) {
@@ -152,8 +135,8 @@ func (m *MessageProcessor) HandleRuntimeCallWithIDs(rw http.ResponseWriter, r *h
m.processContextMenuMethod(method, rw, r, targetWindow, params)
case screensRequest:
m.processScreensMethod(method, rw, r, targetWindow, params)
//case callRequest:
// m.processCallMethod(method, rw, r, targetWindow, params)
case callRequest:
m.processCallMethod(method, rw, r, targetWindow, params)
case systemRequest:
m.processSystemMethod(method, rw, r, targetWindow, params)
default:
+8 -10
View File
@@ -7,6 +7,10 @@ import (
"strconv"
)
const (
CallBinding = 0
)
func (m *MessageProcessor) callErrorCallback(window *WebviewWindow, message string, callID *string, err error) {
errorMsg := fmt.Sprintf(message, err)
m.Error(errorMsg)
@@ -19,7 +23,7 @@ func (m *MessageProcessor) callCallback(window *WebviewWindow, callID *string, r
window.ExecJS(msg)
}
func (m *MessageProcessor) processCallMethod(method string, rw http.ResponseWriter, r *http.Request, window *WebviewWindow, params QueryParams) {
func (m *MessageProcessor) processCallMethod(method int, rw http.ResponseWriter, r *http.Request, window *WebviewWindow, params QueryParams) {
args, err := params.Args()
if err != nil {
m.httpError(rw, "Unable to parse arguments: %s", err)
@@ -31,7 +35,7 @@ func (m *MessageProcessor) processCallMethod(method string, rw http.ResponseWrit
return
}
switch method {
case "Call":
case CallBinding:
var options CallOptions
err := params.ToStruct(&options)
if err != nil {
@@ -39,20 +43,14 @@ func (m *MessageProcessor) processCallMethod(method string, rw http.ResponseWrit
return
}
var boundMethod *BoundMethod
methodID := r.Header.Get("x-wails-method-id")
if methodID == "" {
if options.PackageName != "" {
boundMethod = globalApplication.bindings.Get(&options)
if boundMethod == nil {
m.callErrorCallback(window, "Error getting binding for method: %s", callID, fmt.Errorf("method '%s' not found", options.Name()))
return
}
} else {
id, err := strconv.ParseUint(methodID, 10, 32)
if err != nil {
m.callErrorCallback(window, "Error parsing method id for call: %s", callID, err)
return
}
boundMethod = globalApplication.bindings.GetByID(uint32(id))
boundMethod = globalApplication.bindings.GetByID(options.MethodID)
}
if boundMethod == nil {
m.callErrorCallback(window, "Error getting binding for method: %s", callID, fmt.Errorf("method ID '%s' not found", options.Name()))
@@ -37,7 +37,7 @@ const (
WindowSetZoomLevel = 29
)
var windowMethods = map[int]string{
var windowMethodNames = map[int]string{
WindowCenter: "Center",
WindowSetTitle: "SetTitle",
WindowFullscreen: "Fullscreen",
@@ -255,10 +255,5 @@ func (m *MessageProcessor) processWindowMethod(method int, rw http.ResponseWrite
m.httpError(rw, "Unknown window method id: %s", method)
}
var logArgs = []any{"method", method}
for name, arg := range args.data {
logArgs = append(logArgs, name)
logArgs = append(logArgs, arg)
}
m.Info("Runtime:", logArgs...)
m.Info("Runtime:", "method", "Window."+windowMethodNames[method])
}
+2 -2
View File
@@ -5,7 +5,7 @@
* @returns {Promise<void>}
*/
function OpenURL(url) {
return wails.Plugin("browser", "OpenURL", url);
return wails.CallByID(3188315539, url);
}
/**
@@ -14,7 +14,7 @@ function OpenURL(url) {
* @returns {Promise<void>}
*/
function OpenFile(filename) {
return wails.Plugin("browser", "OpenFile", filename);
return wails.CallByID(3105408620, filename);
}
export default {
+3 -3
View File
@@ -8,7 +8,7 @@
* @returns {Promise<any>} - The value of the key.
*/
export function Get(key) {
return wails.Plugin("kvstore", "Get", key);
return wails.CallByID(3322496224, key);
}
/**
@@ -18,7 +18,7 @@ export function Get(key) {
* @returns {Promise<void>}
*/
export function Set(key, value) {
return wails.Plugin("kvstore", "Set", key, value);
return wails.CallByID(1207638860, key, value);
}
@@ -27,5 +27,5 @@ export function Set(key, value) {
* @returns {Promise<void|Error>}
*/
export function Save() {
return wails.Plugin("kvstore", "Save");
return wails.CallByID(1377075201);
}
+4 -4
View File
@@ -10,7 +10,7 @@
*/
function Debug(input, ...args) {
return wails.Plugin("log", "Debug", input, ...args);
return wails.CallByID(4111675027, input, ...args);
}
/**
@@ -20,7 +20,7 @@ function Debug(input, ...args) {
* @returns {Promise<void|Error>}
*/
function Info(input, ...args) {
return wails.Plugin("log", "Info", input, ...args);
return wails.CallByID(2391172776, input, ...args);
}
/**
@@ -30,7 +30,7 @@ function Info(input, ...args) {
* @returns {Promise<void|Error>}
*/
function Warning(input, ...args) {
return wails.Plugin("log", "Warning", input, ...args);
return wails.CallByID(2762394760, input, ...args);
}
/**
@@ -40,5 +40,5 @@ function Warning(input, ...args) {
* @returns {Promise<void|Error>}
*/
function Error(input, ...args) {
return wails.Plugin("log", "Error", input, ...args);
return wails.CallByID(878590242, input, ...args);
}
+311 -305
View File
File diff suppressed because it is too large Load Diff
+1 -1
View File
@@ -4,5 +4,5 @@
* @returns {Promise<void>}
*/
function Close() {
return wails.Plugin("sqlite", "Close");
return wails.CallByID(3998329564);
}
+2 -2
View File
@@ -6,7 +6,7 @@
* @returns {Promise<void>}
*/
function Execute(statement, ...args) {
return wails.Plugin("sqlite", "Execute", statement, ...args);
return wails.CallByID(2804887383, statement, ...args);
}
/**
@@ -16,5 +16,5 @@ function Execute(statement, ...args) {
* @returns {Promise<any>}
*/
function Select(statement, ...args) {
return wails.Plugin("sqlite", "Select", statement, ...args);
return wails.CallByID(2209315040, statement, ...args);
}
+1 -1
View File
@@ -4,5 +4,5 @@
* @returns {Promise<void>}
*/
function Open(filename) {
return wails.Plugin("sqlite", "Open", filename);
return wails.CallByID(147348976, filename);
}