@@ -159,6 +159,7 @@ class LRUCache {
159159 maxSize = 0 ,
160160 sizeCalculation,
161161 fetchMethod,
162+ fetchContext,
162163 noDeleteOnFetchRejection,
163164 noDeleteOnStaleGet,
164165 } = options
@@ -198,6 +199,13 @@ class LRUCache {
198199 )
199200 }
200201
202+ this . fetchContext = fetchContext
203+ if ( ! this . fetchMethod && fetchContext !== undefined ) {
204+ throw new TypeError (
205+ 'cannot set fetchContext without fetchMethod'
206+ )
207+ }
208+
201209 this . keyMap = new Map ( )
202210 this . keyList = new Array ( max ) . fill ( null )
203211 this . valList = new Array ( max ) . fill ( null )
@@ -676,7 +684,7 @@ class LRUCache {
676684 }
677685 }
678686
679- backgroundFetch ( k , index , options ) {
687+ backgroundFetch ( k , index , options , context ) {
680688 const v = index === undefined ? undefined : this . valList [ index ]
681689 if ( this . isBackgroundFetch ( v ) ) {
682690 return v
@@ -685,6 +693,7 @@ class LRUCache {
685693 const fetchOpts = {
686694 signal : ac . signal ,
687695 options,
696+ context,
688697 }
689698 const cb = v => {
690699 if ( ! ac . signal . aborted ) {
@@ -753,6 +762,7 @@ class LRUCache {
753762 noUpdateTTL = this . noUpdateTTL ,
754763 // fetch exclusive options
755764 noDeleteOnFetchRejection = this . noDeleteOnFetchRejection ,
765+ fetchContext = this . fetchContext ,
756766 } = { }
757767 ) {
758768 if ( ! this . fetchMethod ) {
@@ -773,7 +783,7 @@ class LRUCache {
773783
774784 let index = this . keyMap . get ( k )
775785 if ( index === undefined ) {
776- const p = this . backgroundFetch ( k , index , options )
786+ const p = this . backgroundFetch ( k , index , options , fetchContext )
777787 return ( p . __returned = p )
778788 } else {
779789 // in cache, maybe already fetching
@@ -794,7 +804,7 @@ class LRUCache {
794804
795805 // ok, it is stale, and not already fetching
796806 // refresh the cache.
797- const p = this . backgroundFetch ( k , index , options )
807+ const p = this . backgroundFetch ( k , index , options , fetchContext )
798808 return allowStale && p . __staleWhileFetching !== undefined
799809 ? p . __staleWhileFetching
800810 : ( p . __returned = p )
0 commit comments