{"version":3,"file":"BaseStrategy.cjs","names":["ConnectionState","onShutdown"],"sources":["../../../../../src/components/connect/strategies/core/BaseStrategy.ts"],"sourcesContent":["import type { Logger } from \"../../../../middleware/logger.ts\";\nimport { onShutdown } from \"../../os.ts\";\nimport { type ConnectDebugState, ConnectionState } from \"../../types.ts\";\nimport type { ConnectionStrategy } from \"./types.ts\";\n\n/**\n * Base class for connection strategies providing common functionality for state\n * management, shutdown signal handling, and close lifecycle.\n */\nexport abstract class BaseStrategy implements ConnectionStrategy {\n  protected _state: ConnectionState = ConnectionState.CONNECTING;\n  protected closingPromise: Promise<void>;\n  protected resolveClosingPromise:\n    | ((value: void | PromiseLike<void>) => void)\n    | undefined;\n  protected cleanupShutdownSignal: (() => void) | undefined;\n\n  protected readonly internalLogger: Logger;\n\n  constructor({ logger }: { logger: Logger }) {\n    this.internalLogger = logger;\n    this.closingPromise = new Promise((resolve) => {\n      this.resolveClosingPromise = resolve;\n    });\n  }\n\n  get state(): ConnectionState {\n    return this._state;\n  }\n\n  get closed(): Promise<void> {\n    return this.closingPromise;\n  }\n\n  abstract get connectionId(): string | undefined;\n  abstract connect(attempt?: number): Promise<void>;\n  abstract close(): Promise<void>;\n  abstract getDebugState(): ConnectDebugState;\n\n  /**\n   * Set up shutdown signal handlers that will trigger close() on SIGINT/SIGTERM.\n   */\n  protected setupShutdownSignal(signals: string[]): void {\n    if (this.cleanupShutdownSignal) {\n      return;\n    }\n\n    this.internalLogger.debug(\n      { signals },\n      \"Setting up shutdown signal handler\",\n    );\n\n    const cleanupShutdownHandlers = onShutdown(signals, () => {\n      this.internalLogger.debug(\n        { signals },\n        \"Received shutdown signal, closing connection\",\n      );\n      void this.close();\n    });\n\n    this.cleanupShutdownSignal = () => {\n      this.internalLogger.debug(\"Cleaning up shutdown signal handler\");\n      cleanupShutdownHandlers();\n    };\n  }\n\n  /**\n   * Clean up shutdown signal handlers. Call at the start of close().\n   */\n  protected cleanupShutdown(): void {\n    if (this.cleanupShutdownSignal) {\n      this.cleanupShutdownSignal();\n      this.cleanupShutdownSignal = undefined;\n    }\n  }\n\n  /**\n   * Mark the connection as closing. Call after cleanupShutdown().\n   */\n  protected setClosing(): void {\n    this._state = ConnectionState.CLOSING;\n  }\n\n  /**\n   * Mark the connection as closed and resolve the closing promise.\n   * Call at the end of close().\n   */\n  protected setClosed(): void {\n    this._state = ConnectionState.CLOSED;\n    this.resolveClosingPromise?.();\n  }\n\n  /**\n   * Set up shutdown signals if configured in options.\n   * Call at the start of connect().\n   */\n  protected setupShutdownSignalIfConfigured(\n    handleShutdownSignals: string[] | undefined,\n  ): void {\n    if (handleShutdownSignals && handleShutdownSignals.length > 0) {\n      this.setupShutdownSignal(handleShutdownSignals);\n    }\n  }\n\n  /**\n   * Throw if the connection is closing or closed.\n   * Call at the start of connect().\n   */\n  protected throwIfClosingOrClosed(): void {\n    if (\n      this._state === ConnectionState.CLOSING ||\n      this._state === ConnectionState.CLOSED\n    ) {\n      throw new Error(\"Connection already closed\");\n    }\n  }\n}\n"],"mappings":";;;;;;;;AASA,IAAsB,eAAtB,MAAiE;CAC/D,AAAU,SAA0BA,8BAAgB;CACpD,AAAU;CACV,AAAU;CAGV,AAAU;CAEV,AAAmB;CAEnB,YAAY,EAAE,UAA8B;AAC1C,OAAK,iBAAiB;AACtB,OAAK,iBAAiB,IAAI,SAAS,YAAY;AAC7C,QAAK,wBAAwB;IAC7B;;CAGJ,IAAI,QAAyB;AAC3B,SAAO,KAAK;;CAGd,IAAI,SAAwB;AAC1B,SAAO,KAAK;;;;;CAWd,AAAU,oBAAoB,SAAyB;AACrD,MAAI,KAAK,sBACP;AAGF,OAAK,eAAe,MAClB,EAAE,SAAS,EACX,qCACD;EAED,MAAM,0BAA0BC,sBAAW,eAAe;AACxD,QAAK,eAAe,MAClB,EAAE,SAAS,EACX,+CACD;AACD,GAAK,KAAK,OAAO;IACjB;AAEF,OAAK,8BAA8B;AACjC,QAAK,eAAe,MAAM,sCAAsC;AAChE,4BAAyB;;;;;;CAO7B,AAAU,kBAAwB;AAChC,MAAI,KAAK,uBAAuB;AAC9B,QAAK,uBAAuB;AAC5B,QAAK,wBAAwB;;;;;;CAOjC,AAAU,aAAmB;AAC3B,OAAK,SAASD,8BAAgB;;;;;;CAOhC,AAAU,YAAkB;AAC1B,OAAK,SAASA,8BAAgB;AAC9B,OAAK,yBAAyB;;;;;;CAOhC,AAAU,gCACR,uBACM;AACN,MAAI,yBAAyB,sBAAsB,SAAS,EAC1D,MAAK,oBAAoB,sBAAsB;;;;;;CAQnD,AAAU,yBAA+B;AACvC,MACE,KAAK,WAAWA,8BAAgB,WAChC,KAAK,WAAWA,8BAAgB,OAEhC,OAAM,IAAI,MAAM,4BAA4B"}