Unable to tune in and capture a tauri worldwide event happening concurrently in two separate vue files

I want to develop a small program that includes the following features:

  1. The initial Vue component named "Start.vue" should open a new window upon clicking a button
  2. The new window should consist of another component called "Playboard.vue"
  3. "Start.vue" should also send initialization parameters, input by users, to "Playboard.vue"

I am using Tauri's emit and listen API. Despite extensive console logging, I discovered that even though "Start.vue" emitted the "parameter-init" event, the other side failed to capture it, leading to the loading process of "Playboard.vue" getting stuck.

The code is presented below:

"Start.vue"

<script setup lang="ts">
import { ref } from "vue";
import { winConfig, Windows } from "./Window";
import { emit, listen } from "@tauri-apps/api/event";
import { parameterConfig } from "./Parameters";

const displayWindow: winConfig = {
    label: 'playBoard',
    others: {
      // some window parameters
    }
}
const w = ref("");
const h = ref("");
const m = ref("");

var windows: Windows = new Windows();
windows.listen();

async function start() {
  const params: parameterConfig = {
    width: w.value,
    height: h.value,
  }
  await emit("tauri-win-create", displayWindow)
  await emit("parameter-init", params)
}

</script>

<template>
  <div class = "container" id="startarea">
    <div class = "container" id="inputarea">
      <p><input id="greet-input" v-model="w" placeholder="Width" /></p>
      <p><input id="greet-input" v-model="h" placeholder="height" /></p>
      <p><input id="greet-input" v-model="m" placeholder="number of mines" /></p>
    </div>
    <div id="clickarea">
      <button type="button" @click="start" >Start</button>
    </div>
  </div>
</template>

Playboard.vue

<script setup lang="ts">
import Display from "./components/Display.vue";
import { listen, emit } from "@tauri-apps/api/event";
import { Parameter } from "./components/Parameters";
var params: Parameter = new Parameter();
params.listen();
</script>

<template>
  <div class="container">
    <div>
      <Display :width="params.width" :height="params.height"/>
    </div>
  </div>
</template>

Window.ts

import { WebviewWindow, appWindow, getAll, WindowOptions } from '@tauri-apps/api/window'
import { emit, listen } from '@tauri-apps/api/event'

export type winConfig = {
    label: string,
    others: WindowOptions
}

export class Windows {
    mainWin!: WebviewWindow;

    getWin(label: string) {
        return WebviewWindow.getByLabel(label)
    }

    getAllWin() {
        return getAll()
    }

    async createWin(options: winConfig) {
        let label = options.label
        let args: WindowOptions = options.others
        console.log(args)

        const existWin = getAll().find(w => w.label == label)
        if(existWin) {
            if(existWin.label.indexOf('main') == -1) {
                await existWin?.unminimize()
                await existWin?.setFocus()
                return
            }
            await existWin?.close()
        }

        let win = new WebviewWindow(label, args)
        
        win.once('tauri://created', async() => {
            console.log('window create success!')
        })

        win.once('tauri://error', async() => {
            console.log('window create error!')
        })
    }

    async listen() {
        await listen<winConfig>('tauri-win-create', (event) => {
            console.log(event)
            this.createWin(event.payload)
        })
    }
}

Parameters.ts

import { listen } from '@tauri-apps/api/event'
import { event } from '@tauri-apps/api'

export type parameterConfig = {
    width: string,
    height: string
} 

export class Parameter {
    width: number = 0
    height: number = 0

    init(p: parameterConfig) {
        this.width = Number(p.width)
        this.height = Number(p.height)
    }

    async listen() {
        await listen<parameterConfig>('parameter-init', (event) => {
            console.log(event.payload)
            this.init(event.payload)
        })
    }
}
  1. One approach I attempted was to emit from "Parameters.ts" and then wait for "Start.vue" to receive the signal before emitting. However, being new to this, I am struggling with understanding the async/await operations.
  2. I also experimented with removing the listen function in "Playboard.vue" and instead used static binding for root props, which seemed to solve the issue. I suspect the problem lies in how async/await and emit/listen are being handled.

What is the root cause of this issue? And what would be the best practice to achieve this functionality seamlessly?

Answer №1

Events in Tauri serve as the bridge for communication between the front end and the backend, rather than directly between two frontend components.

While it may not be possible to establish communication between two windows in Rust without involving the backend, a workaround could be to create a Tauri command to transmit the same data and then listen for it on the receiving window.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best method to verify image dimensions and size for three distinct target platforms, all of which have different image dimensions but the same size, within

I'm currently working on a feature that involves an image uploader for various platforms, each with its own set of recommended image dimensions. For example: platform 1 - dimension 920 x 300, platform 2 - 210 x 200, and platform 3 - 790 x 270. When th ...

Issues arising from using async/await in conjunction with .then() in vue.js login function, causing fetch process not to wait for completion

I implemented a login function to verify credentials on my backend server, but I am facing an issue with waiting for the server response. Despite following the es7-async-await.js guide and trying various async/await and promise techniques, the function sti ...

Ways to resolve the error message "Type 'Promise<{}>' is missing certain properties from type 'Observable<any>'" in Angular

