1/* eslint-disable */ 2 3import { remove } from '../../utils/index.ts'; 4 5let uid = 0; 6 7/** 8 * A dep is an observable that can have multiple directives subscribing to it. 9 * @constructor 10 */ 11export default function Dep () { 12 this.id = uid++; 13 this.subs = []; 14} 15 16// The current target watcher being evaluated. 17// This is globally unique because there could be only one watcher being evaluated at any time. 18Dep.target = null; 19let targetStack = []; 20 21export function pushTarget (_target) { 22 if (Dep.target) { 23 targetStack.push(Dep.target); 24 } 25 Dep.target = _target; 26} 27 28export function popTarget () { 29 Dep.target = targetStack.pop(); 30} 31 32export function resetTarget () { 33 Dep.target = null; 34 targetStack = []; 35} 36 37/** 38 * Add a directive subscriber. 39 * @param {Directive} sub 40 */ 41Dep.prototype.addSub = function (sub) { 42 this.subs.push(sub); 43} 44 45/** 46 * Remove a directive subscriber. 47 * @param {Directive} sub 48 */ 49Dep.prototype.removeSub = function (sub) { 50 remove(this.subs, sub); 51} 52 53/** 54 * Add self as a dependency to the target watcher. 55 */ 56Dep.prototype.depend = function () { 57 if (Dep.target) { 58 Dep.target.addDep(this); 59 } 60} 61 62/** 63 * Notify all subscribers of a new value. 64 */ 65Dep.prototype.notify = function () { 66 // Stabilize the subscriber list first. 67 const subs = this.subs.slice(); 68 for (let i = 0, l = subs.length; i < l; i++) { 69 subs[i].update(); 70 } 71} 72