Skip to content

Commit

Permalink
Add CreateDocument variant on FromApp
Browse files Browse the repository at this point in the history
  • Loading branch information
sandreae committed Dec 29, 2024
1 parent 1e97281 commit 70a9e69
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 23 deletions.
7 changes: 7 additions & 0 deletions aardvark-app/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,13 +142,20 @@ impl AardvarkApplication {

{
let application = self.clone();

let tx = self.imp().tx.clone();

let mut rx = self
.imp()
.rx
.take()
.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) => {
Expand Down
43 changes: 20 additions & 23 deletions aardvark-node/src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use crate::operation::{
use crate::topics::{AardvarkTopics, DiscoveryCode, TextDocument};

pub enum FromApp {
CreateDocument,
SubscribeToDocument(ShortCode),
HandleMessage(Vec<u8>),
}
Expand Down Expand Up @@ -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::<LogId, AardvarkExtensions>::new();

let document = init_document(&mut operations_store, &private_key)
.await
.expect("can init document");

let operations_store = MemoryStore::<LogId, AardvarkExtensions>::new();
let documents_store = TextDocumentStore::default();
let sync = LogSyncProtocol::new(documents_store.clone(), operations_store.clone());
let sync_config = SyncConfiguration::<AardvarkTopics>::new(sync);
Expand Down Expand Up @@ -92,30 +88,14 @@ pub fn run() -> Result<(
.await
.expect("network spawning");

let mut node = Node::new(
let node = Node::new(
private_key,
network,
operations_store,
documents_store,
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<Result<()>> = node.run(from_app_rx).await;

shutdown_rx.await.unwrap();
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 70a9e69

Please sign in to comment.