1
1
import {
2
+ ComponentInternalInstance ,
2
3
getCurrentInstance ,
3
4
getVueConstructor ,
4
5
withCurrentInstanceTrackingDisabled ,
@@ -9,7 +10,7 @@ import { warn } from './warn'
9
10
let activeEffectScope : EffectScope | undefined
10
11
const effectScopeStack : EffectScope [ ] = [ ]
11
12
12
- export class EffectScope {
13
+ class EffectScopeImpl {
13
14
active = true
14
15
effects : EffectScope [ ] = [ ]
15
16
cleanups : ( ( ) => void ) [ ] = [ ]
@@ -19,15 +20,8 @@ export class EffectScope {
19
20
**/
20
21
vm : Vue
21
22
22
- constructor ( detached = false ) {
23
- let vm : Vue = undefined !
24
- withCurrentInstanceTrackingDisabled ( ( ) => {
25
- vm = defineComponentInstance ( getVueConstructor ( ) )
26
- } )
23
+ constructor ( vm : Vue ) {
27
24
this . vm = vm
28
- if ( ! detached ) {
29
- recordEffectScope ( this )
30
- }
31
25
}
32
26
33
27
run < T > ( fn : ( ) => T ) : T | undefined {
@@ -68,6 +62,19 @@ export class EffectScope {
68
62
}
69
63
}
70
64
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
+
71
78
export function recordEffectScope (
72
79
effect : EffectScope ,
73
80
scope ?: EffectScope | null
@@ -107,3 +114,17 @@ export function onScopeDispose(fn: () => void) {
107
114
export function getCurrentScopeVM ( ) {
108
115
return getCurrentScope ( ) ?. vm || getCurrentInstance ( ) ?. proxy
109
116
}
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