refactor: encapsulate state management for spec and suggestions generation

- Made the generation status variables private and introduced getter functions for both spec and suggestions generation states.
- Updated relevant route handlers to utilize the new getter functions, improving encapsulation and reducing direct access to shared state.
- Enhanced code maintainability by centralizing state management logic.
This commit is contained in:
Cody Seibert
2025-12-14 18:18:11 -05:00
parent 01bae7d43e
commit 6733de9e0d
19 changed files with 321 additions and 33 deletions

View File

@@ -10,9 +10,19 @@ import {
const logger = createLogger("Suggestions");
// Shared state for tracking generation status
export let isRunning = false;
export let currentAbortController: AbortController | null = null;
// Shared state for tracking generation status - private
let isRunning = false;
let currentAbortController: AbortController | null = null;
/**
* Get the current running state
*/
export function getSuggestionsStatus(): {
isRunning: boolean;
currentAbortController: AbortController | null;
} {
return { isRunning, currentAbortController };
}
/**
* Set the running state and abort controller

View File

@@ -6,7 +6,7 @@ import type { Request, Response } from "express";
import type { EventEmitter } from "../../../lib/events.js";
import { createLogger } from "../../../lib/logger.js";
import {
isRunning,
getSuggestionsStatus,
setRunningState,
getErrorMessage,
logError,
@@ -28,6 +28,7 @@ export function createGenerateHandler(events: EventEmitter) {
return;
}
const { isRunning } = getSuggestionsStatus();
if (isRunning) {
res.json({
success: false,

View File

@@ -3,11 +3,16 @@
*/
import type { Request, Response } from "express";
import { isRunning, getErrorMessage, logError } from "../common.js";
import {
getSuggestionsStatus,
getErrorMessage,
logError,
} from "../common.js";
export function createStatusHandler() {
return async (_req: Request, res: Response): Promise<void> => {
try {
const { isRunning } = getSuggestionsStatus();
res.json({ success: true, isRunning });
} catch (error) {
logError(error, "Get status failed");

View File

@@ -4,7 +4,7 @@
import type { Request, Response } from "express";
import {
currentAbortController,
getSuggestionsStatus,
setRunningState,
getErrorMessage,
logError,
@@ -13,6 +13,7 @@ import {
export function createStopHandler() {
return async (_req: Request, res: Response): Promise<void> => {
try {
const { currentAbortController } = getSuggestionsStatus();
if (currentAbortController) {
currentAbortController.abort();
}