From 037e957a87db314e2209a502c75d9a8d4f6f6138 Mon Sep 17 00:00:00 2001 From: Davis Vaughan Date: Mon, 9 Sep 2024 10:34:42 -0400 Subject: [PATCH] Delete `queue.ts` --- apps/vscode/src/host/queue.ts | 118 ---------------------------------- 1 file changed, 118 deletions(-) delete mode 100644 apps/vscode/src/host/queue.ts diff --git a/apps/vscode/src/host/queue.ts b/apps/vscode/src/host/queue.ts deleted file mode 100644 index 1f49d7fb..00000000 --- a/apps/vscode/src/host/queue.ts +++ /dev/null @@ -1,118 +0,0 @@ -/* - * queue.ts - * - * Copyright (C) 2024 by Posit Software, PBC - * - * Unless you have received this program directly from Posit Software pursuant - * to the terms of a commercial license agreement with Posit Software, then - * this program is licensed to you under the terms of version 3 of the - * GNU Affero General Public License. This program is distributed WITHOUT - * ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT, - * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the - * AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details. - * - */ - -import { Disposable, EventEmitter } from 'vscode'; - -export type TaskId = number; -export type TaskCallback = () => Promise; - -export interface Task { - id: TaskId, - callback: TaskCallback -} - -/** - * A task queue that maintains the ordering of tasks submitted to the queue - */ -export class TaskQueue implements Disposable { - private _id: TaskId = 0; - private _tasks: Task[] = []; - private _running = false; - - private readonly _onDidFinishTask = new EventEmitter(); - private readonly onDidFinishTask = this._onDidFinishTask.event; - - dispose(): void { - this._onDidFinishTask.dispose(); - } - - /** - * Enqueue a `callback` for execution - * - * Returns a promise that resolves when the task finishes - */ - async enqueue(callback: TaskCallback): Promise { - // Create an official task out of this callback - const task = this.task(callback); - - // Create a promise that resolves when the task is done - const out = new Promise((resolve, _reject) => { - const handle = this.onDidFinishTask((id) => { - if (task.id === id) { - handle.dispose(); - resolve(); - } - }); - }); - - // Put the task on the back of the queue. - // Immediately run it if possible. - this._tasks.push(task); - await this.run(); - - return out; - } - - /** - * Runs a task in the queue - * - * If we are currently running something else, bails. `run()` will be called again - * once the other task finishes. - */ - private async run() { - if (this._running) { - // Someone else is running, we will get recalled once they finish - return; - } - - // Pop off the first task in the queue - const task = this._tasks.shift(); - - if (task === undefined) { - // Nothing to run right now - return; - } - - this._running = true; - - try { - await task.callback(); - } finally { - this._running = false; - // Let the promise know this task is done - this._onDidFinishTask.fire(task.id); - } - - // Run next task if one is in the queue - await this.run(); - } - - /** - * Construct a new `Task` that can be pushed onto the queue - */ - private task(callback: TaskCallback): Task { - const id = this.id(); - return { id, callback } - } - - /** - * Retrives an `id` to be used with the next task - */ - private id(): TaskId { - let id = this._id; - this._id++; - return id; - } -}