Fix book provider load to not throw on startup (#8835)

* Fix book provider load to not throw on startup

* Move tests to stable

* Rename method

* Fix floating promises and broken test
This commit is contained in:
Charles Gagnon
2020-01-07 17:32:51 -08:00
committed by GitHub
parent b1526603cc
commit 041f34beda
4 changed files with 153 additions and 115 deletions

View File

@@ -27,7 +27,6 @@ export class BookTreeViewProvider implements vscode.TreeDataProvider<BookTreeIte
// For testing
private _errorMessage: string;
private _onReadAllTOCFiles: vscode.EventEmitter<void> = new vscode.EventEmitter<void>();
private _openAsUntitled: boolean;
public viewId: string;
public books: BookModel[];
@@ -37,29 +36,24 @@ export class BookTreeViewProvider implements vscode.TreeDataProvider<BookTreeIte
this._openAsUntitled = openAsUntitled;
this._extensionContext = extensionContext;
this.books = [];
this.initialize(workspaceFolders.map(a => a.uri.fsPath)).catch(e => console.error(e));
this.initialize(workspaceFolders).catch(e => console.error(e));
this.viewId = view;
this.prompter = new CodeAdapter();
}
private async initialize(bookPaths: string[]): Promise<void> {
private async initialize(workspaceFolders: vscode.WorkspaceFolder[]): Promise<void> {
await vscode.commands.executeCommand('setContext', 'unsavedBooks', this._openAsUntitled);
await Promise.all(bookPaths.map(async (bookPath) => {
let book: BookModel = new BookModel(bookPath, this._openAsUntitled, this._extensionContext);
await book.initializeContents();
this.books.push(book);
if (!this.currentBook) {
this.currentBook = book;
await Promise.all(workspaceFolders.map(async (workspaceFolder) => {
try {
await this.createAndAddBookModel(workspaceFolder.uri.fsPath);
} catch {
// no-op, not all workspace folders are going to be valid books
}
}));
this._initializeDeferred.resolve();
}
public get onReadAllTOCFiles(): vscode.Event<void> {
return this._onReadAllTOCFiles.event;
}
public get initialized(): Promise<void> {
return this._initializeDeferred.promise;
}
@@ -73,7 +67,7 @@ export class BookTreeViewProvider implements vscode.TreeDataProvider<BookTreeIte
await this.showPreviewFile(urlToOpen);
}
else {
await this.initialize([bookPath]);
await this.createAndAddBookModel(bookPath);
let bookViewer = vscode.window.createTreeView(this.viewId, { showCollapseAll: true, treeDataProvider: this });
this.currentBook = this.books.filter(book => book.bookPath === bookPath)[0];
bookViewer.reveal(this.currentBook.bookItems[0], { expand: vscode.TreeItemCollapsibleState.Expanded, focus: true, select: true });
@@ -84,6 +78,20 @@ export class BookTreeViewProvider implements vscode.TreeDataProvider<BookTreeIte
}
}
/**
* Creates a model for the specified folder path and adds it to the known list of books if we
* were able to successfully parse it.
* @param bookPath The path to the book folder to create the model for
*/
private async createAndAddBookModel(bookPath: string): Promise<void> {
const book: BookModel = new BookModel(bookPath, this._openAsUntitled, this._extensionContext);
await book.initializeContents();
this.books.push(book);
if (!this.currentBook) {
this.currentBook = book;
}
}
async showPreviewFile(urlToOpen?: string): Promise<void> {
if (this.currentBook) {
const bookRoot = this.currentBook.bookItems[0];