Skip to main content

yazi_shared/
last_value.rs

1use std::sync::Arc;
2
3use parking_lot::Mutex;
4use tokio::sync::Notify;
5
6#[derive(Clone, Debug, Default)]
7pub struct LastValue<T> {
8	inner: Arc<(Notify, Mutex<Option<T>>)>,
9}
10
11impl<T> LastValue<T> {
12	pub fn set(&self, data: T) {
13		*self.inner.1.lock() = Some(data);
14		self.inner.0.notify_waiters();
15	}
16
17	pub async fn get(&self) -> T {
18		loop {
19			let notified = self.inner.0.notified();
20			if let Some(data) = self.inner.1.lock().take() {
21				return data;
22			}
23
24			notified.await;
25		}
26	}
27}