Add debounce to autoSaveCurrentFlow function
This commit is contained in:
parent
bd1bf4b16e
commit
1bfa6d2a32
1 changed files with 11 additions and 12 deletions
|
|
@ -1,8 +1,11 @@
|
||||||
import { AxiosError } from "axios";
|
import { AxiosError } from "axios";
|
||||||
import { cloneDeep } from "lodash";
|
import { cloneDeep, debounce } from "lodash";
|
||||||
import { Edge, Node, Viewport, XYPosition } from "reactflow";
|
import { Edge, Node, Viewport, XYPosition } from "reactflow";
|
||||||
import { create } from "zustand";
|
import { create } from "zustand";
|
||||||
import { STARTER_FOLDER_NAME } from "../constants/constants";
|
import {
|
||||||
|
SAVE_DEBOUNCE_TIME,
|
||||||
|
STARTER_FOLDER_NAME,
|
||||||
|
} from "../constants/constants";
|
||||||
import {
|
import {
|
||||||
deleteFlowFromDatabase,
|
deleteFlowFromDatabase,
|
||||||
readFlowsFromDatabase,
|
readFlowsFromDatabase,
|
||||||
|
|
@ -92,22 +95,18 @@ const useFlowsManagerStore = create<FlowsManagerStoreType>((set, get) => ({
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
autoSaveCurrentFlow: (nodes: Node[], edges: Edge[], viewport: Viewport) => {
|
autoSaveCurrentFlow: debounce(
|
||||||
// Clear the previous timeout if it exists.
|
(nodes: Node[], edges: Edge[], viewport: Viewport) => {
|
||||||
if (saveTimeoutId) {
|
set({ saveLoading: true });
|
||||||
clearTimeout(saveTimeoutId);
|
|
||||||
}
|
|
||||||
set({ saveLoading: true });
|
|
||||||
// Set up a new timeout.
|
|
||||||
saveTimeoutId = setTimeout(() => {
|
|
||||||
if (get().currentFlow) {
|
if (get().currentFlow) {
|
||||||
get().saveFlow(
|
get().saveFlow(
|
||||||
{ ...get().currentFlow!, data: { nodes, edges, viewport } },
|
{ ...get().currentFlow!, data: { nodes, edges, viewport } },
|
||||||
true
|
true
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}, 500); // Delay of 500ms because chat message depends on it.
|
},
|
||||||
},
|
SAVE_DEBOUNCE_TIME
|
||||||
|
),
|
||||||
saveFlow: (flow: FlowType, silent?: boolean) => {
|
saveFlow: (flow: FlowType, silent?: boolean) => {
|
||||||
set({ saveLoading: true });
|
set({ saveLoading: true });
|
||||||
return new Promise<void>((resolve, reject) => {
|
return new Promise<void>((resolve, reject) => {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue