Skip to content

Commit 4db69bd

Browse files
Ignore empty updates (#107)
1 parent aca7e16 commit 4db69bd

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

src/doc.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,14 @@ impl Doc {
116116
pub fn observe(&mut self, py: Python<'_>, f: PyObject) -> PyResult<Py<Subscription>> {
117117
let sub = self.doc
118118
.observe_transaction_cleanup(move |txn, event| {
119-
Python::with_gil(|py| {
120-
let event = TransactionEvent::new(event, txn);
121-
if let Err(err) = f.call1(py, (event,)) {
122-
err.restore(py)
123-
}
124-
})
119+
if event.before_state != event.after_state {
120+
Python::with_gil(|py| {
121+
let event = TransactionEvent::new(event, txn);
122+
if let Err(err) = f.call1(py, (event,)) {
123+
err.restore(py)
124+
}
125+
})
126+
}
125127
})
126128
.unwrap();
127129
let s: Py<Subscription> = Py::new(py, Subscription::from(sub))?;

tests/test_doc.py

+12
Original file line numberDiff line numberDiff line change
@@ -179,3 +179,15 @@ def test_roots():
179179
# assert str(roots["a"]) == "foo"
180180
# assert list(roots["b"]) == [5, 2, 8]
181181
# assert dict(roots["c"]) == None # {"k1": 1, "k2": 2}
182+
183+
184+
def test_empty_update():
185+
doc = Doc()
186+
doc["text"] = Text()
187+
events = []
188+
sub = doc.observe(partial(callback, events)) # noqa: F841
189+
190+
# this triggers an empty update
191+
doc["text"]
192+
# empty updates should not emit an event
193+
assert not events

0 commit comments

Comments
 (0)