Skip to content

Commit 6ae9a54

Browse files
committed
refactor: Fix singleton pattern in service constructors
- Make constructors private to enforce singleton pattern - Remove conditional logic from constructors that could skip initialization - Remove definite assignment assertions (!) and use proper initialization - Apply consistent singleton pattern across services - Ensures all properties are always initialized correctly
1 parent f8f42b5 commit 6ae9a54

File tree

3 files changed

+19
-28
lines changed

3 files changed

+19
-28
lines changed

src/service/OpenAiService.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,10 @@ export interface Error {
1717

1818
class OpenAiService implements AIService {
1919
static _instance: OpenAiService;
20-
cacheService!: CacheService;
20+
cacheService: CacheService;
2121

22-
constructor() {
23-
if (!OpenAiService._instance) {
24-
OpenAiService._instance = this;
25-
this.cacheService = CacheService.getInstance();
26-
}
22+
private constructor() {
23+
this.cacheService = CacheService.getInstance();
2724
}
2825

2926
/**

src/service/VsCodeLlmService.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,10 @@ import WorkspaceService from "./WorkspaceService";
66

77
class VsCodeLlmService implements AIService {
88
static _instance: VsCodeLlmService;
9-
cacheService!: CacheService;
9+
cacheService: CacheService;
1010

11-
constructor() {
12-
if (!VsCodeLlmService._instance) {
13-
VsCodeLlmService._instance = this;
14-
this.cacheService = CacheService.getInstance();
15-
}
11+
private constructor() {
12+
this.cacheService = CacheService.getInstance();
1613
}
1714

1815
/**

src/service/WorkspaceService.ts

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,17 @@ import { CONSTANTS } from "../Constants";
66
export default class WorkspaceService extends EventEmitter {
77
static _instance: WorkspaceService;
88

9-
constructor() {
9+
private constructor() {
1010
super();
11-
if (!WorkspaceService._instance) {
12-
WorkspaceService._instance = this;
13-
/* Listening for changes to the workspace configuration. */
14-
workspace.onDidChangeConfiguration((_e) => {
15-
this.emit(EventType.WORKSPACE_CHANGED);
16-
});
17-
18-
/* Listening for changes to the workspace configuration. */
19-
workspace.onDidChangeWorkspaceFolders((_e) => {
20-
this.emit(EventType.WORKSPACE_CHANGED);
21-
});
22-
}
11+
/* Listening for changes to the workspace configuration. */
12+
workspace.onDidChangeConfiguration((_e) => {
13+
this.emit(EventType.WORKSPACE_CHANGED);
14+
});
15+
16+
/* Listening for changes to the workspace configuration. */
17+
workspace.onDidChangeWorkspaceFolders((_e) => {
18+
this.emit(EventType.WORKSPACE_CHANGED);
19+
});
2320
}
2421

2522
/**
@@ -40,7 +37,7 @@ export default class WorkspaceService extends EventEmitter {
4037
*/
4138
checkAndWarnWorkSpaceExist(): boolean {
4239
if (!workspace.workspaceFolders || workspace.workspaceFolders.length === 0) {
43-
void this.showErrorMessage("Your are not in a Workspace");
40+
this.showErrorMessage("Your are not in a Workspace");
4441
return false;
4542
}
4643
return true;
@@ -94,7 +91,7 @@ export default class WorkspaceService extends EventEmitter {
9491
getOpenAIKey() {
9592
const value = String(this.getConfiguration().get("openAiKey"));
9693
if (!value) {
97-
void this.showErrorMessage(
94+
this.showErrorMessage(
9895
"Your OpenAI API Key is missing; kindly input it within the Diffy Settings section. You can generate a key by visiting the OpenAI website.",
9996
);
10097
return null;
@@ -119,7 +116,7 @@ export default class WorkspaceService extends EventEmitter {
119116
? String(this.getConfiguration().get("aiInstructions"))
120117
: undefined;
121118
if (!value) {
122-
void this.showErrorMessage(
119+
this.showErrorMessage(
123120
"Instructions for AI are absent; please provide them within the Diffy Settings section.",
124121
);
125122
return null;

0 commit comments

Comments
 (0)