11import {
2+ ComponentInternalInstance ,
23 getCurrentInstance ,
34 getVueConstructor ,
45 withCurrentInstanceTrackingDisabled ,
@@ -9,7 +10,7 @@ import { warn } from './warn'
910let activeEffectScope : EffectScope | undefined
1011const effectScopeStack : EffectScope [ ] = [ ]
1112
12- export class EffectScope {
13+ class EffectScopeImpl {
1314 active = true
1415 effects : EffectScope [ ] = [ ]
1516 cleanups : ( ( ) => void ) [ ] = [ ]
@@ -19,15 +20,8 @@ export class EffectScope {
1920 **/
2021 vm : Vue
2122
22- constructor ( detached = false ) {
23- let vm : Vue = undefined !
24- withCurrentInstanceTrackingDisabled ( ( ) => {
25- vm = defineComponentInstance ( getVueConstructor ( ) )
26- } )
23+ constructor ( vm : Vue ) {
2724 this . vm = vm
28- if ( ! detached ) {
29- recordEffectScope ( this )
30- }
3125 }
3226
3327 run < T > ( fn : ( ) => T ) : T | undefined {
@@ -68,6 +62,19 @@ export class EffectScope {
6862 }
6963}
7064
65+ export class EffectScope extends EffectScopeImpl {
66+ constructor ( detached = false ) {
67+ let vm : Vue = undefined !
68+ withCurrentInstanceTrackingDisabled ( ( ) => {
69+ vm = defineComponentInstance ( getVueConstructor ( ) )
70+ } )
71+ super ( vm )
72+ if ( ! detached ) {
73+ recordEffectScope ( this )
74+ }
75+ }
76+ }
77+
7178export function recordEffectScope (
7279 effect : EffectScope ,
7380 scope ?: EffectScope | null
@@ -107,3 +114,17 @@ export function onScopeDispose(fn: () => void) {
107114export function getCurrentScopeVM ( ) {
108115 return getCurrentScope ( ) ?. vm || getCurrentInstance ( ) ?. proxy
109116}
117+
118+ /**
119+ * @internal
120+ **/
121+ export function bindCurrentScopeToVM (
122+ vm : ComponentInternalInstance
123+ ) : EffectScope {
124+ if ( ! vm . scope ) {
125+ const scope = new EffectScopeImpl ( vm . proxy ) as EffectScope
126+ vm . scope = scope
127+ vm . proxy . $on ( 'hook:destroyed' , ( ) => scope . stop ( ) )
128+ }
129+ return vm . scope
130+ }
0 commit comments