background page
This commit is contained in:
@@ -35,6 +35,7 @@ export const useUpdateCatalog = () => {
|
||||
});
|
||||
|
||||
const timeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null);
|
||||
const pendingParamsRef = useRef<Parameters<typeof api.updateCatalog>[0] | null>(null);
|
||||
const [isScheduledSaving, setIsScheduledSaving] = useState(false);
|
||||
|
||||
const scheduleUpdate = useCallback(
|
||||
@@ -42,12 +43,17 @@ export const useUpdateCatalog = () => {
|
||||
if (timeoutRef.current) {
|
||||
clearTimeout(timeoutRef.current);
|
||||
}
|
||||
pendingParamsRef.current = params;
|
||||
setIsScheduledSaving(true);
|
||||
|
||||
timeoutRef.current = setTimeout(() => {
|
||||
const pendingParams = pendingParamsRef.current;
|
||||
timeoutRef.current = null;
|
||||
pendingParamsRef.current = null;
|
||||
setIsScheduledSaving(false);
|
||||
mutation.mutate(params);
|
||||
if (pendingParams) {
|
||||
mutation.mutate(pendingParams);
|
||||
}
|
||||
}, delayMs);
|
||||
},
|
||||
[mutation],
|
||||
@@ -57,10 +63,17 @@ export const useUpdateCatalog = () => {
|
||||
() => () => {
|
||||
if (timeoutRef.current) {
|
||||
clearTimeout(timeoutRef.current);
|
||||
timeoutRef.current = null;
|
||||
}
|
||||
// اگر کاربر قبل از پایان debounce از ادیتور خارج شد،
|
||||
// آخرین تغییرات معلق را همان لحظه ذخیره کن.
|
||||
if (pendingParamsRef.current) {
|
||||
mutation.mutate(pendingParamsRef.current);
|
||||
pendingParamsRef.current = null;
|
||||
}
|
||||
setIsScheduledSaving(false);
|
||||
},
|
||||
[],
|
||||
[mutation],
|
||||
);
|
||||
|
||||
return {
|
||||
|
||||
Reference in New Issue
Block a user