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 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<BookTreeIte
// add file watcher on toc file.
if (!isNotebook) {
fsw.watch(path.join(this.currentBook.bookPath, '_data', 'toc.yml'), async (event, filename) => {
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<BookTreeIte
}
}
@debounce(1500)
async fireBookRefresh(book: BookModel): Promise<void> {
await book.initializeContents().then(() => {
this._onDidChangeTreeData.fire();
});
}
async closeBook(book: BookTreeItem): Promise<void> {
// remove book from the saved books
let deletedBook: BookModel;
@@ -160,7 +166,7 @@ export class BookTreeViewProvider implements vscode.TreeDataProvider<BookTreeIte
} finally {
// remove watch on toc file.
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;
}
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",
"compilerOptions": {
"experimentalDecorators": true,
"lib": [
"dom"
],