Skip to content

Commit b7b8c9f

Browse files
committed
fix/chat-demo: make possible to quit from chat-demo
call stop_replicator to drop last sender reference
1 parent 2fdab29 commit b7b8c9f

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

chat-demo/src/main.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use couchbase_lite::{
44
};
55
use log::{error, trace};
66
use serde::{Deserialize, Serialize};
7-
use std::{collections::HashSet, env, path::Path};
7+
use std::{collections::HashSet, env, path::Path, sync::mpsc};
88
use tokio::prelude::*;
99

1010
#[derive(Serialize, Deserialize, Debug)]
@@ -56,11 +56,11 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
5656
}
5757
});
5858

59-
let db_exec2 = db_exec.clone();
59+
let db_exec_repl = db_exec.clone();
6060
db_exec.spawn(move |db| {
6161
if let Some(db) = db.as_mut() {
6262
db.register_observer(move || {
63-
db_exec2
63+
db_exec_repl
6464
.spawn(|db| print_external_changes(db).expect("read external changes failed"));
6565
})
6666
.expect("register observer failed");
@@ -69,10 +69,10 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
6969
}
7070
});
7171

72-
let db_exec3 = db_exec.clone();
7372
let mut stdin = tokio::io::BufReader::new(tokio::io::stdin());
7473
static EDIT_PREFIX: &'static str = "edit ";
7574

75+
let db_exec_repl = db_exec.clone();
7676
runtime.block_on(async move {
7777
let mut buf = String::new();
7878
let mut edit_id = None;
@@ -84,7 +84,10 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
8484
let msg = &buf;
8585
let msg = msg.trim_end();
8686
if !msg.is_empty() {
87-
if msg.starts_with(EDIT_PREFIX) {
87+
if msg == "quit" {
88+
println!("Time to quit");
89+
break;
90+
} else if msg.starts_with(EDIT_PREFIX) {
8891
edit_id = Some((&msg[EDIT_PREFIX.len()..]).to_string());
8992
println!("ready to edit message {:?}", edit_id);
9093
} else {
@@ -93,7 +96,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
9396
{
9497
let msg = msg.to_string();
9598
let edit_id = edit_id.take();
96-
db_exec.spawn(move |db| {
99+
db_exec_repl.spawn(move |db| {
97100
if let Some(mut db) = db.as_mut() {
98101
save_msg(&mut db, &msg, edit_id.as_ref().map(String::as_str))
99102
.expect("save to db failed");
@@ -108,14 +111,15 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
108111
}
109112
});
110113

111-
db_exec3.spawn(|db| {
114+
db_exec.spawn(|db| {
112115
if let Some(db) = db.as_mut() {
113116
db.clear_observers();
117+
db.stop_replicator();
114118
} else {
115119
eprintln!("db is NOT open");
116120
}
117121
});
118-
drop(db_exec3);
122+
drop(db_exec);
119123
db_thread.join().unwrap();
120124
println!("exiting");
121125
Ok(())
@@ -125,7 +129,7 @@ type Job<T> = Box<dyn FnOnce(&mut Option<T>) + Send>;
125129

126130
#[derive(Clone)]
127131
struct DbQueryExecutor {
128-
inner: std::sync::mpsc::Sender<Job<Database>>,
132+
inner: mpsc::Sender<Job<Database>>,
129133
}
130134

131135
impl DbQueryExecutor {

0 commit comments

Comments
 (0)