manager.rs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. use crate::services::grid_editor::ClientGridEditor;
  2. use dashmap::DashMap;
  3. use flowy_error::{FlowyError, FlowyResult};
  4. use flowy_sync::{RevisionManager, RevisionPersistence, RevisionWebSocket};
  5. use lib_sqlite::ConnectionPool;
  6. use std::sync::Arc;
  7. pub trait GridUser: Send + Sync {
  8. fn user_id(&self) -> Result<String, FlowyError>;
  9. fn token(&self) -> Result<String, FlowyError>;
  10. fn db_pool(&self) -> Result<Arc<ConnectionPool>, FlowyError>;
  11. }
  12. pub struct GridManager {
  13. grid_editors: Arc<GridEditors>,
  14. grid_user: Arc<dyn GridUser>,
  15. rev_web_socket: Arc<dyn RevisionWebSocket>,
  16. }
  17. impl GridManager {
  18. pub fn new(grid_user: Arc<dyn GridUser>, rev_web_socket: Arc<dyn RevisionWebSocket>) -> Self {
  19. let grid_editors = Arc::new(GridEditors::new());
  20. Self {
  21. grid_editors,
  22. grid_user,
  23. rev_web_socket,
  24. }
  25. }
  26. #[tracing::instrument(level = "debug", skip(self, grid_id), fields(grid_id), err)]
  27. pub async fn open_grid<T: AsRef<str>>(&self, grid_id: T) -> Result<Arc<ClientGridEditor>, FlowyError> {
  28. let grid_id = grid_id.as_ref();
  29. tracing::Span::current().record("grid_id", &grid_id);
  30. self.get_grid_editor(grid_id).await
  31. }
  32. #[tracing::instrument(level = "trace", skip(self, grid_id), fields(grid_id), err)]
  33. pub fn close_grid<T: AsRef<str>>(&self, grid_id: T) -> Result<(), FlowyError> {
  34. let grid_id = grid_id.as_ref();
  35. tracing::Span::current().record("grid_id", &grid_id);
  36. self.grid_editors.remove(grid_id);
  37. Ok(())
  38. }
  39. #[tracing::instrument(level = "debug", skip(self, grid_id), fields(doc_id), err)]
  40. pub fn delete_grid<T: AsRef<str>>(&self, grid_id: T) -> Result<(), FlowyError> {
  41. let grid_id = grid_id.as_ref();
  42. tracing::Span::current().record("grid_id", &grid_id);
  43. self.grid_editors.remove(grid_id);
  44. Ok(())
  45. }
  46. async fn get_grid_editor(&self, grid_id: &str) -> FlowyResult<Arc<ClientGridEditor>> {
  47. match self.grid_editors.get(grid_id) {
  48. None => {
  49. let db_pool = self.grid_user.db_pool()?;
  50. self.make_grid_editor(grid_id, db_pool).await
  51. }
  52. Some(editor) => Ok(editor),
  53. }
  54. }
  55. async fn make_grid_editor(
  56. &self,
  57. grid_id: &str,
  58. pool: Arc<ConnectionPool>,
  59. ) -> Result<Arc<ClientGridEditor>, FlowyError> {
  60. let token = self.grid_user.token()?;
  61. let user_id = self.grid_user.user_id()?;
  62. let grid_editor = ClientGridEditor::new(&user_id, grid_id, &token, pool, self.rev_web_socket.clone()).await?;
  63. self.grid_editors.insert(grid_id, &grid_editor);
  64. Ok(grid_editor)
  65. }
  66. }
  67. pub struct GridEditors {
  68. inner: DashMap<String, Arc<ClientGridEditor>>,
  69. }
  70. impl GridEditors {
  71. fn new() -> Self {
  72. Self { inner: DashMap::new() }
  73. }
  74. pub(crate) fn insert(&self, grid_id: &str, grid_editor: &Arc<ClientGridEditor>) {
  75. if self.inner.contains_key(grid_id) {
  76. tracing::warn!("Grid:{} already exists in cache", grid_id);
  77. }
  78. self.inner.insert(grid_id.to_string(), grid_editor.clone());
  79. }
  80. pub(crate) fn contains(&self, grid_id: &str) -> bool {
  81. self.inner.get(grid_id).is_some()
  82. }
  83. pub(crate) fn get(&self, grid_id: &str) -> Option<Arc<ClientGridEditor>> {
  84. if !self.contains(grid_id) {
  85. return None;
  86. }
  87. let opened_grid = self.inner.get(grid_id).unwrap();
  88. Some(opened_grid.clone())
  89. }
  90. pub(crate) fn remove(&self, grid_id: &str) {
  91. self.inner.remove(grid_id);
  92. }
  93. }