Skip to content

Commit e40957d

Browse files
committed
Add Reader::block_until_changed_timeout
1 parent e025724 commit e40957d

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

src/reactive.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,14 +289,30 @@ impl<T> Reader<T> {
289289
/// If [`Reader::has_changed`] would already return `true` without blocking, this method will
290290
/// return immediately.
291291
pub fn block_until_changed(&self) {
292+
self.block_until_changed_impl(None);
293+
}
294+
295+
/// Blocks the calling thread until [`Reader::has_changed`] becomes `true`, or a timeout
296+
/// elapses.
297+
///
298+
/// If [`Reader::has_changed`] would already return `true` without blocking, this method will
299+
/// return immediately.
300+
pub fn block_until_changed_timeout(&self, timeout: Duration) {
301+
self.block_until_changed_impl(Some(timeout));
302+
}
303+
304+
fn block_until_changed_impl(&self, timeout: Option<Duration>) {
292305
let mut guard = self.shared.inner.lock();
293306
loop {
294307
if guard.generation != self.read_gen
295308
|| self.shared.disconnected() != self.read_disconnected
296309
{
297310
return;
298311
}
299-
guard = self.shared.condvar.wait(guard);
312+
guard = match timeout {
313+
Some(timeout) => self.shared.condvar.wait_timeout(guard, timeout).0,
314+
None => self.shared.condvar.wait(guard),
315+
};
300316
}
301317
}
302318

@@ -307,6 +323,7 @@ impl<T> Reader<T> {
307323
pub fn is_disconnected(&self) -> bool {
308324
self.shared.disconnected()
309325
}
326+
310327
/// Retrieves the current value.
311328
///
312329
/// If all associated [`Value`]s have been dropped, a [`Disconnected`] error is returned

0 commit comments

Comments
 (0)