Skip to content
44 changes: 23 additions & 21 deletions packages/vscode/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { COMMAND_LAUNCH, COMMAND_LOGIN_WITH_GITHUB, COMMAND_RESET_GITHUB_AUTH }
import { Credentials } from "./credentials";
import { GithubTokenUndefinedError, WorkspacePathUndefinedError } from "./errors/ExtensionError";
import { deleteGithubToken, getGithubToken, setGithubToken } from "./setting-repository";
import { ClusterNodesResult } from "./types/Node";
import { mapClusterNodesFrom } from "./utils/csmMapper";
import {
fetchGitLogInParallel,
Expand All @@ -29,7 +30,7 @@ export async function activate(context: vscode.ExtensionContext) {
const { subscriptions, extensionPath, secrets } = context;
const credentials = new Credentials();
let currentPanel: vscode.WebviewPanel | undefined = undefined;

let engine: AnalysisEngine | undefined;
await credentials.initialize(context);

console.log('Congratulations, your extension "githru" is now active!');
Expand Down Expand Up @@ -66,13 +67,7 @@ export async function activate(context: vscode.ExtensionContext) {
const fetchGithubInfo = async () => getRepo(gitConfig);

const fetchCurrentBranch = async () => {
let branchName;
try {
branchName = await getCurrentBranchName(gitPath, currentWorkspacePath);
} catch (error) {
console.error(error);
}

let branchName = await getCurrentBranchName(gitPath, currentWorkspacePath).catch(() => null);
if (!branchName) {
const branchList = (await fetchBranches()).branchList;
branchName = getDefaultBranchName(branchList);
Expand All @@ -86,19 +81,26 @@ export async function activate(context: vscode.ExtensionContext) {
baseBranchName = initialBaseBranchName,
perPage?: number,
lastCommitId?: string
) => {
const workerPath = vscode.Uri.joinPath(context.extensionUri, "dist", "worker.js").fsPath;
const gitLog = await fetchGitLogInParallel(gitPath, currentWorkspacePath, workerPath);
const { owner, repo: initialRepo } = getRepo(gitConfig);
const repo = initialRepo;
const engine = new AnalysisEngine({
isDebugMode: true,
gitLog,
owner,
repo,
auth: githubToken,
baseBranchName,
});
): Promise<ClusterNodesResult> => {
// Cache engine
if (!engine || engine.getBaseBranchName() !== baseBranchName) {

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.

캐싱 로직 좋습니다👍🏻

const workerPath = vscode.Uri.joinPath(context.extensionUri, "dist", "worker.js").fsPath;
const gitLog = await fetchGitLogInParallel(gitPath, currentWorkspacePath, workerPath);
const { owner, repo } = getRepo(gitConfig);
engine = new AnalysisEngine({
isDebugMode: true,
gitLog,
owner,
repo,
auth: githubToken,
baseBranchName,
});
await engine.init();
}

if (!engine) {
throw new Error("Analysis engine is not initialized.");
}

const analysisResult = await engine.analyzeGit(perPage, lastCommitId);

Expand Down
7 changes: 7 additions & 0 deletions packages/vscode/src/types/Node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,10 @@ export type ClusterNode = NodeBase & {
nodeTypeName: "CLUSTER";
commitNodeList: CommitNode[];
};

export type ClusterNodesResult = {
clusterNodes: ClusterNode[];
isLastPage: boolean;
nextCommitId: string | undefined;
isPRSuccess: boolean;
};
19 changes: 7 additions & 12 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 @@ -2,7 +2,7 @@ import * as path from "path";
import * as vscode from "vscode";

import { getTheme, setTheme } from "./setting-repository";
import type { ClusterNode } from "./types/Node";
import type { ClusterNode, ClusterNodesResult } from "./types/Node";

const ANALYZE_DATA_KEY = "memento_analyzed_data";

Expand Down Expand Up @@ -50,13 +50,16 @@ export default class WebviewLoader implements vscode.Disposable {
};
} else {
const cacheKey = `${ANALYZE_DATA_KEY}_${currentBranch}`;
const storedAnalyzedData = context.workspaceState.get<ClusterNode[]>(cacheKey);
// "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("No cache Data");
console.log(command === "refresh" ? "Forced refresh: fetching new data" : "No cache Data");
analyzedData = await fetchClusterNodes(currentBranch);
context.workspaceState.update(cacheKey, analyzedData);
}
Expand Down Expand Up @@ -170,15 +173,7 @@ export default class WebviewLoader implements vscode.Disposable {

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