mirror of
				https://github.com/AppFlowy-IO/AppFlowy.git
				synced 2025-10-29 09:01:05 +00:00 
			
		
		
		
	send delta to server
This commit is contained in:
		
							parent
							
								
									526f1408bc
								
							
						
					
					
						commit
						45e74e48a2
					
				| @ -77,7 +77,7 @@ enum AuthStatus { | ||||
|     NotAuthorized, | ||||
| } | ||||
| 
 | ||||
| pub const EXPIRED_DURATION_DAYS: i64 = 5; | ||||
| pub const EXPIRED_DURATION_DAYS: i64 = 30; | ||||
| 
 | ||||
| pub struct AuthorizedUsers(DashMap<LoggedUser, AuthStatus>); | ||||
| impl AuthorizedUsers { | ||||
|  | ||||
| @ -3,6 +3,7 @@ use crate::{ | ||||
|     errors::{internal_error, DocError}, | ||||
|     services::{doc_controller::DocController, open_doc::OpenedDocManager, server::construct_doc_server, ws::WsManager}, | ||||
| }; | ||||
| use bytes::Bytes; | ||||
| use diesel::SqliteConnection; | ||||
| use flowy_database::ConnectionPool; | ||||
| use parking_lot::RwLock; | ||||
| @ -56,7 +57,7 @@ impl FlowyDocument { | ||||
|     } | ||||
| 
 | ||||
|     pub async fn apply_changeset(&self, params: ApplyChangesetParams, pool: Arc<ConnectionPool>) -> Result<Doc, DocError> { | ||||
|         let _ = self.doc_manager.apply_changeset(¶ms.id, params.data, pool).await?; | ||||
|         let _ = self.doc_manager.apply_changeset(¶ms.id, Bytes::from(params.data), pool).await?; | ||||
|         let data = self.doc_manager.read_doc(¶ms.id).await?; | ||||
|         let doc = Doc { id: params.id, data }; | ||||
|         Ok(doc) | ||||
|  | ||||
| @ -135,8 +135,6 @@ impl OpenedDocPersistence for DocController { | ||||
|     fn save(&self, params: SaveDocParams, pool: Arc<ConnectionPool>) -> Result<(), DocError> { | ||||
|         let changeset = DocTableChangeset::new(params.clone()); | ||||
|         let _ = self.sql.update_doc_table(changeset, &*(pool.get().map_err(internal_error)?))?; | ||||
|         let _ = self.update_doc_on_server(params)?; | ||||
| 
 | ||||
|         Ok(()) | ||||
|     } | ||||
| } | ||||
|  | ||||
| @ -5,6 +5,7 @@ use crate::{ | ||||
|         ws::WsManager, | ||||
|     }, | ||||
| }; | ||||
| use bytes::Bytes; | ||||
| use dashmap::DashMap; | ||||
| use flowy_database::ConnectionPool; | ||||
| use flowy_ot::{core::Delta, errors::OTError}; | ||||
| @ -32,7 +33,12 @@ impl OpenedDocManager { | ||||
|         T: Into<DocId> + Debug, | ||||
|         D: TryInto<Delta, Error = OTError>, | ||||
|     { | ||||
|         let doc = Arc::new(OpenedDoc::new(id.into(), data.try_into()?, self.persistence.clone())); | ||||
|         let doc = Arc::new(OpenedDoc::new( | ||||
|             id.into(), | ||||
|             data.try_into()?, | ||||
|             self.persistence.clone(), | ||||
|             self.ws_manager.read().sender.clone(), | ||||
|         )); | ||||
|         self.ws_manager.write().register_handler(doc.id.as_ref(), doc.clone()); | ||||
|         self.doc_map.insert(doc.id.clone(), doc.clone()); | ||||
|         Ok(()) | ||||
| @ -47,7 +53,7 @@ impl OpenedDocManager { | ||||
|     } | ||||
| 
 | ||||
|     #[tracing::instrument(level = "debug", skip(self, changeset, pool), err)] | ||||
|     pub(crate) async fn apply_changeset<T>(&self, id: T, changeset: Vec<u8>, pool: Arc<ConnectionPool>) -> Result<(), DocError> | ||||
|     pub(crate) async fn apply_changeset<T>(&self, id: T, changeset: Bytes, pool: Arc<ConnectionPool>) -> Result<(), DocError> | ||||
|     where | ||||
|         T: Into<DocId> + Debug, | ||||
|     { | ||||
|  | ||||
| @ -4,8 +4,9 @@ use crate::{ | ||||
|         ws::{WsDocumentData, WsSource}, | ||||
|     }, | ||||
|     errors::DocError, | ||||
|     services::ws::WsHandler, | ||||
|     services::ws::{WsHandler, WsSender}, | ||||
| }; | ||||
| use bytes::Bytes; | ||||
| use flowy_database::ConnectionPool; | ||||
| use flowy_ot::{client::Document, core::Delta}; | ||||
| use parking_lot::RwLock; | ||||
| @ -30,23 +31,28 @@ pub(crate) trait OpenedDocPersistence: Send + Sync { | ||||
| pub(crate) struct OpenedDoc { | ||||
|     pub(crate) id: DocId, | ||||
|     document: RwLock<Document>, | ||||
|     ws_sender: Arc<dyn WsSender>, | ||||
|     persistence: Arc<dyn OpenedDocPersistence>, | ||||
| } | ||||
| 
 | ||||
| impl OpenedDoc { | ||||
|     pub(crate) fn new(id: DocId, delta: Delta, persistence: Arc<dyn OpenedDocPersistence>) -> Self { | ||||
|     pub(crate) fn new(id: DocId, delta: Delta, persistence: Arc<dyn OpenedDocPersistence>, ws_sender: Arc<dyn WsSender>) -> Self { | ||||
|         let document = RwLock::new(Document::from_delta(delta)); | ||||
|         Self { | ||||
|             id, | ||||
|             document: RwLock::new(Document::from_delta(delta)), | ||||
|             document, | ||||
|             ws_sender, | ||||
|             persistence, | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     pub(crate) fn data(&self) -> Vec<u8> { self.document.read().to_bytes() } | ||||
| 
 | ||||
|     pub(crate) fn apply_delta(&self, data: Vec<u8>, pool: Arc<ConnectionPool>) -> Result<(), DocError> { | ||||
|     pub(crate) fn apply_delta(&self, data: Bytes, pool: Arc<ConnectionPool>) -> Result<(), DocError> { | ||||
|         let mut write_guard = self.document.write(); | ||||
|         let _ = write_guard.apply_changeset(data)?; | ||||
|         let _ = write_guard.apply_changeset(data.clone())?; | ||||
| 
 | ||||
|         self.ws_sender.send_data(data); | ||||
| 
 | ||||
|         // Opti: strategy to save the document
 | ||||
|         let mut save = SaveDocParams { | ||||
|  | ||||
| @ -12,12 +12,12 @@ lazy_static! { | ||||
| } | ||||
| 
 | ||||
| pub struct WsManager { | ||||
|     sender: Box<dyn WsSender>, | ||||
|     pub(crate) sender: Arc<dyn WsSender>, | ||||
|     doc_handlers: HashMap<String, Arc<dyn WsHandler>>, | ||||
| } | ||||
| 
 | ||||
| impl WsManager { | ||||
|     pub fn new(sender: Box<dyn WsSender>) -> Self { | ||||
|     pub fn new(sender: Arc<dyn WsSender>) -> Self { | ||||
|         Self { | ||||
|             sender, | ||||
|             doc_handlers: HashMap::new(), | ||||
| @ -45,9 +45,9 @@ impl WsManager { | ||||
|             }, | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     pub fn send_data(&self, data: WsDocumentData) { | ||||
|         let bytes: Bytes = data.try_into().unwrap(); | ||||
| 
 | ||||
|         match self.sender.send_data(bytes) { | ||||
|             Ok(_) => {}, | ||||
|             Err(e) => { | ||||
|  | ||||
| @ -17,6 +17,7 @@ lazy_static = "1.4.0" | ||||
| url = "2.2" | ||||
| strum = "0.21" | ||||
| strum_macros = "0.21" | ||||
| bytes = "1.0" | ||||
| 
 | ||||
| 
 | ||||
| [dev-dependencies] | ||||
|  | ||||
| @ -3,6 +3,8 @@ use crate::{ | ||||
|     errors::{ErrorBuilder, OTError, OTErrorCode}, | ||||
| }; | ||||
| use bytecount::num_chars; | ||||
| use bytes::Bytes; | ||||
| use serde::__private::TryFrom; | ||||
| use std::{ | ||||
|     cmp::{min, Ordering}, | ||||
|     fmt, | ||||
| @ -44,6 +46,15 @@ impl std::convert::TryFrom<Vec<u8>> for Delta { | ||||
|     fn try_from(bytes: Vec<u8>) -> Result<Self, Self::Error> { Delta::from_bytes(bytes) } | ||||
| } | ||||
| 
 | ||||
| impl std::convert::TryFrom<Bytes> for Delta { | ||||
|     type Error = OTError; | ||||
| 
 | ||||
|     fn try_from(value: Bytes) -> Result<Self, Self::Error> { | ||||
|         let bytes = value.to_vec(); | ||||
|         Delta::from_bytes(bytes) | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| // impl<T: AsRef<Vec<u8>>> std::convert::From<T> for Delta {
 | ||||
| //     fn from(bytes: T) -> Self {
 | ||||
| // Delta::from_bytes(bytes.as_ref().to_vec()).unwrap() } }
 | ||||
|  | ||||
| @ -22,7 +22,7 @@ impl DocumentDepsResolver { | ||||
|             user: self.user_session.clone(), | ||||
|         }); | ||||
| 
 | ||||
|         let sender = Box::new(WsSenderImpl { | ||||
|         let sender = Arc::new(WsSenderImpl { | ||||
|             user: self.user_session.clone(), | ||||
|         }); | ||||
| 
 | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 appflowy
						appflowy