From 70a9e69c40d51624a8b0a5906f7825f513dd3ce0 Mon Sep 17 00:00:00 2001 From: Sam Andreae Date: Sun, 29 Dec 2024 21:55:43 +0000 Subject: [PATCH] Add CreateDocument variant on FromApp --- aardvark-app/src/application.rs | 7 ++++++ aardvark-node/src/network.rs | 43 +++++++++++++++------------------ 2 files changed, 27 insertions(+), 23 deletions(-) diff --git a/aardvark-app/src/application.rs b/aardvark-app/src/application.rs index 9c79343..6d9bcd6 100644 --- a/aardvark-app/src/application.rs +++ b/aardvark-app/src/application.rs @@ -142,6 +142,9 @@ impl AardvarkApplication { { let application = self.clone(); + + let tx = self.imp().tx.clone(); + let mut rx = self .imp() .rx @@ -149,6 +152,10 @@ impl AardvarkApplication { .expect("rx should be given at this point"); glib::spawn_future_local(async move { + tx.send(FromApp::CreateDocument) + .await + .expect("app tx channel is open"); + while let Some(message) = rx.recv().await { match message { ToApp::SubscriptionSuccess(text_document) => { diff --git a/aardvark-node/src/network.rs b/aardvark-node/src/network.rs index 51c0407..b865a40 100644 --- a/aardvark-node/src/network.rs +++ b/aardvark-node/src/network.rs @@ -26,6 +26,7 @@ use crate::operation::{ use crate::topics::{AardvarkTopics, DiscoveryCode, TextDocument}; pub enum FromApp { + CreateDocument, SubscribeToDocument(ShortCode), HandleMessage(Vec), } @@ -56,12 +57,7 @@ pub fn run() -> Result<( let private_key = PrivateKey::new(); println!("my public key: {}", private_key.public_key()); - let mut operations_store = MemoryStore::::new(); - - let document = init_document(&mut operations_store, &private_key) - .await - .expect("can init document"); - + let operations_store = MemoryStore::::new(); let documents_store = TextDocumentStore::default(); let sync = LogSyncProtocol::new(documents_store.clone(), operations_store.clone()); let sync_config = SyncConfiguration::::new(sync); @@ -92,7 +88,7 @@ pub fn run() -> Result<( .await .expect("network spawning"); - let mut node = Node::new( + let node = Node::new( private_key, network, operations_store, @@ -100,22 +96,6 @@ pub fn run() -> Result<( to_app_tx.clone(), ); - node.discovered_documents - .insert(document.short_code(), document.clone()); - - node.subscribe(&document) - .await - .expect("node can subscribe to document"); - - node.announce(&document) - .await - .expect("node can announce document"); - - to_app_tx - .send(ToApp::SubscriptionSuccess(document.clone())) - .await - .expect("can send on app channel"); - let _join_handle: JoinHandle> = node.run(from_app_rx).await; shutdown_rx.await.unwrap(); @@ -168,6 +148,7 @@ impl Node { tokio::task::spawn(async move { while let Some(message) = from_app.recv().await { match message { + FromApp::CreateDocument => self.create().await?, FromApp::SubscribeToDocument(short_code) => { let document = match self.discovered_documents.get(&short_code) { Some(document) => document.clone(), @@ -234,6 +215,22 @@ impl Node { Ok(()) } + async fn create(&mut self) -> Result<()> { + let document = init_document(&mut self.operations_store, &self.private_key).await?; + + self.discovered_documents + .insert(document.short_code(), document.clone()); + + self.subscribe(&document).await?; + self.announce(&document).await?; + + self.to_app_tx + .send(ToApp::SubscriptionSuccess(document.clone())) + .await?; + + Ok(()) + } + /// Discover a document based on its `ShortCode`. /// /// Note: this method returns a future which may never resolve if no peers in our network are