#![cfg(feature = "engate")]
use std::sync::Arc;
use std::sync::Mutex;
use std::sync::mpsc;
use engate_attach::Producer;
use engate_types::AttachError;
use tear_types::engate_wrap::PaneSnapshotWrap;
use tear_types::{MultiplexerControl, PaneId};
use crate::Client;
pub struct PaneProducer {
pub client: Arc<Client>,
pub pane: PaneId,
handle: Mutex<Option<crate::SubscribeHandle>>,
}
impl PaneProducer {
#[must_use]
pub fn new(client: Arc<Client>, pane: PaneId) -> Self {
Self {
client,
pane,
handle: Mutex::new(None),
}
}
}
impl Producer for PaneProducer {
type Item = Vec<u8>;
type Snap = PaneSnapshotWrap;
fn snapshot(&self) -> Result<Self::Snap, AttachError> {
self.client
.pane_snapshot(self.pane)
.map(PaneSnapshotWrap)
.map_err(|e| AttachError::SnapshotFailed(e.to_string()))
}
fn subscribe(&self) -> Result<mpsc::Receiver<Self::Item>, AttachError> {
let (tx, rx) = mpsc::channel();
let h = self
.client
.subscribe_pane_bytes(self.pane, move |bytes| {
let _ = tx.send(bytes.to_vec());
})
.map_err(|e| AttachError::SubscribeFailed(e.to_string()))?;
*self.handle.lock().unwrap() = Some(h);
Ok(rx)
}
}