Skip to content

Commit f2d3aa9

Browse files
committed
refactor: rename cachedData to initialSemaphoreState for clarity
- Add JSDoc explaining the purpose of the snapshot - More descriptive name reflects its role as initial state validation - Clarifies that this is a snapshot to avoid race conditions
1 parent b3fd6d8 commit f2d3aa9

1 file changed

Lines changed: 13 additions & 9 deletions

File tree

‎src/commands/statusline.ts‎

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -90,25 +90,29 @@ export const statuslineCommand = define({
9090
// Extract session ID from hook data
9191
const sessionId = hookData.session_id;
9292

93-
const cachedData = Result.pipe(
93+
/**
94+
* Read initial semaphore state for cache validation and process checking
95+
* This is a snapshot taken at the beginning to avoid race conditions
96+
*/
97+
const initialSemaphoreState = Result.pipe(
9498
Result.succeed(getSemaphore(sessionId)),
9599
Result.map(semaphore => semaphore.data),
96100
Result.unwrap(undefined),
97101
);
98102

99-
if (ctx.values.cache && cachedData != null) {
100-
const isSameInput = cachedData.inputHash === inputHash;
103+
if (ctx.values.cache && initialSemaphoreState != null) {
104+
const isSameInput = initialSemaphoreState.inputHash === inputHash;
101105

102106
if (isSameInput) {
103107
// If same input, return cached output
104-
log(cachedData.lastOutput);
108+
log(initialSemaphoreState.lastOutput);
105109
return;
106110
}
107111

108112
// If another process is updating, return stale output
109-
if (cachedData.isUpdating === true) {
113+
if (initialSemaphoreState.isUpdating === true) {
110114
// Check if the updating process is still alive (optional deadlock protection)
111-
const pid = cachedData.pid;
115+
const pid = initialSemaphoreState.pid;
112116
let isProcessAlive = false;
113117
if (pid != null) {
114118
try {
@@ -123,7 +127,7 @@ export const statuslineCommand = define({
123127

124128
if (isProcessAlive) {
125129
// Another process is actively updating, return stale output
126-
log(cachedData.lastOutput);
130+
log(initialSemaphoreState.lastOutput);
127131
return;
128132
}
129133
// Process is dead, continue to update ourselves
@@ -317,8 +321,8 @@ export const statuslineCommand = define({
317321
// Reset updating flag on error to prevent deadlock
318322

319323
// If we have a cached output from previous run, use it
320-
if (cachedData?.lastOutput !== undefined && cachedData.lastOutput !== '') {
321-
log(cachedData.lastOutput);
324+
if (initialSemaphoreState?.lastOutput != null && initialSemaphoreState.lastOutput !== '') {
325+
log(initialSemaphoreState.lastOutput);
322326
}
323327
else {
324328
// Fallback minimal output

0 commit comments

Comments
 (0)