Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding has_edits Flag to YRoomMessage #2

Merged
merged 6 commits into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Changelog

## Unreleased - 11.9.2023

### Added

- `YRoomMessage.has_edits: bool` flag indicating data changes have occurred.

## v0.0.8
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,11 @@ pip install yroom
## API

See `yroom.pyi`.

## Development

[Maturin](https://www.maturin.rs) can be used to build `Yroom` inside a virtual environment.

```
maturin develop
```
19 changes: 13 additions & 6 deletions src/roomsync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,33 +235,36 @@ pub struct YRoomMessage {
pub payloads: PyObject,
#[pyo3(get)]
pub broadcast_payloads: PyObject,
#[pyo3(get)]
pub has_edits: PyObject,
}

fn make_payloads(py: Python, payloads: &[Vec<u8>]) -> PyObject {
PyList::new(py, payloads.iter().map(|payload| PyBytes::new(py, payload))).into()
}

impl YRoomMessage {
fn from_payloads(payloads: &[Vec<u8>], broadcast_payloads: &[Vec<u8>]) -> YRoomMessage {
fn from_payloads(payloads: &[Vec<u8>], broadcast_payloads: &[Vec<u8>], has_edits: bool) -> YRoomMessage {
Python::with_gil(|py| YRoomMessage {
payloads: make_payloads(py, payloads),
broadcast_payloads: make_payloads(py, broadcast_payloads),
has_edits: has_edits.into_py(py),
})
}
}

impl Default for YRoomMessage {
fn default() -> Self {
YRoomMessage::from_payloads(&[], &[])
YRoomMessage::from_payloads(&[], &[], false)
}
}

#[pymethods]
impl YRoomMessage {
pub fn __str__(&self) -> String {
format!(
"YRoomMessage(payloads: {}, broadcast_payloads: {})",
self.payloads, self.broadcast_payloads
"YRoomMessage(payloads: {}, broadcast_payloads: {}, has_edits: {})",
self.payloads, self.broadcast_payloads, self.has_edits
)
}

Expand Down Expand Up @@ -360,7 +363,7 @@ impl YRoomManager {

pub fn disconnect(&mut self, room: String, conn_id: u64) -> YRoomMessage {
let broadcast_payload = self.get_room(&room).disconnect(conn_id);
YRoomMessage::from_payloads(&[], &broadcast_payload)
YRoomMessage::from_payloads(&[], &broadcast_payload, false)
}

pub fn has_room(&self, room: String) -> bool {
Expand Down Expand Up @@ -534,6 +537,7 @@ impl YRoom {
Python::with_gil(|py| YRoomMessage {
payloads: make_payloads(py, &payloads),
broadcast_payloads: make_payloads(py, &[]),
has_edits: false.into_py(py),
})
}

Expand Down Expand Up @@ -563,6 +567,7 @@ impl YRoom {
self.settings.disable_pipelining,
decoder.document_name.clone(),
);
let mut has_edits = false;

decoder.for_each(|message_result| match message_result {
Ok(message) => match message {
Expand Down Expand Up @@ -598,6 +603,7 @@ impl YRoom {
}
Err(e) => log::error!("Error decoding sync step 2: {}", e),
}
has_edits = true;
}
Message::Sync(SyncMessage::Update(data)) => {
let update = Update::decode_v1(&data);
Expand All @@ -610,6 +616,7 @@ impl YRoom {
}
Err(e) => log::error!("Error decoding update: {}", e),
}
has_edits = true;
}
Message::Auth(_) => {
// TODO: check this. Always reply with permission granted
Expand Down Expand Up @@ -657,7 +664,7 @@ impl YRoom {
log::warn!("Bad message from connection {}: {:?}", conn_id, err);
}
});
YRoomMessage::from_payloads(&sync_encoder.to_vecs(), &update_encoder.to_vecs())
YRoomMessage::from_payloads(&sync_encoder.to_vecs(), &update_encoder.to_vecs(), has_edits)
}

pub fn disconnect(&mut self, conn_id: u64) -> Vec<Vec<u8>> {
Expand Down
2 changes: 2 additions & 0 deletions yroom.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ class YRoomMessage:
`payload` is a message that should be sent to the connection that sent the message.
`broadcast_payload` is a message that should be sent to all connections in the room.
Either or both of the members can be of zero length and then must not be sent.
`has_edits` flag indicates if data changes occurred. Does not include awareness updates.
"""

payloads: List[bytes]
broadcast_payloads: List[bytes]
has_edits: bool

class YRoomSettings(TypedDict):
wire_version: int # The Yjs encoding/decoding version to use (1 or 2, default: 1)
Expand Down
Loading