Rename zed-server to collab
Over time, I think we may end up having multiple services, so it seems like a good opportunity to name this one more specifically while the cost is low. It just seems like naming it "zed" and "zed-server" leaves it a bit open ended.
This commit is contained in:
parent
17195e615e
commit
ab8204368c
124 changed files with 71 additions and 113 deletions
43
crates/collab/src/expiring.rs
Normal file
43
crates/collab/src/expiring.rs
Normal file
|
@ -0,0 +1,43 @@
|
|||
use std::{future::Future, time::Instant};
|
||||
|
||||
use async_std::sync::Mutex;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct Expiring<T>(Mutex<Option<ExpiringState<T>>>);
|
||||
|
||||
pub struct ExpiringState<T> {
|
||||
value: T,
|
||||
expires_at: Instant,
|
||||
}
|
||||
|
||||
impl<T: Clone> Expiring<T> {
|
||||
pub async fn get_or_refresh<F, G>(&self, f: F) -> tide::Result<T>
|
||||
where
|
||||
F: FnOnce() -> G,
|
||||
G: Future<Output = tide::Result<(T, Instant)>>,
|
||||
{
|
||||
let mut state = self.0.lock().await;
|
||||
|
||||
if let Some(state) = state.as_mut() {
|
||||
if Instant::now() >= state.expires_at {
|
||||
let (value, expires_at) = f().await?;
|
||||
state.value = value.clone();
|
||||
state.expires_at = expires_at;
|
||||
Ok(value)
|
||||
} else {
|
||||
Ok(state.value.clone())
|
||||
}
|
||||
} else {
|
||||
let (value, expires_at) = f().await?;
|
||||
*state = Some(ExpiringState {
|
||||
value: value.clone(),
|
||||
expires_at,
|
||||
});
|
||||
Ok(value)
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn clear(&self) {
|
||||
self.0.lock().await.take();
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue