Add await to refresh book method (#14236)

* add await to refresh book method

* change name of method

* Reload tree view

* address pr comments

* adding finally on finally
This commit is contained in:
Barbara Valdez
2021-02-11 21:57:53 -08:00
committed by GitHub
parent b5479d0246
commit 75cda19504

View File

@@ -93,7 +93,7 @@ export class BookTreeViewProvider implements vscode.TreeDataProvider<BookTreeIte
setFileWatcher(book: BookModel): void { setFileWatcher(book: BookModel): void {
fs.watchFile(book.tableOfContentsPath, async (curr, prev) => { fs.watchFile(book.tableOfContentsPath, async (curr, prev) => {
if (curr.mtime > prev.mtime) { if (curr.mtime > prev.mtime) {
this.fireBookRefresh(book); await this.initializeBookContents(book);
} }
}); });
} }
@@ -223,14 +223,18 @@ export class BookTreeViewProvider implements vscode.TreeDataProvider<BookTreeIte
await this.bookTocManager.recovery(); await this.bookTocManager.recovery();
vscode.window.showErrorMessage(loc.editBookError(updateBook.book.contentPath, e instanceof Error ? e.message : e)); vscode.window.showErrorMessage(loc.editBookError(updateBook.book.contentPath, e instanceof Error ? e.message : e));
} finally { } finally {
this.fireBookRefresh(targetBook); try {
if (sourceBook) { await targetBook.initializeContents();
// refresh source book model to pick up latest changes if (sourceBook && sourceBook.bookPath !== targetBook.bookPath) {
this.fireBookRefresh(sourceBook); // refresh source book model to pick up latest changes
} await sourceBook.initializeContents();
// even if it fails, we still need to watch the toc file again. }
if (sourceBook) { } finally {
this.setFileWatcher(sourceBook); this._onDidChangeTreeData.fire(undefined);
// even if it fails, we still need to watch the toc file again.
if (sourceBook) {
this.setFileWatcher(sourceBook);
}
} }
} }
} }
@@ -262,7 +266,7 @@ export class BookTreeViewProvider implements vscode.TreeDataProvider<BookTreeIte
if (curr.mtime > prev.mtime) { if (curr.mtime > prev.mtime) {
let book = this.books.find(book => book.bookPath === bookPath); let book = this.books.find(book => book.bookPath === bookPath);
if (book) { if (book) {
this.fireBookRefresh(book); await this.initializeBookContents(book);
} }
} }
}); });
@@ -293,7 +297,7 @@ export class BookTreeViewProvider implements vscode.TreeDataProvider<BookTreeIte
} }
@debounce(1500) @debounce(1500)
async fireBookRefresh(book: BookModel): Promise<void> { async initializeBookContents(book: BookModel): Promise<void> {
await book.initializeContents().then(() => { await book.initializeContents().then(() => {
this._onDidChangeTreeData.fire(undefined); this._onDidChangeTreeData.fire(undefined);
}); });