From 0745ff727097e65694b2ae156e10a60200dbe3e3 Mon Sep 17 00:00:00 2001 From: Jeya Balaji Balasubramanian Date: Sun, 27 Aug 2023 15:38:38 -0400 Subject: [PATCH] Enable preloading files --- wasm-icare.js | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/wasm-icare.js b/wasm-icare.js index f4b3a33..7242ef4 100644 --- a/wasm-icare.js +++ b/wasm-icare.js @@ -20,6 +20,8 @@ class WasmICARE { this.pyICareVersion = '1.0.0'; // Version of Pyodide to load from the CDN. this.pyodideVersion = '0.23.2'; + // Files that are pre-loaded to the Pyodide file system. + this.preloadedFiles = []; } /** @@ -82,6 +84,24 @@ class WasmICARE { return await Promise.all(fileURLs.map(fetchAndWriteFile)); } + /** + * Method to load files from a list of URLs and write them to the Pyodide file system. This utility allows you to + * save time from network latencies when making several calls to computing absolute risk. When files are loaded + * using this option, they will not be re-loaded when computing absolute risk. + * @param fileURLs + * @returns {Promise[]>} + */ + async preloadFiles(fileURLs) { + if (!this.pyodide) { + throw new Error('Please instantiate this class using the WasmICARE.initialize() method.'); + } + + const fileLoadAndWritePromises = await this.fetchFilesAndWriteToPyodideFS(fileURLs); + this.preloadedFiles = fileURLs; + + return fileLoadAndWritePromises; + } + /** * Method to convert the Wasm-iCARE output to JSON. * @param obj @@ -227,7 +247,7 @@ class WasmICARE { modelSnpInfoUrl, applyCovariateProfileUrl, applySnpProfileUrl, - ].filter(url => url !== undefined); + ].filter(url => url !== undefined).filter(url => !this.preloadedFiles.includes(url)); await this.fetchFilesAndWriteToPyodideFS(fileURLs); @@ -446,7 +466,7 @@ result applyCovariateProfileBeforeCutpointUrl, applyCovariateProfileAfterCutpointUrl, applySnpProfileUrl, - ].filter(url => url !== undefined); + ].filter(url => url !== undefined).filter(url => !this.preloadedFiles.includes(url)); await this.fetchFilesAndWriteToPyodideFS(fileURLs); @@ -707,7 +727,7 @@ result icareModelParameters.modelSnpInfoUrl, icareModelParameters.applyCovariateProfileUrl, icareModelParameters.applySnpProfileUrl, - ].filter(url => url !== undefined); + ].filter(url => url !== undefined).filter(url => !this.preloadedFiles.includes(url)); await this.fetchFilesAndWriteToPyodideFS(fileURLs);