From a1e81eece085dd1ed9f11f6f80651b75ae54e004 Mon Sep 17 00:00:00 2001 From: Zixuan Chen Date: Thu, 16 Jan 2025 10:54:24 +0800 Subject: [PATCH] feat(ffi): add get_attached (#621) --- crates/loro-ffi/src/container/counter.rs | 17 +++++++++++++++++ crates/loro-ffi/src/container/list.rs | 7 +++++++ crates/loro-ffi/src/container/map.rs | 7 +++++++ crates/loro-ffi/src/container/movable_list.rs | 7 +++++++ crates/loro-ffi/src/container/text.rs | 9 ++++++++- crates/loro-ffi/src/container/tree.rs | 7 +++++++ 6 files changed, 53 insertions(+), 1 deletion(-) diff --git a/crates/loro-ffi/src/container/counter.rs b/crates/loro-ffi/src/container/counter.rs index 0f206f75..c3972e75 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 088c209c..9132a94c 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 62cbd519..e41217c2 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 90ad264d..adb7c999 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 5fcc9442..439f52fd 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 53ba3f30..1be08435 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.