document.rs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. use std::{
  2. ops::{Deref, DerefMut},
  3. sync::Arc,
  4. };
  5. use collab::core::collab::MutexCollab;
  6. use collab_document::{blocks::DocumentData, document::Document as InnerDocument};
  7. use parking_lot::Mutex;
  8. use flowy_error::FlowyResult;
  9. /// This struct wrap the document::Document
  10. #[derive(Clone)]
  11. pub struct Document(Arc<Mutex<InnerDocument>>);
  12. impl Document {
  13. /// Creates and returns a new Document object.
  14. /// # Arguments
  15. /// * `collab` - the identifier of the collaboration instance
  16. ///
  17. /// # Returns
  18. /// * `Result<Document, FlowyError>` - a Result containing either a new Document object or an Error if the document creation failed
  19. pub fn new(collab: Arc<MutexCollab>) -> FlowyResult<Self> {
  20. InnerDocument::create(collab)
  21. .map(|inner| Self(Arc::new(Mutex::new(inner))))
  22. .map_err(|err| err.into())
  23. }
  24. /// Creates and returns a new Document object with initial data.
  25. /// # Arguments
  26. /// * `collab` - the identifier of the collaboration instance
  27. /// * `data` - the initial data to include in the document
  28. ///
  29. /// # Returns
  30. /// * `Result<Document, FlowyError>` - a Result containing either a new Document object or an Error if the document creation failed
  31. pub fn create_with_data(collab: Arc<MutexCollab>, data: DocumentData) -> FlowyResult<Self> {
  32. InnerDocument::create_with_data(collab, data)
  33. .map(|inner| Self(Arc::new(Mutex::new(inner))))
  34. .map_err(|err| err.into())
  35. }
  36. }
  37. unsafe impl Sync for Document {}
  38. unsafe impl Send for Document {}
  39. impl Deref for Document {
  40. type Target = Arc<Mutex<InnerDocument>>;
  41. fn deref(&self) -> &Self::Target {
  42. &self.0
  43. }
  44. }
  45. impl DerefMut for Document {
  46. fn deref_mut(&mut self) -> &mut Self::Target {
  47. &mut self.0
  48. }
  49. }