Skip to content
7 changes: 4 additions & 3 deletions packages/vscode/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,11 @@ export async function activate(context: vscode.ExtensionContext) {
const fetchClusterNodes = async (
baseBranchName = initialBaseBranchName,
perPage?: number,
lastCommitId?: string
lastCommitId?: string,
command?: string
): Promise<ClusterNodesResult> => {
// Cache engine
if (!engine || engine.getBaseBranchName() !== baseBranchName) {
// Initialize engine if it doesn't exist or if the branch has changed
if (command === "refresh" || !engine || engine.getBaseBranchName() !== baseBranchName) {
const workerPath = vscode.Uri.joinPath(context.extensionUri, "dist", "worker.js").fsPath;
const gitLog = await fetchGitLogInParallel(gitPath, currentWorkspacePath, workerPath);
const { owner, repo } = getRepo(gitConfig);
Expand Down
84 changes: 40 additions & 44 deletions packages/vscode/src/webview-loader.ts

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

불필요한 주석, 콘솔 제거랑 코드 정리가 잘 되어있어서 가독성이 훨씬 좋아졌어요!

Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ export default class WebviewLoader implements vscode.Disposable {
const { fetchClusterNodes, fetchBranches, fetchCurrentBranch, fetchGithubInfo } = fetcher;
const viewColumn = vscode.ViewColumn.One;

console.log("Initialize cache data");
context.workspaceState.keys().forEach((key) => {
context.workspaceState.update(key, undefined);
});
Expand All @@ -35,50 +34,47 @@ export default class WebviewLoader implements vscode.Disposable {
try {
const { command, payload } = message;

if (command === "fetchAnalyzedData" || command === "refresh") {
try {
const requestPayload = payload ? JSON.parse(payload) : {};
const { baseBranchName, perPage, lastCommitId } = requestPayload;
const currentBranch = baseBranchName ?? (await fetchCurrentBranch());

if (perPage) {
console.log(`Paging request: perPage=${perPage}, lastCommitId=${lastCommitId}`);
const clusterData = await fetchClusterNodes(currentBranch, perPage, lastCommitId);
analyzedData = {
...clusterData,
isLoadMore: !!lastCommitId,
};
} else {
const cacheKey = `${ANALYZE_DATA_KEY}_${currentBranch}`;
// "refresh": ignore the cache
// "fetchAnalyzedData": use the cache if exists
const storedAnalyzedData =
command === "fetchAnalyzedData" ? context.workspaceState.get<ClusterNode[]>(cacheKey) : undefined;

if (storedAnalyzedData) {
console.log("Cache data exists");
analyzedData = storedAnalyzedData;
} else {
console.log(command === "refresh" ? "Forced refresh: fetching new data" : "No cache Data");
analyzedData = await fetchClusterNodes(currentBranch);
context.workspaceState.update(cacheKey, analyzedData);
}
console.log("Current Stored data");
context.workspaceState.keys().forEach((key) => {
console.log(key);
});
}

const resMessage = {
command,
payload: analyzedData,
};
if (command === "refresh") {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

command가 늘어났네요!
switch 로 바꿔서 가독성을 올려도 좋고,
command pattern? 같은 패턴들을 써서 따로 분리해도 좋을 것 같습니다!!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

넵! 리팩토링 단계에서 작업해보겠습니다

const requestPayload = payload ? JSON.parse(payload) : {};
const { selectedBranch, perPage, lastCommitId } = requestPayload;
const currentBranch = selectedBranch ?? (await fetchCurrentBranch());

const clusterData = await fetchClusterNodes(currentBranch, perPage, lastCommitId, "refresh");
analyzedData = {
...clusterData,
isLoadMore: !!lastCommitId,
};

await this.respondToMessage({
command,
payload: analyzedData,
});
}

if (command === "fetchAnalyzedData") {
const requestPayload = payload ? JSON.parse(payload) : {};
const { baseBranch, perPage, lastCommitId } = requestPayload;
const currentBranch = baseBranch ?? (await fetchCurrentBranch());

await this.respondToMessage(resMessage);
} catch (e) {
console.error("Error fetching analyzed data:", e);
throw e;
const cacheKey = `${ANALYZE_DATA_KEY}_${currentBranch}_${lastCommitId || "firstPage"}`;

const storedAnalyzedData = context.workspaceState.get<ClusterNodesResult>(cacheKey);

if (storedAnalyzedData) {
analyzedData = storedAnalyzedData;
} else {
const clusterData = await fetchClusterNodes(currentBranch, perPage, lastCommitId);
analyzedData = {
...clusterData,
isLoadMore: !!lastCommitId,
};
context.workspaceState.update(cacheKey, analyzedData);
}

await this.respondToMessage({
command,
payload: analyzedData,
});
}

if (command === "fetchBranchList") {
Expand Down Expand Up @@ -173,7 +169,7 @@ export default class WebviewLoader implements vscode.Disposable {

type GithruFetcher<D = unknown, P extends unknown[] = []> = (...params: P) => Promise<D>;
type GithruFetcherMap = {
fetchClusterNodes: GithruFetcher<ClusterNodesResult, [string?, number?, string?]>;
fetchClusterNodes: GithruFetcher<ClusterNodesResult, [string?, number?, string?, string?]>;
fetchBranches: GithruFetcher<{ branchList: string[]; head: string | null }>;
fetchCurrentBranch: GithruFetcher<string>;
fetchGithubInfo: GithruFetcher<{ owner: string; repo: string }>;
Expand Down