manager.rs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. use std::{collections::HashMap, sync::Arc};
  2. use appflowy_integrate::collab_builder::AppFlowyCollabBuilder;
  3. use appflowy_integrate::RocksCollabDB;
  4. use collab_document::blocks::DocumentData;
  5. use parking_lot::RwLock;
  6. use flowy_error::{FlowyError, FlowyResult};
  7. use crate::{
  8. document::Document,
  9. entities::DocEventPB,
  10. notification::{send_notification, DocumentNotification},
  11. };
  12. pub trait DocumentUser: Send + Sync {
  13. fn user_id(&self) -> Result<i64, FlowyError>;
  14. fn token(&self) -> Result<Option<String>, FlowyError>; // unused now.
  15. fn collab_db(&self) -> Result<Arc<RocksCollabDB>, FlowyError>;
  16. }
  17. pub struct DocumentManager {
  18. user: Arc<dyn DocumentUser>,
  19. collab_builder: Arc<AppFlowyCollabBuilder>,
  20. documents: Arc<RwLock<HashMap<String, Arc<Document>>>>,
  21. }
  22. impl DocumentManager {
  23. pub fn new(user: Arc<dyn DocumentUser>, collab_builder: Arc<AppFlowyCollabBuilder>) -> Self {
  24. Self {
  25. user,
  26. collab_builder,
  27. documents: Default::default(),
  28. }
  29. }
  30. pub fn create_document(&self, doc_id: String, data: DocumentData) -> FlowyResult<Arc<Document>> {
  31. tracing::debug!("create a document: {:?}", &doc_id);
  32. let uid = self.user.user_id()?;
  33. let db = self.user.collab_db()?;
  34. let collab = self.collab_builder.build(uid, &doc_id, "document", db);
  35. let document = Arc::new(Document::create_with_data(collab, data)?);
  36. Ok(document)
  37. }
  38. pub fn open_document(&self, doc_id: String) -> FlowyResult<Arc<Document>> {
  39. tracing::debug!("open a document: {:?}", &doc_id);
  40. if let Some(doc) = self.documents.read().get(&doc_id) {
  41. return Ok(doc.clone());
  42. }
  43. tracing::debug!("open_document: {:?}", &doc_id);
  44. let uid = self.user.user_id()?;
  45. let db = self.user.collab_db()?;
  46. let collab = self.collab_builder.build(uid, &doc_id, "document", db);
  47. // read the existing document from the disk.
  48. let document = Arc::new(Document::new(collab)?);
  49. // save the document to the memory and read it from the memory if we open the same document again.
  50. // and we don't want to subscribe to the document changes if we open the same document again.
  51. self
  52. .documents
  53. .write()
  54. .insert(doc_id.clone(), document.clone());
  55. // subscribe to the document changes.
  56. document.lock().open(move |events, is_remote| {
  57. tracing::trace!(
  58. "document changed: {:?}, from remote: {}",
  59. &events,
  60. is_remote
  61. );
  62. // send notification to the client.
  63. send_notification(&doc_id, DocumentNotification::DidReceiveUpdate)
  64. .payload::<DocEventPB>((events, is_remote).into())
  65. .send();
  66. })?;
  67. Ok(document)
  68. }
  69. pub fn get_document(&self, doc_id: String) -> FlowyResult<Arc<Document>> {
  70. let uid = self.user.user_id()?;
  71. let db = self.user.collab_db()?;
  72. let collab = self.collab_builder.build(uid, &doc_id, "document", db);
  73. // read the existing document from the disk.
  74. let document = Arc::new(Document::new(collab)?);
  75. Ok(document)
  76. }
  77. pub fn close_document(&self, doc_id: String) -> FlowyResult<()> {
  78. self.documents.write().remove(&doc_id);
  79. Ok(())
  80. }
  81. }