Skip to main content

Emitter

Trait Emitter 

Source
pub trait Emitter<R: Runtime>: ManagerBase<R> {
    // Provided methods
    fn emit<S: Serialize + Clone>(&self, event: &str, payload: S) -> Result<()> { ... }
    fn emit_str(&self, event: &str, payload: String) -> Result<()> { ... }
    fn emit_to<I, S>(&self, target: I, event: &str, payload: S) -> Result<()>
       where I: Into<EventTarget>,
             S: Serialize + Clone { ... }
    fn emit_str_to<I>(
        &self,
        target: I,
        event: &str,
        payload: String,
    ) -> Result<()>
       where I: Into<EventTarget> { ... }
    fn emit_filter<S, F>(
        &self,
        event: &str,
        payload: S,
        filter: F,
    ) -> Result<()>
       where S: Serialize + Clone,
             F: Fn(&EventTarget) -> bool { ... }
    fn emit_str_filter<F>(
        &self,
        event: &str,
        payload: String,
        filter: F,
    ) -> Result<()>
       where F: Fn(&EventTarget) -> bool { ... }
}
Expand description

Emit events.

Provided Methods§

Source

fn emit<S: Serialize + Clone>(&self, event: &str, payload: S) -> Result<()>

Emits an event to all targets.

§Examples
use tauri::Emitter;

#[tauri::command]
fn synchronize(app: tauri::AppHandle) {
  // emits the synchronized event to all webviews
  app.emit("synchronized", ());
}
Source

fn emit_str(&self, event: &str, payload: String) -> Result<()>

Similar to Emitter::emit but the payload is json serialized.

Source

fn emit_to<I, S>(&self, target: I, event: &str, payload: S) -> Result<()>
where I: Into<EventTarget>, S: Serialize + Clone,

Emits an event to all targets matching the given target.

§Examples
use tauri::{Emitter, EventTarget};

#[tauri::command]
fn download(app: tauri::AppHandle) {
  for i in 1..100 {
    std::thread::sleep(std::time::Duration::from_millis(150));
    // emit a download progress event to all listeners
    app.emit_to(EventTarget::any(), "download-progress", i);
    // emit an event to listeners that used App::listen or AppHandle::listen
    app.emit_to(EventTarget::app(), "download-progress", i);
    // emit an event to any webview/window/webviewWindow matching the given label
    app.emit_to("updater", "download-progress", i); // similar to using EventTarget::labeled
    app.emit_to(EventTarget::labeled("updater"), "download-progress", i);
    // emit an event to listeners that used WebviewWindow::listen
    app.emit_to(EventTarget::webview_window("updater"), "download-progress", i);
  }
}
Source

fn emit_str_to<I>(&self, target: I, event: &str, payload: String) -> Result<()>
where I: Into<EventTarget>,

Similar to Emitter::emit_to but the payload is json serialized.

Source

fn emit_filter<S, F>(&self, event: &str, payload: S, filter: F) -> Result<()>
where S: Serialize + Clone, F: Fn(&EventTarget) -> bool,

Emits an event to all targets based on the given filter.

§Examples
use tauri::{Emitter, EventTarget};

#[tauri::command]
fn download(app: tauri::AppHandle) {
  for i in 1..100 {
    std::thread::sleep(std::time::Duration::from_millis(150));
    // emit a download progress event to the updater window
    app.emit_filter("download-progress", i, |t| match t {
      EventTarget::WebviewWindow { label } => label == "main",
      _ => false,
    });
  }
}
Source

fn emit_str_filter<F>( &self, event: &str, payload: String, filter: F, ) -> Result<()>
where F: Fn(&EventTarget) -> bool,

Similar to Emitter::emit_filter but the payload is json serialized.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§

Source§

impl<R: Runtime> Emitter<R> for App<R>

Source§

impl<R: Runtime> Emitter<R> for AppHandle<R>

Source§

impl<R: Runtime> Emitter<R> for Webview<R>

Source§

impl<R: Runtime> Emitter<R> for WebviewWindow<R>

Source§

impl<R: Runtime> Emitter<R> for Window<R>