Skip to content

Commit 276c126

Browse files
committed
__observer__ => __emitter__ for better clarity
1 parent e1de434 commit 276c126

File tree

5 files changed

+43
-43
lines changed

5 files changed

+43
-43
lines changed

src/compiler.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,7 @@ CompilerProto.defineProp = function (key, binding) {
565565

566566
var compiler = this,
567567
data = compiler.data,
568-
ob = data.__observer__
568+
ob = data.__emitter__
569569

570570
// make sure the key is present in data
571571
// so it can be observed

src/directives/repeat.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,8 @@ module.exports = {
202202

203203
// listen for collection mutation events
204204
// the collection has been augmented during Binding.set()
205-
if (!collection.__observer__) Observer.watchArray(collection)
206-
collection.__observer__.on('mutate', this.mutationListener)
205+
if (!collection.__emitter__) Observer.watchArray(collection)
206+
collection.__emitter__.on('mutate', this.mutationListener)
207207

208208
// create new VMs and append to DOM
209209
if (collection.length) {
@@ -330,7 +330,7 @@ module.exports = {
330330
vms.splice(index, 0, item)
331331
// for primitive values, listen for value change
332332
if (primitive) {
333-
data.__observer__.on('set', function (key, val) {
333+
data.__emitter__.on('set', function (key, val) {
334334
if (key === '$value') {
335335
col[item.$index] = val
336336
}
@@ -355,7 +355,7 @@ module.exports = {
355355
} else {
356356
delete this.object[key]
357357
}
358-
this.object.__observer__.emit('set', key, val, true)
358+
this.object.__emitter__.emit('set', key, val, true)
359359
}
360360
},
361361

@@ -364,7 +364,7 @@ module.exports = {
364364
delete this.vm.$[this.childId]
365365
}
366366
if (this.collection) {
367-
this.collection.__observer__.off('mutate', this.mutationListener)
367+
this.collection.__emitter__.off('mutate', this.mutationListener)
368368
if (destroyAll) {
369369
var i = this.vms.length
370370
while (i--) {

src/observer.js

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ var ArrayProxy = Object.create(Array.prototype)
3232
methods.forEach(function (method) {
3333
def(ArrayProxy, method, function () {
3434
var result = Array.prototype[method].apply(this, arguments)
35-
this.__observer__.emit('mutate', null, this, {
35+
this.__emitter__.emit('mutate', null, this, {
3636
method: method,
3737
args: slice.call(arguments),
3838
result: result
@@ -110,10 +110,10 @@ function watchObject (obj) {
110110
* and add augmentations by intercepting the prototype chain
111111
*/
112112
function watchArray (arr) {
113-
var observer = arr.__observer__
114-
if (!observer) {
115-
observer = new Emitter()
116-
def(arr, '__observer__', observer)
113+
var emitter = arr.__emitter__
114+
if (!emitter) {
115+
emitter = new Emitter()
116+
def(arr, '__emitter__', emitter)
117117
}
118118
if (hasProto) {
119119
arr.__proto__ = ArrayProxy
@@ -142,8 +142,8 @@ function convert (obj, key) {
142142
// emit set on bind
143143
// this means when an object is observed it will emit
144144
// a first batch of set events.
145-
var observer = obj.__observer__,
146-
values = observer.values
145+
var emitter = obj.__emitter__,
146+
values = emitter.values
147147

148148
init(obj[key])
149149

@@ -152,13 +152,13 @@ function convert (obj, key) {
152152
var value = values[key]
153153
// only emit get on tip values
154154
if (pub.shouldGet && typeOf(value) !== OBJECT) {
155-
observer.emit('get', key)
155+
emitter.emit('get', key)
156156
}
157157
return value
158158
},
159159
set: function (newVal) {
160160
var oldVal = values[key]
161-
unobserve(oldVal, key, observer)
161+
unobserve(oldVal, key, emitter)
162162
copyPaths(newVal, oldVal)
163163
// an immediate property should notify its parent
164164
// to emit set for itself too
@@ -168,11 +168,11 @@ function convert (obj, key) {
168168

169169
function init (val, propagate) {
170170
values[key] = val
171-
observer.emit('set', key, val, propagate)
171+
emitter.emit('set', key, val, propagate)
172172
if (Array.isArray(val)) {
173-
observer.emit('set', key + '.length', val.length)
173+
emitter.emit('set', key + '.length', val.length)
174174
}
175-
observe(val, key, observer)
175+
observe(val, key, emitter)
176176
}
177177
}
178178

@@ -193,7 +193,7 @@ function isWatchable (obj) {
193193
*/
194194
function emitSet (obj) {
195195
var type = typeOf(obj),
196-
emitter = obj && obj.__observer__
196+
emitter = obj && obj.__emitter__
197197
if (type === ARRAY) {
198198
emitter.emit('set', 'length', obj.length)
199199
} else if (type === OBJECT) {
@@ -243,15 +243,15 @@ function ensurePath (obj, key) {
243243
sec = path[i]
244244
if (!obj[sec]) {
245245
obj[sec] = {}
246-
if (obj.__observer__) convert(obj, sec)
246+
if (obj.__emitter__) convert(obj, sec)
247247
}
248248
obj = obj[sec]
249249
}
250250
if (typeOf(obj) === OBJECT) {
251251
sec = path[i]
252252
if (!(sec in obj)) {
253253
obj[sec] = undefined
254-
if (obj.__observer__) convert(obj, sec)
254+
if (obj.__emitter__) convert(obj, sec)
255255
}
256256
}
257257
}
@@ -260,54 +260,54 @@ function ensurePath (obj, key) {
260260
* Observe an object with a given path,
261261
* and proxy get/set/mutate events to the provided observer.
262262
*/
263-
function observe (obj, rawPath, parentOb) {
263+
function observe (obj, rawPath, observer) {
264264

265265
if (!isWatchable(obj)) return
266266

267267
var path = rawPath ? rawPath + '.' : '',
268-
alreadyConverted = !!obj.__observer__,
269-
childOb
268+
alreadyConverted = !!obj.__emitter__,
269+
emitter
270270

271271
if (!alreadyConverted) {
272-
def(obj, '__observer__', new Emitter())
272+
def(obj, '__emitter__', new Emitter())
273273
}
274274

275-
childOb = obj.__observer__
276-
childOb.values = childOb.values || utils.hash()
275+
emitter = obj.__emitter__
276+
emitter.values = emitter.values || utils.hash()
277277

278278
// setup proxy listeners on the parent observer.
279279
// we need to keep reference to them so that they
280280
// can be removed when the object is un-observed.
281-
parentOb.proxies = parentOb.proxies || {}
282-
var proxies = parentOb.proxies[path] = {
281+
observer.proxies = observer.proxies || {}
282+
var proxies = observer.proxies[path] = {
283283
get: function (key) {
284-
parentOb.emit('get', path + key)
284+
observer.emit('get', path + key)
285285
},
286286
set: function (key, val, propagate) {
287-
parentOb.emit('set', path + key, val)
287+
observer.emit('set', path + key, val)
288288
// also notify observer that the object itself changed
289289
// but only do so when it's a immediate property. this
290290
// avoids duplicate event firing.
291291
if (rawPath && propagate) {
292-
parentOb.emit('set', rawPath, obj, true)
292+
observer.emit('set', rawPath, obj, true)
293293
}
294294
},
295295
mutate: function (key, val, mutation) {
296296
// if the Array is a root value
297297
// the key will be null
298298
var fixedPath = key ? path + key : rawPath
299-
parentOb.emit('mutate', fixedPath, val, mutation)
299+
observer.emit('mutate', fixedPath, val, mutation)
300300
// also emit set for Array's length when it mutates
301301
var m = mutation.method
302302
if (m !== 'sort' && m !== 'reverse') {
303-
parentOb.emit('set', fixedPath + '.length', val.length)
303+
observer.emit('set', fixedPath + '.length', val.length)
304304
}
305305
}
306306
}
307307

308308
// attach the listeners to the child observer.
309309
// now all the events will propagate upwards.
310-
childOb
310+
emitter
311311
.on('get', proxies.get)
312312
.on('set', proxies.set)
313313
.on('mutate', proxies.mutate)
@@ -331,14 +331,14 @@ function observe (obj, rawPath, parentOb) {
331331
*/
332332
function unobserve (obj, path, observer) {
333333

334-
if (!obj || !obj.__observer__) return
334+
if (!obj || !obj.__emitter__) return
335335

336336
path = path ? path + '.' : ''
337337
var proxies = observer.proxies[path]
338338
if (!proxies) return
339339

340340
// turn off listeners
341-
obj.__observer__
341+
obj.__emitter__
342342
.off('get', proxies.get)
343343
.off('set', proxies.set)
344344
.off('mutate', proxies.mutate)

test/unit/specs/observer.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ describe('UNIT: Observer', function () {
88
it('should not watch a ViewModel instance', function () {
99
var obj = new Vue(), ob = new Emitter()
1010
Observer.observe(obj, 'test', ob)
11-
assert.notOk(obj.__observer__)
11+
assert.notOk(obj.__emitter__)
1212
})
1313

1414
it('should attach hidden observer and values to the object', function () {
1515
var obj = {}, ob = new Emitter()
1616
Observer.observe(obj, 'test', ob)
17-
assert.ok(obj.__observer__ instanceof Emitter)
18-
assert.ok(obj.__observer__.values)
17+
assert.ok(obj.__emitter__ instanceof Emitter)
18+
assert.ok(obj.__emitter__.values)
1919
})
2020

2121
var o1 = { a: 1, b: { c: 2 } }
@@ -104,7 +104,7 @@ describe('UNIT: Observer', function () {
104104
Observer.observe(arr, 'test', ob)
105105

106106
it('should attach the hidden observer', function () {
107-
assert.ok(arr.__observer__ instanceof Emitter)
107+
assert.ok(arr.__emitter__ instanceof Emitter)
108108
})
109109

110110
it('should overwrite the native array mutator methods', function () {
@@ -414,7 +414,7 @@ describe('UNIT: Observer', function () {
414414
})
415415

416416
it('should turn off corresponding event listeners', function () {
417-
var callbacks = obj.__observer__._callbacks
417+
var callbacks = obj.__emitter__._callbacks
418418
for (var e in callbacks) {
419419
assert.strictEqual(callbacks[e].length, 1)
420420
}

test/unit/specs/viewmodel.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ describe('UNIT: ViewModel', function () {
404404
}
405405
},
406406
data: {
407-
__observer__: {
407+
__emitter__: {
408408
off: function () {
409409
unobserveCalled = true
410410
return this

0 commit comments

Comments
 (0)