Skip to main content
Glama
asyncOperation.js3.33 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.AsyncOperation = void 0; const logger_1 = __importDefault(require("../utils/logger")); /** * AsyncOperation class * * Represents an asynchronous operation that can be executed, cancelled, and report progress. */ class AsyncOperation { /** * Constructor * * @param executor Function that executes the operation */ constructor(executor) { this.isCancelled = false; this.latestProgress = null; this.executor = executor; } /** * Execute the operation * * @param progressCallback Callback for progress updates * @returns Promise that resolves with the operation result */ execute(progressCallback) { return new Promise((resolve, reject) => { // Create a safe progress reporting function const reportProgress = (progress) => { try { this.latestProgress = progress; if (progressCallback && !this.isCancelled) { progressCallback(progress); } } catch (error) { // Don't let progress callback errors affect the operation logger_1.default.error(`Error in progress callback: ${error instanceof Error ? error.message : String(error)}`); } }; // Create safe resolve/reject functions const safeResolve = (value) => { try { if (!this.isCancelled) { resolve(value); } } catch (error) { logger_1.default.error(`Error in resolve: ${error instanceof Error ? error.message : String(error)}`); reject(error); } }; const safeReject = (error) => { try { if (!this.isCancelled) { reject(error); } } catch (innerError) { logger_1.default.error(`Error in reject: ${innerError instanceof Error ? innerError.message : String(innerError)}`); // Still try to reject with the original error reject(error); } }; try { // Execute the operation this.executor(safeResolve, safeReject, reportProgress); } catch (error) { // Handle synchronous errors if (!this.isCancelled) { safeReject(error instanceof Error ? error : new Error(String(error))); } } }); } /** * Cancel the operation */ cancel() { this.isCancelled = true; } /** * Get the latest progress */ getLatestProgress() { return this.latestProgress; } /** * Check if the operation is cancelled */ isOperationCancelled() { return this.isCancelled; } } exports.AsyncOperation = AsyncOperation;

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/TSavo/Unity-MCP'

If you have feedback or need assistance with the MCP directory API, please join our Discord server