1use std::sync::OnceLock;
2
3use crate::{client, Client, ClientOptions, Error, Event};
4
5static GLOBAL_CLIENT: OnceLock<Client> = OnceLock::new();
6static GLOBAL_DISABLE: OnceLock<bool> = OnceLock::new();
7
8#[cfg(feature = "async-client")]
14pub async fn init_global_client<C: Into<ClientOptions>>(options: C) -> Result<(), Error> {
15 if is_disabled() {
16 return Ok(());
17 }
18
19 let client = client(options).await;
20 GLOBAL_CLIENT
21 .set(client)
22 .map_err(|_| Error::AlreadyInitialized)
23}
24
25#[cfg(not(feature = "async-client"))]
31pub fn init_global_client<C: Into<ClientOptions>>(options: C) -> Result<(), Error> {
32 if is_disabled() {
33 return Ok(());
34 }
35
36 let client = client(options);
37 GLOBAL_CLIENT
38 .set(client)
39 .map_err(|_| Error::AlreadyInitialized)
40}
41
42pub fn disable() {
45 let _ = GLOBAL_DISABLE.set(true);
46}
47
48pub fn is_disabled() -> bool {
52 *GLOBAL_DISABLE.get().unwrap_or(&false)
53}
54
55#[cfg(feature = "async-client")]
57pub async fn capture(event: Event) -> Result<(), Error> {
58 let client = GLOBAL_CLIENT.get().ok_or(Error::NotInitialized)?;
59 client.capture(event).await
60}
61
62#[cfg(not(feature = "async-client"))]
64pub fn capture(event: Event) -> Result<(), Error> {
65 let client = GLOBAL_CLIENT.get().ok_or(Error::NotInitialized)?;
66 client.capture(event)
67}