From 4022f443a15a0fd6c500bfa63ad23d57ba10113b Mon Sep 17 00:00:00 2001 From: Eric Amodio Date: Sat, 6 Jan 2018 15:53:23 -0500 Subject: [PATCH] Fixes tracked debounce --- src/currentLineController.ts | 4 ++-- src/git/gitContextTracker.ts | 8 ++++---- src/system/function.ts | 17 +++++++++-------- 3 files changed, 15 insertions(+), 14 deletions(-) diff --git a/src/currentLineController.ts b/src/currentLineController.ts index 22995fd..7b658e4 100644 --- a/src/currentLineController.ts +++ b/src/currentLineController.ts @@ -1,5 +1,5 @@ 'use strict'; -import { Functions, IDeferred } from './system'; +import { Functions, IDeferrable } from './system'; import { CancellationToken, ConfigurationChangeEvent, debug, DecorationRangeBehavior, DecorationRenderOptions, Disposable, ExtensionContext, Hover, HoverProvider, languages, Position, Range, StatusBarAlignment, StatusBarItem, TextDocument, TextEditor, TextEditorDecorationType, TextEditorSelectionChangeEvent, window } from 'vscode'; import { AnnotationController, FileAnnotationType } from './annotations/annotationController'; import { Annotations } from './annotations/annotations'; @@ -36,7 +36,7 @@ export class CurrentLineController extends Disposable { private _isAnnotating: boolean = false; private _statusBarItem: StatusBarItem | undefined; private _trackCurrentLineDisposable: Disposable | undefined; - private _updateBlameDebounced: ((line: number, editor: TextEditor) => Promise) & IDeferred; + private _updateBlameDebounced: ((line: number, editor: TextEditor) => Promise) & IDeferrable; private _uri: GitUri; constructor( diff --git a/src/git/gitContextTracker.ts b/src/git/gitContextTracker.ts index f4c345d..0d0c96c 100644 --- a/src/git/gitContextTracker.ts +++ b/src/git/gitContextTracker.ts @@ -1,5 +1,5 @@ 'use strict'; -import { Functions, IDeferred } from '../system'; +import { Functions, IDeferrable } from '../system'; import { ConfigurationChangeEvent, Disposable, Event, EventEmitter, Range, TextDocumentChangeEvent, TextEditor, TextEditorSelectionChangeEvent, window, workspace } from 'vscode'; import { TextDocumentComparer } from '../comparers'; import { configuration } from '../configuration'; @@ -70,10 +70,10 @@ export class GitContextTracker extends Disposable { private readonly _context: Context = { state: { dirty: false } }; private readonly _disposable: Disposable; private _listenersDisposable: Disposable | undefined; - private _fireDirtyStateChangedDebounced: (() => void) & IDeferred; + private _fireDirtyStateChangedDebounced: (() => void) & IDeferrable; - private _checkLineDirtyStateChangedDebounced: (() => void) & IDeferred; - private _fireLineDirtyStateChangedDebounced: (() => void) & IDeferred; + private _checkLineDirtyStateChangedDebounced: (() => void) & IDeferrable; + private _fireLineDirtyStateChangedDebounced: (() => void) & IDeferrable; private _insiders = false; diff --git a/src/system/function.ts b/src/system/function.ts index 3d5ad9c..d9524ad 100644 --- a/src/system/function.ts +++ b/src/system/function.ts @@ -2,9 +2,10 @@ const _debounce = require('lodash.debounce'); const _once = require('lodash.once'); -export interface IDeferred { +export interface IDeferrable { cancel(): void; flush(...args: any[]): void; + pending?: () => boolean; } interface IPropOfValue { @@ -13,22 +14,22 @@ interface IPropOfValue { } export namespace Functions { - export function debounce(fn: T, wait?: number, options?: { leading?: boolean, maxWait?: number, track?: boolean, trailing?: boolean }): T & IDeferred & { pending?: () => boolean } { + export function debounce(fn: T, wait?: number, options?: { leading?: boolean, maxWait?: number, track?: boolean, trailing?: boolean }): T & IDeferrable { const { track, ...opts } = { track: false, ...(options || {}) } as { leading?: boolean, maxWait?: number, track?: boolean, trailing?: boolean }; if (track !== true) return _debounce(fn, wait, opts); let pending = false; - const debounced = _debounce(function() { + const debounced = _debounce(function(this: any) { pending = false; - return fn.apply(null, arguments); - } as any as T, wait, options) as T & IDeferred; + return fn.apply(this, arguments); + } as any as T, wait, options) as T & IDeferrable; - const tracked = function() { + const tracked = function(this: any) { pending = true; - return debounced.apply(null, arguments); - } as any as T & IDeferred & { pending(): boolean}; + return debounced.apply(this, arguments); + } as any as T & IDeferrable; tracked.pending = function() { return pending; }; tracked.cancel = function() { return debounced.cancel.apply(debounced, arguments); };