Migrate to private class properties/methods
This commit is contained in:
@@ -20,15 +20,13 @@
|
||||
import { drop } from './drop';
|
||||
|
||||
export class LatestQueue {
|
||||
private isRunning: boolean;
|
||||
|
||||
private queuedTask?: () => Promise<void>;
|
||||
|
||||
private onceEmptyCallbacks: Array<() => unknown>;
|
||||
#isRunning: boolean;
|
||||
#queuedTask?: () => Promise<void>;
|
||||
#onceEmptyCallbacks: Array<() => unknown>;
|
||||
|
||||
constructor() {
|
||||
this.isRunning = false;
|
||||
this.onceEmptyCallbacks = [];
|
||||
this.#isRunning = false;
|
||||
this.#onceEmptyCallbacks = [];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -39,25 +37,25 @@ export class LatestQueue {
|
||||
* tasks will be enqueued at a time.
|
||||
*/
|
||||
add(task: () => Promise<void>): void {
|
||||
if (this.isRunning) {
|
||||
this.queuedTask = task;
|
||||
if (this.#isRunning) {
|
||||
this.#queuedTask = task;
|
||||
} else {
|
||||
this.isRunning = true;
|
||||
this.#isRunning = true;
|
||||
drop(
|
||||
task().finally(() => {
|
||||
this.isRunning = false;
|
||||
this.#isRunning = false;
|
||||
|
||||
const { queuedTask } = this;
|
||||
const queuedTask = this.#queuedTask;
|
||||
if (queuedTask) {
|
||||
this.queuedTask = undefined;
|
||||
this.#queuedTask = undefined;
|
||||
this.add(queuedTask);
|
||||
} else {
|
||||
try {
|
||||
this.onceEmptyCallbacks.forEach(callback => {
|
||||
this.#onceEmptyCallbacks.forEach(callback => {
|
||||
callback();
|
||||
});
|
||||
} finally {
|
||||
this.onceEmptyCallbacks = [];
|
||||
this.#onceEmptyCallbacks = [];
|
||||
}
|
||||
}
|
||||
})
|
||||
@@ -69,6 +67,6 @@ export class LatestQueue {
|
||||
* Adds a callback to be called the first time the queue goes from "running" to "empty".
|
||||
*/
|
||||
onceEmpty(callback: () => unknown): void {
|
||||
this.onceEmptyCallbacks.push(callback);
|
||||
this.#onceEmptyCallbacks.push(callback);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user