Reworked latest_doc waiting code

Signed-off-by: faldor20 <eli.jambu@yahoo.com>
This commit is contained in:
Eli Dowling 2023-12-28 17:26:11 +10:00 committed by faldor20
parent e3ffa61c36
commit 41088bb7e5
No known key found for this signature in database
GPG key ID: F2216079B890CD57
2 changed files with 14 additions and 21 deletions

View file

@ -116,30 +116,24 @@ impl Registry {
self.documents.lock().await.get(url).map(|a| a.info.clone()) self.documents.lock().await.get(url).map(|a| a.info.clone())
} }
///Waits for the latest document analysis to be complete.
///Will block forever if the latest document is never written to
///Probably should use `latest_document_by_url`
async fn wait_for_latest_doc(&self, url: &Url) -> Arc<AnalyzedDocument> {
loop {
let docs = self.documents.lock().await;
if let Some(a) = docs.get(url) {
if let Some(a) = a.latest_document.get() {
return a.clone();
}
}
drop(docs);
tokio::task::yield_now().await;
}
}
///Tries to get the latest document from analysis. ///Tries to get the latest document from analysis.
///Gives up and returns none after 5 seconds. ///Gives up and returns none after 5 seconds.
async fn latest_document_by_url(&self, url: &Url) -> Option<Arc<AnalyzedDocument>> { async fn latest_document_by_url(&self, url: &Url) -> Option<Arc<AnalyzedDocument>> {
let duration = std::time::Duration::from_secs(5); let duration = std::time::Duration::from_secs(5);
tokio::select! { tokio::time::timeout(duration, async {
_ = tokio::time::sleep(duration) => { None }, loop {
doc = self.wait_for_latest_doc(url) => { Some(doc) } let docs = self.documents.lock().await;
} if let Some(a) = docs.get(url) {
if let Some(a) = a.latest_document.get() {
return a.clone();
}
}
drop(docs);
tokio::time::sleep(std::time::Duration::from_millis(10)).await;
}
})
.await
.ok()
} }
pub async fn diagnostics(&self, url: &Url) -> Vec<Diagnostic> { pub async fn diagnostics(&self, url: &Url) -> Vec<Diagnostic> {

View file

@ -35,7 +35,6 @@ impl RocLs {
client, client,
} }
} }
///Wait for all the semaphores associated with an in-progress document_info update to be released
pub fn capabilities() -> ServerCapabilities { pub fn capabilities() -> ServerCapabilities {
let text_document_sync = TextDocumentSyncCapability::Options( let text_document_sync = TextDocumentSyncCapability::Options(
// TODO: later on make this incremental // TODO: later on make this incremental