From 1ac97759392f51083eb84ae313e61791b4a97135 Mon Sep 17 00:00:00 2001 From: Brendan Allan Date: Fri, 2 Aug 2024 21:54:28 +0800 Subject: [PATCH 1/2] make EventRegistry global --- src/builder.rs | 17 +++++++++++------ src/event.rs | 36 +++++++++++++++++++++++++++--------- 2 files changed, 38 insertions(+), 15 deletions(-) diff --git a/src/builder.rs b/src/builder.rs index 16309f4..02ac88c 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -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. /// @@ -161,11 +161,16 @@ impl Builder { /// Mount all of the events in the builder onto a Tauri app. pub fn mount_events(&self, handle: &impl Manager) { - 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, + }, + ); } } diff --git a/src/event.rs b/src/event.rs index b47c029..6969969 100644 --- a/src/event.rs +++ b/src/event.rs @@ -1,4 +1,8 @@ -use std::{borrow::Cow, collections::BTreeSet}; +use std::{ + borrow::Cow, + collections::{BTreeSet, HashMap}, + sync::RwLock, +}; use serde::{de::DeserializeOwned, Serialize}; use specta::{NamedType, SpectaID}; @@ -6,12 +10,15 @@ 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, +#[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>); + 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( @@ -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(handle: &impl Manager) -> tauri::State<'_, Self> { + if handle.try_state::().is_none() { + handle.manage(Self::default()); + } + + handle.state::() + } } /// A typed event that was emitted. @@ -78,7 +96,7 @@ macro_rules! make_handler { /// DemoEvent::listen(handle, |event| { /// dbg!(event.payload); /// }); -/// +/// /// DemoEvent("Test".to_string()).emit(handle).ok(); /// } /// ``` From dfbfff5ca97c390ba2bd7030aff8d76e2fd4e2c0 Mon Sep 17 00:00:00 2001 From: Brendan Allan Date: Fri, 2 Aug 2024 22:02:49 +0800 Subject: [PATCH 2/2] fix example tauri api version --- pnpm-lock.yaml | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index c2ccad2..3b654e5 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -10,13 +10,13 @@ importers: dependencies: '@tauri-apps/api': specifier: next - version: 2.0.0-beta.16 + version: 2.0.0-rc.0 examples/app: dependencies: '@tauri-apps/api': specifier: next - version: 2.0.0-beta.16 + version: 2.0.0-rc.0 devDependencies: '@tauri-apps/cli': specifier: next @@ -32,7 +32,7 @@ importers: dependencies: '@tauri-apps/api': specifier: next - version: 2.0.0-beta.16 + version: 2.0.0-rc.0 solid-js: specifier: ^1.8.19 version: 1.8.19 @@ -386,8 +386,8 @@ packages: cpu: [x64] os: [win32] - '@tauri-apps/api@2.0.0-beta.16': - resolution: {integrity: sha512-YGjkR9HxS/YyIoqoXDkk8o9Yy5NW6u9YxzeqEodwwOUoeS0nac6mzLTW3VYIuSelHmyUQCgbyENVY6e5CJXA4Q==} + '@tauri-apps/api@2.0.0-rc.0': + resolution: {integrity: sha512-v454Qs3REHc3Za59U+/eSmBsdmF+3NE5+76+lFDaitVqN4ZglDHENDaMARYKGJVZuxiSkzyqG0SeG7lLQjVkPA==} engines: {node: '>= 18.18', npm: '>= 6.6.0', yarn: '>= 1.19.1'} '@tauri-apps/cli-darwin-arm64@2.0.0-beta.23': @@ -961,7 +961,7 @@ snapshots: '@rollup/rollup-win32-x64-msvc@4.19.1': optional: true - '@tauri-apps/api@2.0.0-beta.16': {} + '@tauri-apps/api@2.0.0-rc.0': {} '@tauri-apps/cli-darwin-arm64@2.0.0-beta.23': optional: true