Skip to content

Commit 39f5911

Browse files
authored
fix(compass-aggregations): Don't lead debounce on aggregation run on input change, force run stage on pipeline changes COMPASS-5764 (#3076)
1 parent 3246d0e commit 39f5911

File tree

8 files changed

+36
-18
lines changed

8 files changed

+36
-18
lines changed

packages/compass-aggregations/src/components/pipeline-builder-workspace/pipeline-builder-workspace.jsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ class PipelineWorkspace extends PureComponent {
6868
onStageMoved = ({ oldIndex, newIndex }) => {
6969
if (oldIndex !== newIndex) {
7070
this.props.stageMoved(oldIndex, newIndex);
71-
this.props.runStage(0);
71+
this.props.runStage(0, true /* force execute */);
7272
}
7373
}
7474

packages/compass-aggregations/src/components/pipeline/modals/confirm-import-pipeline.jsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class ConfirmImportPipeline extends PureComponent {
3131
onConfirm = () => {
3232
this.props.confirmNew();
3333
if (this.props.isAutoPreviewing) {
34-
this.props.runStage(0);
34+
this.props.runStage(0, true /* force execute */);
3535
}
3636
}
3737

packages/compass-aggregations/src/components/settings/settings.jsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class Settings extends PureComponent {
6161
this.props.applySettings();
6262

6363
// Updated settings used to run all stages in the current pipeline.
64-
this.props.runStage(0);
64+
this.props.runStage(0, true /* force execute */);
6565

6666
// Hide the settings panel.
6767
this.props.toggleSettingsIsExpanded();

packages/compass-aggregations/src/components/stage-editor-toolbar/delete-stage.jsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class DeleteStage extends PureComponent {
2323
onStageDeleted = () => {
2424
this.props.stageDeleted(this.props.index);
2525
this.props.setIsModified(true);
26-
this.props.runStage(this.props.index);
26+
this.props.runStage(this.props.index, true /* force execute */);
2727
};
2828

2929
/**

packages/compass-aggregations/src/components/stage-editor-toolbar/toggle-stage.jsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class ToggleStage extends PureComponent {
2525
onStageToggled = () => {
2626
this.props.stageToggled(this.props.index);
2727
this.props.setIsModified(true);
28-
this.props.runStage(this.props.index);
28+
this.props.runStage(this.props.index, true /* force execute */);
2929
};
3030

3131
/**

packages/compass-aggregations/src/modules/auto-preview.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,16 @@ export const toggleAutoPreview = (
2525
newVal: boolean
2626
): ThunkAction<void, RootState, void, AutoPreviewToggledAction> => {
2727
return (dispatch) => {
28+
dispatch({
29+
type: ActionTypes.AutoPreviewToggled,
30+
value: newVal
31+
});
32+
2833
if (
2934
newVal &&
3035
global?.process?.env?.COMPASS_SHOW_NEW_AGGREGATION_TOOLBAR === 'true'
3136
) {
32-
dispatch(runStage(0));
37+
dispatch(runStage(0, true /* force execute */));
3338
}
34-
dispatch({
35-
type: ActionTypes.AutoPreviewToggled,
36-
value: newVal
37-
});
3839
};
3940
};

packages/compass-aggregations/src/modules/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -713,7 +713,7 @@ export const getPipelineFromIndexedDB = (pipelineId: string): ThunkAction<void,
713713
dispatch(clearPipeline());
714714
dispatch(restoreSavedPipeline(pipe));
715715
dispatch(globalAppRegistryEmit('compass:aggregations:pipeline-opened'));
716-
dispatch(runStage(0) as any);
716+
dispatch(runStage(0, true /* force execute */));
717717
} catch (e: unknown) {
718718
console.log(e);
719719
}
@@ -775,7 +775,7 @@ export const modifyView = (
775775
): ThunkAction<void, RootState, void, AnyAction> => {
776776
return (dispatch) => {
777777
dispatch(modifySource(viewName, viewPipeline, readonly, source));
778-
dispatch(runStage(0));
778+
dispatch(runStage(0, true /* force execute */));
779779
};
780780
};
781781

packages/compass-aggregations/src/modules/pipeline.ts

+23-6
Original file line numberDiff line numberDiff line change
@@ -779,9 +779,12 @@ const getCancelableExecuteAggDispatch = <
779779

780780
const ExecuteAggInflightMap = new Map<string, () => void>();
781781

782+
type CancellableDebounceExecuteAggregation = typeof _executeAggregation & {
783+
cancel(): void
784+
};
782785
const ExecuteAggDebounceMap = new Map<
783786
string,
784-
typeof _executeAggregation & { cancel(): void }
787+
CancellableDebounceExecuteAggregation
785788
>();
786789

787790
const _executeAggregation = (
@@ -806,13 +809,16 @@ const _executeAggregation = (
806809
}
807810
};
808811

809-
const getDebouncedExecuteAgg = (id: string): typeof _executeAggregation => {
812+
const getDebouncedExecuteAgg = (id: string): CancellableDebounceExecuteAggregation => {
810813
if (ExecuteAggDebounceMap.has(id)) {
811814
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
812815
return ExecuteAggDebounceMap.get(id)!;
813816
} else {
814817
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,
816822
trailing: true
817823
});
818824
ExecuteAggDebounceMap.set(id, fn);
@@ -838,11 +844,21 @@ const executeAggregation = (
838844
ns: string,
839845
_dispatch: Dispatch,
840846
getState: () => RootState,
841-
index: number
847+
index: number,
848+
forceExecute = false
842849
) => {
843850
const id = `${getState().aggregationWorkspaceId}::${index}`;
844851
const dispatch = getCancelableExecuteAggDispatch(id, _dispatch);
852+
845853
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+
}
846862
debouncedExecuteAgg(dataService, ns, dispatch, getState, index);
847863
};
848864

@@ -1033,7 +1049,8 @@ export const runOutStage = (index: number): ThunkAction<void, RootState, void, A
10331049
};
10341050

10351051
export const runStage = (
1036-
index: number
1052+
index: number,
1053+
forceExecute = false
10371054
): ThunkAction<void, RootState, void, AnyAction> => {
10381055
return (dispatch, getState) => {
10391056
const state = getState();
@@ -1051,7 +1068,7 @@ export const runStage = (
10511068
if (dataService) {
10521069
const ns = state.namespace;
10531070
for (let i = index; i < state.pipeline.length; i++) {
1054-
executeAggregation(dataService, ns, dispatch, getState, i);
1071+
executeAggregation(dataService, ns, dispatch, getState, i, forceExecute);
10551072
}
10561073
}
10571074
};

0 commit comments

Comments
 (0)