Browse Source

Fixes debounce typings

main
Eric Amodio 4 years ago
parent
commit
9d90cbc798
3 changed files with 12 additions and 12 deletions
  1. +9
    -9
      src/system/function.ts
  2. +2
    -2
      src/trackers/documentTracker.ts
  3. +1
    -1
      src/trackers/lineTracker.ts

+ 9
- 9
src/system/function.ts View File

@ -2,9 +2,10 @@
import { Disposable } from 'vscode'; import { Disposable } from 'vscode';
import { debounce as _debounce, once as _once } from 'lodash-es'; import { debounce as _debounce, once as _once } from 'lodash-es';
export interface Deferrable {
export interface Deferrable<T extends (...args: any[]) => any> {
(...args: Parameters<T>): ReturnType<T> | undefined;
cancel(): void; cancel(): void;
flush(...args: any[]): void;
flush(): ReturnType<T> | undefined;
pending?(): boolean; pending?(): boolean;
} }
@ -38,7 +39,7 @@ export namespace Functions {
fn: T, fn: T,
wait?: number, wait?: number,
options?: DebounceOptions, options?: DebounceOptions,
): T & Deferrable {
): Deferrable<T> {
const { track, ...opts }: DebounceOptions = { const { track, ...opts }: DebounceOptions = {
track: false, track: false,
...(options ?? {}), ...(options ?? {}),
@ -55,12 +56,12 @@ export namespace Functions {
} as any) as T, } as any) as T,
wait, wait,
options, options,
) as T & Deferrable;
);
const tracked = (function (this: any, ...args: any[]) {
const tracked: Deferrable<T> = function (this: any, ...args: Parameters<T>) {
pending = true; pending = true;
return debounced.apply(this, args); return debounced.apply(this, args);
} as any) as T & Deferrable;
} as any;
tracked.pending = function () { tracked.pending = function () {
return pending; return pending;
@ -68,9 +69,8 @@ export namespace Functions {
tracked.cancel = function () { tracked.cancel = function () {
return debounced.cancel.apply(debounced); return debounced.cancel.apply(debounced);
}; };
tracked.flush = function (...args: any[]) {
// eslint-disable-next-line prefer-spread
return debounced.flush.apply(debounced, args);
tracked.flush = function () {
return debounced.flush.apply(debounced);
}; };
return tracked; return tracked;

+ 2
- 2
src/trackers/documentTracker.ts View File

@ -315,8 +315,8 @@ export class DocumentTracker implements Disposable {
return doc; return doc;
} }
private _dirtyIdleTriggeredDebounced: (((e: DocumentDirtyIdleTriggerEvent<T>) => void) & Deferrable) | undefined;
private _dirtyStateChangedDebounced: (((e: DocumentDirtyStateChangeEvent<T>) => void) & Deferrable) | undefined;
private _dirtyIdleTriggeredDebounced: Deferrable<(e: DocumentDirtyIdleTriggerEvent<T>) => void> | undefined;
private _dirtyStateChangedDebounced: Deferrable<(e: DocumentDirtyStateChangeEvent<T>) => void> | undefined;
private fireDocumentDirtyStateChanged(e: DocumentDirtyStateChangeEvent<T>) { private fireDocumentDirtyStateChanged(e: DocumentDirtyStateChangeEvent<T>) {
if (e.dirty) { if (e.dirty) {
setImmediate(async () => { setImmediate(async () => {

+ 1
- 1
src/trackers/lineTracker.ts View File

@ -199,7 +199,7 @@ export class LineTracker implements Disposable {
this.onLinesChanged({ editor: this._editor, selections: this.selections, reason: reason }); this.onLinesChanged({ editor: this._editor, selections: this.selections, reason: reason });
} }
private _linesChangedDebounced: (((e: LinesChangeEvent) => void) & Deferrable) | undefined;
private _linesChangedDebounced: Deferrable<(e: LinesChangeEvent) => void> | undefined;
private onLinesChanged(e: LinesChangeEvent) { private onLinesChanged(e: LinesChangeEvent) {
if (e.selections === undefined) { if (e.selections === undefined) {

Loading…
Cancel
Save