Show doc outline on vscode start, wait for cache

This commit is contained in:
James Yu 2023-01-22 12:18:41 +08:00
parent 443b80bb63
commit bf7499669a
2 changed files with 33 additions and 10 deletions

View File

@ -60,6 +60,7 @@ export class Cacher {
private readonly pdfWatcher: PdfWatcher = new PdfWatcher()
private readonly bibWatcher: BibWatcher = new BibWatcher()
private caching = 0
private promises: {[filePath: string]: Promise<void>} = {}
add(filePath: string) {
if (CacherUtils.isExcluded(filePath)) {
@ -88,6 +89,10 @@ export class Cacher {
return this.caches[filePath]
}
promise(filePath: string): Promise<void> | undefined {
return this.promises[filePath]
}
get allPaths() {
return Object.keys(this.caches)
}
@ -126,16 +131,21 @@ export class Cacher {
rootPath = rootPath || lw.manager.rootFile
this.updateChildren(filePath, rootPath, contentTrimmed)
await this.updateAST(filePath, content)
this.updateElements(filePath, content, contentTrimmed)
this.updateBibfiles(filePath, contentTrimmed)
logger.log(`Cached ${filePath} .`)
this.caching--
lw.eventBus.fire(eventbus.FileParsed, filePath)
this.promises[filePath] = this.updateAST(filePath, content).then(() => {
this.updateElements(filePath, content, contentTrimmed)
this.updateBibfiles(filePath, contentTrimmed)
}).finally(() => {
logger.log(`Cached ${filePath} .`)
this.caching--
delete this.promises[filePath]
lw.eventBus.fire(eventbus.FileParsed, filePath)
if (this.caching === 0) {
void lw.structureViewer.computeTreeStructure()
}
if (this.caching === 0) {
void lw.structureViewer.computeTreeStructure()
}
})
return this.promises[filePath]
}
private async updateAST(filePath: string, content: string) {

View File

@ -149,9 +149,22 @@ export class SectionNodeProvider implements vscode.TreeDataProvider<Section> {
}
filesBuilt.add(file)
let waited = 0
while (!lw.cacher.promise(file) && !lw.cacher.get(file)?.content) {
// Just open vscode, has not cached, wait for a bit?
await new Promise(resolve => setTimeout(resolve, 100))
waited++
if (waited > 10) {
// Waited for five seconds before starting cache. Really?
logger.log(`Error loading Cache during structuring: ${file} .`)
return []
}
}
await lw.cacher.promise(file)
const content = lw.cacher.get(file)?.content
if (!content) {
logger.log(`Error loading LaTeX during structuring: ${file} .`)
logger.log(`Error loading content during structuring: ${file} .`)
return []
}