2021-07-07 21:07:19 +08:00
|
|
|
import { ReactiveEffect } from './effect'
|
|
|
|
import { warn } from './warning'
|
|
|
|
|
|
|
|
let activeEffectScope: EffectScope | undefined
|
|
|
|
|
|
|
|
export class EffectScope {
|
|
|
|
active = true
|
2021-07-29 22:26:24 +08:00
|
|
|
effects: ReactiveEffect[] = []
|
2021-07-07 21:07:19 +08:00
|
|
|
cleanups: (() => void)[] = []
|
2021-07-29 22:45:05 +08:00
|
|
|
|
2021-07-29 00:08:01 +08:00
|
|
|
parent: EffectScope | undefined
|
2021-07-29 22:45:05 +08:00
|
|
|
scopes: EffectScope[] | undefined
|
|
|
|
/**
|
|
|
|
* track a child scope's index in its parent's scopes array for optimized
|
|
|
|
* removal
|
|
|
|
*/
|
|
|
|
private index: number | undefined
|
2021-07-07 21:07:19 +08:00
|
|
|
|
|
|
|
constructor(detached = false) {
|
2021-07-29 22:45:05 +08:00
|
|
|
if (!detached && activeEffectScope) {
|
|
|
|
this.parent = activeEffectScope
|
|
|
|
this.index =
|
|
|
|
(activeEffectScope.scopes || (activeEffectScope.scopes = [])).push(
|
|
|
|
this
|
|
|
|
) - 1
|
2021-07-07 21:07:19 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
run<T>(fn: () => T): T | undefined {
|
|
|
|
if (this.active) {
|
|
|
|
try {
|
|
|
|
this.on()
|
|
|
|
return fn()
|
|
|
|
} finally {
|
|
|
|
this.off()
|
|
|
|
}
|
|
|
|
} else if (__DEV__) {
|
|
|
|
warn(`cannot run an inactive effect scope.`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
on() {
|
|
|
|
if (this.active) {
|
|
|
|
activeEffectScope = this
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
off() {
|
|
|
|
if (this.active) {
|
2022-01-28 18:35:09 +08:00
|
|
|
activeEffectScope = this.parent
|
2021-07-07 21:07:19 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-29 22:45:05 +08:00
|
|
|
stop(fromParent?: boolean) {
|
2021-07-07 21:07:19 +08:00
|
|
|
if (this.active) {
|
2022-01-28 18:35:09 +08:00
|
|
|
let i, l
|
|
|
|
for (i = 0, l = this.effects.length; i < l; i++) {
|
|
|
|
this.effects[i].stop()
|
|
|
|
}
|
|
|
|
for (i = 0, l = this.cleanups.length; i < l; i++) {
|
|
|
|
this.cleanups[i]()
|
|
|
|
}
|
2021-07-29 22:45:05 +08:00
|
|
|
if (this.scopes) {
|
2022-01-28 18:35:09 +08:00
|
|
|
for (i = 0, l = this.scopes.length; i < l; i++) {
|
|
|
|
this.scopes[i].stop(true)
|
|
|
|
}
|
2021-07-29 22:45:05 +08:00
|
|
|
}
|
|
|
|
// nested scope, dereference from parent to avoid memory leaks
|
|
|
|
if (this.parent && !fromParent) {
|
|
|
|
// optimized O(1) removal
|
|
|
|
const last = this.parent.scopes!.pop()
|
|
|
|
if (last && last !== this) {
|
|
|
|
this.parent.scopes![this.index!] = last
|
|
|
|
last.index = this.index!
|
|
|
|
}
|
|
|
|
}
|
2021-07-07 21:07:19 +08:00
|
|
|
this.active = false
|
2021-07-29 22:26:24 +08:00
|
|
|
}
|
|
|
|
}
|
2021-07-07 21:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export function effectScope(detached?: boolean) {
|
|
|
|
return new EffectScope(detached)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function recordEffectScope(
|
2021-07-29 22:26:24 +08:00
|
|
|
effect: ReactiveEffect,
|
2022-01-28 18:35:09 +08:00
|
|
|
scope: EffectScope | undefined = activeEffectScope
|
2021-07-07 21:07:19 +08:00
|
|
|
) {
|
|
|
|
if (scope && scope.active) {
|
|
|
|
scope.effects.push(effect)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getCurrentScope() {
|
|
|
|
return activeEffectScope
|
|
|
|
}
|
|
|
|
|
|
|
|
export function onScopeDispose(fn: () => void) {
|
|
|
|
if (activeEffectScope) {
|
|
|
|
activeEffectScope.cleanups.push(fn)
|
|
|
|
} else if (__DEV__) {
|
|
|
|
warn(
|
2021-08-17 06:19:06 +08:00
|
|
|
`onScopeDispose() is called when there is no active effect scope` +
|
2021-07-07 21:07:19 +08:00
|
|
|
` to be associated with.`
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|