Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Global event registry #117

Merged
merged 2 commits into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 11 additions & 6 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use specta::{
};
use tauri::{ipc::Invoke, Manager, Runtime};

use crate::{Commands, EventRegistry, Events, LanguageExt};
use crate::{event::EventRegistryMeta, Commands, EventRegistry, Events, LanguageExt};

/// Builder for configuring Tauri Specta in your application.
///
Expand Down Expand Up @@ -161,11 +161,16 @@ impl<R: Runtime> Builder<R> {

/// Mount all of the events in the builder onto a Tauri app.
pub fn mount_events(&self, handle: &impl Manager<R>) {
if !handle.manage(EventRegistry {
plugin_name: self.plugin_name,
events: self.event_sids.clone(),
}) {
panic!("Attempted to mount Tauri Specta EventRegistry more than once. Did you call `Builder::mount_events` more than once?");
let registry = EventRegistry::get_or_manage(handle);
let mut map = registry.0.write().expect("Failed to lock EventRegistry");

for sid in &self.event_sids {
map.insert(
sid.clone(),
EventRegistryMeta {
plugin_name: self.plugin_name,
},
);
}
}

Expand Down
36 changes: 27 additions & 9 deletions src/event.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
use std::{borrow::Cow, collections::BTreeSet};
use std::{
borrow::Cow,
collections::{BTreeSet, HashMap},
sync::RwLock,
};

use serde::{de::DeserializeOwned, Serialize};
use specta::{NamedType, SpectaID};
use tauri::{Emitter, EventId, EventTarget, Listener, Manager, Runtime};

use crate::{apply_as_prefix, ItemType};

/// A struct for managing events that is put into Tauri's state.
pub(crate) struct EventRegistry {
pub(crate) plugin_name: Option<&'static str>,
pub(crate) events: BTreeSet<SpectaID>,
#[derive(Default)]
pub(crate) struct EventRegistryMeta {
pub plugin_name: Option<&'static str>,
}

/// A struct for managing events that is put into Tauri's state.
#[derive(Default)]
pub(crate) struct EventRegistry(pub(crate) RwLock<HashMap<SpectaID, EventRegistryMeta>>);

impl EventRegistry {
/// gets the name of the event (taking into account plugin prefixes) and ensuring it was correctly mounted to the current app.
pub fn get_event_name<E: Event, R: Runtime>(
Expand All @@ -22,14 +29,25 @@ impl EventRegistry {
"EventRegistry not found in Tauri state - Did you forget to call Builder::mount_events?",
).inner();

this.events
.get(&E::sid())
let sid = E::sid();

let map = this.0.read().expect("Failed to read EventRegistry");
let meta = map
.get(&sid)
.unwrap_or_else(|| panic!("Event {name} not found in registry!"));

this.plugin_name
meta.plugin_name
.map(|n| apply_as_prefix(n, name, ItemType::Event).into())
.unwrap_or_else(|| name.into())
}

pub fn get_or_manage<R: Runtime>(handle: &impl Manager<R>) -> tauri::State<'_, Self> {
if handle.try_state::<Self>().is_none() {
handle.manage(Self::default());
}

handle.state::<Self>()
}
}

/// A typed event that was emitted.
Expand Down Expand Up @@ -78,7 +96,7 @@ macro_rules! make_handler {
/// DemoEvent::listen(handle, |event| {
/// dbg!(event.payload);
/// });
///
///
/// DemoEvent("Test".to_string()).emit(handle).ok();
/// }
/// ```
Expand Down
Loading