5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-03 04:29:35 +08:00

[v2] Do not block during processing of messages

On windows blocking here results in a blocked main thread
and a blocked webview.
This commit is contained in:
stffabi 2021-12-01 13:24:19 +01:00
parent ec1a535acb
commit 155f1fde49
3 changed files with 66 additions and 54 deletions

View File

@ -239,23 +239,26 @@ func (f *Frontend) processMessage(message string) {
// return // return
//} //}
result, err := f.dispatcher.ProcessMessage(message, f) go func() {
if err != nil { result, err := f.dispatcher.ProcessMessage(message, f)
f.logger.Error(err.Error()) if err != nil {
f.Callback(result) f.logger.Error(err.Error())
return f.Callback(result)
} return
if result == "" { }
return if result == "" {
} return
}
switch result[0] {
case 'c':
// Callback from a method call
f.Callback(result[1:])
default:
f.logger.Info("Unknown message returned from dispatcher: %+v", result)
}
}()
switch result[0] {
case 'c':
// Callback from a method call
f.Callback(result[1:])
default:
f.logger.Info("Unknown message returned from dispatcher: %+v", result)
}
} }
func (f *Frontend) Callback(message string) { func (f *Frontend) Callback(message string) {

View File

@ -24,11 +24,6 @@ import "C"
import ( import (
"context" "context"
"encoding/json" "encoding/json"
"github.com/wailsapp/wails/v2/internal/binding"
"github.com/wailsapp/wails/v2/internal/frontend"
"github.com/wailsapp/wails/v2/internal/frontend/assetserver"
"github.com/wailsapp/wails/v2/internal/logger"
"github.com/wailsapp/wails/v2/pkg/options"
"log" "log"
"os" "os"
"strconv" "strconv"
@ -36,6 +31,12 @@ import (
"sync" "sync"
"text/template" "text/template"
"unsafe" "unsafe"
"github.com/wailsapp/wails/v2/internal/binding"
"github.com/wailsapp/wails/v2/internal/frontend"
"github.com/wailsapp/wails/v2/internal/frontend/assetserver"
"github.com/wailsapp/wails/v2/internal/logger"
"github.com/wailsapp/wails/v2/pkg/options"
) )
type Frontend struct { type Frontend struct {
@ -235,23 +236,26 @@ func (f *Frontend) processMessage(message string) {
//} //}
return return
} }
result, err := f.dispatcher.ProcessMessage(message, f)
if err != nil {
f.logger.Error(err.Error())
f.Callback(result)
return
}
if result == "" {
return
}
switch result[0] { go func() {
case 'c': result, err := f.dispatcher.ProcessMessage(message, f)
// Callback from a method call if err != nil {
f.Callback(result[1:]) f.logger.Error(err.Error())
default: f.Callback(result)
f.logger.Info("Unknown message returned from dispatcher: %+v", result) return
} }
if result == "" {
return
}
switch result[0] {
case 'c':
// Callback from a method call
f.Callback(result[1:])
default:
f.logger.Info("Unknown message returned from dispatcher: %+v", result)
}
}()
} }
func (f *Frontend) Callback(message string) { func (f *Frontend) Callback(message string) {

View File

@ -259,7 +259,9 @@ func (f *Frontend) WindowSetRGBA(col *options.RGBA) {
} }
func (f *Frontend) Quit() { func (f *Frontend) Quit() {
winc.Exit() // Exit must be called on the Main-Thread. It calls PostQuitMessage which sends the WM_QUIT message to the thread's
// message queue and our message queue runs on the Main-Thread.
f.mainWindow.Invoke(winc.Exit)
} }
func (f *Frontend) setupChromium() { func (f *Frontend) setupChromium() {
@ -398,23 +400,26 @@ func (f *Frontend) processMessage(message string) {
} }
return return
} }
result, err := f.dispatcher.ProcessMessage(message, f)
if err != nil {
f.logger.Error(err.Error())
f.Callback(result)
return
}
if result == "" {
return
}
switch result[0] { go func() {
case 'c': result, err := f.dispatcher.ProcessMessage(message, f)
// Callback from a method call if err != nil {
f.Callback(result[1:]) f.logger.Error(err.Error())
default: f.Callback(result)
f.logger.Info("Unknown message returned from dispatcher: %+v", result) return
} }
if result == "" {
return
}
switch result[0] {
case 'c':
// Callback from a method call
f.Callback(result[1:])
default:
f.logger.Info("Unknown message returned from dispatcher: %+v", result)
}
}()
} }
func (f *Frontend) Callback(message string) { func (f *Frontend) Callback(message string) {