fix: proper lifetime for cursor, renamed methods

Co-authored-by: zaaarf <me@zaaarf.foo>
This commit is contained in:
əlemi 2024-08-08 02:27:06 +02:00
parent 515cab331c
commit 59d8a4640d
Signed by: alemi
GPG key ID: A4895B84D311642C
4 changed files with 26 additions and 28 deletions

View file

@ -11,7 +11,7 @@ pub(crate) trait ControllerWorker<T : Sized + Send + Sync> {
type Tx; type Tx;
type Rx; type Rx;
fn subscribe(&self) -> Self::Controller; fn controller(&self) -> Self::Controller;
async fn work(self, tx: Self::Tx, rx: Self::Rx); async fn work(self, tx: Self::Tx, rx: Self::Rx);
} }

View file

@ -23,7 +23,7 @@ pub(crate) struct BufferWorker {
poller: mpsc::UnboundedReceiver<oneshot::Sender<()>>, poller: mpsc::UnboundedReceiver<oneshot::Sender<()>>,
pollers: Vec<oneshot::Sender<()>>, pollers: Vec<oneshot::Sender<()>>,
stop: mpsc::UnboundedReceiver<()>, stop: mpsc::UnboundedReceiver<()>,
controller: Arc<BufferControllerInner>, controller: BufferController,
} }
impl BufferWorker { impl BufferWorker {
@ -50,7 +50,7 @@ impl BufferWorker {
poller: poller_rx, poller: poller_rx,
pollers: Vec::new(), pollers: Vec::new(),
stop: end_rx, stop: end_rx,
controller: Arc::new(controller), controller: BufferController(Arc::new(controller)),
} }
} }
} }
@ -61,8 +61,8 @@ impl ControllerWorker<TextChange> for BufferWorker {
type Tx = mpsc::Sender<Operation>; type Tx = mpsc::Sender<Operation>;
type Rx = Streaming<BufferEvent>; type Rx = Streaming<BufferEvent>;
fn subscribe(&self) -> BufferController { fn controller(&self) -> BufferController {
BufferController(self.controller.clone()) self.controller.clone()
} }
async fn work(mut self, tx: Self::Tx, mut rx: Self::Rx) { async fn work(mut self, tx: Self::Tx, mut rx: Self::Rx) {

View file

@ -28,29 +28,29 @@ use codemp_proto::cursor::{CursorEvent, CursorPosition};
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
#[cfg_attr(feature = "python", pyo3::pyclass)] #[cfg_attr(feature = "python", pyo3::pyclass)]
#[cfg_attr(feature = "js", napi_derive::napi)] #[cfg_attr(feature = "js", napi_derive::napi)]
pub struct CursorController(Arc<CursorControllerInner>); pub struct CursorController(pub(crate) Arc<CursorControllerInner>);
#[derive(Debug)] #[derive(Debug)]
struct CursorControllerInner { pub(crate) struct CursorControllerInner {
op: mpsc::UnboundedSender<CursorPosition>, op: mpsc::UnboundedSender<CursorPosition>,
last_op: Mutex<watch::Receiver<CursorEvent>>, last_op: Mutex<watch::Receiver<CursorEvent>>,
stream: Mutex<broadcast::Receiver<CursorEvent>>, stream: Mutex<broadcast::Receiver<CursorEvent>>,
stop: mpsc::UnboundedSender<()>, stop: mpsc::UnboundedSender<()>,
} }
impl CursorController { impl CursorControllerInner {
pub(crate) fn new( pub(crate) fn new(
op: mpsc::UnboundedSender<CursorPosition>, op: mpsc::UnboundedSender<CursorPosition>,
last_op: Mutex<watch::Receiver<CursorEvent>>, last_op: Mutex<watch::Receiver<CursorEvent>>,
stream: Mutex<broadcast::Receiver<CursorEvent>>, stream: Mutex<broadcast::Receiver<CursorEvent>>,
stop: mpsc::UnboundedSender<()>, stop: mpsc::UnboundedSender<()>,
) -> Self { ) -> Self {
Self(Arc::new(CursorControllerInner { Self {
op, op,
last_op, last_op,
stream, stream,
stop, stop,
})) }
} }
} }

View file

@ -6,16 +6,14 @@ use tonic::{Streaming, async_trait};
use crate::{api::{controller::ControllerWorker, Cursor}, errors::IgnorableError}; use crate::{api::{controller::ControllerWorker, Cursor}, errors::IgnorableError};
use codemp_proto::cursor::{CursorPosition, CursorEvent}; use codemp_proto::cursor::{CursorPosition, CursorEvent};
use super::controller::CursorController; use super::controller::{CursorController, CursorControllerInner};
pub(crate) struct CursorWorker { pub(crate) struct CursorWorker {
producer: mpsc::UnboundedSender<CursorPosition>,
op: mpsc::UnboundedReceiver<CursorPosition>, op: mpsc::UnboundedReceiver<CursorPosition>,
changed: watch::Sender<CursorEvent>, changed: watch::Sender<CursorEvent>,
last_op: watch::Receiver<CursorEvent>, channel: broadcast::Sender<CursorEvent>,
channel: Arc<broadcast::Sender<CursorEvent>>,
stop: mpsc::UnboundedReceiver<()>, stop: mpsc::UnboundedReceiver<()>,
stop_control: mpsc::UnboundedSender<()>, controller: CursorController,
} }
impl Default for CursorWorker { impl Default for CursorWorker {
@ -24,14 +22,18 @@ impl Default for CursorWorker {
let (cur_tx, _cur_rx) = broadcast::channel(64); let (cur_tx, _cur_rx) = broadcast::channel(64);
let (end_tx, end_rx) = mpsc::unbounded_channel(); let (end_tx, end_rx) = mpsc::unbounded_channel();
let (change_tx, change_rx) = watch::channel(CursorEvent::default()); let (change_tx, change_rx) = watch::channel(CursorEvent::default());
let controller = CursorControllerInner::new(
op_tx,
Mutex::new(change_rx),
Mutex::new(cur_tx.subscribe()),
end_tx
);
Self { Self {
producer: op_tx,
op: op_rx, op: op_rx,
changed: change_tx, changed: change_tx,
last_op: change_rx, channel: cur_tx,
channel: Arc::new(cur_tx),
stop: end_rx, stop: end_rx,
stop_control: end_tx, controller: CursorController(Arc::new(controller)),
} }
} }
} }
@ -42,24 +44,20 @@ impl ControllerWorker<Cursor> for CursorWorker {
type Tx = mpsc::Sender<CursorPosition>; type Tx = mpsc::Sender<CursorPosition>;
type Rx = Streaming<CursorEvent>; type Rx = Streaming<CursorEvent>;
fn subscribe(&self) -> CursorController { fn controller(&self) -> CursorController {
CursorController::new( self.controller.clone()
self.producer.clone(),
Mutex::new(self.last_op.clone()),
Mutex::new(self.channel.subscribe()),
self.stop_control.clone(),
)
} }
async fn work(mut self, tx: Self::Tx, mut rx: Self::Rx) { async fn work(mut self, tx: Self::Tx, mut rx: Self::Rx) {
loop { loop {
tokio::select!{ tokio::select!{
biased;
Some(()) = self.stop.recv() => { break; },
Some(op) = self.op.recv() => { tx.send(op).await.unwrap_or_warn("could not update cursor"); },
Ok(Some(cur)) = rx.message() => { Ok(Some(cur)) = rx.message() => {
self.channel.send(cur.clone()).unwrap_or_warn("could not broadcast event"); self.channel.send(cur.clone()).unwrap_or_warn("could not broadcast event");
self.changed.send(cur).unwrap_or_warn("could not update last event"); self.changed.send(cur).unwrap_or_warn("could not update last event");
}, },
Some(op) = self.op.recv() => { tx.send(op).await.unwrap_or_warn("could not update cursor"); },
Some(()) = self.stop.recv() => { break; },
else => break, else => break,
} }
} }