some logic to panic if a proper event callback hasn't been set

This commit is contained in:
Ivan Folgueira Bande 2024-11-22 15:34:38 +01:00
parent 14aca7f403
commit dad471a114
No known key found for this signature in database
GPG Key ID: 3C117481F89E24A7
3 changed files with 16 additions and 7 deletions

View File

@ -85,7 +85,7 @@ impl TicTacToeApp {
// Establish a closure that handles the incoming messages
self.waku.ctx.waku_set_event_callback(my_closure).expect("set event call back working");
// Subscribe to desired topic
// Subscribe to desired topic using the relay protocol
// self.waku.relay_subscribe(&self.game_topic.to_string()).expect("waku should subscribe");
let content_topic = WakuContentTopic::new("waku", "2", "tictactoegame", Encoding::Proto);

View File

@ -10,12 +10,21 @@ pub struct WakuNodeContext {
impl WakuNodeContext {
pub fn new(obj_ptr: *mut c_void) -> Self {
Self {
let me = Self {
obj_ptr: obj_ptr,
msg_observer: Arc::new(Mutex::new(Box::new(|_response| {
println!("msg observer not set")
}))),
}
msg_observer: Arc::new(Mutex::new(Box::new(|_| {}))),
};
// By default we set a callback that will panic if the user didn't specify a valid callback.
// And by valid callback we mean a callback that can properly handle the waku events.
me.waku_set_event_callback(WakuNodeContext::panic_callback)
.expect("correctly set default callback");
me
}
// default callback that does nothing. A valid callback should be set
fn panic_callback(_response: LibwakuResponse) {
panic!("callback not set. Please use waku_set_event_callback to set a valid callback")
}
pub fn get_ptr(&self) -> *mut c_void {

View File

@ -32,7 +32,7 @@ async fn test_echo_messages(
content_topic: WakuContentTopic,
) -> Result<(), String> {
// setting a naïve event handler to avoid appearing ERR messages in logs
node1.ctx.waku_set_event_callback(&|_| {});
let _ = node1.ctx.waku_set_event_callback(&|_| {});
let rx_waku_message: Arc<Mutex<WakuMessage>> = Arc::new(Mutex::new(WakuMessage::default()));