From c9c3c9ab908274466db1c56738652a0cc3dad84a Mon Sep 17 00:00:00 2001 From: Lea Anthony Date: Wed, 27 Jan 2021 21:12:17 +1100 Subject: [PATCH] Don't bind startup/shutdown methods --- v2/internal/app/desktop.go | 5 ++++- v2/internal/binding/binding.go | 21 ++++++++++++++++----- v2/internal/binding/reflect.go | 7 ++++++- 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/v2/internal/app/desktop.go b/v2/internal/app/desktop.go index c8e9336d..405d07a9 100644 --- a/v2/internal/app/desktop.go +++ b/v2/internal/app/desktop.go @@ -81,12 +81,15 @@ func CreateApp(appoptions *options.App) (*App, error) { window := ffenestri.NewApplicationWithConfig(appoptions, myLogger, menuManager) + // Create binding exemptions - Ugly hack. There must be a better way + bindingExemptions := []interface{}{appoptions.Startup, appoptions.Shutdown} + result := &App{ appType: "desktop", window: window, servicebus: servicebus.New(myLogger), logger: myLogger, - bindings: binding.NewBindings(myLogger, appoptions.Bind), + bindings: binding.NewBindings(myLogger, appoptions.Bind, bindingExemptions), menuManager: menuManager, startupCallback: appoptions.Startup, shutdownCallback: appoptions.Shutdown, diff --git a/v2/internal/binding/binding.go b/v2/internal/binding/binding.go index 3159f162..ae0fc699 100755 --- a/v2/internal/binding/binding.go +++ b/v2/internal/binding/binding.go @@ -2,23 +2,35 @@ package binding import ( "fmt" + "reflect" + "runtime" "strings" + "github.com/leaanthony/slicer" + "github.com/wailsapp/wails/v2/internal/logger" ) type Bindings struct { - db *DB - logger logger.CustomLogger + db *DB + logger logger.CustomLogger + exemptions slicer.StringSlicer } // NewBindings returns a new Bindings object -func NewBindings(logger *logger.Logger, structPointersToBind []interface{}) *Bindings { +func NewBindings(logger *logger.Logger, structPointersToBind []interface{}, exemptions []interface{}) *Bindings { result := &Bindings{ db: newDB(), logger: logger.CustomLogger("Bindings"), } + for _, exemption := range exemptions { + name := runtime.FuncForPC(reflect.ValueOf(exemption).Pointer()).Name() + // Yuk yuk yuk! Is there a better way? + name = strings.TrimSuffix(name, "-fm") + result.exemptions.Add(name) + } + // Add the structs to bind for _, ptr := range structPointersToBind { err := result.Add(ptr) @@ -33,7 +45,7 @@ func NewBindings(logger *logger.Logger, structPointersToBind []interface{}) *Bin // Add the given struct methods to the Bindings func (b *Bindings) Add(structPtr interface{}) error { - methods, err := getMethods(structPtr) + methods, err := b.getMethods(structPtr) if err != nil { return fmt.Errorf("cannot bind value to app: %s", err.Error()) } @@ -46,7 +58,6 @@ func (b *Bindings) Add(structPtr interface{}) error { // Add it as a regular method b.db.AddMethod(packageName, structName, methodName, method) - } return nil } diff --git a/v2/internal/binding/reflect.go b/v2/internal/binding/reflect.go index 297c9bcd..94b694ae 100755 --- a/v2/internal/binding/reflect.go +++ b/v2/internal/binding/reflect.go @@ -23,7 +23,7 @@ func isStruct(value interface{}) bool { return reflect.ValueOf(value).Kind() == reflect.Struct } -func getMethods(value interface{}) ([]*BoundMethod, error) { +func (b *Bindings) getMethods(value interface{}) ([]*BoundMethod, error) { // Create result placeholder var result []*BoundMethod @@ -56,6 +56,11 @@ func getMethods(value interface{}) ([]*BoundMethod, error) { fullMethodName := baseName + "." + methodName method := structValue.MethodByName(methodName) + methodReflectName := runtime.FuncForPC(methodDef.Func.Pointer()).Name() + if b.exemptions.Contains(methodReflectName) { + continue + } + // Create new method boundMethod := &BoundMethod{ Name: fullMethodName,