Skip to content

Commit

Permalink
refactor: observerState
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Mar 9, 2018
1 parent 318f29f commit a2cd412
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 23 deletions.
8 changes: 4 additions & 4 deletions src/core/instance/inject.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/* @flow */

import { warn, hasSymbol } from '../util/index'
import { defineReactive, observerState } from '../observer/index'
import { hasOwn } from 'shared/util'
import { warn, hasSymbol } from '../util/index'
import { defineReactive, toggleObserving } from '../observer/index'

export function initProvide (vm: Component) {
const provide = vm.$options.provide
Expand All @@ -16,7 +16,7 @@ export function initProvide (vm: Component) {
export function initInjections (vm: Component) {
const result = resolveInject(vm.$options.inject, vm)
if (result) {
observerState.shouldConvert = false
toggleObserving(false)
Object.keys(result).forEach(key => {
/* istanbul ignore else */
if (process.env.NODE_ENV !== 'production') {
Expand All @@ -32,7 +32,7 @@ export function initInjections (vm: Component) {
defineReactive(vm, key, result[key])
}
})
observerState.shouldConvert = true
toggleObserving(true)
}
}

Expand Down
9 changes: 5 additions & 4 deletions src/core/instance/lifecycle.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import config from '../config'
import Watcher from '../observer/watcher'
import { mark, measure } from '../util/perf'
import { createEmptyVNode } from '../vdom/vnode'
import { observerState } from '../observer/index'
import { updateComponentListeners } from './events'
import { resolveSlots } from './render-helpers/resolve-slots'
import { toggleObserving } from '../observer/index'
import { pushTarget, popTarget } from '../observer/dep'

import {
Expand Down Expand Up @@ -245,14 +245,15 @@ export function updateChildComponent (

// update props
if (propsData && vm.$options.props) {
observerState.shouldConvert = false
toggleObserving(false)
const props = vm._props
const propKeys = vm.$options._propKeys || []
for (let i = 0; i < propKeys.length; i++) {
const key = propKeys[i]
props[key] = validateProp(key, vm.$options.props, propsData, vm)
const propOptions: any = vm.$options.props // wtf flow?
props[key] = validateProp(key, propOptions, propsData, vm)
}
observerState.shouldConvert = true
toggleObserving(true)
// keep a copy of raw propsData
vm.$options.propsData = propsData
}
Expand Down
10 changes: 6 additions & 4 deletions src/core/instance/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import {
set,
del,
observe,
observerState,
defineReactive
defineReactive,
toggleObserving
} from '../observer/index'

import {
Expand Down Expand Up @@ -69,7 +69,9 @@ function initProps (vm: Component, propsOptions: Object) {
const keys = vm.$options._propKeys = []
const isRoot = !vm.$parent
// root instance props should be converted
observerState.shouldConvert = isRoot
if (!isRoot) {
toggleObserving(false)
}
for (const key in propsOptions) {
keys.push(key)
const value = validateProp(key, propsOptions, propsData, vm)
Expand Down Expand Up @@ -104,7 +106,7 @@ function initProps (vm: Component, propsOptions: Object) {
proxy(vm, `_props`, key)
}
}
observerState.shouldConvert = true
toggleObserving(true)
}

function initData (vm: Component) {
Expand Down
14 changes: 7 additions & 7 deletions src/core/observer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ import {
const arrayKeys = Object.getOwnPropertyNames(arrayMethods)

/**
* By default, when a reactive property is set, the new value is
* also converted to become reactive. However when passing down props,
* we don't want to force conversion because the value may be a nested value
* under a frozen data structure. Converting it would defeat the optimization.
* In some cases we may want to disable observation inside a component's
* update computation.
*/
export const observerState = {
shouldConvert: true
export let shouldObserve: boolean = true

export function toggleObserving (value: boolean) {
shouldObserve = value
}

/**
Expand Down Expand Up @@ -112,7 +112,7 @@ export function observe (value: any, asRootData: ?boolean): Observer | void {
if (hasOwn(value, '__ob__') && value.__ob__ instanceof Observer) {
ob = value.__ob__
} else if (
observerState.shouldConvert &&
shouldObserve &&
!isServerRendering() &&
(Array.isArray(value) || isPlainObject(value)) &&
Object.isExtensible(value) &&
Expand Down
8 changes: 4 additions & 4 deletions src/core/util/props.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* @flow */

import { warn } from './debug'
import { observe, observerState } from '../observer/index'
import { observe, toggleObserving, shouldObserve } from '../observer/index'
import {
hasOwn,
isObject,
Expand Down Expand Up @@ -40,10 +40,10 @@ export function validateProp (
value = getPropDefaultValue(vm, prop, key)
// since the default value is a fresh copy,
// make sure to observe it.
const prevShouldConvert = observerState.shouldConvert
observerState.shouldConvert = true
const prevShouldObserve = shouldObserve
toggleObserving(true)
observe(value)
observerState.shouldConvert = prevShouldConvert
toggleObserving(prevShouldObserve)
}
if (
process.env.NODE_ENV !== 'production' &&
Expand Down

0 comments on commit a2cd412

Please sign in to comment.