@@ -28,7 +28,7 @@ export function memoExternal<F extends AsyncFn>(
28
28
} else {
29
29
try {
30
30
// Waiting or Removing
31
- if ( cur . state === State . Removing ) {
31
+ while ( cur . state === State . Removing ) {
32
32
await new Promise < void > ( ( res ) => {
33
33
if ( ! cur . removingCallbacks ) {
34
34
cur . removingCallbacks = new Set ( ) ;
@@ -90,33 +90,41 @@ export function memoExternal<F extends AsyncFn>(
90
90
const cur = walkOrBreak < F , any [ ] > ( root , path ) ;
91
91
92
92
if ( cur ) {
93
- if ( cur . state === State . Waiting ) {
94
- await new Promise ( ( res ) => {
95
- if ( ! cur . callbacks ) {
96
- cur . callbacks = new Set ( ) ;
97
- }
98
- // Ignore error
99
- cur . callbacks ! . add ( { res, rej : ( ) => { } } ) ;
100
- } ) ;
101
- } else if ( cur . state === State . Removing ) {
102
- await new Promise < void > ( ( res ) => {
103
- if ( ! cur . removingCallbacks ) {
104
- cur . removingCallbacks = new Set ( ) ;
105
- }
106
- // Ignore error
107
- cur . removingCallbacks ! . add ( { res, rej : ( ) => { } } ) ;
108
- } ) ;
93
+ while ( cur . state === State . Waiting || cur . state === State . Removing ) {
94
+ if ( cur . state === State . Waiting ) {
95
+ await new Promise ( ( res ) => {
96
+ if ( ! cur . callbacks ) {
97
+ cur . callbacks = new Set ( ) ;
98
+ }
99
+ // Ignore error
100
+ cur . callbacks ! . add ( { res, rej : ( ) => { } } ) ;
101
+ } ) ;
102
+ } else if ( cur . state === State . Removing ) {
103
+ await new Promise < void > ( ( res ) => {
104
+ if ( ! cur . removingCallbacks ) {
105
+ cur . removingCallbacks = new Set ( ) ;
106
+ }
107
+ // Ignore error
108
+ cur . removingCallbacks ! . add ( { res, rej : ( ) => { } } ) ;
109
+ } ) ;
110
+ }
109
111
}
110
112
111
- await options . external . remove
112
- . bind ( memoFunc ) ( args as Parameters < F > )
113
- . catch ( options . external ?. error ?? ( ( ) => undefined ) ) ;
113
+ try {
114
+ cur . state = State . Removing ;
115
+
116
+ await options . external . remove
117
+ . bind ( memoFunc ) ( args as Parameters < F > )
118
+ . catch ( options . external ?. error ?? ( ( ) => undefined ) ) ;
114
119
115
- // Resolve other waiting callbacks
116
- for ( const callback of cur . removingCallbacks ?? [ ] ) {
117
- callback . res ( ) ;
120
+ // Resolve other waiting callbacks
121
+ for ( const callback of cur . removingCallbacks ?? [ ] ) {
122
+ callback . res ( ) ;
123
+ }
124
+ cur . removingCallbacks = undefined ;
125
+ } finally {
126
+ cur . state = State . Empty ;
118
127
}
119
- cur . removingCallbacks = undefined ;
120
128
}
121
129
} ;
122
130
0 commit comments