Skip to content

Commit 798e478

Browse files
committed
Update yrs v0.18.0
1 parent 87d9a5c commit 798e478

File tree

5 files changed

+36
-27
lines changed

5 files changed

+36
-27
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ name = "pycrdt"
88
crate-type = ["cdylib"]
99

1010
[dependencies]
11-
yrs = "0.17.4"
11+
yrs = "0.18.0"
1212

1313
[dependencies.pyo3]
1414
version = "0.20.2"

src/array.rs

+11-8
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use yrs::{
88
Doc as _Doc,
99
DeepObservable,
1010
Observable,
11+
Subscription,
1112
TransactionMut,
1213
};
1314
use yrs::types::ToJson;
@@ -124,40 +125,42 @@ impl Array {
124125
}
125126

126127
pub fn observe(&mut self, f: PyObject) -> PyResult<u32> {
127-
let id: u32 = self.array
128+
let sub = self.array
128129
.observe(move |txn, e| {
129130
Python::with_gil(|py| {
130131
let event = ArrayEvent::new(e, txn);
131132
if let Err(err) = f.call1(py, (event,)) {
132133
err.restore(py)
133134
}
134135
})
135-
})
136-
.into();
136+
});
137+
let id: u32 = (&sub as *const Subscription) as u32;
137138
Ok(id)
138139
}
139140

140141
pub fn observe_deep(&mut self, f: PyObject) -> PyResult<u32> {
141-
let id: u32 = self.array
142+
let sub = self.array
142143
.observe_deep(move |txn, events| {
143144
Python::with_gil(|py| {
144145
let events = events_into_py(txn, events);
145146
if let Err(err) = f.call1(py, (events,)) {
146147
err.restore(py)
147148
}
148149
})
149-
})
150-
.into();
150+
});
151+
let id: u32 = (&sub as *const Subscription) as u32;
151152
Ok(id)
152153
}
153154

154155
pub fn unobserve(&mut self, subscription_id: u32) -> PyResult<()> {
155-
self.array.unobserve(subscription_id);
156+
let sub = subscription_id as *mut Subscription;
157+
drop(unsafe { Box::from_raw(sub) });
156158
Ok(())
157159
}
158160

159161
pub fn unobserve_deep(&mut self, subscription_id: u32) -> PyResult<()> {
160-
self.array.unobserve_deep(subscription_id);
162+
let sub = subscription_id as *mut Subscription;
163+
drop(unsafe { Box::from_raw(sub) });
161164
Ok(())
162165
}
163166
}

src/doc.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use yrs::{
88
TransactionCleanupEvent,
99
SubdocsEvent as _SubdocsEvent,
1010
StateVector,
11+
Subscription,
1112
Update,
1213
};
1314
use yrs::updates::encoder::Encode;
@@ -113,7 +114,7 @@ impl Doc {
113114
}
114115

115116
pub fn observe(&mut self, f: PyObject) -> PyResult<u32> {
116-
let id: u32 = self.doc
117+
let sub = self.doc
117118
.observe_transaction_cleanup(move |txn, event| {
118119
Python::with_gil(|py| {
119120
let event = TransactionEvent::new(event, txn);
@@ -122,13 +123,13 @@ impl Doc {
122123
}
123124
})
124125
})
125-
.unwrap()
126-
.into();
126+
.unwrap();
127+
let id: u32 = (&sub as *const Subscription) as u32;
127128
Ok(id)
128129
}
129130

130131
pub fn observe_subdocs(&mut self, f: PyObject) -> PyResult<u32> {
131-
let id: u32 = self.doc
132+
let sub = self.doc
132133
.observe_subdocs(move |_, event| {
133134
Python::with_gil(|py| {
134135
let event = SubdocsEvent::new(event);
@@ -137,8 +138,8 @@ impl Doc {
137138
}
138139
})
139140
})
140-
.unwrap()
141-
.into();
141+
.unwrap();
142+
let id: u32 = (&sub as *const Subscription) as u32;
142143
Ok(id)
143144
}
144145
}

src/map.rs

+11-8
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use yrs::{
88
Map as _Map,
99
DeepObservable,
1010
Observable,
11+
Subscription,
1112
TransactionMut,
1213
};
1314
use yrs::types::ToJson;
@@ -129,40 +130,42 @@ impl Map {
129130
}
130131

131132
pub fn observe(&mut self, f: PyObject) -> PyResult<u32> {
132-
let id: u32 = self.map
133+
let sub = self.map
133134
.observe(move |txn, e| {
134135
Python::with_gil(|py| {
135136
let e = MapEvent::new(e, txn);
136137
if let Err(err) = f.call1(py, (e,)) {
137138
err.restore(py)
138139
}
139140
})
140-
})
141-
.into();
141+
});
142+
let id: u32 = (&sub as *const Subscription) as u32;
142143
Ok(id)
143144
}
144145

145146
pub fn observe_deep(&mut self, f: PyObject) -> PyResult<u32> {
146-
let id: u32 = self.map
147+
let sub = self.map
147148
.observe_deep(move |txn, events| {
148149
Python::with_gil(|py| {
149150
let events = events_into_py(txn, events);
150151
if let Err(err) = f.call1(py, (events,)) {
151152
err.restore(py)
152153
}
153154
})
154-
})
155-
.into();
155+
});
156+
let id: u32 = (&sub as *const Subscription) as u32;
156157
Ok(id)
157158
}
158159

159160
pub fn unobserve(&mut self, subscription_id: u32) -> PyResult<()> {
160-
self.map.unobserve(subscription_id);
161+
let sub = subscription_id as *mut Subscription;
162+
drop(unsafe { Box::from_raw(sub) });
161163
Ok(())
162164
}
163165

164166
pub fn unobserve_deep(&mut self, subscription_id: u32) -> PyResult<()> {
165-
self.map.unobserve_deep(subscription_id);
167+
let sub = subscription_id as *mut Subscription;
168+
drop(unsafe { Box::from_raw(sub) });
166169
Ok(())
167170
}
168171
}

src/text.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use pyo3::types::{PyList, PyString};
33
use yrs::{
44
GetString,
55
Observable,
6+
Subscription,
67
TextRef,
78
Text as _Text,
89
TransactionMut,
@@ -58,20 +59,21 @@ impl Text {
5859
}
5960

6061
fn observe(&mut self, f: PyObject) -> PyResult<u32> {
61-
let id: u32 = self.text.observe(move |txn, e| {
62+
let sub = self.text.observe(move |txn, e| {
6263
Python::with_gil(|py| {
6364
let e = TextEvent::new(e, txn);
6465
if let Err(err) = f.call1(py, (e,)) {
6566
err.restore(py)
6667
}
6768
});
68-
})
69-
.into();
69+
});
70+
let id: u32 = (&sub as *const Subscription) as u32;
7071
Ok(id)
7172
}
7273

7374
fn unobserve(&self, subscription_id: u32) -> PyResult<()> {
74-
self.text.unobserve(subscription_id);
75+
let sub = subscription_id as *mut Subscription;
76+
drop(unsafe { Box::from_raw(sub) });
7577
Ok(())
7678
}
7779
}

0 commit comments

Comments
 (0)