mirror of
https://github.com/microsoft/vscode.git
synced 2026-04-29 21:11:38 +01:00
* Use MD LS for resolving all document links This switches the markdown extension to use the markdown language service when resolving the link. This lets us delete a lot of code that was duplicated between the extension and the LS * Pick up new ls version
65 lines
1.7 KiB
TypeScript
65 lines
1.7 KiB
TypeScript
/*---------------------------------------------------------------------------------------------
|
|
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
* Licensed under the MIT License. See License.txt in the project root for license information.
|
|
*--------------------------------------------------------------------------------------------*/
|
|
|
|
export interface ITask<T> {
|
|
(): T;
|
|
}
|
|
|
|
export class Delayer<T> {
|
|
|
|
public defaultDelay: number;
|
|
private timeout: any; // Timer
|
|
private completionPromise: Promise<T | null> | null;
|
|
private onSuccess: ((value: T | PromiseLike<T> | undefined) => void) | null;
|
|
private task: ITask<T> | null;
|
|
|
|
constructor(defaultDelay: number) {
|
|
this.defaultDelay = defaultDelay;
|
|
this.timeout = null;
|
|
this.completionPromise = null;
|
|
this.onSuccess = null;
|
|
this.task = null;
|
|
}
|
|
|
|
dispose() {
|
|
this.cancelTimeout();
|
|
}
|
|
|
|
public trigger(task: ITask<T>, delay: number = this.defaultDelay): Promise<T | null> {
|
|
this.task = task;
|
|
if (delay >= 0) {
|
|
this.cancelTimeout();
|
|
}
|
|
|
|
if (!this.completionPromise) {
|
|
this.completionPromise = new Promise<T | undefined>((resolve) => {
|
|
this.onSuccess = resolve;
|
|
}).then(() => {
|
|
this.completionPromise = null;
|
|
this.onSuccess = null;
|
|
const result = this.task && this.task();
|
|
this.task = null;
|
|
return result;
|
|
});
|
|
}
|
|
|
|
if (delay >= 0 || this.timeout === null) {
|
|
this.timeout = setTimeout(() => {
|
|
this.timeout = null;
|
|
this.onSuccess?.(undefined);
|
|
}, delay >= 0 ? delay : this.defaultDelay);
|
|
}
|
|
|
|
return this.completionPromise;
|
|
}
|
|
|
|
private cancelTimeout(): void {
|
|
if (this.timeout !== null) {
|
|
clearTimeout(this.timeout);
|
|
this.timeout = null;
|
|
}
|
|
}
|
|
}
|