This repository was archived by the owner on Apr 18, 2024. It is now read-only.
File tree 1 file changed +12
-2
lines changed
1 file changed +12
-2
lines changed Original file line number Diff line number Diff line change @@ -135,6 +135,7 @@ export const AppStore = types
135
135
. volatile ( ( ) => ( {
136
136
needsDataFetch : false ,
137
137
projectFetch : false ,
138
+ requestsInFlight : new Map ( ) ,
138
139
} ) )
139
140
. actions ( ( self ) => ( {
140
141
startPolling ( ) {
@@ -544,12 +545,21 @@ export const AppStore = types
544
545
* @param {{ errorHandler?: fn } } [options] additional options like errorHandler
545
546
*/
546
547
apiCall : flow ( function * ( methodName , params , body , options ) {
548
+ const controller = new AbortController ( ) ;
549
+ const signal = controller . signal ;
547
550
const apiTransform = self . SDK . apiTransform ?. [ methodName ] ;
548
551
const requestParams = apiTransform ?. params ?. ( params ) ?? params ?? { } ;
549
- const requestBody = apiTransform ?. body ?. ( body ) ?? body ?? undefined ;
550
-
552
+ const requestBody = { signal, ...( apiTransform ?. body ?. ( body ) ?? body ) } ;
553
+ const requestKey = `${ methodName } _${ JSON . stringify ( params || { } ) } ` ;
554
+
555
+ if ( self . requestsInFlight . has ( requestKey ) ) {
556
+ /* if already in flight cancel the first in favor of new one */
557
+ self . requestsInFlight . get ( requestKey ) . abort ( ) ;
558
+ }
559
+ self . requestsInFlight . set ( requestKey , controller ) ;
551
560
let result = yield self . API [ methodName ] ( requestParams , requestBody ) ;
552
561
562
+ self . requestsInFlight . delete ( requestKey ) ;
553
563
if ( result . error && result . status !== 404 ) {
554
564
if ( options ?. errorHandler ?. ( result ) ) {
555
565
return result ;
You can’t perform that action at this time.
0 commit comments