Skip to content

Commit 2b58d20

Browse files
committed
refactor + docs
1 parent 27d066a commit 2b58d20

5 files changed

Lines changed: 35 additions & 20 deletions

File tree

README.md

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ fn main() {
3838
}
3939
```
4040

41-
You can then start sending events from Rust by importing the `tauri_plugin_aptabase::EventTracker` trait and calling the `track_event` method on an `App`, `AppHandle` or `Window`. For the app_started event, for example, you could do this:
41+
You can then start sending events from Rust by importing the `tauri_plugin_aptabase::EventTracker` trait and calling the `track_event` method on `App`, `AppHandle` or `Window`.
42+
43+
As an example, you can add `app_started` and `app_exited` events like this:
4244

4345

4446
```rust
@@ -51,8 +53,15 @@ fn main() {
5153
app.track_event("app_started", None);
5254
Ok(())
5355
})
54-
.run(tauri::generate_context!())
55-
.expect("error while running tauri application");
56+
.build(tauri::generate_context!())
57+
.expect("error while running tauri application")
58+
.run(|handler, event| match event {
59+
tauri::RunEvent::Exit { .. } => {
60+
handler.track_event("app_exited", None);
61+
handler.flush_events_blocking();
62+
}
63+
_ => {}
64+
})
5665
}
5766
```
5867

src/client.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,6 @@ use crate::{
1010

1111
static SESSION_TIMEOUT: Duration = Duration::from_secs(4 * 60 * 60);
1212

13-
#[cfg(not(debug_assertions))]
14-
static DEFAULT_FLUSH_INTERVAL: Duration = Duration::from_secs(60);
15-
16-
#[cfg(debug_assertions)]
17-
static DEFAULT_FLUSH_INTERVAL: Duration = Duration::from_secs(2);
18-
1913
/// A tracking session.
2014
#[derive(Debug, Clone)]
2115
pub struct TrackingSession {
@@ -44,7 +38,7 @@ pub struct AptabaseClient {
4438
impl AptabaseClient {
4539

4640
/// Creates a new Aptabase client.
47-
pub fn new(config: Config, app_version: String) -> Self {
41+
pub fn new(config: &Config, app_version: String) -> Self {
4842
let sys_info = sys::get_info();
4943

5044
let is_enabled = !config.app_key.is_empty();
@@ -60,9 +54,8 @@ impl AptabaseClient {
6054
}
6155

6256
/// Starts the event dispatcher loop.
63-
pub(crate) fn start_polling(&self, interval: Option<Duration>) {
57+
pub(crate) fn start_polling(&self, interval: Duration) {
6458
let dispatcher = self.dispatcher.clone();
65-
let interval = interval.unwrap_or(DEFAULT_FLUSH_INTERVAL);
6659

6760
tauri::async_runtime::spawn(async move {
6861
loop {

src/config.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,31 @@
1+
use std::time::Duration;
2+
13
use log::debug;
24
use reqwest::Url;
35

6+
use crate::InitOptions;
7+
48
#[derive(Debug, Clone)]
59
pub struct Config {
610
pub app_key: String,
711
pub ingest_api_url: Url,
12+
pub flush_interval: Duration,
813
}
914

1015
static LOCAL: &str = "http://localhost:3000";
1116
static US_REGION: &str = "https://us.aptabase.com";
1217
static EU_REGION: &str = "https://eu.aptabase.com";
1318

19+
#[cfg(not(debug_assertions))]
20+
static DEFAULT_FLUSH_INTERVAL: Duration = Duration::from_secs(60);
21+
22+
#[cfg(debug_assertions)]
23+
static DEFAULT_FLUSH_INTERVAL: Duration = Duration::from_secs(2);
24+
1425
const VALID_REGIONS: &'static [&'static str] = &["US", "EU", "DEV", "SH"];
1526

1627
impl Config {
17-
pub fn new(app_key: String, host: Option<String>) -> Self {
28+
pub fn new(app_key: String, opts: InitOptions) -> Self {
1829
let parts = app_key.split("-").collect::<Vec<&str>>();
1930
if parts.len() != 3 || !VALID_REGIONS.contains(&parts[1]) {
2031
debug!("The Aptabase App Key '{}' is invalid. Tracking will be disabled.", app_key);
@@ -26,7 +37,7 @@ impl Config {
2637
"US" => US_REGION.into(),
2738
"DEV" => LOCAL.into(),
2839
"SH" => {
29-
if let Some(host) = host {
40+
if let Some(host) = opts.host {
3041
host
3142
} else {
3243
debug!("Host parameter must be defined when using Self-Hosted App Key. Tracking will be disabled.");
@@ -39,6 +50,7 @@ impl Config {
3950
Config {
4051
app_key,
4152
ingest_api_url: format!("{}/api/v0/events", base_url).parse().unwrap(),
53+
flush_interval: opts.flush_interval.clone().unwrap_or(DEFAULT_FLUSH_INTERVAL)
4254
}
4355
}
4456
}
@@ -48,6 +60,7 @@ impl Default for Config {
4860
return Config {
4961
app_key: String::new(),
5062
ingest_api_url: Url::parse(LOCAL).unwrap(),
63+
flush_interval: DEFAULT_FLUSH_INTERVAL,
5164
};
5265
}
5366
}

src/dispatcher.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub(crate) struct EventDispatcher {
1515
}
1616

1717
impl EventDispatcher {
18-
pub fn new(config: Config) -> Self {
18+
pub fn new(config: &Config) -> Self {
1919
let mut headers = HeaderMap::new();
2020
let app_key_header = HeaderValue::from_str(config.app_key.as_str())
2121
.expect("failed to define App Key header value");

src/lib.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ mod commands;
44
mod sys;
55
mod dispatcher;
66

7-
use std::{sync::Arc, panic::PanicInfo, time::Duration, thread};
7+
use std::{sync::Arc, panic::PanicInfo, time::Duration};
88

99
use config::Config;
1010
use serde_json::Value;
1111
use client::AptabaseClient;
1212
use tauri::{
1313
plugin::{TauriPlugin, self},
14-
Runtime, Manager, App, AppHandle, Window,
14+
Runtime, Manager, App, AppHandle, Window,
1515
};
1616

1717
#[derive(Default, Debug, Clone)]
@@ -58,11 +58,11 @@ impl Builder {
5858
plugin::Builder::new("aptabase")
5959
.invoke_handler(tauri::generate_handler![commands::track_event])
6060
.setup(|app| {
61-
let cfg = Config::new(self.app_key, self.options.host);
61+
let cfg = Config::new(self.app_key, self.options);
6262
let app_version = app.package_info().version.to_string();
63-
let client = Arc::new(AptabaseClient::new(cfg, app_version));
63+
let client = Arc::new(AptabaseClient::new(&cfg, app_version));
6464

65-
client.start_polling(self.options.flush_interval);
65+
client.start_polling(cfg.flush_interval);
6666

6767
if let Some(hook) = self.panic_hook {
6868
let hook_client = client.clone();

0 commit comments

Comments
 (0)