Skip to main content

freya_devtools_app/
state.rs

1use std::{
2    collections::{
3        HashMap,
4        HashSet,
5    },
6    sync::Arc,
7};
8
9use async_lock::Mutex;
10use async_tungstenite::WebSocketSender;
11use freya_core::{
12    integration::NodeId,
13    prelude::spawn,
14};
15use freya_devtools::{
16    IncomingMessage,
17    IncomingMessageAction,
18    NodeInfo,
19};
20use freya_radio::hooks::RadioChannel;
21use smol::net::TcpStream;
22use tungstenite::Message;
23
24pub struct DevtoolsState {
25    pub(crate) nodes: HashMap<u64, Vec<NodeInfo>>,
26    pub(crate) expanded_nodes: HashSet<(u64, NodeId)>,
27    pub(crate) client: Arc<Mutex<Option<WebSocketSender<TcpStream>>>>,
28    pub(crate) animation_speed: f32,
29}
30
31impl DevtoolsState {
32    pub fn send_action(&self, action: IncomingMessageAction) {
33        let message = Message::Text(
34            serde_json::to_string(&IncomingMessage { action })
35                .unwrap()
36                .into(),
37        );
38        let client = self.client.clone();
39        spawn(async move {
40            if let Some(client) = client.lock().await.as_mut() {
41                client.send(message).await.ok();
42            }
43        });
44    }
45}
46
47#[derive(PartialEq, Eq, Clone, Debug, Hash)]
48pub enum DevtoolsChannel {
49    Global,
50    UpdatedTree,
51    Misc,
52}
53
54impl RadioChannel<DevtoolsState> for DevtoolsChannel {}