Skip to content

Commit

Permalink
refactor(tauri): remove private params trait methods (#1484)
Browse files Browse the repository at this point in the history
* refactor(tauri): remove private params trait methods

* add changes file

* remove newly unused trait in WindowManager unit test
  • Loading branch information
chippers committed Apr 14, 2021
1 parent f2d24ef commit ec27ca8
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 257 deletions.
10 changes: 10 additions & 0 deletions .changes/no-private-params-trait.md
@@ -0,0 +1,10 @@
---
"tauri": patch
---

internal refactoring of `Params` to allow for easier usage without a private trait with only 1 implementor.

`ParamsPrivate` -> `ParamsBase`
`ManagerPrivate` -> `ManagerBase`
(new) `Args`, crate only. Now implements `Params`/`ParamsBase`.
`App` and `Window` use `WindowManager` directly
2 changes: 1 addition & 1 deletion core/tauri/src/endpoints/event.rs
Expand Up @@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT

use crate::{endpoints::InvokeResponse, sealed::ManagerPrivate, Manager, Params, Window};
use crate::{endpoints::InvokeResponse, sealed::ManagerBase, Manager, Params, Window};
use serde::Deserialize;

/// The API descriptor.
Expand Down
129 changes: 16 additions & 113 deletions core/tauri/src/lib.rs
Expand Up @@ -111,7 +111,7 @@ pub struct Context<A: Assets> {
}

/// Types associated with the running Tauri application.
pub trait Params: sealed::ParamsPrivate<Self> {
pub trait Params: sealed::ParamsBase {
/// The event type used to create and listen to events.
type Event: Tag;

Expand All @@ -126,7 +126,9 @@ pub trait Params: sealed::ParamsPrivate<Self> {
}

/// Manages a running application.
pub trait Manager<M: Params>: sealed::ManagerPrivate<M> {
///
/// TODO: expand these docs
pub trait Manager<M: Params>: sealed::ManagerBase<M> {
/// The [`Config`] the manager was created with.
fn config(&self) -> &Config {
self.manager().config()
Expand Down Expand Up @@ -202,126 +204,27 @@ pub trait Manager<M: Params>: sealed::ManagerPrivate<M> {
/// Prevent implementation details from leaking out of the [`Manager`] and [`Params`] traits.
pub(crate) mod sealed {
use super::Params;
use crate::runtime::Runtime;
use crate::{
api::{config::Config, PackageInfo},
event::{Event, EventHandler},
hooks::{InvokeMessage, PageLoadPayload},
runtime::window::{DetachedWindow, PendingWindow},
Window,
};
use serde::Serialize;
use std::collections::{HashMap, HashSet};
use uuid::Uuid;

/// private manager api
pub trait ParamsPrivate<M: Params>: Clone + Send + Sized + 'static {
/// Pass messages not handled by modules or plugins to the running application
fn run_invoke_handler(&self, message: InvokeMessage<M>);

/// Ran once for every window when the page is loaded.
fn run_on_page_load(&self, window: Window<M>, payload: PageLoadPayload);

/// Pass a message to be handled by a plugin that expects the command.
fn extend_api(&self, command: String, message: InvokeMessage<M>);

/// Initialize all the plugins attached to the [`Manager`].
fn initialize_plugins(&self) -> crate::Result<()>;

/// Prepare a [`PendingWindow`] to be created by the [`Runtime`].
///
/// The passed labels should represent either all the windows in the manager. If the application
/// has not yet been started, the passed labels should represent all windows that will be
/// created before starting.
fn prepare_window(
&self,
pending: PendingWindow<M>,
labels: &[M::Label],
) -> crate::Result<PendingWindow<M>>;

/// Attach a detached window to the manager.
fn attach_window(&self, window: DetachedWindow<M>) -> Window<M>;

/// Emit an event to javascript windows that pass the predicate.
fn emit_filter_internal<S: Serialize + Clone, F: Fn(&Window<Self>) -> bool>(
&self,
event: String,
payload: Option<S>,
filter: F,
) -> crate::Result<()>;

/// Emit an event to javascript windows that pass the predicate.
fn emit_filter<S: Serialize + Clone, F: Fn(&Window<M>) -> bool>(
&self,
event: M::Event,
payload: Option<S>,
predicate: F,
) -> crate::Result<()>;

/// All current window labels existing.
fn labels(&self) -> HashSet<M::Label>;

/// The configuration the [`Manager`] was built with.
fn config(&self) -> &Config;

/// App package information.
fn package_info(&self) -> &PackageInfo;

/// Remove the specified event handler.
fn unlisten(&self, handler_id: EventHandler);

/// Trigger an event.
fn trigger(&self, event: M::Event, window: Option<M::Label>, data: Option<String>);

/// Set up a listener to an event.
fn listen<F: Fn(Event) + Send + 'static>(
&self,
event: M::Event,
window: Option<M::Label>,
handler: F,
) -> EventHandler;

/// Set up a listener to and event that is automatically removed after called once.
fn once<F: Fn(Event) + Send + 'static>(
&self,
event: M::Event,
window: Option<M::Label>,
handler: F,
);

fn event_listeners_object_name(&self) -> String;
fn event_queue_object_name(&self) -> String;
fn event_emit_function_name(&self) -> String;

/// Generate a random salt and store it in the manager
fn generate_salt(&self) -> Uuid;

/// Verify that the passed salt is a valid salt in the manager.
fn verify_salt(&self, salt: String) -> bool;

/// Get a single managed window.
fn get_window(&self, label: &M::Label) -> Option<Window<M>>;

/// Get all managed windows.
fn windows(&self) -> HashMap<M::Label, Window<M>>;
}
use crate::runtime::{manager::WindowManager, Runtime};

/// No downstream implementations of [`Params`].
pub trait ParamsBase: 'static {}

/// Represents either a running [`Runtime`] or a dispatcher to it.
pub enum RuntimeOrDispatch<'m, M: Params> {
/// A running [`Runtime`] or a dispatcher to it.
pub enum RuntimeOrDispatch<'r, P: Params> {
/// Mutable reference to the running [`Runtime`].
Runtime(&'m mut M::Runtime),
Runtime(&'r mut P::Runtime),

/// A dispatcher to the running [`Runtime`].
Dispatch(<M::Runtime as Runtime>::Dispatcher),
Dispatch(<P::Runtime as Runtime>::Dispatcher),
}

/// Represents a managed handle to the application runner.
pub trait ManagerPrivate<M: Params> {
/// Managed handle to the application runtime.
pub trait ManagerBase<P: Params> {
/// The manager behind the [`Managed`] item.
fn manager(&self) -> &M;
fn manager(&self) -> &WindowManager<P>;

/// The runtime or runtime dispatcher of the [`Managed`] item.
fn runtime(&mut self) -> RuntimeOrDispatch<'_, M>;
fn runtime(&mut self) -> RuntimeOrDispatch<'_, P>;
}
}

Expand Down
29 changes: 14 additions & 15 deletions core/tauri/src/runtime/app.rs
Expand Up @@ -10,10 +10,11 @@ use crate::{
flavors::wry::Wry, manager::WindowManager, tag::Tag, webview::Attributes,
window::PendingWindow, Dispatch, Runtime,
},
sealed::{ManagerPrivate, ParamsPrivate, RuntimeOrDispatch},
sealed::{ManagerBase, RuntimeOrDispatch},
Context, Manager, Params, Window,
};

use crate::runtime::manager::Args;
#[cfg(feature = "updater")]
use crate::updater;

Expand All @@ -22,12 +23,12 @@ use crate::updater;
/// This type implements [`Manager`] which allows for manipulation of global application items.
pub struct App<P: Params> {
runtime: P::Runtime,
manager: P,
manager: WindowManager<P>,
}

impl<P: Params> Manager<P> for App<P> {}
impl<P: Params> ManagerPrivate<P> for App<P> {
fn manager(&self) -> &P {
impl<P: Params> ManagerBase<P> for App<P> {
fn manager(&self) -> &WindowManager<P> {
&self.manager
}

Expand Down Expand Up @@ -104,19 +105,19 @@ where
R: Runtime,
{
/// The JS message handler.
invoke_handler: Box<InvokeHandler<WindowManager<E, L, A, R>>>,
invoke_handler: Box<InvokeHandler<Args<E, L, A, R>>>,

/// The setup hook.
setup: SetupHook<WindowManager<E, L, A, R>>,
setup: SetupHook<Args<E, L, A, R>>,

/// Page load hook.
on_page_load: Box<OnPageLoad<WindowManager<E, L, A, R>>>,
on_page_load: Box<OnPageLoad<Args<E, L, A, R>>>,

/// windows to create when starting up.
pending_windows: Vec<PendingWindow<WindowManager<E, L, A, R>>>,
pending_windows: Vec<PendingWindow<Args<E, L, A, R>>>,

/// All passed plugins
plugins: PluginStore<WindowManager<E, L, A, R>>,
plugins: PluginStore<Args<E, L, A, R>>,
}

impl<E, L, A, R> Builder<E, L, A, R>
Expand All @@ -140,7 +141,7 @@ where
/// Defines the JS message handler callback.
pub fn invoke_handler<F>(mut self, invoke_handler: F) -> Self
where
F: Fn(InvokeMessage<WindowManager<E, L, A, R>>) + Send + Sync + 'static,
F: Fn(InvokeMessage<Args<E, L, A, R>>) + Send + Sync + 'static,
{
self.invoke_handler = Box::new(invoke_handler);
self
Expand All @@ -149,9 +150,7 @@ where
/// Defines the setup hook.
pub fn setup<F>(mut self, setup: F) -> Self
where
F: Fn(&mut App<WindowManager<E, L, A, R>>) -> Result<(), Box<dyn std::error::Error>>
+ Send
+ 'static,
F: Fn(&mut App<Args<E, L, A, R>>) -> Result<(), Box<dyn std::error::Error>> + Send + 'static,
{
self.setup = Box::new(setup);
self
Expand All @@ -160,14 +159,14 @@ where
/// Defines the page load hook.
pub fn on_page_load<F>(mut self, on_page_load: F) -> Self
where
F: Fn(Window<WindowManager<E, L, A, R>>, PageLoadPayload) + Send + Sync + 'static,
F: Fn(Window<Args<E, L, A, R>>, PageLoadPayload) + Send + Sync + 'static,
{
self.on_page_load = Box::new(on_page_load);
self
}

/// Adds a plugin to the runtime.
pub fn plugin<P: Plugin<WindowManager<E, L, A, R>> + 'static>(mut self, plugin: P) -> Self {
pub fn plugin<P: Plugin<Args<E, L, A, R>> + 'static>(mut self, plugin: P) -> Self {
self.plugins.register(plugin);
self
}
Expand Down

0 comments on commit ec27ca8

Please sign in to comment.