@@ -71,35 +71,18 @@ 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
76- try {
77- // We can't just return `version` because `0.12.0` should return `0.12.1`.
78- const availableVersions = ( await getVersions ( ) ) . map ( ( item ) => item . version ) ;
79- const selectedVersion = semver . maxSatisfying ( availableVersions , `^${ version . toString ( ) } ` ) ;
80- await context . globalState . update ( cacheKey , selectedVersion ? selectedVersion . raw : undefined ) ;
81- return selectedVersion ?? version ;
82- } catch {
83- const selectedVersion = context . globalState . get < string | null > ( cacheKey , null ) ;
84- return selectedVersion ? new semver . SemVer ( selectedVersion ) : version ;
85- }
75+ // We can't just return `version` because `0.12.0` should return `0.12.1`.
76+ const availableVersions = ( await getVersions ( context ) ) . map ( ( item ) => item . version ) ;
77+ const selectedVersion = semver . maxSatisfying ( availableVersions , `^${ version . toString ( ) } ` ) ;
78+ return selectedVersion ?? version ;
8679}
8780
8881async function getLatestTaggedZigVersion ( context : vscode . ExtensionContext ) : Promise < semver . SemVer | null > {
89- const cacheKey = "zig-latest-tagged" ;
90- try {
91- const zigVersion = await getVersions ( ) ;
92- const latestTagged = zigVersion . find ( ( item ) => item . version . prerelease . length === 0 ) ;
93- const result = latestTagged ?. version ?? null ;
94- await context . globalState . update ( cacheKey , latestTagged ?. version . raw ) ;
95- return result ;
96- } catch {
97- const latestTagged = context . globalState . get < string | null > ( cacheKey , null ) ;
98- if ( latestTagged ) {
99- return new semver . SemVer ( latestTagged ) ;
100- }
101- return null ;
102- }
82+ const zigVersion = await getVersions ( context ) ;
83+ const latestTagged = zigVersion . find ( ( item ) => item . version . prerelease . length === 0 ) ;
84+ const result = latestTagged ?. version ?? null ;
85+ return result ;
10386}
10487
10588/**
@@ -108,13 +91,29 @@ async function getLatestTaggedZigVersion(context: vscode.ExtensionContext): Prom
10891 *
10992 * Throws an exception when no network connection is available.
11093 */
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- ) ;
94+ async function getVersions ( context : vscode . ExtensionContext ) : Promise < zigUtil . ZigVersion [ ] > {
95+ const cacheKey = "zig-versions-list" ;
96+ let zigIndexJson , machIndexJson ;
97+ try {
98+ [ zigIndexJson , machIndexJson ] = await Promise . all (
99+ [ "https://ziglang.org/download/index.json" , "https://pkg.machengine.org/zig/index.json" ] . map (
100+ async ( url ) => {
101+ const response = await fetch ( url ) ;
102+ return response . json ( ) as Promise < zigUtil . VersionIndex > ;
103+ } ,
104+ ) ,
105+ ) ;
106+ } catch ( error ) {
107+ const cached = context . globalState . get < zigUtil . ZigVersion [ ] > ( cacheKey ) ;
108+ if ( cached !== undefined ) {
109+ for ( const version of cached ) {
110+ // Must be instanceof SemVer
111+ version . version = new semver . SemVer ( version . version . raw ) ;
112+ }
113+ return cached ;
114+ }
115+ throw error ;
116+ }
118117 const indexJson = { ...machIndexJson , ...zigIndexJson } ;
119118
120119 const result : zigUtil . ZigVersion [ ] = [ ] ;
@@ -140,6 +139,7 @@ async function getVersions(): Promise<zigUtil.ZigVersion[]> {
140139 ) ;
141140 }
142141 sortVersions ( result ) ;
142+ await context . globalState . update ( cacheKey , result ) ;
143143 return result ;
144144}
145145
@@ -182,7 +182,7 @@ async function selectVersionAndInstall(context: vscode.ExtensionContext) {
182182 } ) ) ;
183183
184184 try {
185- const onlineVersions = await getVersions ( ) ;
185+ const onlineVersions = await getVersions ( context ) ;
186186 outer: for ( const onlineVersion of onlineVersions ) {
187187 for ( const version of versions ) {
188188 if ( semver . eq ( version . version , onlineVersion . version ) ) {
@@ -591,7 +591,7 @@ async function getMirrors(context: vscode.ExtensionContext): Promise<vscode.Uri[
591591 timestamp : new Date ( ) . getTime ( ) ,
592592 mirrors : mirrorList ,
593593 } ;
594- context . globalState . update ( key , cached ) ;
594+ await context . globalState . update ( key , cached ) ;
595595 } catch {
596596 // Cannot fetch mirrors, rely on cache.
597597 }
0 commit comments