Skip to content

Commit

Permalink
feat(ffi): add get_attached (#621)
Browse files Browse the repository at this point in the history
  • Loading branch information
zxch3n authored Jan 16, 2025
1 parent 2df2472 commit a1e81ee
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 1 deletion.
17 changes: 17 additions & 0 deletions crates/loro-ffi/src/container/counter.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::sync::Arc;

use loro::{ContainerTrait, LoroResult};

use crate::ContainerID;
Expand All @@ -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()
Expand Down
7 changes: 7 additions & 0 deletions crates/loro-ffi/src/container/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
7 changes: 7 additions & 0 deletions crates/loro-ffi/src/container/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions crates/loro-ffi/src/container/movable_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
9 changes: 8 additions & 1 deletion crates/loro-ffi/src/container/text.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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()
Expand Down
7 changes: 7 additions & 0 deletions crates/loro-ffi/src/container/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit a1e81ee

Please sign in to comment.