Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 | 2854x 2854x 2854x 2854x 2854x 2854x 2854x 2854x 2854x 3x 2x 2x 1x 1x 2384x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 37x 35x 35x 35x 35x 35x 2x 1x 6810x 6806x 6806x 6861x 6801x 6784x 17x 17x 18x 17x 17x 1x 6801x 509x 509x 509x 523x 509x 509x 4x 509x 509x 1x 1x 1x 509x 1x 1x 509x 32x 316x 16x 15x 1x 1x | import type { ReactiveEffect } from './effect'
import { warn } from './warning'
export let activeEffectScope: EffectScope | undefined
export class EffectScope {
/**
* @internal
*/
private _active = true
/**
* @internal track `on` calls, allow `on` call multiple times
*/
private _on = 0
/**
* @internal
*/
effects: ReactiveEffect[] = []
/**
* @internal
*/
cleanups: (() => void)[] = []
private _isPaused = false
private _warnOnRun = true
/**
* only assigned by undetached scope
* @internal
*/
parent: EffectScope | undefined
/**
* record undetached scopes
* @internal
*/
scopes: EffectScope[] | undefined
/**
* track a child scope's index in its parent's scopes array for optimized
* removal
* @internal
*/
private index: number | undefined
readonly __v_skip = true
// TODO isolatedDeclarations ReactiveFlags.SKIP
constructor(public detached = false) {
if (!detached && activeEffectScope) {
if (activeEffectScope.active) {
this.parent = activeEffectScope
this.index =
(activeEffectScope.scopes || (activeEffectScope.scopes = [])).push(
this,
) - 1
} else {
// The parent scope has already stopped, so this child must not become
// a detached live scope.
this._active = false
this._warnOnRun = false
}
}
}
get active(): boolean {
return this._active
}
pause(): void {
Eif (this._active) {
this._isPaused = true
let i, l
Iif (this.scopes) {
for (i = 0, l = this.scopes.length; i < l; i++) {
this.scopes[i].pause()
}
}
for (i = 0, l = this.effects.length; i < l; i++) {
this.effects[i].pause()
}
}
}
/**
* Resumes the effect scope, including all child scopes and effects.
*/
resume(): void {
Eif (this._active) {
Eif (this._isPaused) {
this._isPaused = false
let i, l
Iif (this.scopes) {
for (i = 0, l = this.scopes.length; i < l; i++) {
this.scopes[i].resume()
}
}
for (i = 0, l = this.effects.length; i < l; i++) {
this.effects[i].resume()
}
}
}
}
run<T>(fn: () => T): T | undefined {
if (this._active) {
const currentEffectScope = activeEffectScope
try {
activeEffectScope = this
return fn()
} finally {
activeEffectScope = currentEffectScope
}
} else if (__DEV__ && this._warnOnRun) {
warn(`cannot run an inactive effect scope.`)
}
}
prevScope: EffectScope | undefined
/**
* This should only be called on non-detached scopes
* @internal
*/
on(): void {
if (++this._on === 1) {
this.prevScope = activeEffectScope
activeEffectScope = this
}
}
/**
* This should only be called on non-detached scopes
* @internal
*/
off(): void {
if (this._on > 0 && --this._on === 0) {
// Fast path: in the common LIFO case this scope is still at the top
// of the active chain, so we can restore the previous scope directly.
if (activeEffectScope === this) {
activeEffectScope = this.prevScope
} else {
// withAsyncContext() restores the current component scope for the
// current async continuation, then defers its cleanup to a microtask.
// If sibling continuations interleave (A restore -> B restore ->
// A cleanup), activeEffectScope is already B instead of this scope A
// when A's cleanup calls off(). Unlink A from the middle of the
// active chain so a stale scope doesn't remain globally reachable.
let current = activeEffectScope
while (current) {
if (current.prevScope === this) {
current.prevScope = this.prevScope
break
}
current = current.prevScope
}
}
this.prevScope = undefined
}
}
stop(fromParent?: boolean): void {
Eif (this._active) {
this._active = false
let i, l
for (i = 0, l = this.effects.length; i < l; i++) {
this.effects[i].stop()
}
this.effects.length = 0
for (i = 0, l = this.cleanups.length; i < l; i++) {
this.cleanups[i]()
}
this.cleanups.length = 0
if (this.scopes) {
for (i = 0, l = this.scopes.length; i < l; i++) {
this.scopes[i].stop(true)
}
this.scopes.length = 0
}
// nested scope, dereference from parent to avoid memory leaks
if (!this.detached && this.parent && !fromParent) {
// optimized O(1) removal
const last = this.parent.scopes!.pop()
Iif (last && last !== this) {
this.parent.scopes![this.index!] = last
last.index = this.index!
}
}
this.parent = undefined
}
}
}
/**
* Creates an effect scope object which can capture the reactive effects (i.e.
* computed and watchers) created within it so that these effects can be
* disposed together. For detailed use cases of this API, please consult its
* corresponding {@link https://github.com/vuejs/rfcs/blob/master/active-rfcs/0041-reactivity-effect-scope.md | RFC}.
*
* @param detached - Can be used to create a "detached" effect scope.
* @see {@link https://vuejs.org/api/reactivity-advanced.html#effectscope}
*/
export function effectScope(detached?: boolean): EffectScope {
return new EffectScope(detached)
}
/**
* Returns the current active effect scope if there is one.
*
* @see {@link https://vuejs.org/api/reactivity-advanced.html#getcurrentscope}
*/
export function getCurrentScope(): EffectScope | undefined {
return activeEffectScope
}
/**
* Registers a dispose callback on the current active effect scope. The
* callback will be invoked when the associated effect scope is stopped.
*
* @param fn - The callback function to attach to the scope's cleanup.
* @see {@link https://vuejs.org/api/reactivity-advanced.html#onscopedispose}
*/
export function onScopeDispose(fn: () => void, failSilently = false): void {
if (activeEffectScope) {
activeEffectScope.cleanups.push(fn)
} else if (E__DEV__ && !failSilently) {
warn(
`onScopeDispose() is called when there is no active effect scope` +
` to be associated with.`,
)
}
}
|