diff --git a/extensions/notebook/src/book/bookTreeView.ts b/extensions/notebook/src/book/bookTreeView.ts index 8d762e4426..42869eb393 100644 --- a/extensions/notebook/src/book/bookTreeView.ts +++ b/extensions/notebook/src/book/bookTreeView.ts @@ -8,7 +8,6 @@ import * as vscode from 'vscode'; import * as path from 'path'; import * as fs from 'fs-extra'; import * as constants from '../common/constants'; -import * as fsw from 'fs'; import { IPrompter, QuestionTypes, IQuestion } from '../prompts/question'; import CodeAdapter from '../prompts/adapter'; import { BookTreeItem, BookTreeItemType } from './bookTreeItem'; @@ -19,6 +18,7 @@ import * as loc from '../common/localizedConstants'; import { ApiWrapper } from '../common/apiWrapper'; import * as glob from 'fast-glob'; import { isNullOrUndefined } from 'util'; +import { debounce } from '../common/utils'; const Content = 'content'; @@ -126,13 +126,12 @@ export class BookTreeViewProvider implements vscode.TreeDataProvider { - if (event === 'change') { - let changedBook = this.books.find(book => book.bookPath === bookPath); - await changedBook.initializeContents().then(() => { - this._onDidChangeTreeData.fire(changedBook.bookItems[0]); - }); - this._onDidChangeTreeData.fire(); + fs.watchFile(path.join(bookPath, '_data', 'toc.yml'), async (curr, prev) => { + if (curr.mtime > prev.mtime) { + let book = this.books.find(book => book.bookPath === bookPath); + if (book) { + this.fireBookRefresh(book); + } } }); } @@ -141,6 +140,13 @@ export class BookTreeViewProvider implements vscode.TreeDataProvider { + await book.initializeContents().then(() => { + this._onDidChangeTreeData.fire(); + }); + } + async closeBook(book: BookTreeItem): Promise { // remove book from the saved books let deletedBook: BookModel; @@ -160,7 +166,7 @@ export class BookTreeViewProvider implements vscode.TreeDataProvider { + const timerKey = `$debounce$${key}`; + + return function (this: any, ...args: any[]) { + clearTimeout(this[timerKey]); + this[timerKey] = setTimeout(() => fn.apply(this, args), delay); + }; + }); +} + +function decorate(decorator: (fn: Function, key: string) => Function): Function { + return (_target: any, key: string, descriptor: any) => { + let fnKey: string | null = null; + let fn: Function | null = null; + + if (typeof descriptor.value === 'function') { + fnKey = 'value'; + fn = descriptor.value; + } else if (typeof descriptor.get === 'function') { + fnKey = 'get'; + fn = descriptor.get; + } + + if (!fn || !fnKey) { + throw new Error('not supported'); + } + + descriptor[fnKey] = decorator(fn, key); + }; +} diff --git a/extensions/notebook/tsconfig.json b/extensions/notebook/tsconfig.json index af90070c7d..79f9699f6e 100644 --- a/extensions/notebook/tsconfig.json +++ b/extensions/notebook/tsconfig.json @@ -1,6 +1,7 @@ { "extends": "../shared.tsconfig.json", "compilerOptions": { + "experimentalDecorators": true, "lib": [ "dom" ],