-
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.
feat: add useSafeOnMounted composable
Workaround for vuejs/core#5844
- Loading branch information
Showing
1 changed file
with
31 additions
and
0 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import type { Ref } from 'vue' | ||
import { onMounted, onUnmounted } from 'vue' | ||
|
||
export default (element: Ref<HTMLElement>, listener: () => void, checkTimeLimit = 100) => { | ||
if (process.client) { | ||
let timeId = 0 | ||
const checkInterval = 100 | ||
let checksLeft = checkTimeLimit / checkInterval | ||
|
||
const check = () => { | ||
if (element.value) { | ||
listener() | ||
} else { | ||
if (checksLeft > 0) { | ||
timeId = setTimeout(check, checkInterval) | ||
checksLeft-- | ||
} else { | ||
console.warn(`pass ${checkTimeLimit}ms, the element not ready! Please check.`) | ||
} | ||
} | ||
}; | ||
|
||
onMounted(() => { | ||
check() | ||
}) | ||
|
||
onUnmounted(() => { | ||
clearTimeout(timeId) | ||
}) | ||
} | ||
} |