Closed Mikkel-T closed 3 weeks ago
@Mikkel-T thank you for the detailed bug and tracing, please feel free to open a PR.
I will definitely be doing so! But before I start, I would like to ensure that my implementation is going to align with the intended behaviour.
From my understanding, there are two main scenarios to consider:
There are currently four target kinds that support a label: Window
, Webview
, WebviewWindow
and AnyLabel
.
For the first scenario, I expect that an event emitted to a target of kind AnyLabel
should invoke listeners associated with any of the specified kinds. For example:
emitTo({kind: "AnyLabel", label: "main"}, "event", "payload");
// The following listeners should be invoked by this event:
listen("event", () => {}, {target: {kind: "Window", label: "main"}});
listen("event", () => {}, {target: {kind: "Webview", label: "main"}});
listen("event", () => {}, {target: {kind: "WebviewWindow", label: "main"}});
listen("event", () => {}, {target: {kind: "AnyLabel", label: "main"}});
listen("event", () => {}, {target: "main"}); // Default is AnyLabel
For the second scenario, I believe that a listener initialized with a target of kind AnyLabel
should be invoked when receiving an event from any of the specified kinds:
listen("event", () => {}, {target: {kind: "AnyLabel", label: "main"}});
// The following events should invoke this listener:
emitTo({kind: "Window", label: "main"}, "event", "payload");
emitTo({kind: "Webview", label: "main"}, "event", "payload");
emitTo({kind: "WebviewWindow", label: "main"}, "event", "payload");
emitTo({kind: "AnyLabel", label: "main"}, "event", "payload");
emitTo("main", "event", "payload"); // Default is AnyLabel
Could you please confirm if my understanding is correct? I want to ensure that I’m on the right track before proceeding with a PR.
Describe the bug
The new event system, which primarily relies on
target
s to work, has some major flaws when it comes to handling these targets. Specifically targets of the kindAnyLabel
, which is the default when just adding a string as target in bothemitTo
andlisten
.For example, if a listener is set with the target
"main"
, andemitTo("main", "eventName", "payload")
is run, the listener will not be triggered.I have tried to trace this behavior back to the source, and have stumbled upon this snippet from the implementation of the
emit_to
function in Rust.Here,
target
is the target set inemit_to
. When this is of the kindAnyLabel
, the code will enter the secondmatch
, where it checks fort
which is the listener target. It checks ift
is of kindWindow
,Webview
orWebviewWindow
, none of which apply toAnyLabel
by which it will return false when the listener is initialized without a specific kind.If a kind has been specified for
emit_to
, it will go to the bottom line, and only match the same targets. Again, if the listener is initialized without a specific kind, and therefore gets the kindAnyLabel
, it will not trigger the listener.Reproduction
No response
Expected behavior
As far as I understand it, the expected behaviour would be to let
AnyLabel
serve as a "wildcard" for bothemitTo
andlisten
. So if any of the two are of the kindAnyLabel
, any event with a label would be served between the two.Full
tauri info
outputStack trace
No response
Additional context
I would be willing to work on a PR fixing this issue, but I would like to discuss the exact issue and expected behavior in depth before starting to change things in the event API.