tauri-apps / tauri

Build smaller, faster, and more secure desktop applications with a web frontend.
https://tauri.app
Apache License 2.0
79.66k stars 2.37k forks source link

[bug] [v2] emit_to emits to all targets regardless of EventTarget #10182

Open jaydevelopsstuff opened 5 days ago

jaydevelopsstuff commented 5 days ago

Note: This is likely not a bug but rather strange API design

Describe the bug

In v2, the emit_to method is straight up dysfuntional.

The docs describe its functionality as "Emits an event to all [EventTarget]'s matching the given target," however, it seems to completely ignore the EventTarget requirements that are passed.

Reproduction

MRE Repo

Simply build, run and observe that when you press the "Dispatch Event to window w/ label 'main-1'," the event listener is triggered in both windows, not just main-1.

Expected behavior

emit_to is expected to function exactly as its documentation describes, only emitting to EventTargets that have a matching label.

Full tauri info output

[✔] Environment
    - OS: Mac OS 14.4.1 X64
    ✔ Xcode Command Line Tools: installed
    ✔ rustc: 1.81.0-nightly (e9e6e2e44 2024-06-28)
    ✔ cargo: 1.81.0-nightly (4ed7bee47 2024-06-25)
    ✔ rustup: 1.27.1 (2024-04-24)
    ✔ Rust toolchain: nightly-aarch64-apple-darwin (environment override by RUSTUP_TOOLCHAIN)
    - node: 22.1.0
    - pnpm: 9.1.0
    - npm: 10.7.0

[-] Packages
    - tauri [RUST]: 2.0.0-beta.23
    - tauri-build [RUST]: 2.0.0-beta.18
    - wry [RUST]: 0.41.0
    - tao [RUST]: 0.28.1
    - tauri-cli [RUST]: 2.0.0-beta.17
    - @tauri-apps/api [NPM]: 2.0.0-beta.14
    - @tauri-apps/cli [NPM]: 2.0.0-beta.17

[-] App
    - build-type: bundle
    - CSP: unset
    - frontendDist: ../build
    - devUrl: http://localhost:1420/
    - framework: Svelte
    - bundler: Vite

Stack trace

No response

Additional context

No response

jaydevelopsstuff commented 4 days ago

Based off @amrbashir's comment here, this seems to be a misunderstanding on my end about how the frontend Tauri events API works.

Apparently using listen will receive all events with the specified name, ignoring the EventTarget that may have been specified when emitting it. The only way to listen to events that target the current window is either getCurrent().listen("<event>", <handler>) or listen("<event>", <handler>, { target: getCurrent().label }).

This is strange.

emit_to's documentation describes itself as "[emitting] an event to all [EventTarget]'s matching the given target," so you would not expect that this matching could be overridden on the frontend simply by using the default listen function.

In my honest opinion, I think the current functionality of listen is flawed. listen, by default, should only receive global events and events targeted at the window/webview it was called in. Ignoring the targeting intended by the backend should be separate, customizable functionality (perhaps listenAny or listenWithFilter functions).

If there is some other reasoning behind the current design of the listening API that I have missed, please let me know.