feat(data): pulse sender registry
This commit is contained in:
@@ -10,10 +10,15 @@ use std::rc::Rc;
|
|||||||
use std::sync::{Arc, RwLock};
|
use std::sync::{Arc, RwLock};
|
||||||
use std::thread::{self, JoinHandle};
|
use std::thread::{self, JoinHandle};
|
||||||
|
|
||||||
|
use super::registry::Registry;
|
||||||
|
use crate::nodes::data::PulseSender;
|
||||||
|
|
||||||
pub struct EventLoop {
|
pub struct EventLoop {
|
||||||
pub socket_path: String,
|
pub socket_path: String,
|
||||||
join_handle: RwLock<Option<JoinHandle<Result<()>>>>,
|
join_handle: RwLock<Option<JoinHandle<Result<()>>>>,
|
||||||
stop_write: pipe::Sender,
|
stop_write: pipe::Sender,
|
||||||
|
|
||||||
|
pub pulse_senders: Registry<Arc<PulseSender>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl EventLoop {
|
impl EventLoop {
|
||||||
@@ -26,6 +31,8 @@ impl EventLoop {
|
|||||||
socket_path,
|
socket_path,
|
||||||
join_handle: RwLock::new(None),
|
join_handle: RwLock::new(None),
|
||||||
stop_write: sender,
|
stop_write: sender,
|
||||||
|
|
||||||
|
pulse_senders: Default::default(),
|
||||||
});
|
});
|
||||||
let event_loop_arc_captured = event_loop_arc.clone();
|
let event_loop_arc_captured = event_loop_arc.clone();
|
||||||
let join_handle = thread::Builder::new()
|
let join_handle = thread::Builder::new()
|
||||||
|
|||||||
46
src/nodes/data.rs
Normal file
46
src/nodes/data.rs
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
use super::core::Node;
|
||||||
|
use crate::core::eventloop::EventLoop;
|
||||||
|
use anyhow::{ensure, Result};
|
||||||
|
use rccell::RcCell;
|
||||||
|
use std::sync::{Arc, RwLock, Weak};
|
||||||
|
|
||||||
|
pub struct PulseSender {
|
||||||
|
event_loop: Weak<EventLoop>,
|
||||||
|
registry_idx: RwLock<Option<usize>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PulseSender {
|
||||||
|
pub fn add_to(node: &RcCell<Node>) -> Result<()> {
|
||||||
|
ensure!(
|
||||||
|
node.borrow().spatial.is_some(),
|
||||||
|
"Node does not have a spatial attached!"
|
||||||
|
);
|
||||||
|
|
||||||
|
let sender = Arc::new(PulseSender {
|
||||||
|
event_loop: node.borrow().get_client().map_or(Weak::new(), |client| {
|
||||||
|
Arc::downgrade(&client.get_event_loop())
|
||||||
|
}),
|
||||||
|
registry_idx: RwLock::new(None),
|
||||||
|
});
|
||||||
|
let idx = sender
|
||||||
|
.event_loop
|
||||||
|
.upgrade()
|
||||||
|
.and_then(|event_loop| event_loop.pulse_senders.add(sender.clone()).ok());
|
||||||
|
*sender.registry_idx.write().unwrap() = idx;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Drop for PulseSender {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
let event_loop = self.event_loop.upgrade();
|
||||||
|
let idx = self
|
||||||
|
.registry_idx
|
||||||
|
.write()
|
||||||
|
.ok()
|
||||||
|
.and_then(|registry_idx| registry_idx.clone());
|
||||||
|
event_loop
|
||||||
|
.zip(idx)
|
||||||
|
.and_then(|(event_loop, idx)| event_loop.pulse_senders.remove(idx).ok());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,3 +1,4 @@
|
|||||||
pub mod core;
|
pub mod core;
|
||||||
|
pub mod data;
|
||||||
pub mod field;
|
pub mod field;
|
||||||
pub mod spatial;
|
pub mod spatial;
|
||||||
|
|||||||
Reference in New Issue
Block a user