Skip to content

Commit f2aeb45

Browse files
tolkingantfu
andauthored
feat(tryOnMounted): support target arguement (#3185)
Co-authored-by: Anthony Fu <[email protected]>
1 parent 3456d1b commit f2aeb45

File tree

5 files changed

+36
-20
lines changed

5 files changed

+36
-20
lines changed

packages/shared/tryOnBeforeMount/index.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1-
import { getCurrentInstance, nextTick, onBeforeMount } from 'vue-demi'
2-
import type { Fn } from '../utils'
1+
import { nextTick, onBeforeMount } from 'vue-demi'
2+
import { type Fn, getLifeCycleTarget } from '../utils'
33

44
/**
55
* Call onBeforeMount() if it's inside a component lifecycle, if not, just call the function
66
*
77
* @param fn
88
* @param sync if set to false, it will run in the nextTick() of Vue
9+
* @param target
910
*/
10-
export function tryOnBeforeMount(fn: Fn, sync = true) {
11-
if (getCurrentInstance())
12-
onBeforeMount(fn)
11+
export function tryOnBeforeMount(fn: Fn, sync = true, target?: any) {
12+
const instance = getLifeCycleTarget(target)
13+
if (instance)
14+
onBeforeMount(fn, instance)
1315
else if (sync)
1416
fn()
1517
else
Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
import { getCurrentInstance, onBeforeUnmount } from 'vue-demi'
2-
import type { Fn } from '../utils'
1+
import { onBeforeUnmount } from 'vue-demi'
2+
import { type Fn, getLifeCycleTarget } from '../utils'
33

44
/**
55
* Call onBeforeUnmount() if it's inside a component lifecycle, if not, do nothing
66
*
77
* @param fn
8+
* @param target
89
*/
9-
export function tryOnBeforeUnmount(fn: Fn) {
10-
if (getCurrentInstance())
11-
onBeforeUnmount(fn)
10+
export function tryOnBeforeUnmount(fn: Fn, target?: any) {
11+
const instance = getLifeCycleTarget(target)
12+
if (instance)
13+
onBeforeUnmount(fn, instance)
1214
}

packages/shared/tryOnMounted/index.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
// eslint-disable-next-line no-restricted-imports
2-
import { getCurrentInstance, nextTick, onMounted } from 'vue-demi'
3-
import type { Fn } from '../utils'
2+
import { nextTick, onMounted } from 'vue-demi'
3+
import { type Fn, getLifeCycleTarget } from '../utils'
44

55
/**
66
* Call onMounted() if it's inside a component lifecycle, if not, just call the function
77
*
88
* @param fn
99
* @param sync if set to false, it will run in the nextTick() of Vue
10+
* @param target
1011
*/
11-
export function tryOnMounted(fn: Fn, sync = true) {
12-
if (getCurrentInstance())
13-
onMounted(fn)
12+
export function tryOnMounted(fn: Fn, sync = true, target?: any) {
13+
const instance = getLifeCycleTarget(target)
14+
if (instance)
15+
onMounted(fn, instance)
1416
else if (sync)
1517
fn()
1618
else
Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
// eslint-disable-next-line no-restricted-imports
2-
import { getCurrentInstance, onUnmounted } from 'vue-demi'
3-
import type { Fn } from '../utils'
2+
import { onUnmounted } from 'vue-demi'
3+
import { type Fn, getLifeCycleTarget } from '../utils'
44

55
/**
66
* Call onUnmounted() if it's inside a component lifecycle, if not, do nothing
77
*
88
* @param fn
9+
* @param target
910
*/
10-
export function tryOnUnmounted(fn: Fn) {
11-
if (getCurrentInstance())
12-
onUnmounted(fn)
11+
export function tryOnUnmounted(fn: Fn, target?: any) {
12+
const instance = getLifeCycleTarget(target)
13+
if (instance)
14+
onUnmounted(fn, instance)
1315
}

packages/shared/utils/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { getCurrentInstance, isVue3 } from 'vue-demi'
2+
13
export * from './is'
24
export * from './filters'
35
export * from './types'
@@ -113,3 +115,9 @@ export function objectOmit<O extends object, T extends keyof O>(obj: O, keys: T[
113115
export function objectEntries<T extends object>(obj: T) {
114116
return Object.entries(obj) as Array<[keyof T, T[keyof T]]>
115117
}
118+
119+
export function getLifeCycleTarget(target?: any) {
120+
const instance = target || getCurrentInstance()
121+
122+
return isVue3 ? instance : instance?.proxy
123+
}

0 commit comments

Comments
 (0)