Files
tools/vue/toolkit/src/utils/lifecycle.ts
robonen aa2938cb34 refactor(toolkit): type source any with proper types
Genuinely type composable any usages (useStepper/useStorage/useForm/
createEventHook/useSorted/etc.) as proper generics/unknown; keep idiomatic
any-function and overload-impl signatures with comments; skipped test -> .todo.
2026-06-15 16:55:07 +07:00

52 lines
1.6 KiB
TypeScript

import { nextTick } from 'vue';
import type { ComponentInternalInstance } from 'vue';
import type { VoidFunction } from '@robonen/stdlib';
import { getLifeCycleTarger } from './components';
/**
* Shared options for the `tryOn*` lifecycle helpers.
*/
export interface TryOnLifecycleOptions {
/**
* Run the callback synchronously when invoked outside a component instance
* (i.e. when there is no lifecycle hook to defer to). When `false`, the
* callback is scheduled on the next microtask via `nextTick`.
*
* @default true
*/
sync?: boolean;
/**
* The component instance the lifecycle hook should be bound to. Defaults to
* the current active instance.
*/
target?: ComponentInternalInstance;
}
/**
* The shape shared by Vue's lifecycle registrars (`onMounted`, `onBeforeMount`, …):
* a callback plus an optional target instance.
*/
type LifecycleHook = (hook: VoidFunction, target?: ComponentInternalInstance | null) => void;
/**
* Register `fn` on the given Vue lifecycle `hook` when called inside a component
* instance; otherwise run it immediately (or on the next tick when `sync` is
* `false`). Factored out so the `tryOnMounted` / `tryOnBeforeMount` helpers share
* one implementation of the instance-resolution and fallback branching.
*/
export function runTryOnLifecycle(
hook: LifecycleHook,
fn: VoidFunction,
options: TryOnLifecycleOptions = {},
): void {
const { sync = true, target } = options;
const instance = getLifeCycleTarger(target);
if (instance)
hook(fn, instance);
else if (sync)
fn();
else
nextTick(fn);
}