feat: use lock fn

This commit is contained in:
GyDi 2022-02-26 17:39:36 +08:00
parent c7232522ee
commit 98b8a122b6
No known key found for this signature in database
GPG Key ID: 1C95E0D3467B3084
2 changed files with 12 additions and 24 deletions

View File

@ -1,4 +1,4 @@
import React, { useRef, useState } from "react"; import React, { useState } from "react";
import dayjs from "dayjs"; import dayjs from "dayjs";
import { import {
alpha, alpha,
@ -11,6 +11,7 @@ import {
MenuItem, MenuItem,
Menu, Menu,
} from "@mui/material"; } from "@mui/material";
import { useLockFn } from "ahooks";
import { useSWRConfig } from "swr"; import { useSWRConfig } from "swr";
import { RefreshRounded } from "@mui/icons-material"; import { RefreshRounded } from "@mui/icons-material";
import { CmdType } from "../../services/types"; import { CmdType } from "../../services/types";
@ -93,20 +94,16 @@ const ProfileItem: React.FC<Props> = (props) => {
} }
}; };
const deleteRef = useRef(false); const onDelete = useLockFn(async () => {
const onDelete = async () => {
setAnchorEl(null); setAnchorEl(null);
if (deleteRef.current) return;
deleteRef.current = true;
try { try {
await deleteProfile(index); await deleteProfile(index);
mutate("getProfiles"); mutate("getProfiles");
} catch (err: any) { } catch (err: any) {
Notice.error(err.toString()); Notice.error(err.toString());
} finally {
deleteRef.current = false;
} }
}; });
const handleContextMenu = ( const handleContextMenu = (
event: React.MouseEvent<HTMLDivElement, MouseEvent> event: React.MouseEvent<HTMLDivElement, MouseEvent>

View File

@ -1,5 +1,6 @@
import useSWR, { useSWRConfig } from "swr"; import useSWR, { useSWRConfig } from "swr";
import { useEffect, useRef, useState } from "react"; import { useEffect, useState } from "react";
import { useLockFn } from "ahooks";
import { Box, Button, Grid, TextField } from "@mui/material"; import { Box, Button, Grid, TextField } from "@mui/material";
import { import {
getProfiles, getProfiles,
@ -81,37 +82,27 @@ const ProfilePage = () => {
} }
}; };
const lockRef = useRef(false); const onSelect = useLockFn(async (index: number, force: boolean) => {
const onSelect = async (index: number, force: boolean) => {
if (lockRef.current) return;
if (!force && index === profiles.current) return; if (!force && index === profiles.current) return;
lockRef.current = true;
try { try {
await selectProfile(index); await selectProfile(index);
mutate("getProfiles", { ...profiles, current: index }, true); mutate("getProfiles", { ...profiles, current: index }, true);
} catch (err: any) { } catch (err: any) {
err && Notice.error(err.toString()); err && Notice.error(err.toString());
} finally {
lockRef.current = false;
} }
}; });
const lockNewRef = useRef(false);
const [dialogOpen, setDialogOpen] = useState(false); const [dialogOpen, setDialogOpen] = useState(false);
const onNew = async (name: string, desc: string) => { const onNew = useLockFn(async (name: string, desc: string) => {
if (lockNewRef.current) return;
lockNewRef.current = true;
try { try {
await newProfile(name, desc); await newProfile(name, desc);
setDialogOpen(false); setDialogOpen(false);
mutate("getProfiles"); mutate("getProfiles");
} catch (err: any) { } catch (err: any) {
err && Notice.error(err.toString()); err && Notice.error(err.toString());
} finally {
lockNewRef.current = false;
} }
}; });
return ( return (
<BasePage title="Profiles"> <BasePage title="Profiles">