Skip to content

Commit

Permalink
Ignore empty updates (#107)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbrochart authored May 6, 2024
1 parent aca7e16 commit 4db69bd
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,14 @@ impl Doc {
pub fn observe(&mut self, py: Python<'_>, f: PyObject) -> PyResult<Py<Subscription>> {
let sub = self.doc
.observe_transaction_cleanup(move |txn, event| {
Python::with_gil(|py| {
let event = TransactionEvent::new(event, txn);
if let Err(err) = f.call1(py, (event,)) {
err.restore(py)
}
})
if event.before_state != event.after_state {
Python::with_gil(|py| {
let event = TransactionEvent::new(event, txn);
if let Err(err) = f.call1(py, (event,)) {
err.restore(py)
}
})
}
})
.unwrap();
let s: Py<Subscription> = Py::new(py, Subscription::from(sub))?;
Expand Down
12 changes: 12 additions & 0 deletions tests/test_doc.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,3 +179,15 @@ def test_roots():
# assert str(roots["a"]) == "foo"
# assert list(roots["b"]) == [5, 2, 8]
# assert dict(roots["c"]) == None # {"k1": 1, "k2": 2}


def test_empty_update():
doc = Doc()
doc["text"] = Text()
events = []
sub = doc.observe(partial(callback, events)) # noqa: F841

# this triggers an empty update
doc["text"]
# empty updates should not emit an event
assert not events

0 comments on commit 4db69bd

Please sign in to comment.