Skip to content

Commit

Permalink
fix: there are differences in APIs between 2 and 3
Browse files Browse the repository at this point in the history
  • Loading branch information
tolking committed Nov 29, 2023
1 parent fde4d75 commit 1b9673a
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 20 deletions.
9 changes: 4 additions & 5 deletions packages/shared/tryOnBeforeMount/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { getCurrentInstance, nextTick, onBeforeMount } from 'vue-demi'
import type { ComponentInternalInstance } from 'vue-demi'
import type { Fn } from '../utils'
import { nextTick, onBeforeMount } from 'vue-demi'
import { type Fn, getLifeCycleTarget } from '../utils'

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

/**
* Call onBeforeUnmount() if it's inside a component lifecycle, if not, do nothing
*
* @param fn
* @param target
*/
export function tryOnBeforeUnmount(fn: Fn, target?: ComponentInternalInstance | null) {
const instance = target || getCurrentInstance()
export function tryOnBeforeUnmount(fn: Fn, target?: any) {
const instance = getLifeCycleTarget(target)
if (instance)
onBeforeUnmount(fn, instance)
}
9 changes: 4 additions & 5 deletions packages/shared/tryOnMounted/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// 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'
import { nextTick, onMounted } from 'vue-demi'
import { type Fn, getLifeCycleTarget } from '../utils'

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

/**
* Call onUnmounted() if it's inside a component lifecycle, if not, do nothing
*
* @param fn
* @param target
*/
export function tryOnUnmounted(fn: Fn, target?: ComponentInternalInstance | null) {
const instance = target || getCurrentInstance()
export function tryOnUnmounted(fn: Fn, target?: any) {
const instance = getLifeCycleTarget(target)
if (instance)
onUnmounted(fn, instance)
}
8 changes: 8 additions & 0 deletions packages/shared/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { getCurrentInstance, isVue3 } from 'vue-demi'

export * from './is'
export * from './filters'
export * from './types'
Expand Down Expand Up @@ -113,3 +115,9 @@ export function objectOmit<O extends object, T extends keyof O>(obj: O, keys: T[
export function objectEntries<T extends object>(obj: T) {
return Object.entries(obj) as Array<[keyof T, T[keyof T]]>
}

export function getLifeCycleTarget(target?: any) {
const instance = target || getCurrentInstance()

return isVue3 ? instance : instance?.proxy
}

0 comments on commit 1b9673a

Please sign in to comment.