mirror of
https://github.com/Ark0N/Codeman.git
synced 2026-10-09 16:59:43 +02:00
docs: add JSDoc to StateStore methods
- Add method-level documentation to all public methods - Document save behavior (debounced vs immediate) - Document inner state methods for Ralph Loop tracking - Improve code readability with clear comments Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
+38
-5
@@ -92,7 +92,10 @@ export class StateStore {
|
|||||||
return createInitialState();
|
return createInitialState();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Debounced save - batches multiple updates into a single write
|
/**
|
||||||
|
* Schedules a debounced save.
|
||||||
|
* Multiple calls within 500ms are batched into a single disk write.
|
||||||
|
*/
|
||||||
save(): void {
|
save(): void {
|
||||||
this.dirty = true;
|
this.dirty = true;
|
||||||
if (this.saveTimeout) {
|
if (this.saveTimeout) {
|
||||||
@@ -103,7 +106,10 @@ export class StateStore {
|
|||||||
}, SAVE_DEBOUNCE_MS);
|
}, SAVE_DEBOUNCE_MS);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Immediate save - use when you need guaranteed persistence
|
/**
|
||||||
|
* Immediately writes state to disk.
|
||||||
|
* Use when guaranteed persistence is required (e.g., before shutdown).
|
||||||
|
*/
|
||||||
saveNow(): void {
|
saveNow(): void {
|
||||||
if (this.saveTimeout) {
|
if (this.saveTimeout) {
|
||||||
clearTimeout(this.saveTimeout);
|
clearTimeout(this.saveTimeout);
|
||||||
@@ -117,69 +123,83 @@ export class StateStore {
|
|||||||
writeFileSync(this.filePath, JSON.stringify(this.state, null, 2), 'utf-8');
|
writeFileSync(this.filePath, JSON.stringify(this.state, null, 2), 'utf-8');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Flush any pending saves (call before shutdown)
|
/** Flushes any pending main state save. Call before shutdown. */
|
||||||
flush(): void {
|
flush(): void {
|
||||||
this.saveNow();
|
this.saveNow();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Returns the full application state object. */
|
||||||
getState(): AppState {
|
getState(): AppState {
|
||||||
return this.state;
|
return this.state;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Returns all session states keyed by session ID. */
|
||||||
getSessions() {
|
getSessions() {
|
||||||
return this.state.sessions;
|
return this.state.sessions;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Returns a session state by ID, or null if not found. */
|
||||||
getSession(id: string) {
|
getSession(id: string) {
|
||||||
return this.state.sessions[id] || null;
|
return this.state.sessions[id] || null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Sets a session state and triggers a debounced save. */
|
||||||
setSession(id: string, session: AppState['sessions'][string]) {
|
setSession(id: string, session: AppState['sessions'][string]) {
|
||||||
this.state.sessions[id] = session;
|
this.state.sessions[id] = session;
|
||||||
this.save();
|
this.save();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Removes a session state and triggers a debounced save. */
|
||||||
removeSession(id: string) {
|
removeSession(id: string) {
|
||||||
delete this.state.sessions[id];
|
delete this.state.sessions[id];
|
||||||
this.save();
|
this.save();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Returns all task states keyed by task ID. */
|
||||||
getTasks() {
|
getTasks() {
|
||||||
return this.state.tasks;
|
return this.state.tasks;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Returns a task state by ID, or null if not found. */
|
||||||
getTask(id: string) {
|
getTask(id: string) {
|
||||||
return this.state.tasks[id] || null;
|
return this.state.tasks[id] || null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Sets a task state and triggers a debounced save. */
|
||||||
setTask(id: string, task: AppState['tasks'][string]) {
|
setTask(id: string, task: AppState['tasks'][string]) {
|
||||||
this.state.tasks[id] = task;
|
this.state.tasks[id] = task;
|
||||||
this.save();
|
this.save();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Removes a task state and triggers a debounced save. */
|
||||||
removeTask(id: string) {
|
removeTask(id: string) {
|
||||||
delete this.state.tasks[id];
|
delete this.state.tasks[id];
|
||||||
this.save();
|
this.save();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Returns the Ralph Loop state. */
|
||||||
getRalphLoopState() {
|
getRalphLoopState() {
|
||||||
return this.state.ralphLoop;
|
return this.state.ralphLoop;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Updates Ralph Loop state (partial merge) and triggers a debounced save. */
|
||||||
setRalphLoopState(ralphLoop: Partial<AppState['ralphLoop']>) {
|
setRalphLoopState(ralphLoop: Partial<AppState['ralphLoop']>) {
|
||||||
this.state.ralphLoop = { ...this.state.ralphLoop, ...ralphLoop };
|
this.state.ralphLoop = { ...this.state.ralphLoop, ...ralphLoop };
|
||||||
this.save();
|
this.save();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Returns the application configuration. */
|
||||||
getConfig() {
|
getConfig() {
|
||||||
return this.state.config;
|
return this.state.config;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Updates configuration (partial merge) and triggers a debounced save. */
|
||||||
setConfig(config: Partial<AppState['config']>) {
|
setConfig(config: Partial<AppState['config']>) {
|
||||||
this.state.config = { ...this.state.config, ...config };
|
this.state.config = { ...this.state.config, ...config };
|
||||||
this.save();
|
this.save();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Resets all state to initial values and saves immediately. */
|
||||||
reset(): void {
|
reset(): void {
|
||||||
this.state = createInitialState();
|
this.state = createInitialState();
|
||||||
this.state.config.stateFilePath = this.filePath;
|
this.state.config.stateFilePath = this.filePath;
|
||||||
@@ -188,7 +208,7 @@ export class StateStore {
|
|||||||
this.saveInnerStatesNow();
|
this.saveInnerStatesNow();
|
||||||
}
|
}
|
||||||
|
|
||||||
// ========== Inner State Methods ==========
|
// ========== Inner State Methods (Ralph Loop tracking) ==========
|
||||||
|
|
||||||
private loadInnerStates(): void {
|
private loadInnerStates(): void {
|
||||||
try {
|
try {
|
||||||
@@ -233,15 +253,22 @@ export class StateStore {
|
|||||||
writeFileSync(this.innerStatePath, JSON.stringify(data, null, 2), 'utf-8');
|
writeFileSync(this.innerStatePath, JSON.stringify(data, null, 2), 'utf-8');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Returns inner state for a session, or null if not found. */
|
||||||
getInnerState(sessionId: string): InnerSessionState | null {
|
getInnerState(sessionId: string): InnerSessionState | null {
|
||||||
return this.innerStates.get(sessionId) || null;
|
return this.innerStates.get(sessionId) || null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Sets inner state for a session and triggers a debounced save. */
|
||||||
setInnerState(sessionId: string, state: InnerSessionState): void {
|
setInnerState(sessionId: string, state: InnerSessionState): void {
|
||||||
this.innerStates.set(sessionId, state);
|
this.innerStates.set(sessionId, state);
|
||||||
this.saveInnerStates();
|
this.saveInnerStates();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Updates inner state for a session (partial merge).
|
||||||
|
* Creates initial state if none exists.
|
||||||
|
* @returns The updated inner state.
|
||||||
|
*/
|
||||||
updateInnerState(sessionId: string, updates: Partial<InnerSessionState>): InnerSessionState {
|
updateInnerState(sessionId: string, updates: Partial<InnerSessionState>): InnerSessionState {
|
||||||
let state = this.innerStates.get(sessionId);
|
let state = this.innerStates.get(sessionId);
|
||||||
if (!state) {
|
if (!state) {
|
||||||
@@ -253,6 +280,7 @@ export class StateStore {
|
|||||||
return state;
|
return state;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Removes inner state for a session and triggers a debounced save. */
|
||||||
removeInnerState(sessionId: string): void {
|
removeInnerState(sessionId: string): void {
|
||||||
if (this.innerStates.has(sessionId)) {
|
if (this.innerStates.has(sessionId)) {
|
||||||
this.innerStates.delete(sessionId);
|
this.innerStates.delete(sessionId);
|
||||||
@@ -260,11 +288,12 @@ export class StateStore {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Returns a copy of all inner states as a Map. */
|
||||||
getAllInnerStates(): Map<string, InnerSessionState> {
|
getAllInnerStates(): Map<string, InnerSessionState> {
|
||||||
return new Map(this.innerStates);
|
return new Map(this.innerStates);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Flush all pending saves (call before shutdown)
|
/** Flushes all pending saves (main and inner state). Call before shutdown. */
|
||||||
flushAll(): void {
|
flushAll(): void {
|
||||||
this.saveNow();
|
this.saveNow();
|
||||||
this.saveInnerStatesNow();
|
this.saveInnerStatesNow();
|
||||||
@@ -274,6 +303,10 @@ export class StateStore {
|
|||||||
// Singleton instance
|
// Singleton instance
|
||||||
let storeInstance: StateStore | null = null;
|
let storeInstance: StateStore | null = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets or creates the singleton StateStore instance.
|
||||||
|
* @param filePath Optional custom file path (only used on first call).
|
||||||
|
*/
|
||||||
export function getStore(filePath?: string): StateStore {
|
export function getStore(filePath?: string): StateStore {
|
||||||
if (!storeInstance) {
|
if (!storeInstance) {
|
||||||
storeInstance = new StateStore(filePath);
|
storeInstance = new StateStore(filePath);
|
||||||
|
|||||||
Reference in New Issue
Block a user