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,10 +116,11 @@ 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. ///Tries to get the latest document from analysis.
///Will block forever if the latest document is never written to ///Gives up and returns none after 5 seconds.
///Probably should use `latest_document_by_url` async fn latest_document_by_url(&self, url: &Url) -> Option<Arc<AnalyzedDocument>> {
async fn wait_for_latest_doc(&self, url: &Url) -> Arc<AnalyzedDocument> { let duration = std::time::Duration::from_secs(5);
tokio::time::timeout(duration, async {
loop { loop {
let docs = self.documents.lock().await; let docs = self.documents.lock().await;
if let Some(a) = docs.get(url) { if let Some(a) = docs.get(url) {
@ -128,18 +129,11 @@ impl Registry {
} }
} }
drop(docs); drop(docs);
tokio::task::yield_now().await; tokio::time::sleep(std::time::Duration::from_millis(10)).await;
}
}
///Tries to get the latest document from analysis.
///Gives up and returns none after 5 seconds.
async fn latest_document_by_url(&self, url: &Url) -> Option<Arc<AnalyzedDocument>> {
let duration = std::time::Duration::from_secs(5);
tokio::select! {
_ = tokio::time::sleep(duration) => { None },
doc = self.wait_for_latest_doc(url) => { Some(doc) }
} }
})
.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