8000 Merge pull request #2394 from saul/2393-observe-non-configurable · ctyu/vue@5a51b4a · GitHub
[go: up one dir, main page]

Skip to content

Commit 5a51b4a

Browse files
committed
Merge pull request vuejs#2394 from saul/2393-observe-non-configurable
Test whether a property is configurable before defining it as reactive
2 parents 3f5c916 + 98f06e1 commit 5a51b4a

File tree

4 files changed

+7
-58
lines changed

4 files changed

+7
-58
lines changed

src/config.js

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,6 @@ const config = {
3535

3636
warnExpressionErrors: true,
3737

38-
/**
39-
* Whether or not to handle fully object properties which
40-
* are already backed by getters and seters. Depending on
41-
* use case and environment, this might introduce non-neglible
42-
* performance penalties.
43-
*/
44-
convertAllProperties: false,
45-
4638
/**
4739
* Internal flag to indicate the delimiters have been
4840
* changed.

src/observer/index.js

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import config from '../config'
21
import Dep from './dep'
32
import { arrayMethods } from './array'
43
import {
@@ -177,17 +176,15 @@ export function observe (value, vm) {
177176
export function defineReactive (obj, key, val) {
178177
var dep = new Dep()
179178

180-
// cater for pre-defined getter/setters
181-
var getter, setter
182-
if (config.convertAllProperties) {
183-
var property = Object.getOwnPropertyDescriptor(obj, key)
184-
if (property && property.configurable === false) {
185-
return
186-
}
187-
getter = property && property.get
188-
setter = property && property.set
179+
var property = Object.getOwnPropertyDescriptor(obj, key)
180+
if (property && property.configurable === false) {
181+
return
189182
}
190183

184+
// cater for pre-defined getter/setters
185+
var getter = property && property.get
186+
var setter = property && property.set
187+
191188
var childOb = observe(val)
192189
Object.defineProperty(obj, key, {
193190
enumerable: true,

test/unit/specs/directives/public/for/for_spec.js

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
var _ = require('src/util')
22
var Vue = require('src')
3-
var config = require('src/config')
43

54
describe('v-for', function () {
65
var el
76
beforeEach(function () {
87
el = document.createElement('div')
98
spyWarns()
10-
config.convertAllProperties = false
119
})
1210

1311
it('objects', function (done) {
@@ -21,18 +19,6 @@ describe('v-for', function () {
2119
assertMutations(vm, el, done)
2220
})
2321

24-
it('objects with convertAllProperties on', function (done) {
25-
config.convertAllProperties = true
26-
var vm = new Vue({
27-
el: el,
28-
data: {
29-
items: [{a: 1}, {a: 2}]
30-
},
31-
template: '<div v-for="item in items">{{$index}} {{item.a}}</div>'
32-
})
33-
assertMutations(vm, el, done)
34-
})
35-
3622
it('primitives', function (done) {
3723
var vm = new Vue({
3824
el: el,

test/unit/specs/observer/observer_spec.js

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ var Observer = ob.Observer
44
var observe = ob.observe
55
var Dep = require('src/observer/dep')
66
var _ = require('src/util')
7-
var config = require('src/config')
87

98
describe('Observer', function () {
109
beforeEach(function () {
@@ -59,9 +58,6 @@ describe('Observer', function () {
5958
})
6059

6160
it('create on already observed object', function () {
62-
var previousConvertAllProperties = config.convertAllProperties
63-
config.convertAllProperties = true
64-
6561
// on object
6662
var obj = {}
6763
var val = 0
@@ -97,14 +93,9 @@ describe('Observer', function () {
9793
// should call underlying setter
9894
obj.a = 10
9995
expect(val).toBe(10)
100-
101-
config.convertAllProperties = previousConvertAllProperties
10296
})
10397

10498
it('create on property with only getter', function () {
105-
var previousConvertAllProperties = config.convertAllProperties
106-
config.convertAllProperties = true
107-
10899
// on object
109100
var obj = {}
110101
Object.defineProperty(obj, 'a', {
@@ -134,14 +125,9 @@ describe('Observer', function () {
134125
obj.a = 101
135126
} catch (e) {}
136127
expect(obj.a).toBe(123)
137-
138-
config.convertAllProperties = previousConvertAllProperties
139128
})
140129

141130
it('create on property with only setter', function () {
142-
var previousConvertAllProperties = config.convertAllProperties
143-
config.convertAllProperties = true
144-
145131
// on object
146132
var obj = {}
147133
var val = 10
@@ -168,14 +154,9 @@ describe('Observer', function () {
168154
// writes should call the set function
169155
obj.a = 100
170156
expect(val).toBe(100)
171-
172-
config.convertAllProperties = previousConvertAllProperties
173157
})
174158

175159
it('create on property which is marked not configurable', function () {
176-
var previousConvertAllProperties = config.convertAllProperties
177-
config.convertAllProperties = true
178-
179160
// on object
180161
var obj = {}
181162
Object.defineProperty(obj, 'a', {
@@ -188,8 +169,6 @@ describe('Observer', function () {
188169
expect(ob instanceof Observer).toBe(true)
189170
expect(ob.value).toBe(obj)
190171
expect(obj.__ob__).toBe(ob)
191-
192-
config.convertAllProperties = previousConvertAllProperties
193172
})
194173

195174
it('create on array', function () {
@@ -237,9 +216,6 @@ describe('Observer', function () {
237216
})
238217

239218
it('observing object prop change on defined property', function () {
240-
var previousConvertAllProperties = config.convertAllProperties
241-
config.convertAllProperties = true
242-
243219
var obj = { val: 2 }
244220
Object.defineProperty(obj, 'a', {
245221
configurable: true,
@@ -271,8 +247,6 @@ describe('Observer', function () {
271247
expect(obj.val).toBe(3) // make sure 'setter' was called
272248
obj.val = 5
273249
expect(obj.a).toBe(5) // make sure 'getter' was called
274-
275-
config.convertAllProperties = previousConvertAllProperties
276250
})
277251

278252
it('observing set/delete', function () {

0 commit comments

Comments
 (0)
0