@@ -779,9 +779,12 @@ const getCancelableExecuteAggDispatch = <
779
779
780
780
const ExecuteAggInflightMap = new Map < string , ( ) => void > ( ) ;
781
781
782
+ type CancellableDebounceExecuteAggregation = typeof _executeAggregation & {
783
+ cancel ( ) : void
784
+ } ;
782
785
const ExecuteAggDebounceMap = new Map <
783
786
string ,
784
- typeof _executeAggregation & { cancel ( ) : void }
787
+ CancellableDebounceExecuteAggregation
785
788
> ( ) ;
786
789
787
790
const _executeAggregation = (
@@ -806,13 +809,16 @@ const _executeAggregation = (
806
809
}
807
810
} ;
808
811
809
- const getDebouncedExecuteAgg = ( id : string ) : typeof _executeAggregation => {
812
+ const getDebouncedExecuteAgg = ( id : string ) : CancellableDebounceExecuteAggregation => {
810
813
if ( ExecuteAggDebounceMap . has ( id ) ) {
811
814
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
812
815
return ExecuteAggDebounceMap . get ( id ) ! ;
813
816
} else {
814
817
const fn = debounce ( _executeAggregation , 750 , {
815
- leading : true ,
818
+ // We don't want the documents to re-render while the user is typing
819
+ // so we want to only have the documents return when the user
820
+ // finishing typing at the end (trailing) of the debounce.
821
+ leading : false ,
816
822
trailing : true
817
823
} ) ;
818
824
ExecuteAggDebounceMap . set ( id , fn ) ;
@@ -838,11 +844,21 @@ const executeAggregation = (
838
844
ns : string ,
839
845
_dispatch : Dispatch ,
840
846
getState : ( ) => RootState ,
841
- index : number
847
+ index : number ,
848
+ forceExecute = false
842
849
) => {
843
850
const id = `${ getState ( ) . aggregationWorkspaceId } ::${ index } ` ;
844
851
const dispatch = getCancelableExecuteAggDispatch ( id , _dispatch ) ;
852
+
845
853
const debouncedExecuteAgg = getDebouncedExecuteAgg ( id ) ;
854
+
855
+ if ( forceExecute ) {
856
+ // Cancel previously in-flight requests.
857
+ debouncedExecuteAgg . cancel ( ) ;
858
+
859
+ _executeAggregation ( dataService , ns , dispatch , getState , index ) ;
860
+ return ;
861
+ }
846
862
debouncedExecuteAgg ( dataService , ns , dispatch , getState , index ) ;
847
863
} ;
848
864
@@ -1033,7 +1049,8 @@ export const runOutStage = (index: number): ThunkAction<void, RootState, void, A
1033
1049
} ;
1034
1050
1035
1051
export const runStage = (
1036
- index : number
1052
+ index : number ,
1053
+ forceExecute = false
1037
1054
) : ThunkAction < void , RootState , void , AnyAction > => {
1038
1055
return ( dispatch , getState ) => {
1039
1056
const state = getState ( ) ;
@@ -1051,7 +1068,7 @@ export const runStage = (
1051
1068
if ( dataService ) {
1052
1069
const ns = state . namespace ;
1053
1070
for ( let i = index ; i < state . pipeline . length ; i ++ ) {
1054
- executeAggregation ( dataService , ns , dispatch , getState , i ) ;
1071
+ executeAggregation ( dataService , ns , dispatch , getState , i , forceExecute ) ;
1055
1072
}
1056
1073
}
1057
1074
} ;
0 commit comments