feat: callback gets the controller itself as arg

This commit is contained in:
əlemi 2024-08-15 20:41:53 +02:00
parent c0d586df90
commit a8ce56bcc0
Signed by: alemi
GPG key ID: A4895B84D311642C
5 changed files with 16 additions and 16 deletions

View file

@ -45,7 +45,7 @@ pub trait Controller<T : Sized + Send + Sync> : Sized + Send + Sync {
} }
} }
fn callback(&self, cb: ControllerCallback); fn callback(&self, cb: ControllerCallback<Self>);
fn clear_callback(&self); fn clear_callback(&self);
@ -71,22 +71,22 @@ pub trait Controller<T : Sized + Send + Sync> : Sized + Send + Sync {
/// type wrapper for Boxed dyn callback /// type wrapper for Boxed dyn callback
pub struct ControllerCallback(Box<dyn Sync + Send + Fn()>); pub struct ControllerCallback<T>(Box<dyn Sync + Send + Fn(T)>);
impl ControllerCallback { impl<T> ControllerCallback<T> {
pub fn call(&self) { pub fn call(&self, x: T) {
self.0() // lmao at this syntax self.0(x) // lmao at this syntax
} }
} }
impl std::fmt::Debug for ControllerCallback { impl<T> std::fmt::Debug for ControllerCallback<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "ControllerCallback {{ {:p} }}", self.0) write!(f, "ControllerCallback {{ {:p} }}", self.0)
} }
} }
impl<T: Sync + Send + Fn() + 'static> From<T> for ControllerCallback { impl<T, X: Sync + Send + Fn(T) + 'static> From<X> for ControllerCallback<T> {
fn from(value: T) -> Self { fn from(value: X) -> Self {
Self(Box::new(value)) Self(Box::new(value))
} }
} }

View file

@ -53,7 +53,7 @@ pub(crate) struct BufferControllerInner {
pub(crate) stopper: mpsc::UnboundedSender<()>, // just exist pub(crate) stopper: mpsc::UnboundedSender<()>, // just exist
pub(crate) content_request: mpsc::Sender<oneshot::Sender<String>>, pub(crate) content_request: mpsc::Sender<oneshot::Sender<String>>,
pub(crate) delta_request: mpsc::Sender<(LocalVersion, oneshot::Sender<(LocalVersion, TextChange)>)>, pub(crate) delta_request: mpsc::Sender<(LocalVersion, oneshot::Sender<(LocalVersion, TextChange)>)>,
pub(crate) callback: watch::Sender<Option<ControllerCallback>>, pub(crate) callback: watch::Sender<Option<ControllerCallback<BufferController>>>,
} }
#[async_trait] #[async_trait]
@ -98,7 +98,7 @@ impl Controller<TextChange> for BufferController {
Ok(()) Ok(())
} }
fn callback(&self, cb: ControllerCallback) { fn callback(&self, cb: ControllerCallback<BufferController>) {
if self.0.callback.send(Some(cb)).is_err() { if self.0.callback.send(Some(cb)).is_err() {
// TODO should we panic? we failed what we were supposed to do // TODO should we panic? we failed what we were supposed to do
tracing::error!("no active buffer worker to run registered callback!"); tracing::error!("no active buffer worker to run registered callback!");

View file

@ -24,7 +24,7 @@ pub(crate) struct BufferWorker {
delta_req: mpsc::Receiver<(LocalVersion, oneshot::Sender<(LocalVersion, TextChange)>)>, delta_req: mpsc::Receiver<(LocalVersion, oneshot::Sender<(LocalVersion, TextChange)>)>,
stop: mpsc::UnboundedReceiver<()>, stop: mpsc::UnboundedReceiver<()>,
controller: BufferController, controller: BufferController,
callback: watch::Receiver<Option<ControllerCallback>>, callback: watch::Receiver<Option<ControllerCallback<BufferController>>>,
} }
impl BufferWorker { impl BufferWorker {
@ -135,7 +135,7 @@ impl ControllerWorker<TextChange> for BufferWorker {
tx.send(()).unwrap_or_warn("could not wake up poller"); tx.send(()).unwrap_or_warn("could not wake up poller");
} }
if let Some(cb) = self.callback.borrow().as_ref() { if let Some(cb) = self.callback.borrow().as_ref() {
cb.call(); // TODO should we run this on another task/thread? cb.call(self.controller.clone()); // TODO should we run this on another task/thread?
} }
}, },
Err(e) => tracing::error!("could not deserialize operation from server: {}", e), Err(e) => tracing::error!("could not deserialize operation from server: {}", e),

View file

@ -29,7 +29,7 @@ pub(crate) struct CursorControllerInner {
pub(crate) op: mpsc::Sender<CursorPosition>, pub(crate) op: mpsc::Sender<CursorPosition>,
pub(crate) last_op: Mutex<watch::Receiver<CursorEvent>>, pub(crate) last_op: Mutex<watch::Receiver<CursorEvent>>,
pub(crate) stream: Mutex<broadcast::Receiver<CursorEvent>>, pub(crate) stream: Mutex<broadcast::Receiver<CursorEvent>>,
pub(crate) callback: watch::Sender<Option<ControllerCallback>>, pub(crate) callback: watch::Sender<Option<ControllerCallback<CursorController>>>,
pub(crate) stop: mpsc::UnboundedSender<()>, pub(crate) stop: mpsc::UnboundedSender<()>,
} }
@ -63,7 +63,7 @@ impl Controller<Cursor> for CursorController {
Ok(self.0.last_op.lock().await.changed().await?) Ok(self.0.last_op.lock().await.changed().await?)
} }
fn callback(&self, cb: ControllerCallback) { fn callback(&self, cb: ControllerCallback<CursorController>) {
if self.0.callback.send(Some(cb)).is_err() { if self.0.callback.send(Some(cb)).is_err() {
// TODO should we panic? we failed what we were supposed to do // TODO should we panic? we failed what we were supposed to do
tracing::error!("no active cursor worker to run registered callback!"); tracing::error!("no active cursor worker to run registered callback!");

View file

@ -14,7 +14,7 @@ pub(crate) struct CursorWorker {
channel: broadcast::Sender<CursorEvent>, channel: broadcast::Sender<CursorEvent>,
stop: mpsc::UnboundedReceiver<()>, stop: mpsc::UnboundedReceiver<()>,
controller: CursorController, controller: CursorController,
callback: watch::Receiver<Option<ControllerCallback>>, callback: watch::Receiver<Option<ControllerCallback<CursorController>>>,
} }
impl Default for CursorWorker { impl Default for CursorWorker {
@ -62,7 +62,7 @@ impl ControllerWorker<Cursor> for CursorWorker {
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");
if let Some(cb) = self.callback.borrow().as_ref() { if let Some(cb) = self.callback.borrow().as_ref() {
cb.call(); // TODO should this run in its own task/thread? cb.call(self.controller.clone()); // TODO should this run in its own task/thread?
} }
}, },
else => break, else => break,