Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(tryOnMounted): support target arguement #3185

Merged
merged 11 commits into from
Dec 4, 2023
9 changes: 6 additions & 3 deletions packages/shared/tryOnBeforeMount/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import { getCurrentInstance, nextTick, onBeforeMount } from 'vue-demi'
import type { ComponentInternalInstance } from 'vue-demi'
import type { Fn } from '../utils'

/**
* Call onBeforeMount() if it's inside a component lifecycle, if not, just call the function
*
* @param fn
* @param sync if set to false, it will run in the nextTick() of Vue
* @param target
*/
export function tryOnBeforeMount(fn: Fn, sync = true) {
if (getCurrentInstance())
onBeforeMount(fn)
export function tryOnBeforeMount(fn: Fn, sync = true, target?: ComponentInternalInstance | null) {
const instance = target || getCurrentInstance()
if (instance)
onBeforeMount(fn, instance)
else if (sync)
fn()
else
Expand Down
9 changes: 6 additions & 3 deletions packages/shared/tryOnBeforeUnmount/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import type { ComponentInternalInstance } from 'vue-demi'
import { getCurrentInstance, onBeforeUnmount } from 'vue-demi'
import type { Fn } from '../utils'

/**
* Call onBeforeUnmount() if it's inside a component lifecycle, if not, do nothing
*
* @param fn
* @param target
*/
export function tryOnBeforeUnmount(fn: Fn) {
if (getCurrentInstance())
onBeforeUnmount(fn)
export function tryOnBeforeUnmount(fn: Fn, target?: ComponentInternalInstance | null) {
const instance = target || getCurrentInstance()
if (instance)
onBeforeUnmount(fn, instance)
}
9 changes: 6 additions & 3 deletions packages/shared/tryOnMounted/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
// eslint-disable-next-line no-restricted-imports
import { getCurrentInstance, nextTick, onMounted } from 'vue-demi'
import type { ComponentInternalInstance } from 'vue-demi'
import type { Fn } from '../utils'

/**
* Call onMounted() if it's inside a component lifecycle, if not, just call the function
*
* @param fn
* @param sync if set to false, it will run in the nextTick() of Vue
* @param target
*/
export function tryOnMounted(fn: Fn, sync = true) {
if (getCurrentInstance())
onMounted(fn)
export function tryOnMounted(fn: Fn, sync = true, target?: ComponentInternalInstance | null) {
const instance = target || getCurrentInstance()
if (instance)
onMounted(fn, instance)
else if (sync)
fn()
else
Expand Down
9 changes: 6 additions & 3 deletions packages/shared/tryOnUnmounted/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
// eslint-disable-next-line no-restricted-imports
import { getCurrentInstance, onUnmounted } from 'vue-demi'
import type { ComponentInternalInstance } from 'vue-demi'
import type { Fn } from '../utils'

/**
* Call onUnmounted() if it's inside a component lifecycle, if not, do nothing
*
* @param fn
* @param target
*/
export function tryOnUnmounted(fn: Fn) {
if (getCurrentInstance())
onUnmounted(fn)
export function tryOnUnmounted(fn: Fn, target?: ComponentInternalInstance | null) {
const instance = target || getCurrentInstance()
if (instance)
onUnmounted(fn, instance)
}