feat(python): added callback support, reaching feature parity with the lib.

This commit is contained in:
cschen 2024-08-22 15:22:51 +02:00
parent 4004f2011f
commit 472c33f734
2 changed files with 79 additions and 36 deletions

12
dist/py/codemp.pyi vendored
View file

@ -8,7 +8,7 @@ class Driver:
def stop(self) -> None: ... def stop(self) -> None: ...
def init(logger_cb: Callable, debug: bool) -> Driver: ... def init(logger_cb: Callable[[str], None], debug: bool) -> Driver: ...
class Promise[T]: class Promise[T]:
""" """
@ -50,6 +50,9 @@ class BufferController:
def try_recv(self) -> Optional[TextChange]: ... def try_recv(self) -> Optional[TextChange]: ...
def recv(self) -> Promise[TextChange]: ... def recv(self) -> Promise[TextChange]: ...
def poll(self) -> Promise[None]: ... def poll(self) -> Promise[None]: ...
def callback(self,
cb: Callable[[BufferController], None]) -> None: ...
def clear_callback(self) -> None: ...
def stop(self) -> bool: ... def stop(self) -> bool: ...
@ -76,6 +79,9 @@ class CursorController:
def try_recv(self) -> Optional[Cursor]: ... def try_recv(self) -> Optional[Cursor]: ...
def recv(self) -> Promise[Cursor]: ... def recv(self) -> Promise[Cursor]: ...
def poll(self) -> Promise[None]: ... def poll(self) -> Promise[None]: ...
def callback(self,
cb: Callable[[CursorController], None]) -> None: ...
def clear_callback(self) -> None: ...
def stop(self) -> bool: ... def stop(self) -> bool: ...
@ -103,7 +109,9 @@ class Client:
Handle to the actual client that manages the session. It manages the connection Handle to the actual client that manages the session. It manages the connection
to a server and joining/creating new workspaces to a server and joining/creating new workspaces
""" """
def __new__(cls, host: str, username: str, password: str) -> None: ... def __new__(cls,
host: str,
username: str, password: str) -> Client: ...
def join_workspace(self, workspace: str) -> Promise[Workspace]: ... def join_workspace(self, workspace: str) -> Promise[Workspace]: ...
def leave_workspace(self, workspace: str) -> bool: ... def leave_workspace(self, workspace: str) -> bool: ...
def get_workspace(self, id: str) -> Workspace: ... def get_workspace(self, id: str) -> Workspace: ...

View file

@ -4,6 +4,7 @@ use crate::api::TextChange;
use crate::buffer::Controller as BufferController; use crate::buffer::Controller as BufferController;
use crate::cursor::Controller as CursorController; use crate::cursor::Controller as CursorController;
use pyo3::prelude::*; use pyo3::prelude::*;
use pyo3::types::PyFunction;
use super::Promise; use super::Promise;
use crate::a_sync_allow_threads; use crate::a_sync_allow_threads;
@ -48,6 +49,23 @@ impl CursorController {
a_sync_allow_threads!(py, this.poll().await) a_sync_allow_threads!(py, this.poll().await)
} }
#[pyo3(name = "callback")]
fn pycallback(&self, py: Python, cb: Py<PyFunction>) {
py.allow_threads(move || {
self.callback(move |ctl| {
Python::with_gil(|py| {
// TODO what to do with this error?
let _ = cb.call1(py, (ctl,));
})
})
})
}
#[pyo3(name = "clear_callback")]
fn pyclear_callback(&self) {
self.clear_callback();
}
#[pyo3(name = "stop")] #[pyo3(name = "stop")]
fn pystop(&self) -> bool { fn pystop(&self) -> bool {
self.stop() self.stop()
@ -94,6 +112,23 @@ impl BufferController {
a_sync_allow_threads!(py, this.poll().await) a_sync_allow_threads!(py, this.poll().await)
} }
#[pyo3(name = "callback")]
fn pycallback(&self, py: Python, cb: Py<PyFunction>) {
py.allow_threads(move || {
self.callback(move |ctl| {
Python::with_gil(|py| {
// TODO what to do with this error?
let _ = cb.call1(py, (ctl,));
})
})
})
}
#[pyo3(name = "clear_callback")]
fn pyclear_callback(&self) {
self.clear_callback();
}
#[pyo3(name = "stop")] #[pyo3(name = "stop")]
fn pystop(&self) -> bool { fn pystop(&self) -> bool {
self.stop() self.stop()