|
| 1 | +import { |
| 2 | + getCurrentInstance, |
| 3 | + getVueConstructor, |
| 4 | + withCurrentInstanceTrackingDisabled, |
| 5 | +} from '../runtimeContext' |
| 6 | +import { defineComponentInstance } from '../utils' |
| 7 | +import { warn } from './warn' |
| 8 | + |
| 9 | +let activeEffectScope: EffectScope | undefined |
| 10 | +const effectScopeStack: EffectScope[] = [] |
| 11 | + |
| 12 | +export class EffectScope { |
| 13 | + active = true |
| 14 | + effects: EffectScope[] = [] |
| 15 | + cleanups: (() => void)[] = [] |
| 16 | + |
| 17 | + /** |
| 18 | + * @internal |
| 19 | + **/ |
| 20 | + vm: Vue |
| 21 | + |
| 22 | + constructor(detached = false) { |
| 23 | + let vm: Vue = undefined! |
| 24 | + withCurrentInstanceTrackingDisabled(() => { |
| 25 | + vm = defineComponentInstance(getVueConstructor()) |
| 26 | + }) |
| 27 | + this.vm = vm |
| 28 | + if (!detached) { |
| 29 | + recordEffectScope(this) |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + run<T>(fn: () => T): T | undefined { |
| 34 | + if (this.active) { |
| 35 | + try { |
| 36 | + this.on() |
| 37 | + return fn() |
| 38 | + } finally { |
| 39 | + this.off() |
| 40 | + } |
| 41 | + } else if (__DEV__) { |
| 42 | + warn(`cannot run an inactive effect scope.`) |
| 43 | + } |
| 44 | + return |
| 45 | + } |
| 46 | + |
| 47 | + on() { |
| 48 | + if (this.active) { |
| 49 | + effectScopeStack.push(this) |
| 50 | + activeEffectScope = this |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + off() { |
| 55 | + if (this.active) { |
| 56 | + effectScopeStack.pop() |
| 57 | + activeEffectScope = effectScopeStack[effectScopeStack.length - 1] |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + stop() { |
| 62 | + if (this.active) { |
| 63 | + this.vm.$destroy() |
| 64 | + this.effects.forEach((e) => e.stop()) |
| 65 | + this.cleanups.forEach((cleanup) => cleanup()) |
| 66 | + this.active = false |
| 67 | + } |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +export function recordEffectScope( |
| 72 | + effect: EffectScope, |
| 73 | + scope?: EffectScope | null |
| 74 | +) { |
| 75 | + scope = scope || activeEffectScope |
| 76 | + if (scope && scope.active) { |
| 77 | + scope.effects.push(effect) |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +export function effectScope(detached?: boolean) { |
| 82 | + return new EffectScope(detached) |
| 83 | +} |
| 84 | + |
| 85 | +export function getCurrentScope() { |
| 86 | + return activeEffectScope |
| 87 | +} |
| 88 | + |
| 89 | +export function onScopeDispose(fn: () => void) { |
| 90 | + if (activeEffectScope) { |
| 91 | + activeEffectScope.cleanups.push(fn) |
| 92 | + } else if (__DEV__) { |
| 93 | + warn( |
| 94 | + `onDispose() is called when there is no active effect scope ` + |
| 95 | + ` to be associated with.` |
| 96 | + ) |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +/** |
| 101 | + * @internal |
| 102 | + **/ |
| 103 | +export function getCurrentScopeVM() { |
| 104 | + return getCurrentScope()?.vm || getCurrentInstance()?.proxy |
| 105 | +} |
0 commit comments