5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-05 13:42:16 +08:00
wails/v3/pkg/application/messageprocessor_call.go
Lea Anthony c531c714d4
Update vanilla+js template and dependencies
The frontend interaction has been significantly updated, including improvements to the Greet function and the addition of an ongoing time display. Additionally, dependencies in package.json have been updated to their latest versions. Also, redundant print statement was removed from messageprocessor_call.go and changes were made to layout structure and styles in the main.go.tmpl and index.html files.
2023-12-30 21:11:38 +11:00

80 lines
2.2 KiB
Go

package application
import (
"encoding/json"
"fmt"
"net/http"
)
const (
CallBinding = 0
)
func (m *MessageProcessor) callErrorCallback(window Window, message string, callID *string, err error) {
errorMsg := fmt.Sprintf(message, err)
m.Error(errorMsg)
window.CallError(*callID, errorMsg)
}
func (m *MessageProcessor) callCallback(window Window, callID *string, result string, isJSON bool) {
window.CallResponse(*callID, result)
}
func (m *MessageProcessor) processCallMethod(method int, rw http.ResponseWriter, r *http.Request, window Window, params QueryParams) {
args, err := params.Args()
if err != nil {
m.httpError(rw, "Unable to parse arguments: %s", err.Error())
return
}
callID := args.String("call-id")
if callID == nil {
m.Error("call-id is required")
return
}
switch method {
case CallBinding:
var options CallOptions
err := params.ToStruct(&options)
if err != nil {
m.callErrorCallback(window, "Error parsing call options: %s", callID, err)
return
}
var boundMethod *BoundMethod
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 {
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()))
return
}
go func() {
result, err := boundMethod.Call(options.Args)
if err != nil {
m.callErrorCallback(window, "Error calling method: %s", callID, err)
return
}
var jsonResult = []byte("{}")
if result != nil {
// convert result to json
jsonResult, err = json.Marshal(result)
if err != nil {
m.callErrorCallback(window, "Error converting result to json: %s", callID, err)
return
}
}
m.callCallback(window, callID, string(jsonResult), true)
m.Info("Call Binding:", "method", boundMethod, "args", options.Args, "result", result)
}()
m.ok(rw)
default:
m.httpError(rw, "Unknown call method: %d", method)
}
}