Skip to content
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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "posthog-rs"
license = "MIT"
version = "0.3.1"
version = "0.3.2"
authors = ["christos <[email protected]>"]
description = "An unofficial Rust client for Posthog (https://posthog.com/)."
repository = "https://github.com/openquery-io/posthog-rs"
Expand Down
19 changes: 18 additions & 1 deletion src/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@ use std::sync::OnceLock;

use crate::{client, Client, ClientOptions, Error, Event};

pub static GLOBAL_CLIENT: OnceLock<Client> = OnceLock::new();
static GLOBAL_CLIENT: OnceLock<Client> = OnceLock::new();
static GLOBAL_DISABLE: OnceLock<bool> = OnceLock::new();

#[cfg(feature = "async-client")]
pub async fn init_global_client<C: Into<ClientOptions>>(options: C) -> Result<(), Error> {
if is_disabled() {
return Ok(());
}

let client = client(options).await;
GLOBAL_CLIENT
.set(client)
Expand All @@ -14,12 +19,24 @@ pub async fn init_global_client<C: Into<ClientOptions>>(options: C) -> Result<()

#[cfg(not(feature = "async-client"))]
pub fn init_global_client<C: Into<ClientOptions>>(options: C) -> Result<(), Error> {
if is_disabled() {
return Ok(());
}

let client = client(options);
GLOBAL_CLIENT
.set(client)
.map_err(|_| Error::AlreadyInitialized)
}

pub fn disable() {
let _ = GLOBAL_DISABLE.set(true);
}

pub fn is_disabled() -> bool {
*GLOBAL_DISABLE.get().unwrap_or(&false)
}

#[cfg(feature = "async-client")]
pub async fn capture(event: Event) -> Result<(), Error> {
let client = GLOBAL_CLIENT.get().ok_or(Error::NotInitialized)?;
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ pub use event::Event;

// We expose a global capture function as a convenience, that uses a global client
pub use global::capture;
pub use global::disable as disable_global;
pub use global::init_global_client as init_global;
pub use global::is_disabled as global_is_disabled;