@@ -164,11 +164,11 @@ function get_ipa_no_cache(text, args) {
164164 case "Czech" :
165165 if ( langForm === "Phonemic" ) {
166166 let dictRecord = globalThis . lexicon . get (
167- cleanText . replace ( / [ ^ \p{ Letter} \p{ Mark} - ] + / gu, "" ) ,
167+ cleanText . replace ( / [ ^ \p{ Letter} \p{ Mark} - ] + / gu, "" ) ,
168168 ) ;
169169 if ( ! dictRecord ) {
170170 dictRecord = globalThis . lexicon . get (
171- cleanText . replace ( / [ ^ \p{ Letter} \p{ Mark} - ] + / gu, "" ) . toLowerCase ( ) ,
171+ cleanText . replace ( / [ ^ \p{ Letter} \p{ Mark} - ] + / gu, "" ) . toLowerCase ( ) ,
172172 ) ;
173173 }
174174 console . log ( cleanText , dictRecord ) ;
@@ -264,6 +264,63 @@ async function loadFileFromZipOrPath(zipPathOrBlob, filename) {
264264 } ) ;
265265}
266266
267+ async function sendParallelRequests ( urls ) {
268+ const promises = urls . map ( ( url ) => fetch ( url ) ) ;
269+
270+ try {
271+ const results = await Promise . any ( promises ) ;
272+ return results ;
273+ } catch ( error ) {
274+ return null ; // Handle errors if no promises resolve successfully
275+ }
276+ }
277+
278+ async function fetchWithCacheMultiple ( urls ) {
279+ const url = urls [ 0 ] . split ( "/" ) . slice ( - 2 ) . join ( "/" ) ;
280+
281+ console . log ( "reading cache" , url ) ;
282+ const cachedResponse = await localforage . getItem ( url ) ;
283+ if ( cachedResponse ) {
284+ console . log ( "reading from cache" , url ) ;
285+ if ( cachedResponse instanceof Blob ) {
286+ const response = new Response ( cachedResponse ) ;
287+ response . headers . set ( "X-From-Cache" , "true" ) ;
288+ console . log ( "Returned cached blob " , url ) ;
289+ return response ;
290+ }
291+ const response = new Response ( JSON . parse ( cachedResponse ) ) ;
292+ response . headers . set ( "X-From-Cache" , "true" ) ;
293+ console . log ( "Returned cached string " , url ) ;
294+ return response ;
295+ }
296+ console . log ( "caching " , url ) ;
297+
298+ // const response = await fetch(url);
299+ const response = await sendParallelRequests ( urls ) ;
300+
301+ const contentType = response . headers . get ( "content-type" ) ;
302+ let responseContent ;
303+ let responseWithHeaders ;
304+
305+ if ( contentType == "application/zip" ) {
306+ responseContent = await response . blob ( ) ;
307+ try {
308+ await localforage . setItem ( url , responseContent ) ;
309+ } catch ( err ) {
310+ console . log ( err ) ;
311+ }
312+ } else {
313+ responseContent = await response . text ( ) ;
314+ try {
315+ await localforage . setItem ( url , JSON . stringify ( responseContent ) ) ;
316+ } catch ( err ) {
317+ console . log ( err ) ;
318+ }
319+ }
320+ responseWithHeaders = new Response ( responseContent , response ) ;
321+ return responseWithHeaders ;
322+ }
323+
267324async function fetchWithCache ( url ) {
268325 console . log ( "reading cache" , url ) ;
269326 const cachedResponse = await localforage . getItem ( url ) ;
@@ -358,10 +415,10 @@ function enableAll(include_elements = []) {
358415 forms . forEach ( ( form ) => {
359416 // Enable all elements in the form
360417 Array . from ( form . elements )
361- . concat ( include_elements )
362- . forEach ( ( element ) => {
363- element . disabled = false ;
364- } ) ;
418+ . concat ( include_elements )
419+ . forEach ( ( element ) => {
420+ element . disabled = false ;
421+ } ) ;
365422 } ) ;
366423}
367424
@@ -376,6 +433,7 @@ export {
376433 loadFileFromZipOrPath ,
377434 createElementFromHTML ,
378435 fetchWithCache ,
436+ fetchWithCacheMultiple ,
379437 disableAll ,
380438 enableAll ,
381439} ;
0 commit comments