Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(iroh): update example to use correct subscription API #1452

Merged
merged 1 commit into from
Sep 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions iroh-sync/src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,15 @@ impl<S: ranger::Store<SignedEntry> + PublicKeyStore + 'static> Replica<S> {
/// the receiver to be received from.
// TODO: Allow to clear a previous subscription?
pub fn subscribe(&self) -> Option<flume::Receiver<(InsertOrigin, SignedEntry)>> {
println!("subscribing..");
let mut on_insert_sender = self.on_insert_sender.write();
if let Some(_sender) = on_insert_sender.as_ref() {
None
} else {
let (s, r) = flume::bounded(16); // TODO: should this be configurable?
*on_insert_sender = Some(s);
Some(r)
match &*on_insert_sender {
Some(_sender) => None,
None => {
let (s, r) = flume::bounded(16); // TODO: should this be configurable?
*on_insert_sender = Some(s);
Some(r)
}
}
}

Expand Down
23 changes: 16 additions & 7 deletions iroh/examples/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ use std::{
use anyhow::{anyhow, bail};
use bytes::Bytes;
use clap::{CommandFactory, FromArgMatches, Parser};
use futures::StreamExt;
use indicatif::HumanBytes;
use iroh::{
download::Downloader,
sync_engine::{PeerSource, SyncEngine, SYNC_ALPN},
sync_engine::{LiveEvent, PeerSource, SyncEngine, SYNC_ALPN},
};
use iroh_bytes::util::runtime;
use iroh_bytes::{
Expand Down Expand Up @@ -271,15 +272,23 @@ async fn run(args: Args) -> anyhow::Result<()> {
Arc::new(tokio::sync::Mutex::new(None));

let watch = current_watch.clone();
let doc_events = doc.subscribe().expect("already subscribed");
let mut doc_events = live_sync
.doc_subscribe(iroh::rpc_protocol::DocSubscribeRequest {
doc_id: doc.namespace(),
})
.await;
rt.main().spawn(async move {
while let Ok((_origin, entry)) = doc_events.recv_async().await {
let entry = entry.entry();
while let Some(Ok(event)) = doc_events.next().await {
let matcher = watch.lock().await;
if let Some(matcher) = &*matcher {
let key = entry.id().key();
if key.starts_with(matcher.as_bytes()) {
println!("change: {}", fmt_entry(entry));
match event.event {
LiveEvent::ContentReady { .. } => {}
LiveEvent::InsertLocal { entry } | LiveEvent::InsertRemote { entry, .. } => {
let key = entry.id().key();
if key.starts_with(matcher.as_bytes()) {
println!("change: {}", fmt_entry(&entry));
}
}
}
}
}
Expand Down