forked from opentiny/tiny-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(toolkits/pro) prevent the ficker of some pages (opentiny#121)
- Loading branch information
Showing
5 changed files
with
31 additions
and
14 deletions.
There are no files selected for viewing
31 changes: 24 additions & 7 deletions
31
packages/toolkits/pro/template/tinyvue/src/hooks/loading.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters