mirror of
https://github.com/robonen/tools.git
synced 2026-03-20 02:44:45 +00:00
46 lines
1.2 KiB
TypeScript
46 lines
1.2 KiB
TypeScript
import { onMounted, nextTick } from 'vue';
|
|
import type { ComponentInternalInstance } from 'vue';
|
|
import { getLifeCycleTarger } from '@/utils';
|
|
import type { VoidFunction } from '@robonen/stdlib';
|
|
|
|
// TODO: tests
|
|
|
|
export interface TryOnMountedOptions {
|
|
sync?: boolean;
|
|
target?: ComponentInternalInstance;
|
|
}
|
|
|
|
/**
|
|
* @name tryOnMounted
|
|
* @category Lifecycle
|
|
* @description Call onMounted if it's inside a component lifecycle hook, otherwise just calls it
|
|
*
|
|
* @param {VoidFunction} fn The function to call
|
|
* @param {TryOnMountedOptions} options The options to use
|
|
* @param {boolean} [options.sync=true] If the function should be called synchronously
|
|
* @param {ComponentInternalInstance} [options.target] The target instance to use
|
|
* @returns {void}
|
|
*
|
|
* @example
|
|
* tryOnMounted(() => console.log('Mounted!'));
|
|
*
|
|
* @example
|
|
* tryOnMounted(() => console.log('Mounted!'), { sync: false });
|
|
*
|
|
* @since 0.0.1
|
|
*/
|
|
export function tryOnMounted(fn: VoidFunction, options: TryOnMountedOptions = {}) {
|
|
const {
|
|
sync = true,
|
|
target,
|
|
} = options;
|
|
|
|
const instance = getLifeCycleTarger(target);
|
|
|
|
if (instance)
|
|
onMounted(fn, instance);
|
|
else if (sync)
|
|
fn();
|
|
else
|
|
nextTick(fn);
|
|
} |