Skip to content

Commit a94087c

Browse files
committed
Remove singleton logic from internal cloud services
1 parent b0f4ea7 commit a94087c

File tree

4 files changed

+11
-55
lines changed

4 files changed

+11
-55
lines changed

packages/cloud/src/AuthService.ts

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -472,23 +472,4 @@ export class AuthService extends EventEmitter<AuthServiceEvents> {
472472
return getUserAgent(this.context)
473473
}
474474

475-
private static _instance: AuthService | null = null
476-
477-
static get instance() {
478-
if (!this._instance) {
479-
throw new Error("AuthService not initialized")
480-
}
481-
482-
return this._instance
483-
}
484-
485-
static async createInstance(context: vscode.ExtensionContext, log?: (...args: unknown[]) => void) {
486-
if (this._instance) {
487-
throw new Error("AuthService instance already created")
488-
}
489-
490-
this._instance = new AuthService(context, log)
491-
await this._instance.initialize()
492-
return this._instance
493-
}
494475
}

packages/cloud/src/CloudService.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,18 @@ export class CloudService {
3737
}
3838

3939
try {
40-
this.authService = await AuthService.createInstance(this.context, this.log)
40+
this.authService = new AuthService(this.context, this.log)
41+
await this.authService.initialize()
4142

4243
this.authService.on("inactive-session", this.authListener)
4344
this.authService.on("active-session", this.authListener)
4445
this.authService.on("logged-out", this.authListener)
4546
this.authService.on("user-info", this.authListener)
4647

47-
this.settingsService = await SettingsService.createInstance(this.context, () =>
48+
this.settingsService = new SettingsService(this.context, this.authService, () =>
4849
this.callbacks.stateChanged?.(),
4950
)
51+
this.settingsService.initialize()
5052

5153
this.telemetryClient = new TelemetryClient(this.authService, this.settingsService)
5254

@@ -162,13 +164,7 @@ export class CloudService {
162164
}
163165

164166
private ensureInitialized(): void {
165-
if (
166-
!this.isInitialized ||
167-
!this.authService ||
168-
!this.settingsService ||
169-
!this.telemetryClient ||
170-
!this.shareService
171-
) {
167+
if (!this.isInitialized) {
172168
throw new Error("CloudService not initialized.")
173169
}
174170
}

packages/cloud/src/SettingsService.ts

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,13 @@ import { RefreshTimer } from "./RefreshTimer"
1414
const ORGANIZATION_SETTINGS_CACHE_KEY = "organization-settings"
1515

1616
export class SettingsService {
17-
private static _instance: SettingsService | null = null
1817

1918
private context: vscode.ExtensionContext
2019
private authService: AuthService
2120
private settings: OrganizationSettings | undefined = undefined
2221
private timer: RefreshTimer
2322

24-
private constructor(context: vscode.ExtensionContext, authService: AuthService, callback: () => void) {
23+
constructor(context: vscode.ExtensionContext, authService: AuthService, callback: () => void) {
2524
this.context = context
2625
this.authService = authService
2726

@@ -122,21 +121,4 @@ export class SettingsService {
122121
this.timer.stop()
123122
}
124123

125-
static get instance() {
126-
if (!this._instance) {
127-
throw new Error("SettingsService not initialized")
128-
}
129-
130-
return this._instance
131-
}
132-
133-
static async createInstance(context: vscode.ExtensionContext, callback: () => void) {
134-
if (this._instance) {
135-
throw new Error("SettingsService instance already created")
136-
}
137-
138-
this._instance = new SettingsService(context, AuthService.instance, callback)
139-
this._instance.initialize()
140-
return this._instance
141-
}
142124
}

packages/cloud/src/__tests__/CloudService.test.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ describe("CloudService", () => {
7979
} as unknown as vscode.ExtensionContext
8080

8181
mockAuthService = {
82-
initialize: vi.fn(),
82+
initialize: vi.fn().mockResolvedValue(undefined),
8383
login: vi.fn(),
8484
logout: vi.fn(),
8585
isAuthenticated: vi.fn().mockReturnValue(false),
@@ -108,11 +108,8 @@ describe("CloudService", () => {
108108
},
109109
}
110110

111-
vi.mocked(AuthService.createInstance).mockResolvedValue(mockAuthService as unknown as AuthService)
112-
Object.defineProperty(AuthService, "instance", { get: () => mockAuthService, configurable: true })
113-
114-
vi.mocked(SettingsService.createInstance).mockResolvedValue(mockSettingsService as unknown as SettingsService)
115-
Object.defineProperty(SettingsService, "instance", { get: () => mockSettingsService, configurable: true })
111+
vi.mocked(AuthService).mockImplementation(() => mockAuthService as unknown as AuthService)
112+
vi.mocked(SettingsService).mockImplementation(() => mockSettingsService as unknown as SettingsService)
116113

117114
vi.mocked(TelemetryService.hasInstance).mockReturnValue(true)
118115
Object.defineProperty(TelemetryService, "instance", {
@@ -135,8 +132,8 @@ describe("CloudService", () => {
135132
const cloudService = await CloudService.createInstance(mockContext, callbacks)
136133

137134
expect(cloudService).toBeInstanceOf(CloudService)
138-
expect(AuthService.createInstance).toHaveBeenCalledWith(mockContext, expect.any(Function))
139-
expect(SettingsService.createInstance).toHaveBeenCalledWith(mockContext, expect.any(Function))
135+
expect(AuthService).toHaveBeenCalledWith(mockContext, expect.any(Function))
136+
expect(SettingsService).toHaveBeenCalledWith(mockContext, mockAuthService, expect.any(Function))
140137
})
141138

142139
it("should throw error if instance already exists", async () => {

0 commit comments

Comments
 (0)