2023-04-13 10:53:51 +00:00
|
|
|
use std::{
|
|
|
|
ops::{Deref, DerefMut},
|
|
|
|
sync::Arc,
|
|
|
|
};
|
|
|
|
|
2023-05-15 14:16:05 +00:00
|
|
|
use collab::core::collab::MutexCollab;
|
2023-05-16 06:58:24 +00:00
|
|
|
use collab_document::{blocks::DocumentData, document::Document as InnerDocument};
|
2023-04-13 10:53:51 +00:00
|
|
|
use parking_lot::Mutex;
|
|
|
|
|
2023-05-16 06:58:24 +00:00
|
|
|
use flowy_error::FlowyResult;
|
2023-04-13 10:53:51 +00:00
|
|
|
|
2023-05-16 06:58:24 +00:00
|
|
|
/// This struct wrap the document::Document
|
2023-04-13 10:53:51 +00:00
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct Document(Arc<Mutex<InnerDocument>>);
|
|
|
|
|
|
|
|
impl Document {
|
2023-05-16 06:58:24 +00:00
|
|
|
/// Creates and returns a new Document object.
|
|
|
|
/// # Arguments
|
|
|
|
/// * `collab` - the identifier of the collaboration instance
|
|
|
|
///
|
|
|
|
/// # Returns
|
|
|
|
/// * `Result<Document, FlowyError>` - a Result containing either a new Document object or an Error if the document creation failed
|
2023-05-15 14:16:05 +00:00
|
|
|
pub fn new(collab: Arc<MutexCollab>) -> FlowyResult<Self> {
|
2023-05-16 06:58:24 +00:00
|
|
|
InnerDocument::create(collab)
|
|
|
|
.map(|inner| Self(Arc::new(Mutex::new(inner))))
|
|
|
|
.map_err(|err| err.into())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates and returns a new Document object with initial data.
|
|
|
|
/// # Arguments
|
|
|
|
/// * `collab` - the identifier of the collaboration instance
|
|
|
|
/// * `data` - the initial data to include in the document
|
|
|
|
///
|
|
|
|
/// # Returns
|
|
|
|
/// * `Result<Document, FlowyError>` - a Result containing either a new Document object or an Error if the document creation failed
|
2023-05-15 14:16:05 +00:00
|
|
|
pub fn create_with_data(collab: Arc<MutexCollab>, data: DocumentData) -> FlowyResult<Self> {
|
2023-05-16 06:58:24 +00:00
|
|
|
InnerDocument::create_with_data(collab, data)
|
|
|
|
.map(|inner| Self(Arc::new(Mutex::new(inner))))
|
|
|
|
.map_err(|err| err.into())
|
2023-04-24 06:25:00 +00:00
|
|
|
}
|
2023-04-13 10:53:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unsafe impl Sync for Document {}
|
|
|
|
unsafe impl Send for Document {}
|
|
|
|
|
|
|
|
impl Deref for Document {
|
|
|
|
type Target = Arc<Mutex<InnerDocument>>;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DerefMut for Document {
|
|
|
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
|
|
|
&mut self.0
|
|
|
|
}
|
|
|
|
}
|