Skip to content

Commit

Permalink
Merge pull request #2 from atheler/feat/has-edits-flag
Browse files Browse the repository at this point in the history
Adding has_edits Flag to YRoomMessage
  • Loading branch information
stefanw authored Sep 27, 2023
2 parents 15568f8 + c0a8625 commit 631c174
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 6 deletions.
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

0 comments on commit 631c174

Please sign in to comment.