replaced watch with watchFile (#9873)

* replaced watch with watchFile

* testing debounce

* changed the beounce delay to 1500
This commit is contained in:
Maddy
2020-04-15 10:03:48 -07:00
committed by GitHub
parent 3cb49686a5
commit a58d4d7d05
3 changed files with 48 additions and 9 deletions

View File

@@ -8,7 +8,6 @@ import * as vscode from 'vscode';
import * as path from 'path'; import * as path from 'path';
import * as fs from 'fs-extra'; import * as fs from 'fs-extra';
import * as constants from '../common/constants'; import * as constants from '../common/constants';
import * as fsw from 'fs';
import { IPrompter, QuestionTypes, IQuestion } from '../prompts/question'; import { IPrompter, QuestionTypes, IQuestion } from '../prompts/question';
import CodeAdapter from '../prompts/adapter'; import CodeAdapter from '../prompts/adapter';
import { BookTreeItem, BookTreeItemType } from './bookTreeItem'; import { BookTreeItem, BookTreeItemType } from './bookTreeItem';
@@ -19,6 +18,7 @@ import * as loc from '../common/localizedConstants';
import { ApiWrapper } from '../common/apiWrapper'; import { ApiWrapper } from '../common/apiWrapper';
import * as glob from 'fast-glob'; import * as glob from 'fast-glob';
import { isNullOrUndefined } from 'util'; import { isNullOrUndefined } from 'util';
import { debounce } from '../common/utils';
const Content = 'content'; const Content = 'content';
@@ -126,13 +126,12 @@ export class BookTreeViewProvider implements vscode.TreeDataProvider<BookTreeIte
// add file watcher on toc file. // add file watcher on toc file.
if (!isNotebook) { if (!isNotebook) {
fsw.watch(path.join(this.currentBook.bookPath, '_data', 'toc.yml'), async (event, filename) => { fs.watchFile(path.join(bookPath, '_data', 'toc.yml'), async (curr, prev) => {
if (event === 'change') { if (curr.mtime > prev.mtime) {
let changedBook = this.books.find(book => book.bookPath === bookPath); let book = this.books.find(book => book.bookPath === bookPath);
await changedBook.initializeContents().then(() => { if (book) {
this._onDidChangeTreeData.fire(changedBook.bookItems[0]); this.fireBookRefresh(book);
}); }
this._onDidChangeTreeData.fire();
} }
}); });
} }
@@ -141,6 +140,13 @@ export class BookTreeViewProvider implements vscode.TreeDataProvider<BookTreeIte
} }
} }
@debounce(1500)
async fireBookRefresh(book: BookModel): Promise<void> {
await book.initializeContents().then(() => {
this._onDidChangeTreeData.fire();
});
}
async closeBook(book: BookTreeItem): Promise<void> { async closeBook(book: BookTreeItem): Promise<void> {
// remove book from the saved books // remove book from the saved books
let deletedBook: BookModel; let deletedBook: BookModel;
@@ -160,7 +166,7 @@ export class BookTreeViewProvider implements vscode.TreeDataProvider<BookTreeIte
} finally { } finally {
// remove watch on toc file. // remove watch on toc file.
if (deletedBook && !deletedBook.isNotebook) { if (deletedBook && !deletedBook.isNotebook) {
fsw.unwatchFile(path.join(deletedBook.bookPath, '_data', 'toc.yml')); fs.unwatchFile(path.join(deletedBook.bookPath, '_data', 'toc.yml'));
} }
} }
} }

View File

@@ -262,3 +262,35 @@ export function getIgnoreSslVerificationConfigSetting(): boolean {
} }
return true; return true;
} }
export function debounce(delay: number): Function {
return decorate((fn, key) => {
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);
};
}

View File

@@ -1,6 +1,7 @@
{ {
"extends": "../shared.tsconfig.json", "extends": "../shared.tsconfig.json",
"compilerOptions": { "compilerOptions": {
"experimentalDecorators": true,
"lib": [ "lib": [
"dom" "dom"
], ],