diff --git a/packages/vscode/src/credentials.ts b/packages/vscode/src/credentials.ts index 2b85add5..868bb0eb 100644 --- a/packages/vscode/src/credentials.ts +++ b/packages/vscode/src/credentials.ts @@ -38,7 +38,7 @@ export class Credentials { this.octokit = undefined; } - registerListeners(context: vscode.ExtensionContext): void { + private registerListeners(context: vscode.ExtensionContext): void { context.subscriptions.push( vscode.authentication.onDidChangeSessions(async (e) => { if (e.provider.id === GITHUB_AUTH_PROVIDER_ID) { @@ -48,25 +48,21 @@ export class Credentials { ); } - async getOctokit(): Promise { - if (this.octokit) { - return this.octokit; + /** Octokit 인스턴스가 없다면, 인증 세션을 새로 만듦으로써, Octokit 인스턴스가 항상 존재하도록 보장한다. */ + private async ensureOctokitInstance(): Promise { + if (!this.octokit) { + const session = await vscode.authentication.getSession(GITHUB_AUTH_PROVIDER_ID, SCOPES, { createIfNone: true }); + this.octokit = await this.createOctokit(session); } + } - const session = await vscode.authentication.getSession(GITHUB_AUTH_PROVIDER_ID, SCOPES, { createIfNone: true }); - this.octokit = await this.createOctokit(session); - - return this.octokit; + async getOctokit(): Promise { + await this.ensureOctokitInstance(); + return this.octokit!; } async getAuth(): Promise { - if (this.octokit) { - return this.octokit.auth() as Promise; - } - - const session = await vscode.authentication.getSession(GITHUB_AUTH_PROVIDER_ID, SCOPES, { createIfNone: true }); - this.octokit = await this.createOctokit(session); - - return this.octokit.auth() as Promise; + await this.ensureOctokitInstance(); + return this.octokit!.auth() as Promise; } }