mirror of
https://github.com/robonen/tools.git
synced 2026-03-20 10:54:44 +00:00
refactor: change separate tools by category
This commit is contained in:
45
web/vue/src/composables/useRenderInfo/index.ts
Normal file
45
web/vue/src/composables/useRenderInfo/index.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import { timestamp } from '@robonen/stdlib';
|
||||
import { onBeforeMount, onBeforeUpdate, onMounted, onUpdated, readonly, ref, type ComponentInternalInstance } from 'vue';
|
||||
import { useRenderCount } from '../useRenderCount';
|
||||
import { getLifeCycleTarger } from '../..';
|
||||
|
||||
/**
|
||||
* @name useRenderInfo
|
||||
* @category Components
|
||||
* @description Returns information about the component's render count and the last time it was rendered
|
||||
*
|
||||
* @param {ComponentInternalInstance} [instance] The component instance to track the render count for
|
||||
*
|
||||
*
|
||||
* @example
|
||||
* const { component, count, duration, lastRendered } = useRenderInfo();
|
||||
*
|
||||
* @example
|
||||
* const { component, count, duration, lastRendered } = useRenderInfo(getCurrentInstance());
|
||||
*
|
||||
* @since 0.0.1
|
||||
*/
|
||||
export function useRenderInfo(instance?: ComponentInternalInstance) {
|
||||
const target = getLifeCycleTarger(instance);
|
||||
const duration = ref(0);
|
||||
let renderStartTime = 0;
|
||||
|
||||
const startMark = () => renderStartTime = performance.now();
|
||||
const endMark = () => {
|
||||
duration.value = Math.max(performance.now() - renderStartTime, 0);
|
||||
renderStartTime = 0;
|
||||
};
|
||||
|
||||
onBeforeMount(startMark, target);
|
||||
onMounted(endMark, target);
|
||||
|
||||
onBeforeUpdate(startMark, target);
|
||||
onUpdated(endMark, target);
|
||||
|
||||
return {
|
||||
component: target?.type.name ?? target?.uid,
|
||||
count: useRenderCount(instance),
|
||||
duration: readonly(duration),
|
||||
lastRendered: timestamp(),
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user