Skip to content

Commit

Permalink
revert: "dx(computed): warn incorrect use of getCurrentInstance insid…
Browse files Browse the repository at this point in the history
…e computed"

This reverts commit 324e817.
  • Loading branch information
yyx990803 committed Jan 9, 2024
1 parent 3135fcb commit 2fd3905
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 92 deletions.
44 changes: 0 additions & 44 deletions packages/runtime-core/__tests__/apiComputed.spec.ts

This file was deleted.

38 changes: 3 additions & 35 deletions packages/runtime-core/src/apiComputed.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,10 @@
import {
type ComputedGetter,
type WritableComputedOptions,
computed as _computed,
} from '@vue/reactivity'
import { computed as _computed } from '@vue/reactivity'
import { isInSSRComponentSetup } from './component'
import { isFunction } from '@vue/shared'

/**
* For dev warning only.
* Context: https://github.com/vuejs/core/discussions/9974
*/
export let isInComputedGetter = false

function wrapComputedGetter(
getter: ComputedGetter<unknown>,
): ComputedGetter<unknown> {
return () => {
isInComputedGetter = true
try {
return getter()
} finally {
isInComputedGetter = false
}
}
}

export const computed: typeof _computed = (
getterOrOptions: ComputedGetter<unknown> | WritableComputedOptions<unknown>,
getterOrOptions: any,
debugOptions?: any,
) => {
if (__DEV__) {
if (isFunction(getterOrOptions)) {
getterOrOptions = wrapComputedGetter(getterOrOptions)
} else {
getterOrOptions.get = wrapComputedGetter(getterOrOptions.get)
}
}

// @ts-expect-error the 3rd argument is hidden from public types
// @ts-expect-error
return _computed(getterOrOptions, debugOptions, isInSSRComponentSetup)
}
15 changes: 2 additions & 13 deletions packages/runtime-core/src/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ import {
} from './compat/compatConfig'
import type { SchedulerJob } from './scheduler'
import type { LifecycleHooks } from './enums'
import { isInComputedGetter } from './apiComputed'

export type Data = Record<string, unknown>

Expand Down Expand Up @@ -632,18 +631,8 @@ export function createComponentInstance(

export let currentInstance: ComponentInternalInstance | null = null

export const getCurrentInstance: () => ComponentInternalInstance | null =
() => {
if (__DEV__ && isInComputedGetter) {
warn(
`getCurrentInstance() called inside a computed getter. ` +
`This is incorrect usage as computed getters are not guaranteed ` +
`to be executed with an active component instance. If you are using ` +
`a composable inside a computed getter, move it ouside to the setup scope.`,
)
}
return currentInstance || currentRenderingInstance
}
export const getCurrentInstance: () => ComponentInternalInstance | null = () =>
currentInstance || currentRenderingInstance

let internalSetCurrentInstance: (
instance: ComponentInternalInstance | null,
Expand Down

2 comments on commit 2fd3905

@ferferga
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yyx990803 Sorry for commenting in a commit, but it's the only reference I could find for this change.

May we know the reason for the revert?

@ferferga
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found the ongoing discussion (leaving this for future lurkers): #9974 (comment)

Please sign in to comment.