@@ -65,10 +65,7 @@ export abstract class CachedResource<V> {
6565 if ( duration < this . expirationInMilli ) {
6666 logger . info ( `cache hit, duration(%sms) is less than expiration(%sms)` , duration , this . expirationInMilli )
6767 // release the lock
68- await this . updateResourceCache ( cachedValue , {
69- ...resource ,
70- locked : false ,
71- } )
68+ await this . releaseLock ( resource , cachedValue )
7269 return resource . result
7370 } else {
7471 logger . info (
@@ -97,7 +94,7 @@ export abstract class CachedResource<V> {
9794 result : latest ,
9895 }
9996 logger . info ( `doen loading the latest of resource(%s), updating resource cache` , this . key )
100- await this . updateResourceCache ( cachedValue , r )
97+ await this . releaseLock ( r )
10198 return latest
10299 } catch ( e ) {
103100 logger . info (
@@ -115,10 +112,7 @@ export abstract class CachedResource<V> {
115112 const cachedValue = this . readCacheOrDefault ( )
116113
117114 if ( ! cachedValue . resource . locked ) {
118- await this . updateResourceCache ( cachedValue , {
119- ...cachedValue . resource ,
120- locked : true ,
121- } )
115+ await this . lockResource ( cachedValue )
122116 return cachedValue
123117 }
124118
@@ -136,18 +130,32 @@ export abstract class CachedResource<V> {
136130 return lock
137131 }
138132
139- // TODO: releaseLock and updateCache do similar things, how to improve
140- async releaseLock ( ) {
141- await globals . globalState . update ( this . key , {
142- ...this . readCacheOrDefault ( ) ,
143- locked : false ,
144- } )
133+ async lockResource ( baseCache : GlobalStateSchema < V > ) : Promise < void > {
134+ await this . updateResourceCache ( { locked : true } , baseCache )
135+ }
136+
137+ async releaseLock ( ) : Promise < void >
138+ async releaseLock ( resource : Partial < Resource < V > > ) : Promise < void >
139+ async releaseLock ( resource : Partial < Resource < V > > , baseCache : GlobalStateSchema < V > ) : Promise < void >
140+ async releaseLock ( resource ?: Partial < Resource < V > > , baseCache ?: GlobalStateSchema < V > ) : Promise < void > {
141+ if ( ! resource ) {
142+ await this . updateResourceCache ( { locked : false } , undefined )
143+ } else if ( baseCache ) {
144+ await this . updateResourceCache ( resource , baseCache )
145+ } else {
146+ await this . updateResourceCache ( resource , undefined )
147+ }
145148 }
146149
147- private async updateResourceCache ( cache : GlobalStateSchema < any > | undefined , resource : Resource < any > ) {
150+ private async updateResourceCache ( resource : Partial < Resource < any > > , cache : GlobalStateSchema < any > | undefined ) {
151+ const baseCache = cache ?? this . readCacheOrDefault ( )
152+
148153 const toUpdate : GlobalStateSchema < V > = {
149- ...( cache ? cache : this . readCacheOrDefault ( ) ) ,
150- resource : resource ,
154+ ...baseCache ,
155+ resource : {
156+ ...baseCache . resource ,
157+ ...resource ,
158+ } ,
151159 }
152160
153161 await globals . globalState . update ( this . key , toUpdate )
0 commit comments