1
0
mirror of https://github.com/robonen/tools.git synced 2026-03-20 10:54:44 +00:00

feat(packages/vue): add build config

This commit is contained in:
2024-10-06 07:31:29 +07:00
parent d48e6469a3
commit 4bbc3b45a2
5 changed files with 58 additions and 6 deletions

View File

@@ -6,4 +6,3 @@ export * from './useMounted';
export * from './useRenderCount';
export * from './useSupported';
export * from './useSyncRefs';
export * from './useToggle';

View File

@@ -0,0 +1,38 @@
import { onMounted, nextTick, type ComponentInternalInstance } from 'vue';
import { getLifeCycleTarger } from '../../utils';
export interface TryOnMountedOptions {
sync?: boolean;
target?: ComponentInternalInstance;
}
/**
* @name tryOnMounted
* @category Components
* @description Calls a function if it's inside a component lifecycle hook, otherwise just calls it
*
* @param {Function} fn The function to call
* @param {TryOnMountedOptions} [options={}] The options for the try on mounted function
* @returns {void}
*
* @example
* tryOnMounted(() => console.log('Mounted!'));
*
* @example
* tryOnMounted(() => console.log('Mounted!'), { sync: false });
*/
export function tryOnMounted(fn: () => void, options: TryOnMountedOptions = {}) {
const instance = getLifeCycleTarger();
const {
sync = true,
target,
} = options;
if (instance)
onMounted(fn, target);
else if (sync)
fn();
else
nextTick(fn);
}