"use strict";
//
//  Copyright (c) Microsoft Corporation. All rights reserved.
//
Object.defineProperty(exports, "__esModule", { value: true });
exports.withCancellation = exports.CancellationError = exports.CancellationTokenSource = exports.CancellationToken = void 0;
const vscode_jsonrpc_1 = require("vscode-jsonrpc");
Object.defineProperty(exports, "CancellationToken", { enumerable: true, get: function () { return vscode_jsonrpc_1.CancellationToken; } });
Object.defineProperty(exports, "CancellationTokenSource", { enumerable: true, get: function () { return vscode_jsonrpc_1.CancellationTokenSource; } });
/**
 * Error thrown when an operation is cancelled via a CancellationToken.
 */
class CancellationError extends Error {
    constructor(message) {
        super(message || 'Operation cancelled.');
    }
}
exports.CancellationError = CancellationError;
function withCancellation(promise, cancellation) {
    if (!cancellation) {
        return promise;
    }
    return Promise.race([
        promise,
        new Promise((resolve, reject) => {
            if (cancellation.isCancellationRequested) {
                reject(new CancellationError());
            }
            else {
                cancellation.onCancellationRequested(() => {
                    reject(new CancellationError());
                });
            }
        }),
    ]);
}
exports.withCancellation = withCancellation;
//# sourceMappingURL=https://github.com/VSCodium/sourcemaps/releases/download/stable-824c4c46a288b839f13b24022655329c2aeb9f81/node_modules-@microsoft-dev-tunnels-ssh-util-cancellation.js.map