New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Potential solution for getting the calling window of a bound method. #3424
Conversation
func (*GreetService) GreetPerson(person data.Person) string { | ||
return "Hello " + person.Name | ||
// GreetWithCtx greets a person | ||
func (*GreetService) GreetWithCtx(ctx context.Context, name string) string { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I actually like making this a specific type of Context aka a CallContext
such that you can add porcelain
to ask for the things you want to pull out. Would allow the user to not have to typecast
for example.
type CallIdentifier string
type CallContext interface {
Application() *application.App
Attrs() map[string]any
CallID() CallIdentifier
Get(string) any
Window() application.Window
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
empty comment
I was having a look at the code again and I noticed that checks for the window parameter happen in Do you have a reason for that? I'm asking because @stffabi added very similar code in
and I was thinking this change probably belongs there too? Also please note that my PR #3431 is going to break this in its current form, because Sorry for the late review, didn't notice this before. |
Also, I would ignore a window parameter in second place unless the first place is a context. I would do something along these lines: In if (!boundMethod.needsContext && inputIndex == 0) || (boundMethod.needsContext && inputIndex == 1) {
if input.AssignableTo(windowType) {
boundMethod.needsWindow = true
}
} In if b.needsWindow {
args = append([]any{wnd}, args...)
}
if b.needsContext {
... |
Yeah, the bindings doesn't know about windows.
Would you pass nil instead? |
That's true, but do you plan on using the binding system for anything else? If not, you could just pass the (possibly nil) window as an additional argument to Anyways if you prefer the current approach that's ok, I can rework #3431 once this gets merged.
I would just ignore it and let the binding system throw an error. We can also add a warning in the binding generator. Let me explain better what I have in mind. With the current implementation the following method: func (s *Service) Method(num int, wnd application.Window, str string) would receive a window as its second argument. I think this is a bit contrived and should be avoided. I'd go with something similar to the following check: windowIndex := 0
if boundMethod.needsContext {
windowIndex = 1
}
if windowIndex < len(boundMethod.Inputs) && boundMethod.Inputs[windowIndex].ReflectType == reflect.TypeFor[Window]() {
options.Args = append([]any{window}, options.Args...)
} Any other situation should trigger a runtime error. |
* Unmarshal arguments to appropriate type in binding calls * Marshal multiple return values to arrays in binding calls * Improve logging of remote method calls * Add tests for `BoundMethod.Call` * Fix return value if error is nil * Update changelog --------- Co-authored-by: Andreas Bichinger <[email protected]>
* Add `port` flag to dev command, ... Add support for environment variable WAILS_VITE_PORT * Check if port is already in use * Update changelog --------- Co-authored-by: Lea Anthony <[email protected]>
* add missing map init from `application.init()` * add changelog entry for #3426 --------- Co-authored-by: Lea Anthony <[email protected]>
- this is no longer needed
unknown key codes shouldn't print out to stdout
text was colored white and therefore invisible.
go mod update
git fun... |
Moved to #3457 |
Description
** This PR has been opened up to start a discussion on what the right solution is **
There will be times when you want to know what window made a call to a bound method. This PR offers 2 solutions for 2 different scenarios:
context.Context
in the first parameter, the context will have a value "window" which will return the calling window.application.Window
as the first parameter. It will always be populated with the calling window if called from JS.The
binding
example has been updated to show both usages.