@@ -4,6 +4,7 @@ import { isInternetConnected } from "./downloadHelpers.mjs";
44import { get } from "https" ;
55import Logger from "../logger.mjs" ;
66import { CURRENT_DATA_VERSION } from "./sharedConstants.mjs" ;
7+ import { unknownErrorToString } from "./errorHelper.mjs" ;
78
89const versionBundlesUrl =
910 "https://raspberrypi.github.io/pico-vscode/" +
@@ -27,11 +28,27 @@ export interface VersionBundles {
2728}
2829
2930export 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}
0 commit comments