Skip to content

Commit a2cd412

Browse files
committed
refactor: observerState
1 parent 318f29f commit a2cd412

File tree

5 files changed

+26
-23
lines changed

5 files changed

+26
-23
lines changed

src/core/instance/inject.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/* @flow */
22

3-
import { warn, hasSymbol } from '../util/index'
4-
import { defineReactive, observerState } from '../observer/index'
53
import { hasOwn } from 'shared/util'
4+
import { warn, hasSymbol } from '../util/index'
5+
import { defineReactive, toggleObserving } from '../observer/index'
66

77
export function initProvide (vm: Component) {
88
const provide = vm.$options.provide
@@ -16,7 +16,7 @@ export function initProvide (vm: Component) {
1616
export function initInjections (vm: Component) {
1717
const result = resolveInject(vm.$options.inject, vm)
1818
if (result) {
19-
observerState.shouldConvert = false
19+
toggleObserving(false)
2020
Object.keys(result).forEach(key => {
2121
/* istanbul ignore else */
2222
if (process.env.NODE_ENV !== 'production') {
@@ -32,7 +32,7 @@ export function initInjections (vm: Component) {
3232
defineReactive(vm, key, result[key])
3333
}
3434
})
35-
observerState.shouldConvert = true
35+
toggleObserving(true)
3636
}
3737
}
3838

src/core/instance/lifecycle.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import config from '../config'
44
import Watcher from '../observer/watcher'
55
import { mark, measure } from '../util/perf'
66
import { createEmptyVNode } from '../vdom/vnode'
7-
import { observerState } from '../observer/index'
87
import { updateComponentListeners } from './events'
98
import { resolveSlots } from './render-helpers/resolve-slots'
9+
import { toggleObserving } from '../observer/index'
1010
import { pushTarget, popTarget } from '../observer/dep'
1111

1212
import {
@@ -245,14 +245,15 @@ export function updateChildComponent (
245245

246246
// update props
247247
if (propsData && vm.$options.props) {
248-
observerState.shouldConvert = false
248+
toggleObserving(false)
249249
const props = vm._props
250250
const propKeys = vm.$options._propKeys || []
251251
for (let i = 0; i < propKeys.length; i++) {
252252
const key = propKeys[i]
253-
props[key] = validateProp(key, vm.$options.props, propsData, vm)
253+
const propOptions: any = vm.$options.props // wtf flow?
254+
props[key] = validateProp(key, propOptions, propsData, vm)
254255
}
255-
observerState.shouldConvert = true
256+
toggleObserving(true)
256257
// keep a copy of raw propsData
257258
vm.$options.propsData = propsData
258259
}

src/core/instance/state.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import {
99
set,
1010
del,
1111
observe,
12-
observerState,
13-
defineReactive
12+
defineReactive,
13+
toggleObserving
1414
} from '../observer/index'
1515

1616
import {
@@ -69,7 +69,9 @@ function initProps (vm: Component, propsOptions: Object) {
6969
const keys = vm.$options._propKeys = []
7070
const isRoot = !vm.$parent
7171
// root instance props should be converted
72-
observerState.shouldConvert = isRoot
72+
if (!isRoot) {
73+
toggleObserving(false)
74+
}
7375
for (const key in propsOptions) {
7476
keys.push(key)
7577
const value = validateProp(key, propsOptions, propsData, vm)
@@ -104,7 +106,7 @@ function initProps (vm: Component, propsOptions: Object) {
104106
proxy(vm, `_props`, key)
105107
}
106108
}
107-
observerState.shouldConvert = true
109+
toggleObserving(true)
108110
}
109111

110112
function initData (vm: Component) {

src/core/observer/index.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ import {
1717
const arrayKeys = Object.getOwnPropertyNames(arrayMethods)
1818

1919
/**
20-
* By default, when a reactive property is set, the new value is
21-
* also converted to become reactive. However when passing down props,
22-
* we don't want to force conversion because the value may be a nested value
23-
* under a frozen data structure. Converting it would defeat the optimization.
20+
* In some cases we may want to disable observation inside a component's
21+
* update computation.
2422
*/
25-
export const observerState = {
26-
shouldConvert: true
23+
export let shouldObserve: boolean = true
24+
25+
export function toggleObserving (value: boolean) {
26+
shouldObserve = value
2727
}
2828

2929
/**
@@ -112,7 +112,7 @@ export function observe (value: any, asRootData: ?boolean): Observer | void {
112112
if (hasOwn(value, '__ob__') && value.__ob__ instanceof Observer) {
113113
ob = value.__ob__
114114
} else if (
115-
observerState.shouldConvert &&
115+
shouldObserve &&
116116
!isServerRendering() &&
117117
(Array.isArray(value) || isPlainObject(value)) &&
118118
Object.isExtensible(value) &&

src/core/util/props.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* @flow */
22

33
import { warn } from './debug'
4-
import { observe, observerState } from '../observer/index'
4+
import { observe, toggleObserving, shouldObserve } from '../observer/index'
55
import {
66
hasOwn,
77
isObject,
@@ -40,10 +40,10 @@ export function validateProp (
4040
value = getPropDefaultValue(vm, prop, key)
4141
// since the default value is a fresh copy,
4242
// make sure to observe it.
43-
const prevShouldConvert = observerState.shouldConvert
44-
observerState.shouldConvert = true
43+
const prevShouldObserve = shouldObserve
44+
toggleObserving(true)
4545
observe(value)
46-
observerState.shouldConvert = prevShouldConvert
46+
toggleObserving(prevShouldObserve)
4747
}
4848
if (
4949
process.env.NODE_ENV !== 'production' &&

0 commit comments

Comments
 (0)