diff --git a/crates/loro-ffi/src/container/counter.rs b/crates/loro-ffi/src/container/counter.rs index 0f206f750..c3972e75c 100644 --- a/crates/loro-ffi/src/container/counter.rs +++ b/crates/loro-ffi/src/container/counter.rs @@ -1,3 +1,5 @@ +use std::sync::Arc; + use loro::{ContainerTrait, LoroResult}; use crate::ContainerID; @@ -14,6 +16,21 @@ impl LoroCounter { } } + /// Whether the container is attached to a document + /// + /// The edits on a detached container will not be persisted. + /// To attach the container to the document, please insert it into an attached container. + pub fn is_attached(&self) -> bool { + self.counter.is_attached() + } + + /// If a detached container is attached, this method will return its corresponding attached handler. + pub fn get_attached(&self) -> Option> { + self.counter + .get_attached() + .map(|x| Arc::new(LoroCounter { counter: x })) + } + /// Return container id of the Counter. pub fn id(&self) -> ContainerID { self.counter.id().into() diff --git a/crates/loro-ffi/src/container/list.rs b/crates/loro-ffi/src/container/list.rs index 088c209c9..9132a94c8 100644 --- a/crates/loro-ffi/src/container/list.rs +++ b/crates/loro-ffi/src/container/list.rs @@ -26,6 +26,13 @@ impl LoroList { self.list.is_attached() } + /// If a detached container is attached, this method will return its corresponding attached handler. + pub fn get_attached(&self) -> Option> { + self.list + .get_attached() + .map(|x| Arc::new(LoroList { list: x })) + } + /// Insert a value at the given position. pub fn insert(&self, pos: u32, v: Arc) -> LoroResult<()> { self.list.insert(pos as usize, v.as_loro_value()) diff --git a/crates/loro-ffi/src/container/map.rs b/crates/loro-ffi/src/container/map.rs index 62cbd5197..e41217c28 100644 --- a/crates/loro-ffi/src/container/map.rs +++ b/crates/loro-ffi/src/container/map.rs @@ -22,6 +22,13 @@ impl LoroMap { self.map.is_attached() } + /// If a detached container is attached, this method will return its corresponding attached handler. + pub fn get_attached(&self) -> Option> { + self.map + .get_attached() + .map(|x| Arc::new(LoroMap { map: x })) + } + /// Delete a key-value pair from the map. pub fn delete(&self, key: &str) -> LoroResult<()> { self.map.delete(key) diff --git a/crates/loro-ffi/src/container/movable_list.rs b/crates/loro-ffi/src/container/movable_list.rs index 90ad264d6..adb7c9997 100644 --- a/crates/loro-ffi/src/container/movable_list.rs +++ b/crates/loro-ffi/src/container/movable_list.rs @@ -31,6 +31,13 @@ impl LoroMovableList { self.list.is_attached() } + /// If a detached container is attached, this method will return its corresponding attached handler. + pub fn get_attached(&self) -> Option> { + self.list + .get_attached() + .map(|x| Arc::new(LoroMovableList { list: x })) + } + /// Insert a value at the given position. pub fn insert(&self, pos: u32, v: Arc) -> LoroResult<()> { self.list.insert(pos as usize, v.as_loro_value()) diff --git a/crates/loro-ffi/src/container/text.rs b/crates/loro-ffi/src/container/text.rs index 5fcc9442c..439f52fdc 100644 --- a/crates/loro-ffi/src/container/text.rs +++ b/crates/loro-ffi/src/container/text.rs @@ -1,6 +1,6 @@ use std::{fmt::Display, sync::Arc}; -use loro::{cursor::Side, LoroResult, PeerID, UpdateOptions, UpdateTimeoutError}; +use loro::{cursor::Side, ContainerTrait, LoroResult, PeerID, UpdateOptions, UpdateTimeoutError}; use loro_internal::handler::TextDelta as InternalTextDelta; use crate::{ContainerID, LoroValue, LoroValueLike, TextDelta}; @@ -31,6 +31,13 @@ impl LoroText { self.text.is_attached() } + /// If a detached container is attached, this method will return its corresponding attached handler. + pub fn get_attached(&self) -> Option> { + self.text + .get_attached() + .map(|x| Arc::new(LoroText { text: x })) + } + /// Get the [ContainerID] of the text container. pub fn id(&self) -> ContainerID { self.text.id().into() diff --git a/crates/loro-ffi/src/container/tree.rs b/crates/loro-ffi/src/container/tree.rs index 53ba3f30e..1be084350 100644 --- a/crates/loro-ffi/src/container/tree.rs +++ b/crates/loro-ffi/src/container/tree.rs @@ -33,6 +33,13 @@ impl LoroTree { self.tree.is_attached() } + /// If a detached container is attached, this method will return its corresponding attached handler. + pub fn get_attached(&self) -> Option> { + self.tree + .get_attached() + .map(|x| Arc::new(LoroTree { tree: x })) + } + /// Create a new tree node and return the [`TreeID`]. /// /// If the `parent` is `None`, the created node is the root of a tree.