diff --git a/crates/loro-ffi/src/doc.rs b/crates/loro-ffi/src/doc.rs index 22596433..851169ed 100644 --- a/crates/loro-ffi/src/doc.rs +++ b/crates/loro-ffi/src/doc.rs @@ -419,7 +419,7 @@ impl LoroDoc { callback.on_local_update(update.to_vec()); true })); - Arc::new(Subscription(Arc::new(Mutex::new(s)))) + Arc::new(Subscription(Mutex::new(Some(s)))) } /// Estimate the size of the document states in memory. @@ -684,30 +684,26 @@ pub trait Unsubscriber: Sync + Send { /// A handle to a subscription created by GPUI. When dropped, the subscription /// is cancelled and the callback will no longer be invoked. -pub struct Subscription(Arc>); +pub struct Subscription(Mutex>); impl Subscription { pub fn new(unsubscribe: Arc) -> Self { - Self(Arc::new(Mutex::new(loro::Subscription::new(move || { + Self(Mutex::new(Some(loro::Subscription::new(move || { unsubscribe.on_unsubscribe() })))) } pub fn detach(self: Arc) { - let s = Arc::try_unwrap(self) - .map_err(|_| "Arc::try_unwrap Subscription failed") - .unwrap() - .0; - let s = Arc::try_unwrap(s) - .map_err(|_| "Arc::try_unwrap Subscription failed") - .unwrap(); - s.into_inner().unwrap().detach(); + let s = self.0.try_lock().unwrap().take().unwrap(); + // let s = Arc::try_unwrap(self) + // .map_err(|_| "Arc::try_unwrap Subscription failed") + // .unwrap() + // .0; + + s.detach(); } } -unsafe impl Send for Subscription {} -unsafe impl Sync for Subscription {} - impl std::fmt::Debug for Subscription { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.write_str("Subscription") @@ -716,7 +712,7 @@ impl std::fmt::Debug for Subscription { impl From for Subscription { fn from(value: loro::Subscription) -> Self { - Self(Arc::new(Mutex::new(value))) + Self(Mutex::new(Some(value))) } }