|
1 |
| -use std::cell::RefCell; |
2 |
| -use std::convert::{TryFrom, TryInto}; |
3 |
| -use std::rc::Rc; |
4 |
| - |
5 | 1 | use crate::json_builder::JsonBuilder;
|
6 | 2 | use crate::shared_types::{
|
7 |
| - CompatiblePyType, DeepSubscription, DefaultPyErr, PreliminaryObservationException, |
8 |
| - ShallowSubscription, SubId, TypeWithDoc, |
| 3 | + CompatiblePyType, DefaultPyErr, ObservationId, PreliminaryObservationException, TypeWithDoc, |
9 | 4 | };
|
10 | 5 | use crate::type_conversions::{events_into_py, WithDocToPython};
|
11 | 6 | use crate::y_doc::{WithDoc, YDocInner};
|
12 | 7 | use crate::y_transaction::{YTransaction, YTransactionInner};
|
| 8 | +use std::cell::RefCell; |
| 9 | +use std::clone::Clone; |
| 10 | +use std::convert::{TryFrom, TryInto}; |
| 11 | +use std::rc::Rc; |
| 12 | +use std::string::ToString; |
13 | 13 |
|
14 | 14 | use super::shared_types::SharedType;
|
15 | 15 | use crate::type_conversions::ToPython;
|
16 |
| -use lib0::any::Any; |
17 | 16 | use pyo3::exceptions::PyIndexError;
|
18 | 17 |
|
19 | 18 | use crate::type_conversions::PyObjectWrapper;
|
20 | 19 | use pyo3::prelude::*;
|
21 | 20 | use pyo3::types::{PyList, PySlice, PySliceIndices};
|
22 | 21 | use yrs::types::array::ArrayEvent;
|
23 | 22 | use yrs::types::{DeepObservable, ToJson};
|
24 |
| -use yrs::{Array, ArrayRef, Assoc, Observable, SubscriptionId, TransactionMut}; |
| 23 | +use yrs::{Any, Array, ArrayRef, Assoc, Observable, TransactionMut}; |
25 | 24 |
|
26 | 25 | /// A collection used to store data in an indexed sequence structure. This type is internally
|
27 | 26 | /// implemented as a double linked list, which may squash values inserted directly one after another
|
@@ -365,59 +364,55 @@ impl YArray {
|
365 | 364 |
|
366 | 365 | /// Subscribes to all operations happening over this instance of `YArray`. All changes are
|
367 | 366 | /// batched and eventually triggered during transaction commit phase.
|
368 |
| - /// Returns a `SubscriptionId` which can be used to cancel the callback with `unobserve`. |
369 |
| - pub fn observe(&mut self, f: PyObject) -> PyResult<ShallowSubscription> { |
| 367 | + /// Returns a `ObservationId` which can be used to cancel the callback with `unobserve`. |
| 368 | + pub fn observe(&mut self, f: PyObject) -> PyResult<ObservationId> { |
370 | 369 | match &mut self.0 {
|
371 | 370 | SharedType::Integrated(array) => {
|
372 | 371 | let doc = array.doc.clone();
|
373 |
| - let sub: SubscriptionId = array |
374 |
| - .inner |
375 |
| - .observe(move |txn, e| { |
376 |
| - Python::with_gil(|py| { |
377 |
| - let event = YArrayEvent::new(e, txn, doc.clone()); |
378 |
| - if let Err(err) = f.call1(py, (event,)) { |
379 |
| - err.restore(py) |
380 |
| - } |
381 |
| - }) |
| 372 | + let subscription = array.inner.observe(move |txn, e| { |
| 373 | + Python::with_gil(|py| { |
| 374 | + let event = YArrayEvent::new(e, txn, doc.clone()); |
| 375 | + if let Err(err) = f.call1(py, (event,)) { |
| 376 | + err.restore(py) |
| 377 | + } |
382 | 378 | })
|
383 |
| - .into(); |
384 |
| - Ok(ShallowSubscription(sub)) |
| 379 | + }); |
| 380 | + Ok(ObservationId(subscription)) |
385 | 381 | }
|
386 | 382 | SharedType::Prelim(_) => Err(PreliminaryObservationException::default_message()),
|
387 | 383 | }
|
388 | 384 | }
|
389 | 385 | /// Observes YArray events and events of all child elements.
|
390 |
| - pub fn observe_deep(&mut self, f: PyObject) -> PyResult<DeepSubscription> { |
| 386 | + pub fn observe_deep(&mut self, f: PyObject) -> PyResult<ObservationId> { |
391 | 387 | match &mut self.0 {
|
392 | 388 | SharedType::Integrated(array) => {
|
393 | 389 | let doc = array.doc.clone();
|
394 |
| - let sub: SubscriptionId = array |
395 |
| - .inner |
396 |
| - .observe_deep(move |txn, events| { |
397 |
| - Python::with_gil(|py| { |
398 |
| - let events = events_into_py(txn, events, doc.clone()); |
399 |
| - if let Err(err) = f.call1(py, (events,)) { |
400 |
| - err.restore(py) |
401 |
| - } |
402 |
| - }) |
| 390 | + let subscription = array.inner.observe_deep(move |txn, events| { |
| 391 | + Python::with_gil(|py| { |
| 392 | + let events = events_into_py(txn, events, doc.clone()); |
| 393 | + if let Err(err) = f.call1(py, (events,)) { |
| 394 | + err.restore(py) |
| 395 | + } |
403 | 396 | })
|
404 |
| - .into(); |
405 |
| - Ok(DeepSubscription(sub)) |
| 397 | + }); |
| 398 | + Ok(ObservationId(subscription)) |
406 | 399 | }
|
407 | 400 | SharedType::Prelim(_) => Err(PreliminaryObservationException::default_message()),
|
408 | 401 | }
|
409 | 402 | }
|
410 | 403 |
|
411 |
| - /// Cancels the callback of an observer using the Subscription ID returned from the `observe` method. |
412 |
| - pub fn unobserve(&mut self, subscription_id: SubId) -> PyResult<()> { |
| 404 | + /// Cancels the callback of an observer using the `observation_d` returned from the `observe` method. |
| 405 | + pub fn unobserve(&mut self, observation_d: ObservationId) -> PyResult<()> { |
413 | 406 | match &mut self.0 {
|
414 |
| - SharedType::Integrated(arr) => { |
415 |
| - match subscription_id { |
416 |
| - SubId::Shallow(ShallowSubscription(id)) => arr.unobserve(id), |
417 |
| - SubId::Deep(DeepSubscription(id)) => arr.unobserve_deep(id), |
418 |
| - } |
419 |
| - Ok(()) |
420 |
| - } |
| 407 | + SharedType::Integrated(_) => Ok(drop(observation_d.0)), |
| 408 | + SharedType::Prelim(_) => Err(PreliminaryObservationException::default_message()), |
| 409 | + } |
| 410 | + } |
| 411 | + |
| 412 | + /// Cancels the callback of an observer using the `observation_d` returned from the `observe_deep` method. |
| 413 | + pub fn unobserve_deep(&mut self, observation_d: ObservationId) -> PyResult<()> { |
| 414 | + match &mut self.0 { |
| 415 | + SharedType::Integrated(_) => Ok(drop(observation_d.0)), |
421 | 416 | SharedType::Prelim(_) => Err(PreliminaryObservationException::default_message()),
|
422 | 417 | }
|
423 | 418 | }
|
|
0 commit comments