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