mirror of
https://github.com/microsoft/vscode.git
synced 2026-04-20 00:28:52 +01:00
Remove old code, simplify properties (#237512)
This commit is contained in:
committed by
GitHub
parent
3fbcc9ac75
commit
691eaea3bd
@@ -5,11 +5,6 @@
|
||||
|
||||
import { CancellationError, CancellationToken, Disposable, Event } from 'vscode';
|
||||
|
||||
/**
|
||||
* Can be passed into the Delayed to defer using a microtask
|
||||
*/
|
||||
export const MicrotaskDelay = Symbol('MicrotaskDelay');
|
||||
|
||||
export class SequencerByKey<TKey> {
|
||||
|
||||
private promiseMap = new Map<TKey, Promise<unknown>>();
|
||||
@@ -57,7 +52,7 @@ export class IntervalTimer extends Disposable {
|
||||
* Returns a promise that rejects with an {@CancellationError} as soon as the passed token is cancelled.
|
||||
* @see {@link raceCancellation}
|
||||
*/
|
||||
export function raceCancellationError<T>(promise: Promise<T>, token: CancellationToken): Promise<T> {
|
||||
function raceCancellationError<T>(promise: Promise<T>, token: CancellationToken): Promise<T> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const ref = token.onCancellationRequested(() => {
|
||||
ref.dispose();
|
||||
@@ -67,7 +62,7 @@ export function raceCancellationError<T>(promise: Promise<T>, token: Cancellatio
|
||||
});
|
||||
}
|
||||
|
||||
export function raceTimeoutError<T>(promise: Promise<T>, timeout: number): Promise<T> {
|
||||
function raceTimeoutError<T>(promise: Promise<T>, timeout: number): Promise<T> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const ref = setTimeout(() => {
|
||||
reject(new CancellationError());
|
||||
@@ -80,138 +75,12 @@ export function raceCancellationAndTimeoutError<T>(promise: Promise<T>, token: C
|
||||
return raceCancellationError(raceTimeoutError(promise, timeout), token);
|
||||
}
|
||||
|
||||
interface IScheduledLater extends Disposable {
|
||||
isTriggered(): boolean;
|
||||
}
|
||||
|
||||
const timeoutDeferred = (timeout: number, fn: () => void): IScheduledLater => {
|
||||
let scheduled = true;
|
||||
const handle = setTimeout(() => {
|
||||
scheduled = false;
|
||||
fn();
|
||||
}, timeout);
|
||||
return {
|
||||
isTriggered: () => scheduled,
|
||||
dispose: () => {
|
||||
clearTimeout(handle);
|
||||
scheduled = false;
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
const microtaskDeferred = (fn: () => void): IScheduledLater => {
|
||||
let scheduled = true;
|
||||
queueMicrotask(() => {
|
||||
if (scheduled) {
|
||||
scheduled = false;
|
||||
fn();
|
||||
}
|
||||
});
|
||||
|
||||
return {
|
||||
isTriggered: () => scheduled,
|
||||
dispose: () => { scheduled = false; },
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
* A helper to delay (debounce) execution of a task that is being requested often.
|
||||
*
|
||||
* Following the throttler, now imagine the mail man wants to optimize the number of
|
||||
* trips proactively. The trip itself can be long, so he decides not to make the trip
|
||||
* as soon as a letter is submitted. Instead he waits a while, in case more
|
||||
* letters are submitted. After said waiting period, if no letters were submitted, he
|
||||
* decides to make the trip. Imagine that N more letters were submitted after the first
|
||||
* one, all within a short period of time between each other. Even though N+1
|
||||
* submissions occurred, only 1 delivery was made.
|
||||
*
|
||||
* The delayer offers this behavior via the trigger() method, into which both the task
|
||||
* to be executed and the waiting period (delay) must be passed in as arguments. Following
|
||||
* the example:
|
||||
*
|
||||
* const delayer = new Delayer(WAITING_PERIOD);
|
||||
* const letters = [];
|
||||
*
|
||||
* function letterReceived(l) {
|
||||
* letters.push(l);
|
||||
* delayer.trigger(() => { return makeTheTrip(); });
|
||||
* }
|
||||
*/
|
||||
export class Delayer<T> implements Disposable {
|
||||
|
||||
private deferred: IScheduledLater | null;
|
||||
private completionPromise: Promise<any> | null;
|
||||
private doResolve: ((value?: any | Promise<any>) => void) | null;
|
||||
private doReject: ((err: any) => void) | null;
|
||||
private task: (() => T | Promise<T>) | null;
|
||||
|
||||
constructor(public defaultDelay: number | typeof MicrotaskDelay) {
|
||||
this.deferred = null;
|
||||
this.completionPromise = null;
|
||||
this.doResolve = null;
|
||||
this.doReject = null;
|
||||
this.task = null;
|
||||
}
|
||||
|
||||
trigger(task: () => T | Promise<T>, delay = this.defaultDelay): Promise<T> {
|
||||
this.task = task;
|
||||
this.cancelTimeout();
|
||||
|
||||
if (!this.completionPromise) {
|
||||
this.completionPromise = new Promise((resolve, reject) => {
|
||||
this.doResolve = resolve;
|
||||
this.doReject = reject;
|
||||
}).then(() => {
|
||||
this.completionPromise = null;
|
||||
this.doResolve = null;
|
||||
if (this.task) {
|
||||
const task = this.task;
|
||||
this.task = null;
|
||||
return task();
|
||||
}
|
||||
return undefined;
|
||||
});
|
||||
}
|
||||
|
||||
const fn = () => {
|
||||
this.deferred = null;
|
||||
this.doResolve?.(null);
|
||||
};
|
||||
|
||||
this.deferred = delay === MicrotaskDelay ? microtaskDeferred(fn) : timeoutDeferred(delay, fn);
|
||||
|
||||
return this.completionPromise;
|
||||
}
|
||||
|
||||
isTriggered(): boolean {
|
||||
return !!this.deferred?.isTriggered();
|
||||
}
|
||||
|
||||
cancel(): void {
|
||||
this.cancelTimeout();
|
||||
|
||||
if (this.completionPromise) {
|
||||
this.doReject?.(new CancellationError());
|
||||
this.completionPromise = null;
|
||||
}
|
||||
}
|
||||
|
||||
private cancelTimeout(): void {
|
||||
this.deferred?.dispose();
|
||||
this.deferred = null;
|
||||
}
|
||||
|
||||
dispose(): void {
|
||||
this.cancel();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Given an event, returns another event which only fires once.
|
||||
*
|
||||
* @param event The event source for the new event.
|
||||
*/
|
||||
export function once<T>(event: Event<T>): Event<T> {
|
||||
function once<T>(event: Event<T>): Event<T> {
|
||||
return (listener, thisArgs = null, disposables?) => {
|
||||
// we need this, in case the event fires during the listener call
|
||||
let didFire = false;
|
||||
|
||||
Reference in New Issue
Block a user