fix user discovery not working some times

This commit is contained in:
otsmr 2026-06-05 10:56:17 +02:00
parent 1d3b8dbd8a
commit 34ecb66e0b
2 changed files with 11 additions and 7 deletions

View file

@ -7,9 +7,10 @@ use protocols::user_discovery::traits::{AnnouncedUser, OtherPromotion};
use crate::error::{Result, TwonlyError};
use crate::{callback_generator, frb_generated::StreamSink};
use std::sync::{Arc, OnceLock};
use std::sync::Arc;
static FLUTTER_CALLBACKS: OnceLock<FlutterCallbacks> = OnceLock::new();
static FLUTTER_CALLBACKS: std::sync::RwLock<Option<FlutterCallbacks>> =
std::sync::RwLock::new(None);
// This will also generate the function init_flutter_callbacks which MUST be called from Flutter to initialize the callbacks
callback_generator! {
@ -39,8 +40,10 @@ callback_generator! {
}
}
pub(crate) fn get_callbacks() -> Result<&'static FlutterCallbacks> {
pub(crate) fn get_callbacks() -> Result<FlutterCallbacks> {
FLUTTER_CALLBACKS
.get()
.read()
.unwrap()
.clone()
.ok_or(TwonlyError::MissingCallbackInitialization)
}

View file

@ -13,6 +13,7 @@ macro_rules! callback_generator {
) => {
// 1. Generate the Nested Sub-Structs
$(
#[derive(Clone)]
pub(crate) struct $sub_struct_name {
$(
pub(crate) $fn_name: Arc<dyn Fn($($input),*) -> DartFnFuture<$output> + Send + Sync + 'static>,
@ -21,6 +22,7 @@ macro_rules! callback_generator {
)*
// 2. Generate the Main Container Struct
#[derive(Clone)]
pub(crate) struct $struct_name {
$(
pub(crate) $sub_struct_field: $sub_struct_name,
@ -48,9 +50,8 @@ macro_rules! callback_generator {
};
// Use the static global strictly named FLUTTER_CALLBACKS
FLUTTER_CALLBACKS.set(callbacks).unwrap_or_else(|_| {
println!("Callbacks were already initialized!");
});
let mut lock = FLUTTER_CALLBACKS.write().unwrap();
*lock = Some(callbacks);
}
}
};