Skip to content

Commit 82ae156

Browse files
committed
Use singleton VersionBundlesLoader
1 parent dd99f11 commit 82ae156

File tree

6 files changed

+49
-38
lines changed

6 files changed

+49
-38
lines changed

src/commands/switchBoard.mts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Command } from "./command.mjs";
22
import Logger from "../logger.mjs";
33
import {
4-
commands, ProgressLocation, window, workspace, type Uri
4+
commands, ProgressLocation, window, workspace
55
} from "vscode";
66
import { existsSync, readdirSync, readFileSync } from "fs";
77
import {
@@ -26,10 +26,10 @@ export default class SwitchBoardCommand extends Command {
2626
private _versionBundlesLoader: VersionBundlesLoader;
2727
public static readonly id = "switchBoard";
2828

29-
constructor(private readonly _ui: UI, extensionUri: Uri) {
29+
constructor(private readonly _ui: UI) {
3030
super(SwitchBoardCommand.id);
3131

32-
this._versionBundlesLoader = new VersionBundlesLoader(extensionUri);
32+
this._versionBundlesLoader = VersionBundlesLoader.getInstance();
3333
}
3434

3535
public static async askBoard(sdkVersion: string):

src/commands/switchSDK.mts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { Command } from "./command.mjs";
22
import {
33
ProgressLocation,
4-
type Uri,
54
window,
65
workspace,
76
commands,
@@ -53,10 +52,10 @@ export default class SwitchSDKCommand extends Command {
5352

5453
public static readonly id = "switchSDK";
5554

56-
constructor(private readonly _ui: UI, extensionUri: Uri) {
55+
constructor(private readonly _ui: UI) {
5756
super(SwitchSDKCommand.id);
5857

59-
this._versionBundlesLoader = new VersionBundlesLoader(extensionUri);
58+
this._versionBundlesLoader = VersionBundlesLoader.getInstance();
6059
}
6160

6261
// TODO: maybe move into UI helper file or something

src/extension.mts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ import SwitchBoardCommand from "./commands/switchBoard.mjs";
7979
import UninstallPicoSDKCommand from "./commands/uninstallPicoSDK.mjs";
8080
import FlashProjectSWDCommand from "./commands/flashProjectSwd.mjs";
8181
import ReleaseTagsLoader from "./utils/releaseTags.mjs";
82+
import VersionBundlesLoader from "./utils/versionBundles.mjs";
8283
// eslint-disable-next-line max-len
8384
import { NewMicroPythonProjectPanel } from "./webview/newMicroPythonProjectPanel.mjs";
8485
import type { Progress as GotProgress } from "got";
@@ -94,6 +95,7 @@ export async function activate(context: ExtensionContext): Promise<void> {
9495
);
9596
GithubApiCache.createInstance(context);
9697
ReleaseTagsLoader.createInstance(context.extensionUri);
98+
VersionBundlesLoader.createInstance(context.extensionUri);
9799

98100
const picoProjectActivityBarProvider = new PicoProjectActivityBar();
99101
const ui = new UI(picoProjectActivityBarProvider);
@@ -107,8 +109,8 @@ export async function activate(context: ExtensionContext): Promise<void> {
107109
| CommandWithArgs
108110
> = [
109111
new NewProjectCommand(context.extensionUri),
110-
new SwitchSDKCommand(ui, context.extensionUri),
111-
new SwitchBoardCommand(ui, context.extensionUri),
112+
new SwitchSDKCommand(ui),
113+
new SwitchBoardCommand(ui),
112114
new LaunchTargetPathCommand(),
113115
new GetPythonPathCommand(),
114116
new GetEnvPathCommand(),

src/utils/releaseTags.mts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { isInternetConnected } from "./downloadHelpers.mjs";
44
import { get } from "https";
55
import Logger from "../logger.mjs";
66
import { CURRENT_DATA_VERSION } from "./sharedConstants.mjs";
7+
import { unknownErrorToString } from "./errorHelper.mjs";
78

89
const releaseTagsUrl =
910
"https://raspberrypi.github.io/pico-vscode/" +
@@ -68,9 +69,9 @@ export default class ReleaseTagsLoader {
6869
} catch (error) {
6970
reject(
7071
new Error(
71-
`Failed to parse release tags JSON: ${
72-
error instanceof Error ? error.message : (error as string)
73-
}`
72+
`Failed to parse release tags JSON: ${unknownErrorToString(
73+
error
74+
)}`
7475
)
7576
);
7677
}
@@ -87,7 +88,7 @@ export default class ReleaseTagsLoader {
8788
} catch (error) {
8889
Logger.log(
8990
"Failed to download release tags:",
90-
error instanceof Error ? error.message : (error as string)
91+
unknownErrorToString(error)
9192
);
9293

9394
try {
@@ -104,7 +105,7 @@ export default class ReleaseTagsLoader {
104105
} catch (e) {
105106
Logger.log(
106107
"Failed to load release tags from local file:",
107-
e instanceof Error ? e.message : (e as string)
108+
unknownErrorToString(e)
108109
);
109110
this.tags = {
110111
tools: {},

src/utils/versionBundles.mts

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { isInternetConnected } from "./downloadHelpers.mjs";
44
import { get } from "https";
55
import Logger from "../logger.mjs";
66
import { CURRENT_DATA_VERSION } from "./sharedConstants.mjs";
7+
import { unknownErrorToString } from "./errorHelper.mjs";
78

89
const versionBundlesUrl =
910
"https://raspberrypi.github.io/pico-vscode/" +
@@ -27,11 +28,27 @@ export interface VersionBundles {
2728
}
2829

2930
export default class VersionBundlesLoader {
31+
private static instance: VersionBundlesLoader | undefined;
3032
private bundles?: VersionBundles;
3133

32-
constructor(private readonly _extensionUri: Uri) {}
34+
private constructor(private readonly _extensionUri: Uri) {}
35+
36+
public static createInstance(extensionUri: Uri): VersionBundlesLoader {
37+
if (!VersionBundlesLoader.instance) {
38+
VersionBundlesLoader.instance = new VersionBundlesLoader(extensionUri);
39+
}
40+
41+
return VersionBundlesLoader.instance;
42+
}
43+
44+
public static getInstance(): VersionBundlesLoader {
45+
if (!VersionBundlesLoader.instance) {
46+
throw new Error("VersionBundlesLoader not initialized");
47+
}
48+
49+
return VersionBundlesLoader.instance;
50+
}
3351

34-
// TODO: may add singleton pattern
3552
private async loadBundles(): Promise<void> {
3653
try {
3754
if (!(await isInternetConnected())) {
@@ -57,8 +74,18 @@ export default class VersionBundlesLoader {
5774

5875
// Parse the JSON data when the download is complete
5976
response.on("end", () => {
60-
// Resolve with the array of VersionBundles
61-
resolve(JSON.parse(data) as VersionBundles);
77+
try {
78+
// Resolve with the array of VersionBundles
79+
resolve(JSON.parse(data) as VersionBundles);
80+
} catch (error) {
81+
reject(
82+
new Error(
83+
`Failed to parse version bundles JSON: ${unknownErrorToString(
84+
error
85+
)}`
86+
)
87+
);
88+
}
6289
});
6390

6491
// Handle errors
@@ -72,7 +99,7 @@ export default class VersionBundlesLoader {
7299
} catch (error) {
73100
Logger.log(
74101
"Failed to download version bundles:",
75-
error instanceof Error ? error.message : (error as string)
102+
unknownErrorToString(error)
76103
);
77104

78105
try {
@@ -89,7 +116,7 @@ export default class VersionBundlesLoader {
89116
} catch (e) {
90117
Logger.log(
91118
"Failed to load version bundles from local file:",
92-
e instanceof Error ? e.message : (e as string)
119+
unknownErrorToString(e)
93120
);
94121
this.bundles = {};
95122
}
@@ -105,22 +132,4 @@ export default class VersionBundlesLoader {
105132

106133
return (this.bundles ?? {})[version];
107134
}
108-
109-
public async getPythonWindowsAmd64Url(
110-
pythonVersion: string
111-
): Promise<VersionBundle | undefined> {
112-
if (this.bundles === undefined) {
113-
await this.loadBundles();
114-
}
115-
if (this.bundles === undefined) {
116-
return undefined;
117-
}
118-
119-
const bundle = Object.values(this.bundles).find(
120-
bundle => bundle.python.version === pythonVersion
121-
);
122-
123-
//return bundle?.python.windowsAmd64;
124-
return bundle;
125-
}
126135
}

src/webview/newProjectPanel.mts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1513,7 +1513,7 @@ export class NewProjectPanel {
15131513
: "light";
15141514
const riscvDefaultSvgUri = defaultTheme === "dark" ? riscvWhiteSvgUri : riscvBlackSvgUri;
15151515

1516-
this._versionBundlesLoader = new VersionBundlesLoader(this._extensionUri);
1516+
this._versionBundlesLoader = VersionBundlesLoader.getInstance();
15171517

15181518
// construct auxiliary html
15191519
// TODO: add offline handling - only load installed ones

0 commit comments

Comments
 (0)