Skip to content

Commit

Permalink
use Emitter and Listener instead of ManagerExt
Browse files Browse the repository at this point in the history
  • Loading branch information
Brendonovich committed Jul 23, 2024
1 parent 5c09319 commit c5373f1
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 136 deletions.
78 changes: 39 additions & 39 deletions src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use serde::{de::DeserializeOwned, Serialize};
use specta::{DataType, NamedType, SpectaID};
use tauri::{Emitter, EventId, EventTarget, Listener, Manager, Runtime};

use crate::{ManagerExt, PluginName};
use crate::PluginName;

#[derive(Clone, Copy)]
pub struct EventRegistryMeta {
Expand Down Expand Up @@ -107,82 +107,82 @@ macro_rules! get_meta {
pub trait Event: NamedType {
const NAME: &'static str;

fn listen<F, R: Runtime>(handle: &impl ManagerExt<R>, handler: F) -> EventId
fn listen<F, R: Runtime, H: Listener<R> + Manager<R>>(handle: &H, handler: F) -> EventId
where
F: Fn(TypedEvent<Self>) + Send + 'static,
Self: DeserializeOwned,
{
let meta = get_meta!(handle);

handle.listen(meta.wrap_with_plugin(Self::NAME), make_handler!(handler))
handle.listen(
get_meta!(handle).wrap_with_plugin(Self::NAME),
make_handler!(handler),
)
}

fn listen_any<F, R: Runtime>(handle: &impl Manager<R>, handler: F) -> EventId
fn listen_any<F, R: Runtime, H: Listener<R> + Manager<R>>(handle: &H, handler: F) -> EventId
where
F: Fn(TypedEvent<Self>) + Send + 'static,
Self: DeserializeOwned,
{
let meta = get_meta!(handle);

handle
.app_handle()
.listen_any(meta.wrap_with_plugin(Self::NAME), make_handler!(handler))
handle.listen_any(
get_meta!(handle).wrap_with_plugin(Self::NAME),
make_handler!(handler),
)
}

fn once<F, R: Runtime>(handle: &impl ManagerExt<R>, handler: F) -> EventId
fn once<F, R: Runtime, H: Listener<R> + Manager<R>>(handle: &H, handler: F) -> EventId
where
F: Fn(TypedEvent<Self>) + Send + 'static,
Self: DeserializeOwned,
{
let meta = get_meta!(handle);

handle.once(meta.wrap_with_plugin(Self::NAME), make_handler!(handler))
handle.once(
get_meta!(handle).wrap_with_plugin(Self::NAME),
make_handler!(handler),
)
}

fn once_any<F, R: Runtime>(handle: &impl Manager<R>, handler: F) -> EventId
fn once_any<F, R: Runtime, H: Listener<R> + Manager<R>>(handle: &H, handler: F) -> EventId
where
F: FnOnce(TypedEvent<Self>) + Send + 'static,
Self: DeserializeOwned,
{
let meta = get_meta!(handle);

handle
.app_handle()
.once_any(meta.wrap_with_plugin(Self::NAME), make_handler!(handler))
handle.once_any(
get_meta!(handle).wrap_with_plugin(Self::NAME),
make_handler!(handler),
)
}

fn emit<R: Runtime>(&self, handle: &impl Manager<R>) -> tauri::Result<()>
fn emit<R: Runtime, H: Emitter<R> + Manager<R>>(&self, handle: &H) -> tauri::Result<()>
where
Self: Serialize + Clone,
{
let meta = get_meta!(handle);

handle
.app_handle()
.emit(&meta.wrap_with_plugin(Self::NAME), self)
handle.emit(&get_meta!(handle).wrap_with_plugin(Self::NAME), self)
}

fn emit_to<R: Runtime>(&self, handle: &impl Manager<R>, label: &str) -> tauri::Result<()>
fn emit_to<R: Runtime, H: Emitter<R> + Manager<R>>(
&self,
handle: &H,
label: &str,
) -> tauri::Result<()>
where
Self: Serialize + Clone,
{
let meta = get_meta!(handle);

handle
.app_handle()
.emit_to(label, &meta.wrap_with_plugin(Self::NAME), self)
handle.emit_to(label, &get_meta!(handle).wrap_with_plugin(Self::NAME), self)
}

fn emit_filter<F, R: Runtime>(&self, handle: &impl Manager<R>, filter: F) -> tauri::Result<()>
fn emit_filter<F, R: Runtime, H: Emitter<R> + Manager<R>>(
&self,
handle: &H,
filter: F,
) -> tauri::Result<()>
where
F: Fn(&EventTarget) -> bool,
Self: Serialize + Clone,
{
let meta = get_meta!(handle);

handle
.app_handle()
.emit_filter(&meta.wrap_with_plugin(Self::NAME), self, filter)
handle.emit_filter(
&get_meta!(handle).wrap_with_plugin(Self::NAME),
self,
filter,
)
}
}

Expand Down
2 changes: 0 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,9 @@ pub mod ts;
mod js_ts;

mod event;
mod manager_ext;
mod statics;

pub use event::*;
pub use manager_ext::*;
pub use statics::StaticCollection;

pub type CollectCommandsTuple<TInvokeHandler> =
Expand Down
95 changes: 0 additions & 95 deletions src/manager_ext.rs

This file was deleted.

0 comments on commit c5373f1

Please sign in to comment.