Skip to content

Commit

Permalink
feat: callback gets the controller itself as arg
Browse files Browse the repository at this point in the history
  • Loading branch information
alemidev committed Aug 15, 2024
1 parent c0d586d commit a8ce56b
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 16 deletions.
16 changes: 8 additions & 8 deletions src/api/controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -71,22 +71,22 @@ pub trait Controller<T : Sized + Send + Sync> : Sized + Send + Sync {


/// 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 {
pub fn call(&self) {
self.0() // lmao at this syntax
impl<T> ControllerCallback<T> {
pub fn call(&self, x: T) {
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 {
write!(f, "ControllerCallback {{ {:p} }}", self.0)
}
}

impl<T: Sync + Send + Fn() + 'static> From<T> for ControllerCallback {
fn from(value: T) -> Self {
impl<T, X: Sync + Send + Fn(T) + 'static> From<X> for ControllerCallback<T> {
fn from(value: X) -> Self {
Self(Box::new(value))
}
}
4 changes: 2 additions & 2 deletions src/buffer/controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ pub(crate) struct BufferControllerInner {
pub(crate) stopper: mpsc::UnboundedSender<()>, // just exist
pub(crate) content_request: mpsc::Sender<oneshot::Sender<String>>,
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]
Expand Down Expand Up @@ -98,7 +98,7 @@ impl Controller<TextChange> for BufferController {
Ok(())
}

fn callback(&self, cb: ControllerCallback) {
fn callback(&self, cb: ControllerCallback<BufferController>) {
if self.0.callback.send(Some(cb)).is_err() {
// TODO should we panic? we failed what we were supposed to do
tracing::error!("no active buffer worker to run registered callback!");
Expand Down
4 changes: 2 additions & 2 deletions src/buffer/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub(crate) struct BufferWorker {
delta_req: mpsc::Receiver<(LocalVersion, oneshot::Sender<(LocalVersion, TextChange)>)>,
stop: mpsc::UnboundedReceiver<()>,
controller: BufferController,
callback: watch::Receiver<Option<ControllerCallback>>,
callback: watch::Receiver<Option<ControllerCallback<BufferController>>>,
}

impl BufferWorker {
Expand Down Expand Up @@ -135,7 +135,7 @@ impl ControllerWorker<TextChange> for BufferWorker {
tx.send(()).unwrap_or_warn("could not wake up poller");
}
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),
Expand Down
4 changes: 2 additions & 2 deletions src/cursor/controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub(crate) struct CursorControllerInner {
pub(crate) op: mpsc::Sender<CursorPosition>,
pub(crate) last_op: Mutex<watch::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<()>,
}

Expand Down Expand Up @@ -63,7 +63,7 @@ impl Controller<Cursor> for CursorController {
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() {
// TODO should we panic? we failed what we were supposed to do
tracing::error!("no active cursor worker to run registered callback!");
Expand Down
4 changes: 2 additions & 2 deletions src/cursor/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub(crate) struct CursorWorker {
channel: broadcast::Sender<CursorEvent>,
stop: mpsc::UnboundedReceiver<()>,
controller: CursorController,
callback: watch::Receiver<Option<ControllerCallback>>,
callback: watch::Receiver<Option<ControllerCallback<CursorController>>>,
}

impl Default for CursorWorker {
Expand Down Expand Up @@ -62,7 +62,7 @@ impl ControllerWorker<Cursor> for CursorWorker {
self.channel.send(cur.clone()).unwrap_or_warn("could not broadcast event");
self.changed.send(cur).unwrap_or_warn("could not update last event");
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,
Expand Down

0 comments on commit a8ce56b

Please sign in to comment.