Skip to content

Commit

Permalink
fix(toolkits/pro) prevent the ficker of some pages (opentiny#121)
Browse files Browse the repository at this point in the history
  • Loading branch information
fengyon committed Feb 2, 2024
1 parent cc4d4f7 commit 09ce692
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 14 deletions.
31 changes: 24 additions & 7 deletions packages/toolkits/pro/template/tinyvue/src/hooks/loading.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,33 @@
import { ref } from 'vue';

export default function useLoading(initValue = false) {
/**
* use logding hook
* @param initValue
* @param closeDelay the dealy time of turning off loading to prevent ficker
* @returns
*/
export default function useLoading(initValue = false, closeDelay = 200) {
let preOpenTime = Date.now();
let closeTimer: ReturnType<typeof setTimeout> | null;
const loading = ref(initValue);
const setLoading = (value: boolean) => {
if (value) {
preOpenTime = Date.now();
} else {
const declinedTime = closeDelay - preOpenTime + Date.now();
if (declinedTime > 0) {
closeTimer ||
(closeTimer = setTimeout(() => {
loading.value = value;
closeTimer = null;
}, declinedTime));
return;
}
}
loading.value = value;
};
const toggle = () => {
loading.value = !loading.value;
};
return {
loading,
setLoading,
toggle,
setLoading(!loading.value);
};
return [loading, setLoading, toggle];
}
8 changes: 4 additions & 4 deletions packages/toolkits/pro/template/tinyvue/src/hooks/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ import useLoading from './loading';

export default function useRequest<T>(
api: () => Promise<AxiosResponse<HttpResponse>>,
defaultValue = [] as unknown as T,
defaultValue = ([] as unknown) as T,
isLoading = true
) {
const { loading, setLoading } = useLoading(isLoading);
const [loading, setLoading] = useLoading(isLoading);
const response = ref<T>(defaultValue);
api()
.then((res) => {
response.value = res.data as unknown as UnwrapRef<T>;
.then(res => {
response.value = (res.data as unknown) as UnwrapRef<T>;
})
.finally(() => {
setLoading(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
const router = useRouter();
const { t } = useI18n();
const { loading, setLoading } = useLoading();
const [ loading, setLoading ] = useLoading();
const userStore = useUserStore();
const loginFormInfo = ref();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
const router = useRouter();
const { t } = useI18n();
const { loading, setLoading } = useLoading();
const [loading, setLoading] = useLoading();
const userStore = useUserStore();
const loginFormMail = ref();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
// 注册
const { t } = useI18n();
const { loading, setLoading } = useLoading();
const [loading, setLoading] = useLoading();
const ruleForm = ref();
// 切换模式
Expand Down

0 comments on commit 09ce692

Please sign in to comment.