@@ -71,33 +71,24 @@ async function findClosestSatisfyingZigVersion(
7171 version : semver . SemVer ,
7272) : Promise < semver . SemVer > {
7373 if ( version . prerelease . length !== 0 ) return version ;
74- const cacheKey = `zig-satisfying-version-${ version . raw } ` ;
7574
7675 try {
7776 // We can't just return `version` because `0.12.0` should return `0.12.1`.
78- const availableVersions = ( await getVersions ( ) ) . map ( ( item ) => item . version ) ;
77+ const availableVersions = ( await getVersions ( context ) ) . map ( ( item ) => item . version ) ;
7978 const selectedVersion = semver . maxSatisfying ( availableVersions , `^${ version . toString ( ) } ` ) ;
80- await context . globalState . update ( cacheKey , selectedVersion ? selectedVersion . raw : undefined ) ;
8179 return selectedVersion ?? version ;
8280 } catch {
83- const selectedVersion = context . globalState . get < string | null > ( cacheKey , null ) ;
84- return selectedVersion ? new semver . SemVer ( selectedVersion ) : version ;
81+ return version ;
8582 }
8683}
8784
8885async function getLatestTaggedZigVersion ( context : vscode . ExtensionContext ) : Promise < semver . SemVer | null > {
89- const cacheKey = "zig-latest-tagged" ;
9086 try {
91- const zigVersion = await getVersions ( ) ;
87+ const zigVersion = await getVersions ( context ) ;
9288 const latestTagged = zigVersion . find ( ( item ) => item . version . prerelease . length === 0 ) ;
9389 const result = latestTagged ?. version ?? null ;
94- await context . globalState . update ( cacheKey , latestTagged ?. version . raw ) ;
9590 return result ;
9691 } catch {
97- const latestTagged = context . globalState . get < string | null > ( cacheKey , null ) ;
98- if ( latestTagged ) {
99- return new semver . SemVer ( latestTagged ) ;
100- }
10192 return null ;
10293 }
10394}
@@ -108,13 +99,29 @@ async function getLatestTaggedZigVersion(context: vscode.ExtensionContext): Prom
10899 *
109100 * Throws an exception when no network connection is available.
110101 */
111- async function getVersions ( ) : Promise < zigUtil . ZigVersion [ ] > {
112- const [ zigIndexJson , machIndexJson ] = await Promise . all (
113- [ "https://ziglang.org/download/index.json" , "https://pkg.machengine.org/zig/index.json" ] . map ( async ( url ) => {
114- const response = await fetch ( url ) ;
115- return response . json ( ) as Promise < zigUtil . VersionIndex > ;
116- } ) ,
117- ) ;
102+ async function getVersions ( context : vscode . ExtensionContext ) : Promise < zigUtil . ZigVersion [ ] > {
103+ const cacheKey = "zig-version-list" ;
104+ let zigIndexJson , machIndexJson ;
105+ try {
106+ [ zigIndexJson , machIndexJson ] = await Promise . all (
107+ [ "https://ziglang.org/download/index.json" , "https://pkg.machengine.org/zig/index.json" ] . map (
108+ async ( url ) => {
109+ const response = await fetch ( url ) ;
110+ return response . json ( ) as Promise < zigUtil . VersionIndex > ;
111+ } ,
112+ ) ,
113+ ) ;
114+ } catch ( error ) {
115+ const cached = context . globalState . get < zigUtil . ZigVersion [ ] > ( cacheKey ) ;
116+ if ( cached !== undefined ) {
117+ for ( const version of cached ) {
118+ // Must be instanceof SemVer
119+ version . version = new semver . SemVer ( version . version . raw ) ;
120+ }
121+ return cached ;
122+ }
123+ throw error ;
124+ }
118125 const indexJson = { ...machIndexJson , ...zigIndexJson } ;
119126
120127 const result : zigUtil . ZigVersion [ ] = [ ] ;
@@ -140,6 +147,7 @@ async function getVersions(): Promise<zigUtil.ZigVersion[]> {
140147 ) ;
141148 }
142149 sortVersions ( result ) ;
150+ await context . globalState . update ( cacheKey , result ) ;
143151 return result ;
144152}
145153
@@ -182,7 +190,7 @@ async function selectVersionAndInstall(context: vscode.ExtensionContext) {
182190 } ) ) ;
183191
184192 try {
185- const onlineVersions = await getVersions ( ) ;
193+ const onlineVersions = await getVersions ( context ) ;
186194 outer: for ( const onlineVersion of onlineVersions ) {
187195 for ( const version of versions ) {
188196 if ( semver . eq ( version . version , onlineVersion . version ) ) {
@@ -578,8 +586,8 @@ async function updateStatus(context: vscode.ExtensionContext): Promise<void> {
578586}
579587
580588async function getMirrors ( context : vscode . ExtensionContext ) : Promise < vscode . Uri [ ] > {
581- const key = "zig-mirror-cache " ;
582- let cached = context . globalState . get ( key , { timestamp : 0 , mirrors : "" } ) ;
589+ const cacheKey = "zig-mirror-list " ;
590+ let cached = context . globalState . get ( cacheKey , { timestamp : 0 , mirrors : "" } ) ;
583591
584592 const millisecondsInDay = 24 * 60 * 60 * 1000 ;
585593 if ( new Date ( ) . getTime ( ) - cached . timestamp > millisecondsInDay ) {
@@ -591,7 +599,7 @@ async function getMirrors(context: vscode.ExtensionContext): Promise<vscode.Uri[
591599 timestamp : new Date ( ) . getTime ( ) ,
592600 mirrors : mirrorList ,
593601 } ;
594- context . globalState . update ( key , cached ) ;
602+ await context . globalState . update ( cacheKey , cached ) ;
595603 } catch {
596604 // Cannot fetch mirrors, rely on cache.
597605 }
0 commit comments