@@ -40,19 +40,14 @@ TABLE_NAMES_ARR.forEach(t => {
4040 cache [ t ] = null ;
4141} ) ;
4242
43- const updateCacheQueue : {
44- [ x : string ] : Array < { resolve : ( ) => void ; reject : ( error : any ) => void } > ;
45- } = { } ;
43+ const updateCacheQueue : Record < string , Promise < void > > = { } ;
4644
47- async function updateCache ( type : TABLE_NAMES ) : Promise < void > {
48- return new Promise ( ( resolve , reject ) => {
49- // 如果正在Update,则放到回调组里面
50- if ( typeof updateCacheQueue [ type ] !== 'undefined' ) {
51- updateCacheQueue [ type ] . push ( { resolve, reject } ) ;
52- return ;
53- } else {
54- updateCacheQueue [ type ] = [ { resolve, reject } ] ;
55- }
45+ function updateCache ( type : TABLE_NAMES ) : Promise < void > {
46+ if ( typeof updateCacheQueue [ type ] !== 'undefined' ) {
47+ return updateCacheQueue [ type ] ;
48+ }
49+
50+ const p = new Promise < void > ( ( resolve , reject ) => {
5651 getDatabase ( )
5752 . then ( db => {
5853 const tx = db . transaction ( [ type ] , 'readonly' ) ;
@@ -73,20 +68,21 @@ async function updateCache(type: TABLE_NAMES): Promise<void> {
7368 cursor . continue ( ) ;
7469 } else {
7570 cache [ type ] = all ;
76- updateCacheQueue [ type ] . forEach ( it => {
77- it . resolve ( ) ;
78- } ) ;
79- delete updateCacheQueue [ type ] ;
71+ resolve ( ) ;
8072 }
8173 } ;
8274 } )
8375 . catch ( e => {
84- updateCacheQueue [ type ] . forEach ( it => {
85- it . reject ( e ) ;
86- } ) ;
76+ reject ( e ) ;
77+ } )
78+ . finally ( ( ) => {
8779 delete updateCacheQueue [ type ] ;
8880 } ) ;
8981 } ) ;
82+
83+ updateCacheQueue [ type ] = p ;
84+
85+ return p ;
9086}
9187
9288function filter ( fromRules : InitdRule [ ] , options ?: RuleFilterOptions ) {
0 commit comments