From 31aeeb9f92a38aa19f9ed96331ae4b1c36f5d80b Mon Sep 17 00:00:00 2001 From: ronen barzel Date: Mon, 10 Jul 2023 04:35:06 +0100 Subject: [PATCH] Use yew::Callbacks instead of custom (for now) Ultimate goal for #74 is to move away from yew dependency, so originally used custom callbacks instead of yew's. But in working on websocket/webtransport connection (in another branch) it's currently deeply tied to yew Callback. So for now, for consistency it makes sense to use yew Callbacks everywhere. Later on in working on #74 will try to come up with a consistent approach to extracting the yew Callback dependency. --- yew-ui/src/components/attendants.rs | 17 ++++----- .../src/model/decode/peer_decode_manager.rs | 38 +++++-------------- 2 files changed, 16 insertions(+), 39 deletions(-) diff --git a/yew-ui/src/components/attendants.rs b/yew-ui/src/components/attendants.rs index 6075bdc6..20d504d4 100644 --- a/yew-ui/src/components/attendants.rs +++ b/yew-ui/src/components/attendants.rs @@ -161,19 +161,16 @@ impl Component for AttendantsComponent { let webtransport_enabled = ctx.props().webtransport_enabled; let mut peer_decode_manager = PeerDecodeManager::new(); let link = ctx.link().clone(); - peer_decode_manager.on_peer_added.set(move |email| { + peer_decode_manager.on_peer_added = Callback::from(move |email| { link.send_message(Msg::OnPeerAdded(email)); }); let link = ctx.link().clone(); - peer_decode_manager - .on_first_frame - .set(move |(email, media_type)| { - link.send_message(Msg::OnFirstFrame((email, media_type))); - }); - peer_decode_manager.get_video_canvas_id.set(|email| email); - peer_decode_manager - .get_screen_canvas_id - .set(|email| format!("screen-share-{}", &email)); + peer_decode_manager.on_first_frame = Callback::from(move |(email, media_type)| { + link.send_message(Msg::OnFirstFrame((email, media_type))); + }); + peer_decode_manager.get_video_canvas_id = Callback::from(|email| email); + peer_decode_manager.get_screen_canvas_id = + Callback::from(|email| format!("screen-share-{}", &email)); Self { connection: None, connected: false, diff --git a/yew-ui/src/model/decode/peer_decode_manager.rs b/yew-ui/src/model/decode/peer_decode_manager.rs index b1a9b73d..148326f8 100644 --- a/yew-ui/src/model/decode/peer_decode_manager.rs +++ b/yew-ui/src/model/decode/peer_decode_manager.rs @@ -3,30 +3,10 @@ use std::collections::HashMap; use std::sync::Arc; use types::protos::media_packet::media_packet::MediaType; use types::protos::media_packet::MediaPacket; +use yew::prelude::Callback; use super::{AudioPeerDecoder, VideoPeerDecoder}; -pub struct Callback { - func: Option OUT + 'static>>, -} - -impl Callback { - fn new() -> Self { - Self { func: None } - } - - pub fn set(&mut self, func: impl FnMut(IN) -> OUT + 'static) { - self.func = Some(Box::new(func)); - } - - fn call(&mut self, arg: IN) -> OUT { - match &mut self.func { - Some(func) => func(arg), - None => Default::default(), - } - } -} - pub struct MultiDecoder { pub audio: AudioPeerDecoder, pub video: VideoPeerDecoder, @@ -72,10 +52,10 @@ impl PeerDecodeManager { Self { connected_peers: HashMap::new(), sorted_connected_peers_keys: vec![], - on_peer_added: Callback::new(), - on_first_frame: Callback::new(), - get_video_canvas_id: Callback::new(), - get_screen_canvas_id: Callback::new(), + on_peer_added: Callback::noop(), + on_first_frame: Callback::noop(), + get_video_canvas_id: Callback::from(|key| format!("video-{}", &key)), + get_screen_canvas_id: Callback::from(|key| format!("screen-{}", &key)), } } @@ -98,7 +78,7 @@ impl PeerDecodeManager { match decoded { Some(first_frame) => { if first_frame { - self.on_first_frame.call((email.clone(), media_type)); + self.on_first_frame.emit((email.clone(), media_type)); } } None => { @@ -110,15 +90,15 @@ impl PeerDecodeManager { fn add_peer(&mut self, email: &String) { self.insert_peer(&email); - self.on_peer_added.call(email.clone()) + self.on_peer_added.emit(email.clone()) } fn insert_peer(&mut self, email: &String) { self.connected_peers.insert( email.clone(), MultiDecoder::new( - self.get_video_canvas_id.call(email.clone()), - self.get_screen_canvas_id.call(email.clone()), + self.get_video_canvas_id.emit(email.clone()), + self.get_screen_canvas_id.emit(email.clone()), ), ); self.sorted_connected_peers_keys.push(email.clone());