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

example(sync): add watch command #1302

Merged
merged 3 commits into from
Jul 26, 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
36 changes: 35 additions & 1 deletion iroh/examples/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,20 @@ async fn run(args: Args) -> anyhow::Result<()> {
// process commands in a loop
println!("> ready to accept commands");
println!("> type `help` for a list of commands");

let current_watch: Arc<std::sync::Mutex<Option<String>>> =
Arc::new(std::sync::Mutex::new(None));
let watch = current_watch.clone();
doc.on_insert(Box::new(move |_origin, entry| {
let matcher = watch.lock().unwrap();
if let Some(matcher) = &*matcher {
let key = entry.entry().id().key();
if key.starts_with(matcher.as_bytes()) {
println!("change: {}", fmt_entry(&entry));
}
}
}));

loop {
// wait for a command from the input repl thread
let Some((cmd, to_repl_tx)) = cmd_rx.recv().await else {
Expand All @@ -225,7 +239,7 @@ async fn run(args: Args) -> anyhow::Result<()> {
_ = tokio::signal::ctrl_c() => {
println!("> aborted");
}
res = handle_command(cmd, &doc, &our_ticket, &log_filter) => if let Err(err) = res {
res = handle_command(cmd, &doc, &our_ticket, &log_filter, &current_watch) => if let Err(err) = res {
println!("> error: {err}");
},
};
Expand All @@ -249,6 +263,7 @@ async fn handle_command(
doc: &Doc,
ticket: &Ticket,
log_filter: &LogLevelReload,
current_watch: &Arc<std::sync::Mutex<Option<String>>>,
) -> anyhow::Result<()> {
match cmd {
Cmd::Set { key, value } => {
Expand All @@ -263,6 +278,18 @@ async fn handle_command(
}
}
}
Cmd::Watch { key } => {
println!("watching key: '{key}'");
current_watch.lock().unwrap().replace(key);
}
Cmd::WatchCancel => match current_watch.lock().unwrap().take() {
Some(key) => {
println!("canceled watching key: '{key}'");
}
None => {
println!("no watch active");
}
},
Cmd::Ls { prefix } => {
let entries = match prefix {
None => doc.replica().all(),
Expand Down Expand Up @@ -325,6 +352,13 @@ pub enum Cmd {
#[clap(verbatim_doc_comment)]
directive: String,
},
/// Watch for changes.
Watch {
/// The key to watch.
key: String,
},
/// Cancels any running watch command.
WatchCancel,
/// Quit
Exit,
}
Expand Down
7 changes: 6 additions & 1 deletion iroh/src/sync/content.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ use iroh_gossip::net::util::Dialer;
use iroh_io::{AsyncSliceReader, AsyncSliceReaderExt};
use iroh_net::{tls::PeerId, MagicEndpoint};
use iroh_sync::sync::{
Author, InsertOrigin, Namespace, NamespaceId, Replica, ReplicaStore, SignedEntry,
Author, InsertOrigin, Namespace, NamespaceId, OnInsertCallback, Replica, ReplicaStore,
SignedEntry,
};
use tokio::{
io::AsyncRead,
Expand Down Expand Up @@ -127,6 +128,10 @@ impl Doc {
doc
}

pub fn on_insert(&self, callback: OnInsertCallback) {
self.replica.on_insert(callback);
}

pub fn replica(&self) -> &Replica {
&self.replica
}
Expand Down