Skip to content

Commit 3693ca7

Browse files
committed
Internalize emitter implementation
This has a few benefits - no longer need to shiv the difference between Component's emitter & Browserify's emitter (node version) - no longer need to hack around Browserify's static require parsing - able to drop methods not used in Vue - able to add custom callback context control, as mentioned in #130
1 parent 79f6193 commit 3693ca7

File tree

6 files changed

+77
-28
lines changed

6 files changed

+77
-28
lines changed

.travis.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ branches:
55
only:
66
- master
77
before_install:
8-
- npm install -g grunt-cli component phantomjs casperjs
9-
before_script:
10-
- component install
8+
- npm install -g grunt-cli phantomjs casperjs
119
env:
1210
global:
1311
- secure: Ce9jxsESszOnGyj3A6wILO5W412El9iD/HCHiFgbr8/cSXa4Yt0ZOEZysZeyaBX6IFUCjHtQPLasVgCxijDHrhi7/drmyCE+ksruk/6LJWn9C46PZK6nI+N04iYA2TRnocFllhGbyttpbpxY04smCmGWqXwLppu9nb+VIDkKGmE=

CONTRIBUTING.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ var a = superLongConditionalStatement
3333

3434
## Development Setup
3535

36-
You will need [Node](http://nodejs.org), [Grunt](http://gruntjs.com), [Component](https://github.com/component/component), [PhantomJS](http://phantomjs.org) and [CasperJS](http://casperjs.org).
36+
You will need [Node](http://nodejs.org), [Grunt](http://gruntjs.com), [PhantomJS](http://phantomjs.org) and [CasperJS](http://casperjs.org).
3737

3838
``` bash
39-
# in case you don't already have them:
40-
# npm install -g grunt-cli component
41-
$ npm install && component install
39+
# in case you don't already it:
40+
# npm install -g grunt-cli
41+
$ npm install
4242
```
4343

4444
To watch and auto-build `dist/vue.js` during development:

component.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,5 @@
3030
"src/directives/with.js",
3131
"src/directives/html.js",
3232
"src/directives/style.js"
33-
],
34-
"dependencies": {
35-
"component/emitter": "*"
36-
}
33+
]
3734
}

src/compiler.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ function Compiler (vm, options) {
5757
compiler.computed = []
5858
compiler.childCompilers = []
5959
compiler.emitter = new Emitter()
60+
compiler.emitter._ctx = vm
6061

6162
// set inenumerable VM properties
6263
def(vm, '$', makeHash())

src/emitter.js

Lines changed: 69 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,73 @@
1-
// shiv to make this work for Component, Browserify and Node at the same time.
2-
var Emitter,
3-
componentEmitter = 'emitter'
4-
5-
try {
6-
// Requiring without a string literal will make browserify
7-
// unable to parse the dependency, thus preventing it from
8-
// stopping the compilation after a failed lookup.
9-
Emitter = require(componentEmitter)
10-
} catch (e) {
11-
Emitter = require('events').EventEmitter
12-
Emitter.prototype.off = function () {
13-
var method = arguments.length > 1
14-
? this.removeListener
15-
: this.removeAllListeners
16-
return method.apply(this, arguments)
1+
function Emitter () {}
2+
3+
var EmitterProto = Emitter.prototype,
4+
slice = [].slice
5+
6+
EmitterProto.on = function(event, fn){
7+
this._cbs = this._cbs || {}
8+
;(this._cbs[event] = this._cbs[event] || [])
9+
.push(fn)
10+
return this
11+
}
12+
13+
Emitter.prototype.once = function(event, fn){
14+
var self = this
15+
this._cbs = this._cbs || {}
16+
17+
function on() {
18+
self.off(event, on)
19+
fn.apply(this, arguments)
20+
}
21+
22+
on.fn = fn
23+
this.on(event, on)
24+
return this
25+
}
26+
27+
Emitter.prototype.off = function(event, fn){
28+
this._cbs = this._cbs || {}
29+
30+
// all
31+
if (!arguments.length) {
32+
this._cbs = {}
33+
return this
1734
}
35+
36+
// specific event
37+
var callbacks = this._cbs[event]
38+
if (!callbacks) return this
39+
40+
// remove all handlers
41+
if (arguments.length === 1) {
42+
delete this._cbs[event]
43+
return this
44+
}
45+
46+
// remove specific handler
47+
var cb
48+
for (var i = 0; i < callbacks.length; i++) {
49+
cb = callbacks[i]
50+
if (cb === fn || cb.fn === fn) {
51+
callbacks.splice(i, 1)
52+
break
53+
}
54+
}
55+
return this
56+
}
57+
58+
Emitter.prototype.emit = function(event){
59+
this._cbs = this._cbs || {}
60+
var args = slice.call(arguments, 1),
61+
callbacks = this._cbs[event]
62+
63+
if (callbacks) {
64+
callbacks = callbacks.slice(0)
65+
for (var i = 0, len = callbacks.length; i < len; i++) {
66+
callbacks[i].apply(this._ctx || this, args)
67+
}
68+
}
69+
70+
return this
1871
}
1972

2073
module.exports = Emitter

test/unit/specs/observer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
describe('UNIT: Observer', function () {
22

33
var Observer = require('vue/src/observer'),
4-
Emitter = require('emitter')
4+
Emitter = require('vue/src/emitter')
55

66
describe('Observing Object', function () {
77

0 commit comments

Comments
 (0)