Check out this code snippet: const reportModules = [ { url: '', params: { to: format(TODAY, DATE_FORMAT).toString(), from: format(TODAY, DATE_FORMAT).toString() } }, { url: 'application1', params: { to: for ...

Displaying HTML content using Typescript

As a newcomer to typescript, I have a question regarding displaying HTML using typescript. Below is the HTML code snippet: <div itemprop="copy-paste-block"> <ul> <li><span style="font-size:11pt;"><span style="font-family ...

How can you define a generic type alias in TypeScript for a function that is also generic?

I am facing a challenge with creating a generic alias for a typed generic function. Despite my efforts, I have not been successful in achieving this. Here is an example of what I'm encountering: // foo.tsx function foo<T>(arg: T): T { return a ...

Encountering a problem while attempting to utilize node-postgres transactions - using a pooled client in conjunction with async/

I'm currently exploring the use of node-postgres transactions - found at this link Specifically, I am working on implementing a pooled client with async/await, but have encountered an error in doing so. The content of my db.js file, used below, is a ...

What is the best way to pass the modal dialog parameter sent from HTML to the TypeScript page?

I have implemented a dialog window using Angular where it opens by passing a parameter in the HTML side along with a transaction. To further enhance the functionality of my page, I want to incorporate the fab button. However, I am facing a challenge in sen ...

What is the correct way to implement fetch in a React/Redux/TS application?

Currently, I am developing an application using React, Redux, and TypeScript. I have encountered an issue with Promises and TypeScript. Can you assist me in type-defining functions/Promises? An API call returns a list of post IDs like [1, 2, ..., 1000]. I ...

Encountering the error "Unable to use the '+' operator with 'symbol' type when attempting to combine $route.name"

Looking to retrieve the current route name from a template in order to pass it to a router link (specifically passing the current route to the login view so I can redirect users there after authentication). Everything functions as expected, but when perfo ...

Karma's connection was lost due to a lack of communication within 10000 milliseconds

The Karma test suite is encountering issues with the following message: Disconnected, because no message in 10000 ms. The tests are not running properly. "@angular/core": "7.1.3", "jasmine-core": "3.3.0", "karma-jasmine": "1.1.2", The failure seems t ...

The 'newPassword' must be included as a String parameter

SOLVED The issue has been resolved. It was found that the backend required data in param format, so the form sent by Vue was changed to param instead of data! I am currently working on a password change page. I encountered an error stating that there was ...

What is the best approach to creating multiple dropdowns in ant-design with unique options for each?

It seems like I may be overlooking a simple solution here. Ant-Design dropdowns utilize an array of ItemProp objects to show the options, but this restricts me to having only one list of options. const choices: MenuProps['items'] = [ { label: ...

The TypeScript error states that the argument type 'string | undefined' cannot be assigned to the parameter type 'string'

Receiving TS error that says "Object is undefined" I am attempting to retrieve the "userid" from my headers. However, I keep encountering the error message "Argument of type 'string | undefined' is not assignable to parameter of type 'str ...

I am receiving a 401 error when attempting to verify the token following a successful login

I've been working on a small project utilizing VueJS, Vue Router, and Laravel for the backend. Despite several attempts, I haven't been successful in implementing navigation guards. My login component is functioning properly. Here's my log ...

Can you please provide instructions on how to obtain the TypeScript definitions file for a specific version of Jquery, such as 3.2.1

As I navigate my way through TypeScript, I find myself in need of accessing type definitions for a jQuery project in Visual Studio. The project currently utilizes jquery version 3.2.1, and I'm on the lookout for TypeScript type definitions for it. Af ...

Observing, contrasting, and sending revised form information to an API through Axios in Vue 3

Can anyone assist me in finalizing my code? This is the progress I have made so far: I am fetching options from an API, which is why I initially set the state to empty. After receiving a response from the API, I update the options state. The form is disp ...

Struggling to retrieve API data in Angular 6

I am facing an issue with my code where the Get request is unable to fetch api values for posts, although it was successful for users. The code is simple, but I can't seem to figure out why it fails for posts. Here is my posts.components.ts file: im ...

Passing a variable as a property to a nested child component in Vue.js

I am curious about how to efficiently pass variables to nested components. Within my setup, I have a total of 3 components: Main Secondary Tertiary All of these components share a common variable (referred to as sharedVar). If I want to avoid using Vue ...

Can you explain the purpose of this TypeScript code snippet? It declares a variable testOptions that can only be assigned one of the values "Undecided," "Yes," or "No," with a default value of "Undecided."

const testOptions: "Undecided" | "Yes" | "No" = "Undecided"; Can you explain the significance of this code snippet in typescript? How would you classify the variable testOptions? Is testOptions considered an array, string, or another d ...

Oops! It seems like there is an issue with reading the property 'filter' of an undefined object. Any ideas on how to resolve this error

Having an issue with a custom filter that is causing an error "ERROR TypeError: Cannot read property 'filter' of undefined". I need help fixing this as it's preventing anything from rendering on the page. Any suggestions on what changes I sh ...