-
Notifications
You must be signed in to change notification settings - Fork 546
refactor: extract AnalyticsRuntime trait #4231
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| use crate::{AnalyticsPayload, Error}; | ||
|
|
||
| pub trait AnalyticsRuntime: Send + Sync + 'static { | ||
| fn enrich(&self, payload: &mut AnalyticsPayload); | ||
| fn distinct_id(&self) -> String; | ||
| fn is_disabled(&self) -> bool; | ||
| fn set_disabled(&self, disabled: bool) -> Result<(), Error>; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,17 +1,20 @@ | ||
| use std::sync::Arc; | ||
|
|
||
| use tauri::Manager; | ||
|
|
||
| mod commands; | ||
| mod error; | ||
| mod ext; | ||
| mod store; | ||
| mod tauri_runtime; | ||
|
|
||
| pub use error::{Error, Result}; | ||
| pub use ext::*; | ||
| use store::*; | ||
|
|
||
| pub use hypr_analytics::*; | ||
|
|
||
| pub type ManagedState = hypr_analytics::AnalyticsClient; | ||
| pub type ManagedState = hypr_analytics::AnalyticsService; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Flag plugin breaks: managed state type changedMedium Severity The Tauri managed state type changed from Additional Locations (1) |
||
|
|
||
| const PLUGIN_NAME: &str = "analytics"; | ||
|
|
||
|
|
@@ -62,7 +65,10 @@ pub fn init<R: tauri::Runtime>() -> tauri::plugin::TauriPlugin<R> { | |
| builder.build() | ||
| }; | ||
|
|
||
| assert!(app.manage(client)); | ||
| let runtime = Arc::new(tauri_runtime::TauriAnalyticsRuntime::new(app)); | ||
| let service = hypr_analytics::AnalyticsService::new(client, runtime); | ||
|
|
||
| assert!(app.manage(service)); | ||
| Ok(()) | ||
| }) | ||
| .build() | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| use hypr_analytics::{AnalyticsPayload, AnalyticsRuntime}; | ||
| use tauri_plugin_store2::Store2PluginExt; | ||
|
|
||
| pub struct TauriAnalyticsRuntime<R: tauri::Runtime> { | ||
| app_handle: tauri::AppHandle<R>, | ||
| app_version: String, | ||
| git_hash: String, | ||
| bundle_id: String, | ||
| app_identifier: String, | ||
| } | ||
|
|
||
| impl<R: tauri::Runtime> TauriAnalyticsRuntime<R> { | ||
| pub fn new(app_handle: &tauri::AppHandle<R>) -> Self { | ||
| use tauri_plugin_misc::MiscPluginExt; | ||
|
|
||
| let app_version = env!("APP_VERSION").to_string(); | ||
| let git_hash = app_handle.misc().get_git_hash(); | ||
| let bundle_id = app_handle.config().identifier.clone(); | ||
| let app_identifier = app_handle.config().identifier.clone(); | ||
|
|
||
| Self { | ||
| app_handle: app_handle.clone(), | ||
| app_version, | ||
| git_hash, | ||
| bundle_id, | ||
| app_identifier, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl<R: tauri::Runtime> AnalyticsRuntime for TauriAnalyticsRuntime<R> { | ||
| fn enrich(&self, payload: &mut AnalyticsPayload) { | ||
| payload | ||
| .props | ||
| .entry("app_version".into()) | ||
| .or_insert(self.app_version.clone().into()); | ||
|
|
||
| payload | ||
| .props | ||
| .entry("app_identifier".into()) | ||
| .or_insert(self.app_identifier.clone().into()); | ||
|
|
||
| payload | ||
| .props | ||
| .entry("git_hash".into()) | ||
| .or_insert(self.git_hash.clone().into()); | ||
|
|
||
| payload | ||
| .props | ||
| .entry("bundle_id".into()) | ||
| .or_insert(self.bundle_id.clone().into()); | ||
|
|
||
| payload.props.entry("$set".into()).or_insert_with(|| { | ||
| serde_json::json!({ | ||
| "app_version": self.app_version | ||
| }) | ||
| }); | ||
| } | ||
|
|
||
| fn distinct_id(&self) -> String { | ||
| hypr_host::fingerprint() | ||
| } | ||
|
|
||
| fn is_disabled(&self) -> bool { | ||
| let result: Result<bool, tauri_plugin_store2::Error> = (|| { | ||
| let store = self.app_handle.store2().scoped_store(crate::PLUGIN_NAME)?; | ||
| let v: bool = store.get(crate::StoreKey::Disabled)?.unwrap_or(false); | ||
| Ok(v) | ||
| })(); | ||
| result.unwrap_or(true) | ||
| } | ||
|
|
||
| fn set_disabled(&self, disabled: bool) -> Result<(), hypr_analytics::Error> { | ||
| let store = self | ||
| .app_handle | ||
| .store2() | ||
| .scoped_store(crate::PLUGIN_NAME) | ||
| .map_err(|e| hypr_analytics::Error::Runtime(e.to_string()))?; | ||
| store | ||
| .set(crate::StoreKey::Disabled, disabled) | ||
| .map_err(|e| hypr_analytics::Error::Runtime(e.to_string()))?; | ||
| Ok(()) | ||
| } | ||
| } |


Uh oh!
There was an error while loading. Please reload this page.