mirror of
https://github.com/hexedtech/codemp.git
synced 2024-11-22 15:24:48 +01:00
feat: callback gets the controller itself as arg
This commit is contained in:
parent
c0d586df90
commit
a8ce56bcc0
5 changed files with 16 additions and 16 deletions
|
@ -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);
|
||||
|
||||
|
@ -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))
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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]
|
||||
|
@ -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!");
|
||||
|
|
|
@ -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 {
|
||||
|
@ -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),
|
||||
|
|
|
@ -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<()>,
|
||||
}
|
||||
|
||||
|
@ -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!");
|
||||
|
|
|
@ -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 {
|
||||
|
@ -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,
|
||||
|
|
Loading…
Reference in a new issue