| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315 |
- var _ = require('./util')
- var config = require('./config')
- var Dep = require('./observer/dep')
- var expParser = require('./parsers/expression')
- var batcher = require('./batcher')
- var uid = 0
- /**
- * A watcher parses an expression, collects dependencies,
- * and fires callback when the expression value changes.
- * This is used for both the $watch() api and directives.
- *
- * @param {Vue} vm
- * @param {String} expression
- * @param {Function} cb
- * @param {Object} options
- * - {Array} filters
- * - {Boolean} twoWay
- * - {Boolean} deep
- * - {Boolean} user
- * - {Boolean} sync
- * - {Boolean} lazy
- * - {Function} [preProcess]
- * @constructor
- */
- function Watcher (vm, expOrFn, cb, options) {
- // mix in options
- if (options) {
- _.extend(this, options)
- }
- var isFn = typeof expOrFn === 'function'
- this.vm = vm
- vm._watchers.push(this)
- this.expression = isFn ? expOrFn.toString() : expOrFn
- this.cb = cb
- this.id = ++uid // uid for batching
- this.active = true
- this.dirty = this.lazy // for lazy watchers
- this.deps = []
- this.newDeps = null
- this.prevError = null // for async error stacks
- // parse expression for getter/setter
- if (isFn) {
- this.getter = expOrFn
- this.setter = undefined
- } else {
- var res = expParser.parse(expOrFn, this.twoWay)
- this.getter = res.get
- this.setter = res.set
- }
- this.value = this.lazy
- ? undefined
- : this.get()
- // state for avoiding false triggers for deep and Array
- // watchers during vm._digest()
- this.queued = this.shallow = false
- }
- /**
- * Add a dependency to this directive.
- *
- * @param {Dep} dep
- */
- Watcher.prototype.addDep = function (dep) {
- var newDeps = this.newDeps
- var old = this.deps
- if (_.indexOf(newDeps, dep) < 0) {
- newDeps.push(dep)
- var i = _.indexOf(old, dep)
- if (i < 0) {
- dep.addSub(this)
- } else {
- old[i] = null
- }
- }
- }
- /**
- * Evaluate the getter, and re-collect dependencies.
- */
- Watcher.prototype.get = function () {
- this.beforeGet()
- var vm = this.vm
- var scope = this.scope || vm
- var value
- try {
- value = this.getter.call(scope, scope)
- } catch (e) {
- if (
- process.env.NODE_ENV !== 'production' &&
- config.warnExpressionErrors
- ) {
- _.warn(
- 'Error when evaluating expression "' +
- this.expression + '". ' +
- (config.debug
- ? ''
- : 'Turn on debug mode to see stack trace.'
- ), e
- )
- }
- }
- // "touch" every property so they are all tracked as
- // dependencies for deep watching
- if (this.deep) {
- traverse(value)
- }
- if (this.preProcess) {
- value = this.preProcess(value)
- }
- if (this.filters) {
- value = vm._applyFilters(value, null, this.filters, false)
- }
- this.afterGet()
- return value
- }
- /**
- * Set the corresponding value with the setter.
- *
- * @param {*} value
- */
- Watcher.prototype.set = function (value) {
- var vm = this.vm
- var scope = this.scope || vm
- if (this.filters) {
- value = vm._applyFilters(
- value, this.value, this.filters, true)
- }
- try {
- this.setter.call(scope, scope, value)
- } catch (e) {
- if (
- process.env.NODE_ENV !== 'production' &&
- config.warnExpressionErrors
- ) {
- _.warn(
- 'Error when evaluating setter "' +
- this.expression + '"', e
- )
- }
- }
- }
- /**
- * Prepare for dependency collection.
- */
- Watcher.prototype.beforeGet = function () {
- Dep.target = this
- this.newDeps = []
- }
- /**
- * Clean up for dependency collection.
- */
- Watcher.prototype.afterGet = function () {
- Dep.target = null
- var i = this.deps.length
- while (i--) {
- var dep = this.deps[i]
- if (dep) {
- dep.removeSub(this)
- }
- }
- this.deps = this.newDeps
- this.newDeps = null
- }
- /**
- * Subscriber interface.
- * Will be called when a dependency changes.
- *
- * @param {Boolean} shallow
- */
- Watcher.prototype.update = function (shallow) {
- if (this.lazy) {
- this.dirty = true
- } else if (this.sync || !config.async) {
- this.run()
- } else {
- // if queued, only overwrite shallow with non-shallow,
- // but not the other way around.
- this.shallow = this.queued
- ? shallow
- ? this.shallow
- : false
- : !!shallow
- this.queued = true
- // record before-push error stack in debug mode
- /* istanbul ignore if */
- if (process.env.NODE_ENV !== 'production' && config.debug) {
- this.prevError = new Error('[vue] async stack trace')
- }
- batcher.push(this)
- }
- }
- /**
- * Batcher job interface.
- * Will be called by the batcher.
- */
- Watcher.prototype.run = function () {
- if (this.active) {
- var value = this.get()
- if (
- value !== this.value ||
- // Deep watchers and Array watchers should fire even
- // when the value is the same, because the value may
- // have mutated; but only do so if this is a
- // non-shallow update (caused by a vm digest).
- ((_.isArray(value) || this.deep) && !this.shallow)
- ) {
- // set new value
- var oldValue = this.value
- this.value = value
- // in debug + async mode, when a watcher callbacks
- // throws, we also throw the saved before-push error
- // so the full cross-tick stack trace is available.
- var prevError = this.prevError
- /* istanbul ignore if */
- if (process.env.NODE_ENV !== 'production' &&
- config.debug && prevError) {
- this.prevError = null
- try {
- this.cb.call(this.vm, value, oldValue)
- } catch (e) {
- _.nextTick(function () {
- throw prevError
- }, 0)
- throw e
- }
- } else {
- this.cb.call(this.vm, value, oldValue)
- }
- }
- this.queued = this.shallow = false
- }
- }
- /**
- * Evaluate the value of the watcher.
- * This only gets called for lazy watchers.
- */
- Watcher.prototype.evaluate = function () {
- // avoid overwriting another watcher that is being
- // collected.
- var current = Dep.target
- this.value = this.get()
- this.dirty = false
- Dep.target = current
- }
- /**
- * Depend on all deps collected by this watcher.
- */
- Watcher.prototype.depend = function () {
- var i = this.deps.length
- while (i--) {
- this.deps[i].depend()
- }
- }
- /**
- * Remove self from all dependencies' subcriber list.
- */
- Watcher.prototype.teardown = function () {
- if (this.active) {
- // remove self from vm's watcher list
- // we can skip this if the vm if being destroyed
- // which can improve teardown performance.
- if (!this.vm._isBeingDestroyed) {
- this.vm._watchers.$remove(this)
- }
- var i = this.deps.length
- while (i--) {
- this.deps[i].removeSub(this)
- }
- this.active = false
- this.vm = this.cb = this.value = null
- }
- }
- /**
- * Recrusively traverse an object to evoke all converted
- * getters, so that every nested property inside the object
- * is collected as a "deep" dependency.
- *
- * @param {Object} obj
- */
- function traverse (obj) {
- var key, val, i
- for (key in obj) {
- val = obj[key]
- if (_.isArray(val)) {
- i = val.length
- while (i--) traverse(val[i])
- } else if (_.isObject(val)) {
- traverse(val)
- }
- }
- }
- module.exports = Watcher
|