feat(ffi): add get_attached (#621)

This commit is contained in:
Zixuan Chen 2025-01-16 10:54:24 +08:00 committed by GitHub
parent 2df24725df
commit a1e81eece0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 53 additions and 1 deletions

View file

@ -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<Arc<LoroCounter>> {
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()

View file

@ -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<Arc<LoroList>> {
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<dyn LoroValueLike>) -> LoroResult<()> {
self.list.insert(pos as usize, v.as_loro_value())

View file

@ -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<Arc<LoroMap>> {
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)

View file

@ -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<Arc<LoroMovableList>> {
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<dyn LoroValueLike>) -> LoroResult<()> {
self.list.insert(pos as usize, v.as_loro_value())

View file

@ -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<Arc<LoroText>> {
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()

View file

@ -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<Arc<LoroTree>> {
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.