* Add cancel task

* remove unneeded exports
This commit is contained in:
Charles Gagnon 2021-08-14 12:06:24 -07:00 коммит произвёл GitHub
Родитель 9842141559
Коммит 5cedf0c5a6
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
1 изменённых файлов: 29 добавлений и 6 удалений

Просмотреть файл

@ -5,7 +5,7 @@
import * as vscode from 'vscode';
import SqlToolsServiceClient from '../languageservice/serviceclient';
import { NotificationType } from 'vscode-languageclient';
import { NotificationType, RequestType } from 'vscode-languageclient';
import { TaskExecutionMode } from 'vscode-mssql';
import { Deferred } from '../protocol';
import * as utils from '../models/utils';
@ -43,14 +43,22 @@ export interface TaskInfo {
isCancelable: boolean;
}
export namespace TaskStatusChangedNotification {
namespace TaskStatusChangedNotification {
export const type = new NotificationType<TaskProgressInfo, void>('tasks/statuschanged');
}
export namespace TaskCreatedNotification {
namespace TaskCreatedNotification {
export const type = new NotificationType<TaskInfo, void>('tasks/newtaskcreated');
}
interface CancelTaskParams {
taskId: string;
}
namespace CancelTaskRequest {
export const type = new RequestType<CancelTaskParams, boolean, void, void>('tasks/canceltask');
}
type ActiveTaskInfo = {
taskInfo: TaskInfo,
progressCallback: ProgressCallback,
@ -71,6 +79,13 @@ export class SqlTasksService {
this._client.onNotification(TaskStatusChangedNotification.type, taskProgressInfo => this.handleTaskChangedNotification(taskProgressInfo));
}
private cancelTask(taskId: string): Thenable<boolean> {
const params: CancelTaskParams = {
taskId
};
return this._client.sendRequest(CancelTaskRequest.type, params);
}
/**
* Handles a new task being created. This will start up a progress notification toast for the task and set up
* callbacks to update the status of that task as it runs.
@ -91,10 +106,13 @@ export class SqlTasksService {
{
location: vscode.ProgressLocation.Notification,
title: taskInfo.name,
cancellable: false
cancellable: taskInfo.isCancelable
},
async (progress, _token): Promise<void> => {
async (progress, token): Promise<void> => {
newTaskInfo.progressCallback = value => progress.report(value);
token.onCancellationRequested(() => {
this.cancelTask(taskInfo.taskId);
});
await newTaskInfo.completionPromise;
}
);
@ -118,7 +136,12 @@ export class SqlTasksService {
// Task is completed, complete the progress notification and display a final toast informing the
// user of the final status.
this._activeTasks.delete(taskProgressInfo.taskId);
if (taskProgressInfo.status === TaskStatus.Canceled) {
taskInfo.completionPromise.reject(new Error('Task cancelled'));
} else {
taskInfo.completionPromise.resolve();
}
// Only include the message if it isn't the same as the task status string we already have - some (but not all) task status
// notifications include this string as the message
const taskMessage = taskProgressInfo.message && taskProgressInfo.message.toLowerCase() !== taskStatusString.toLowerCase() ?