11import { request , cacheWrapper , HTTP_GET } from './request.js' ;
22
3- const objectPool = ( ( ) => {
4- /**
5- * @type {Map<string, Promise<Cache>>|null }
6- */
7- let cachePool = null ;
8-
9- return {
10- /**
11- * @param {string } name
12- * @returns {Promise<Cache> }
13- */
14- getInstance : ( name ) => {
15- if ( ! cachePool ) {
16- cachePool = new Map ( ) ;
17- }
18-
19- if ( ! cachePool . has ( name ) ) {
20- cachePool . set ( name , window . caches . open ( name ) ) ;
21- }
22-
23- return cachePool . get ( name ) ;
24- } ,
25- } ;
26- } ) ( ) ;
27-
283export const cache = ( cacheName ) => {
294
305 /**
@@ -38,49 +13,38 @@ export const cache = (cacheName) => {
3813 const inFlightRequests = new Map ( ) ;
3914
4015 /**
41- * @type {Cache|null }
16+ * @type {ReturnType<typeof cacheWrapper> }
4217 */
43- let cacheObject = null ;
18+ const cw = cacheWrapper ( cacheName ) ;
4419
4520 let ttl = 1000 * 60 * 60 * 6 ;
4621
4722 let forceCache = false ;
4823
49- /**
50- * @returns {Promise<Cache>|null }
51- */
52- const open = async ( ) => {
53- if ( ! cacheObject && window . isSecureContext ) {
54- cacheObject = await objectPool . getInstance ( cacheName ) ;
55- }
56-
57- return cacheObject ;
58- } ;
59-
6024 /**
6125 * @param {string|URL } input
6226 * @param {Response } res
6327 * @returns {Response }
6428 */
65- const set = ( input , res ) => open ( ) . then ( cacheWrapper ) . then ( ( cw ) => {
29+ const set = ( input , res ) => {
6630 if ( ! res . ok ) {
6731 throw new Error ( res . statusText ) ;
6832 }
6933
7034 return cw . set ( input , res , forceCache , ttl ) ;
71- } ) ;
35+ } ;
7236
7337 /**
7438 * @param {string|URL } input
7539 * @returns {Promise<Response|null> }
7640 */
77- const has = ( input ) => open ( ) . then ( cacheWrapper ) . then ( ( cw ) => cw . has ( input ) ) ;
41+ const has = ( input ) => cw . has ( input ) ;
7842
7943 /**
8044 * @param {string|URL } input
8145 * @returns {Promise<boolean> }
8246 */
83- const del = ( input ) => open ( ) . then ( cacheWrapper ) . then ( ( cw ) => cw . del ( input ) ) ;
47+ const del = ( input ) => cw . del ( input ) ;
8448
8549 /**
8650 * @param {string } input
@@ -101,8 +65,8 @@ export const cache = (cacheName) => {
10165 */
10266 const fetchPut = ( ) => request ( HTTP_GET , input ) . withCancel ( cancel ) . withRetry ( ) . default ( ) ;
10367
104- const inflightPromise = open ( )
105- . then ( ( ) => window . isSecureContext ? has ( input ) . then ( ( res ) => res ? Promise . resolve ( res ) : del ( input ) . then ( fetchPut ) . then ( ( r ) => set ( input , r ) ) ) : fetchPut ( ) )
68+ const inflightPromise = has ( input )
69+ . then ( ( res ) => res ? Promise . resolve ( res ) : del ( input ) . then ( fetchPut ) . then ( ( r ) => set ( input , r ) ) )
10670 . then ( ( r ) => r . blob ( ) )
10771 . then ( ( b ) => objectUrls . set ( input , URL . createObjectURL ( b ) ) )
10872 . then ( ( ) => objectUrls . get ( input ) )
@@ -117,13 +81,9 @@ export const cache = (cacheName) => {
11781 * @param {Promise<void>|null } cancel
11882 * @returns {Promise<void> }
11983 */
120- const run = ( items , cancel = null ) => open ( ) . then ( ( ) => {
84+ const run = ( items , cancel = null ) => {
12185 const uniq = new Map ( ) ;
12286
123- if ( ! window . isSecureContext ) {
124- console . warn ( 'Cache is not supported in insecure context' ) ;
125- }
126-
12787 if ( items . length === 0 ) {
12888 return Promise . resolve ( ) ;
12989 }
@@ -143,7 +103,7 @@ export const cache = (cacheName) => {
143103 return r ;
144104 } )
145105 ) ) ;
146- } ) ;
106+ } ;
147107
148108 /**
149109 * @param {string } input
0 commit comments