8000 use fn.apply in external vm event methods · hwclass/vue@81ae36c · GitHub
[go: up one dir, main page]

Skip to content

Commit 81ae36c

Browse files
author
Evan You
committed
use fn.apply in external vm event methods
1 parent e0fbc20 commit 81ae36c

File tree

2 files changed

+37
-7
lines changed

2 files changed

+37
-7
lines changed

src/emitter.js

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
1+
var slice = [].slice
2+
13
function Emitter (ctx) {
24
this._ctx = ctx || this
35
}
46

57
var EmitterProto = Emitter.prototype
68

7-
EmitterProto.on = function(event, fn){
9+
EmitterProto.on = function (event, fn) {
810
this._cbs = this._cbs || {}
911
;(this._cbs[event] = this._cbs[event] || [])
1012
.push(fn)
1113
return this
1214
}
1315

14-
EmitterProto.once = function(event, fn){
16+
EmitterProto.once = function (event, fn) {
1517
var self = this
1618
this._cbs = this._cbs || {}
1719

@@ -25,7 +27,7 @@ EmitterProto.once = function(event, fn){
2527
return this
2628
}
2729

28-
EmitterProto.off = function(event, fn){
30+
EmitterProto.off = function (event, fn) {
2931
this._cbs = this._cbs || {}
3032

3133
// all
@@ -56,7 +58,11 @@ EmitterProto.off = function(event, fn){
5658
return this
5759
}
5860

59-
EmitterProto.emit = function(event, a, b, c){
61+
/**
62+
* The internal, faster emit with fixed amount of arguments
63+
* using Function.call
64+
*/
65+
EmitterProto.emit = function (event, a, b, c) {
6066
this._cbs = this._cbs || {}
6167
var callbacks = this._cbs[event]
6268

@@ -70,4 +76,22 @@ EmitterProto.emit = function(event, a, b, c){
7076
return this
7177
}
7278

79+
/**
80+
* The external emit using Function.apply
81+
*/
82+
EmitterProto.applyEmit = function (event) {
83+
this._cbs = this._cbs || {}
84+
var callbacks = this._cbs[event], args
85+
86+
if (callbacks) {
87+
callbacks = callbacks.slice(0)
88+
args = slice.call(arguments, 1)
89+
for (var i = 0, len = callbacks.length; i < len; i++) {
90+
callbacks[i].apply(this._ctx, args)
91+
}
92+
}
93+
94+
return this
95+
}
96+
7397
module.exports = Emitter

src/viewmodel.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ def(VMProto, '$broadcast', function () {
9494
child
9595
while (i--) {
9696
child = children[i]
97-
child.emitter.emit.apply(child.emitter, arguments)
97+
child.emitter.applyEmit.apply(child.emitter, arguments)
9898
child.vm.$broadcast.apply(child.vm, arguments)
9999
}
100100
})
@@ -106,7 +106,7 @@ def(VMProto, '$dispatch', function () {
106106
var compiler = this.$compiler,
107107
emitter = compiler.emitter,
108108
parent = compiler.parent
109-
emitter.emit.apply(emitter, arguments)
109+
emitter.applyEmit.apply(emitter, arguments)
110110
if (parent) {
111111
parent.vm.$dispatch.apply(parent.vm, arguments)
112112
}
@@ -116,9 +116,15 @@ def(VMProto, '$dispatch', function () {
116116
* delegate on/off/once to the compiler's emitter
117117
*/
118118
;['emit', 'on', 'off', 'once'].forEach(function (method) {
119+
// internal emit has fixed number of arguments.
120+
// exposed emit uses the external version
121+
// with fn.apply.
122+
var realMethod = method === 'emit'
123+
? 'applyEmit'
124+
: method
119125
def(VMProto, '$' + method, function () {
120126
var emitter = this.$compiler.emitter
121-
emitter[method].apply(emitter, arguments)
127+
emitter[realMethod].apply(emitter, arguments)
122128
})
123129
})
124130

0 commit comments

Comments
 (0)
0