Skip to content

Commit ca9f9a6

Browse files
committed
refactor: extract storage path state
1 parent 21aaa89 commit ca9f9a6

2 files changed

Lines changed: 30 additions & 25 deletions

File tree

lib/storage.ts

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ import {
4141
migrateV1ToV3,
4242
type RateLimitStateV3,
4343
} from "./storage/migrations.js";
44+
import {
45+
getStoragePathState,
46+
setStoragePathState,
47+
} from "./storage/path-state.js";
4448
import {
4549
findProjectRoot,
4650
getConfigDir,
@@ -334,31 +338,6 @@ async function ensureGitignore(storagePath: string): Promise<void> {
334338
}
335339
}
336340

337-
type StoragePathState = {
338-
currentStoragePath: string | null;
339-
currentLegacyProjectStoragePath: string | null;
340-
currentLegacyWorktreeStoragePath: string | null;
341-
currentProjectRoot: string | null;
342-
};
343-
344-
let currentStorageState: StoragePathState = {
345-
currentStoragePath: null,
346-
currentLegacyProjectStoragePath: null,
347-
currentLegacyWorktreeStoragePath: null,
348-
currentProjectRoot: null,
349-
};
350-
351-
const storagePathStateContext = new AsyncLocalStorage<StoragePathState>();
352-
353-
function getStoragePathState(): StoragePathState {
354-
return storagePathStateContext.getStore() ?? currentStorageState;
355-
}
356-
357-
function setStoragePathState(state: StoragePathState): void {
358-
currentStorageState = state;
359-
storagePathStateContext.enterWith(state);
360-
}
361-
362341
export function setStorageBackupEnabled(enabled: boolean): void {
363342
storageBackupEnabled = enabled;
364343
}

lib/storage/path-state.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { AsyncLocalStorage } from "node:async_hooks";
2+
3+
export type StoragePathState = {
4+
currentStoragePath: string | null;
5+
currentLegacyProjectStoragePath: string | null;
6+
currentLegacyWorktreeStoragePath: string | null;
7+
currentProjectRoot: string | null;
8+
};
9+
10+
const storagePathStateContext = new AsyncLocalStorage<StoragePathState>();
11+
12+
let currentStorageState: StoragePathState = {
13+
currentStoragePath: null,
14+
currentLegacyProjectStoragePath: null,
15+
currentLegacyWorktreeStoragePath: null,
16+
currentProjectRoot: null,
17+
};
18+
19+
export function getStoragePathState(): StoragePathState {
20+
return storagePathStateContext.getStore() ?? currentStorageState;
21+
}
22+
23+
export function setStoragePathState(state: StoragePathState): void {
24+
currentStorageState = state;
25+
storagePathStateContext.enterWith(state);
26+
}

0 commit comments

Comments
 (0)