Skip to content

Commit

Permalink
try fix core#9974
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsoncodehk committed Jan 9, 2024
1 parent a3725a7 commit e411f97
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
2 changes: 1 addition & 1 deletion packages/reactivity/src/effect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export class ReactiveEffect<T = any> {
public fn: () => T,
public trigger: () => void,
public scheduler?: EffectScheduler,
scope?: EffectScope,
public scope?: EffectScope,
) {
recordEffectScope(this, scope)
}
Expand Down
1 change: 1 addition & 0 deletions packages/reactivity/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export {
} from './computed'
export { deferredComputed } from './deferredComputed'
export {
activeEffect,
effect,
stop,
enableTracking,
Expand Down
24 changes: 22 additions & 2 deletions packages/runtime-core/src/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
EffectScope,
type ReactiveEffect,
TrackOpTypes,
activeEffect,
isRef,
markRaw,
pauseTracking,
Expand Down Expand Up @@ -613,6 +614,8 @@ export function createComponentInstance(
ec: null,
sp: null,
}
// @ts-expect-error
instance.scope.__componentInstance = instance
if (__DEV__) {
instance.ctx = createDevRenderContext(instance)
} else {
Expand All @@ -631,8 +634,25 @@ export function createComponentInstance(

export let currentInstance: ComponentInternalInstance | null = null

export const getCurrentInstance: () => ComponentInternalInstance | null = () =>
currentInstance || currentRenderingInstance
export const getCurrentInstance: () => ComponentInternalInstance | null =
() => {
if (currentInstance) {
return currentInstance
} else if (currentRenderingInstance) {
return currentRenderingInstance
} else if (activeEffect?.scope) {
let current: EffectScope | undefined = activeEffect.scope
while (current) {
// @ts-expect-error
if (current.__componentInstance) {
// @ts-expect-error
return current.__componentInstance
}
current = current.parent
}
}
return null
}

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

0 comments on commit e411f97

Please sign in to comment.