Skip to content

Commit 1b03cb6

Browse files
authored
update to samod 0.3.0 and improve document subscription handling (#131)
* chore: update iroh-automerge-repo to samod 0.3.0 * fix: iroh-automerge-repo uses doc change events vs polling * fix formatting
1 parent 8f4eb7b commit 1b03cb6

File tree

4 files changed

+18
-31
lines changed

4 files changed

+18
-31
lines changed

iroh-automerge-repo/Cargo.lock

Lines changed: 4 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

iroh-automerge-repo/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ bytes = "1.10.1"
99
derive_more = { version = "2.0.1", features = ["debug"] }
1010
iroh = "0.91.1"
1111
n0-future = "0.2"
12-
samod = { version = "0.2.1", features = ["tokio"] }
12+
samod = { version = "0.3.0", features = ["tokio"] }
1313
tokio = { version = "1.47.1", features = ["sync", "net"] }
1414
tokio-util = { version = "0.7.15", features = ["codec"] }
1515
tracing = "0.1.41"

iroh-automerge-repo/src/lib.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,32 @@
22
//! in rust that speak the automerge repo protocol.
33
use anyhow::Result;
44
use codec::Codec;
5-
use samod::{ConnDirection, ConnFinishedReason, Samod};
5+
use samod::{ConnDirection, ConnFinishedReason, Repo};
66
use tokio_util::codec::{FramedRead, FramedWrite};
77

88
mod codec;
99

10-
/// Combines an [`iroh::Endpoint`] with a [`Samod`] (automerge repository) and
10+
/// Combines an [`iroh::Endpoint`] with a [`Repo`] (automerge repository) and
1111
/// implements [`iroh::protocol::ProtocolHandler`] to accept incoming connections
1212
/// in an [`iroh::protocol::Router`].
1313
#[derive(derive_more::Debug, Clone)]
1414
pub struct IrohRepo {
1515
endpoint: iroh::Endpoint,
1616
#[debug(skip)]
17-
repo: Samod,
17+
repo: Repo,
1818
}
1919

2020
impl IrohRepo {
2121
pub const SYNC_ALPN: &[u8] = b"iroh/automerge-repo/1";
2222

2323
/// Constructs a new [`IrohRepo`].
24-
pub fn new(endpoint: iroh::Endpoint, repo: Samod) -> Self {
24+
pub fn new(endpoint: iroh::Endpoint, repo: Repo) -> Self {
2525
IrohRepo { endpoint, repo }
2626
}
2727

2828
/// Attempts to continuously sync with a peer at given address.
2929
///
30-
/// To wait for the connection to be established use [`Samod::when_connected`]
30+
/// To wait for the connection to be established use [`Repo::when_connected`]
3131
/// (accessible via [`Self::repo`]: `iroh_repo.repo().when_connected(..)`).
3232
/// with the other node's string-encoded [`NodeId`] as the [`PeerId`].
3333
///
@@ -56,8 +56,8 @@ impl IrohRepo {
5656
Ok(conn_finished)
5757
}
5858

59-
/// Returns a reference to the stored [`Samod`] instance inside.
60-
pub fn repo(&self) -> &Samod {
59+
/// Returns a reference to the stored [`Repo`] instance inside.
60+
pub fn repo(&self) -> &Repo {
6161
&self.repo
6262
}
6363
}

iroh-automerge-repo/src/main.rs

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use hex::encode;
77
use iroh::NodeId;
88
use iroh_automerge_repo::IrohRepo;
99

10-
use samod::{DocumentId, PeerId, Samod, storage::TokioFilesystemStorage};
10+
use samod::{DocumentId, PeerId, Repo, storage::TokioFilesystemStorage};
1111

1212
#[derive(Parser, Debug)]
1313
#[command(author, version, about, long_about = None)]
@@ -123,7 +123,7 @@ async fn main() -> anyhow::Result<()> {
123123

124124
println!("Node ID: {}", endpoint.node_id());
125125

126-
let samod = Samod::build_tokio()
126+
let samod = Repo::build_tokio()
127127
.with_peer_id(PeerId::from_string(endpoint.node_id().to_string()))
128128
.with_storage(TokioFilesystemStorage::new(format!(
129129
"{}/{}",
@@ -217,23 +217,11 @@ async fn main() -> anyhow::Result<()> {
217217
anyhow::Ok(())
218218
})?;
219219

220-
// Set up polling for changes (no push available yet)
220+
// Listen for change events and print latest document contents
221221
tokio::spawn(async move {
222-
// Track the last known heads to detect changes
223-
let mut last_heads = doc.with_document(|doc| doc.get_heads());
224-
loop {
225-
tokio::time::sleep(tokio::time::Duration::from_millis(1000)).await;
226-
227-
let current_heads = doc.with_document(|doc| doc.get_heads());
228-
if current_heads == last_heads {
229-
continue;
230-
}
231-
232-
last_heads = current_heads;
233-
234-
println!("Document changed! New state:");
235-
236-
// When changes are detected, print the updated document contents...
222+
use n0_future::StreamExt;
223+
let mut changes = doc.changes();
224+
while let Some(_change) = changes.next().await {
237225
if let Err(e) = doc.with_document(|current_doc| {
238226
for key in current_doc.keys(automerge::ROOT) {
239227
let (value, _) = current_doc

0 commit comments

Comments
 (0)