Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions src/core/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,10 @@ export default class ExtensionCore extends EventEmitter<Events> implements Dispo
return workspace.getWorkspaceFolder(Uri.file(filePath))?.uri;
}

if (!options.silent) {
if (options.token && options.token.isCancellationRequested) return;
const picked = await window.showWorkspaceFolderPick();
if (picked) return picked.uri;
}
if (options.silent || options.token?.isCancellationRequested) return;

const picked = await window.showWorkspaceFolderPick();
if (picked) return picked.uri;
}

async activate(context: ExtensionContext = this.context) {
Expand Down
61 changes: 38 additions & 23 deletions src/structures/Timeout.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,51 @@
const MAX_TIMER_DELAY = 2147483647;
const _maxTimerDelay = 2147483647;

export default class Timeout {
constructor(callback: () => unknown, delay: number = 0) {
export default class Timeout implements NodeJS.Timeout {
constructor(callback: () => unknown, delay: number = 1) {
this.#callback = callback;
this.#remain = this.#delay = delay;
this.#start();
}

_onTimeout(...args: any[]): void {
this.#timeout._onTimeout(...args);
}

#callback: () => unknown;
#called: boolean = false;
#called!: boolean;
#delay: number;
#remain!: number;
#timeout!: NodeJS.Timeout;

#start() {
this.#called = false;
if (this.#remain > MAX_TIMER_DELAY) {
this.#timeout = setTimeout(() => {
this.#remain -= MAX_TIMER_DELAY;
if (this.#remain > MAX_TIMER_DELAY)
this.#timeout.refresh();
else this.#start();
}, MAX_TIMER_DELAY).unref();
if (this.#remain > _maxTimerDelay) {
this.#timeout = setTimeout(this.#autoRefreshCallback.bind(this), _maxTimerDelay).unref();
} else {
this.#timeout = setTimeout(() => {
this.#called = true;
this.#callback();
}, this.#remain).unref();
this.#timeout = setTimeout(this.#lastCallback.bind(this), this.#remain).unref();
}
}

#autoRefreshCallback() {
this.#remain -= _maxTimerDelay;
if (this.#remain > _maxTimerDelay) return this.#timeout.refresh();
this.#start();
}

#lastCallback() {
this.#called = true;
this.#callback();
}

get delay() { return this.#delay; }
/** @readonly */
get remain() { return this.#remain; }

set delay(delay) {
const diff = delay - this.#delay;

this.#delay = delay;

const diff = delay - this.#delay;

this.#remain += diff;

if (this.#called) return;
Expand All @@ -47,33 +54,41 @@ export default class Timeout {
}

/** If true, the `Timeout` object will keep the Node.js event loop active. */
get hasRef() {
return this.#timeout.hasRef();
}
hasRef() { return this.#timeout.hasRef(); }

ref() {
this.#timeout.ref();
return this;
}

unref() {
this.#timeout.unref();
return this;
}

reset() {
this.#remain = this.#delay;
this.restart();
}

refresh() {
this.#timeout.refresh();
return this;
}

restart() {
this.close();
this.#start();
}

close() {
clearTimeout(this.#timeout);
return this;
}

dispose() {
this.close();
}
dispose() { this.#timeout[Symbol.dispose](); }

[Symbol.toPrimitive]() { return this.#timeout[Symbol.toPrimitive](); }

[Symbol.dispose]() { this.#timeout[Symbol.dispose](); }
}
29 changes: 16 additions & 13 deletions src/structures/TimerMap.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Disposable } from "vscode";

const MAX_TIMER_DELAY = 2147483647;
const _maxTimerDelay = 2147483647;

export interface TimerMapOptions {
/**
Expand All @@ -13,9 +13,9 @@ export interface TimerMapOptions {
autoUnref?: boolean
}

export default class TimerMap<K, V extends NodeJS.Timeout = NodeJS.Timeout> extends Map<K, V> implements Disposable {
export default class TimerMap<K = keyof any, T extends NodeJS.Timeout = NodeJS.Timeout> extends Map<K, T> implements Disposable {
constructor(
entries?: readonly (readonly [K, V])[] | Iterable<readonly [K, V]> | null,
entries?: readonly (readonly [K, T])[] | Iterable<readonly [K, T]> | null,
options?: TimerMapOptions,
) {
super(entries);
Expand Down Expand Up @@ -50,7 +50,7 @@ export default class TimerMap<K, V extends NodeJS.Timeout = NodeJS.Timeout> exte
return this.get(key)?.refresh();
}

set(key: K, timeout: V): this {
set(key: K, timeout: T): this {
this.clearTimeout(key);
return super.set(key, timeout);
}
Expand All @@ -72,25 +72,28 @@ export default class TimerMap<K, V extends NodeJS.Timeout = NodeJS.Timeout> exte
setTimeout(key: K, callback: () => void, delay: number = 1): void {
this.clearTimeout(key);

if (delay > MAX_TIMER_DELAY) return this._autoRefresh(key, callback, delay);
if (delay > _maxTimerDelay) return this.#autoRefresh(key, callback, delay);

const timer = setTimeout(this.#wrapCallback(key, callback), delay);

const timer = setTimeout(this._wrapCallback(key, callback), delay);
if (this.autoUnref) timer.unref();
super.set(key, timer as V);

super.set(key, timer as T);
}

protected _autoRefresh(key: K, callback: () => void, delay: number) {
#autoRefresh(key: K, callback: () => void, delay: number) {
const timer = setTimeout(() => {
delay -= MAX_TIMER_DELAY;
if (delay > MAX_TIMER_DELAY) return timer.refresh();
delay -= _maxTimerDelay;
if (delay > _maxTimerDelay) return timer.refresh();
this.setTimeout(key, callback, delay);
}, MAX_TIMER_DELAY);
}, _maxTimerDelay);

if (this.autoUnref) timer.unref();
super.set(key, timer as V);

super.set(key, timer as T);
}

protected _wrapCallback(key: K, callback: () => void) {
#wrapCallback(key: K, callback: () => void) {
return () => {
super.delete(key);
callback();
Expand Down
Loading