tauri-apps / tauri

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

[bug] Tauri appWindow not listening to webview emit event #7835

Open fogine opened 9 months ago

fogine commented 9 months ago

Describe the bug

I have an issue receiving events emitted to a specific window. From the main window I'm creating a new window and sending sync-data event to it:

    //main window
    const window = await new Promise<WebviewWindow>((resolve, reject) => {

      const webview = new WebviewWindow('test', {
        url: 'window.html'
      });

      webview.once('tauri://created', function () {
        setTimeout(async function () {
          //for test purposes I'm delaying emitting of the event to make sure listeners in the new window are setup
          await webview?.emit('sync-data', {test: 'data'}});
        }, 3000)
        resolve(webview);
      });

      webview.once('tauri://error', function (e) {
        reject(e);
      });

    });

and in the test window, I'm listening to sync-data event:

  import { appWindow } from "@tauri-apps/api/window";

  //appWindow.label === 'test' => true
  const unlisten = await appWindow.listen<Channel2ChannelWindowData>('sync-data', (event) => {
    console.log('======GOT DATA=====');
    //THIS LISTENER NEVER GETS CALLED :(
    console.log(event.payload);
  });

However the listener in the test window is never triggered unless I listen for events globally via

import { listen, UnlistenFn } from '@tauri-apps/api/event';
//appWindow.label === 'test' => true
 const unlisten = await listen<Channel2ChannelWindowData>('sync-data', (event) => {
   //GETS TRIGGERED!
    console.log(event.payload);
  });

Reproduction

No response

Expected behavior

I'd expect a new webview window to receive sync-data event and trigger registered listener.

Platform and versions

yarn run v1.22.19
$ tauri info

[✔] Environment
    - OS: Arch Linux Unknown X64
    ✔ webkit2gtk-4.0: 2.40.5
    ✔ rsvg2: 2.56.3
    ✔ rustc: 1.72.0 (5680fa18f 2023-08-23)
    ✔ Cargo: 1.72.0 (103a7ff2e 2023-08-15)
    ✔ rustup: 1.26.0 (2023-05-04)
    ✔ Rust toolchain: stable-x86_64-unknown-linux-gnu (default)
    - node: 16.20.2
    - pnpm: 8.7.4
    - yarn: 1.22.19
    - npm: 8.19.4

[-] Packages
    - tauri [RUST]: 1.4.0
    - tauri-build [RUST]: 1.4.0
    - wry [RUST]: 0.24.3
    - tao [RUST]: 0.16.2
    - @tauri-apps/api [NPM]: 1.4.0
    - @tauri-apps/cli [NPM]: 1.4.0

[-] App
    - build-type: bundle
    - CSP: unset
    - distDir: ../dist
    - devPath: http://localhost:1420/
    - framework: Vue.js
    - bundler: Vite
Done in 9.54s.

Stack trace

No response

Additional context

No response

tnrich commented 9 months ago

Also seeing this issue. I'd made a stackoverflow question for it here - https://stackoverflow.com/questions/77063579/tauri-appwindow-not-listening-to-webview-emit-event

Thanks!

Borber commented 4 months ago

I have also encountered this problem now. Now the backend emits a specific window, but the frontend listens directly instead of appWindow.listen(). Only in this way does it work.