Skip to content

Commit

Permalink
feat: unwatch return boolean
Browse files Browse the repository at this point in the history
  • Loading branch information
vincent-herlemont committed Mar 31, 2024
1 parent 49d442e commit 7d72ff2
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 8 deletions.
10 changes: 5 additions & 5 deletions src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,12 @@ impl Database<'_> {

/// Unwatch the given `id`.
/// You can get the `id` from the return value of [`watch`](Self::watch).
/// If the `id` is not valid anymore, this function will do nothing.
/// If the `id` is valid, the corresponding watcher will be removed.
pub fn unwatch(&self, id: u64) -> Result<()> {
/// If the `id` is not valid anymore, this function will do nothing and return `false`.
/// If the `id` is valid, the corresponding watcher will be removed and return `true`.
/// If the `id` is valid but the watcher is already removed, this function will return `false`.
pub fn unwatch(&self, id: u64) -> Result<bool> {
let mut watchers = self.watchers.write().unwrap();
watchers.remove_sender(id);
Ok(())
Ok(watchers.remove_sender(id))
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/watch/sender.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ impl Watchers {
self.0.insert(id, (table_filter.clone(), event_sender));
}

pub(crate) fn remove_sender(&mut self, id: u64) {
self.0.remove(&id);
pub(crate) fn remove_sender(&mut self, id: u64) -> bool {
self.0.remove(&id).is_some()
}

pub(crate) fn find_senders(
Expand Down
2 changes: 1 addition & 1 deletion tests/watch/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ fn unwatch() {
assert_eq!(inner_event, item_a);
}

db.unwatch(recv_id).unwrap();
assert!(db.unwatch(recv_id).unwrap());

let rw = db.rw_transaction().unwrap();
rw.insert(item_a.clone()).unwrap();
Expand Down

0 comments on commit 7d72ff2

Please sign in to comment.