Skip to content

Commit

Permalink
Merge pull request #18303 from ElectronicBlueberry/fix-label-updated-…
Browse files Browse the repository at this point in the history
…popup

[24.1] Fix "label updated" popup triggers too often
  • Loading branch information
mvdbeek authored Jun 3, 2024
2 parents 0a7de5c + 51ae8d7 commit fa9af6f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 18 deletions.
56 changes: 39 additions & 17 deletions client/src/components/Workflow/Editor/Actions/stepActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -423,34 +423,46 @@ export function useStepActions(
}
}

interface ChangeValueOrCreateActionOptions<K extends keyof Step> {
step: Step;
key: K;
value: Step[K];
name?: string;
actionConstructor?: () => LazyMutateStepAction<K>;
keepActionAlive?: boolean;
timeout?: number;
}

/**
* Mutates a queued lazy action, if a matching one exists,
* otherwise creates a new lazy action ans queues it.
*/
function changeValueOrCreateAction<K extends keyof Step>(
step: Step,
key: K,
value: Step[K],
name?: string,
actionConstructor?: () => LazyMutateStepAction<K>
options: ChangeValueOrCreateActionOptions<K>
): InstanceType<typeof LazyMutateStepAction<K>> {
const { step, key, value, name, keepActionAlive, timeout } = options;
const actionForKey = actionForIdAndKey(step.id, key);

if (actionForKey) {
actionForKey.changeValue(value);

if (keepActionAlive) {
undoRedoStore.setLazyActionTimeout(timeout);
}

return actionForKey;
} else {
actionConstructor =
actionConstructor ?? (() => new LazyMutateStepAction(stepStore, step.id, key, step[key], value));
const actionConstructor =
options.actionConstructor ??
(() => new LazyMutateStepAction(stepStore, step.id, key, step[key], value));

const action = actionConstructor();

if (name) {
action.name = name;
}

undoRedoStore.applyLazyAction(action);
undoRedoStore.applyLazyAction(action, timeout);

action.onUndoRedo = () => {
stateStore.activeNodeId = step.id;
Expand All @@ -462,11 +474,11 @@ export function useStepActions(
}

function setPosition(step: Step, position: NonNullable<Step["position"]>) {
changeValueOrCreateAction(step, "position", position, "change step position");
changeValueOrCreateAction({ step, key: "position", value: position, name: "change step position" });
}

function setAnnotation(step: Step, annotation: Step["annotation"]) {
changeValueOrCreateAction(step, "annotation", annotation, "modify step annotation");
changeValueOrCreateAction({ step, key: "annotation", value: annotation, name: "modify step annotation" });
}

function setOutputLabel(
Expand All @@ -478,18 +490,28 @@ export function useStepActions(
const actionConstructor = () =>
new LazySetOutputLabelAction(stepStore, stateStore, step.id, fromLabel, toLabel, workflowOutputs);

changeValueOrCreateAction(
changeValueOrCreateAction({
step,
"workflow_outputs",
workflowOutputs,
"modify step output label",
actionConstructor
);
key: "workflow_outputs",
value: workflowOutputs,
name: "modify step output label",
actionConstructor,
keepActionAlive: true,
timeout: 2000,
});
}

function setLabel(step: Step, label: Step["label"]) {
const actionConstructor = () => new LazySetLabelAction(stepStore, stateStore, step.id, step.label, label);
changeValueOrCreateAction(step, "label", label, "modify step label", actionConstructor);
changeValueOrCreateAction({
step,
key: "label",
value: label,
name: "modify step label",
actionConstructor,
keepActionAlive: true,
timeout: 2000,
});
}

const { refresh } = useRefreshFromStore();
Expand Down
2 changes: 1 addition & 1 deletion client/src/stores/undoRedoStore/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export const useUndoRedoStore = defineScopedStore("undoRedoStore", () => {
}
}

function setLazyActionTimeout(timeout: number) {
function setLazyActionTimeout(timeout = 1000) {
clearTimeout(lazyActionTimeout);
lazyActionTimeout = setTimeout(() => flushLazyAction(), timeout);
}
Expand Down

0 comments on commit fa9af6f

Please sign in to comment.