-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #184 from flatironinstitute/reduce-redundant-compi…
…le-jobs reduce redundant compile jobs, cache main.js URL
- Loading branch information
Showing
3 changed files
with
87 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
export const checkMainJsUrlCache = async ( | ||
stanProgram: string, | ||
baseStanWasmServerUrl: string, | ||
): Promise<string | null> => { | ||
const mainJsCache = localStorage.getItem("mainJsCache"); | ||
if (!mainJsCache) return null; | ||
try { | ||
const cache = JSON.parse(mainJsCache); | ||
const stanProgramHash = stringChecksum(stanProgram); | ||
const url = cache[stanProgramHash]; | ||
if (!url) return null; | ||
if (!url.startsWith(baseStanWasmServerUrl)) { | ||
// the url is not from the current server | ||
return null; | ||
} | ||
// check to see if the url is still valid | ||
const exists = await checkRemoteFileExists(url); | ||
if (!exists) { | ||
delete cache[stanProgramHash]; | ||
localStorage.setItem("mainJsCache", JSON.stringify(cache)); | ||
return null; | ||
} | ||
return url; | ||
} catch (e) { | ||
console.error("Problem parsing mainJsCache"); | ||
console.error(e); | ||
try { | ||
localStorage.removeItem("mainJsCache"); | ||
} catch (e) { | ||
console.error("Problem removing mainJsCache"); | ||
console.error(e); | ||
} | ||
return null; | ||
} | ||
}; | ||
|
||
export const setToMainJsUrlCache = (stanProgram: string, url: string) => { | ||
try { | ||
const mainJsCache = localStorage.getItem("mainJsCache"); | ||
const stanProgramHash = stringChecksum(stanProgram); | ||
let cache = mainJsCache ? JSON.parse(mainJsCache) : {}; | ||
cache = cleanupIfGettingTooBig(cache); | ||
cache[stanProgramHash] = url; | ||
localStorage.setItem("mainJsCache", JSON.stringify(cache)); | ||
} catch (e) { | ||
console.error("Problem setting mainJsCache"); | ||
console.error(e); | ||
} | ||
}; | ||
|
||
const cleanupIfGettingTooBig = (cache: { [key: string]: string }) => { | ||
// for now we just clear the whole thing if it's getting too big | ||
if (Object.keys(cache).length > 50) { | ||
return {}; | ||
} | ||
return cache; | ||
}; | ||
|
||
const stringChecksum = (str: string) => { | ||
let hash = 0; | ||
if (str.length == 0) return hash; | ||
for (let i = 0; i < str.length; i++) { | ||
const char = str.charCodeAt(i); | ||
hash = (hash << 5) - hash + char; | ||
hash = hash & hash; // Convert to 32bit integer | ||
} | ||
return hash; | ||
}; | ||
|
||
const checkRemoteFileExists = async (url: string): Promise<boolean> => { | ||
const response = await fetch(url, { method: "HEAD" }); | ||
return response.ok; | ||
}; |