Skip to content

Commit

Permalink
Use yew::Callbacks instead of custom (for now)
Browse files Browse the repository at this point in the history
Ultimate goal for security-union#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 security-union#74 will try to come up with a consistent approach
to extracting the yew Callback dependency.
  • Loading branch information
ronen committed Jul 10, 2023
1 parent a9c1922 commit 31aeeb9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 39 deletions.
17 changes: 7 additions & 10 deletions yew-ui/src/components/attendants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
38 changes: 9 additions & 29 deletions yew-ui/src/model/decode/peer_decode_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<IN, OUT = ()> {
func: Option<Box<dyn FnMut(IN) -> OUT + 'static>>,
}

impl<IN, OUT: std::default::Default> Callback<IN, OUT> {
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,
Expand Down Expand Up @@ -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)),
}
}

Expand All @@ -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 => {
Expand All @@ -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());
Expand Down

0 comments on commit 31aeeb9

Please sign in to comment.