| 2399 | * This is used for both the $watch() api and directives. |
| 2400 | */ |
| 2401 | var Watcher = function Watcher ( |
| 2402 | vm, |
| 2403 | expOrFn, |
| 2404 | cb, |
| 2405 | options |
| 2406 | ) { |
| 2407 | this.vm = vm; |
| 2408 | vm._watchers.push(this); |
| 2409 | // options |
| 2410 | if (options) { |
| 2411 | this.deep = !!options.deep; |
| 2412 | this.user = !!options.user; |
| 2413 | this.lazy = !!options.lazy; |
| 2414 | this.sync = !!options.sync; |
| 2415 | } else { |
| 2416 | this.deep = this.user = this.lazy = this.sync = false; |
| 2417 | } |
| 2418 | this.cb = cb; |
| 2419 | this.id = ++uid$2; // uid for batching |
| 2420 | this.active = true; |
| 2421 | this.dirty = this.lazy; // for lazy watchers |
| 2422 | this.deps = []; |
| 2423 | this.newDeps = []; |
| 2424 | this.depIds = new _Set(); |
| 2425 | this.newDepIds = new _Set(); |
| 2426 | this.expression = process.env.NODE_ENV !== 'production' |
| 2427 | ? expOrFn.toString() |
| 2428 | : ''; |
| 2429 | // parse expression for getter |
| 2430 | if (typeof expOrFn === 'function') { |
| 2431 | this.getter = expOrFn; |
| 2432 | } else { |
| 2433 | this.getter = parsePath(expOrFn); |
| 2434 | if (!this.getter) { |
| 2435 | this.getter = function () {}; |
| 2436 | process.env.NODE_ENV !== 'production' && warn( |
| 2437 | "Failed watching path: \"" + expOrFn + "\" " + |
| 2438 | 'Watcher only accepts simple dot-delimited paths. ' + |
| 2439 | 'For full control, use a function instead.', |
| 2440 | vm |
| 2441 | ); |
| 2442 | } |
| 2443 | } |
| 2444 | this.value = this.lazy |
| 2445 | ? undefined |
| 2446 | : this.get(); |
| 2447 | }; |
| 2448 | |
| 2449 | /** |
| 2450 | * Evaluate the getter, and re-collect dependencies. |