Comparing version 0.0.16 to 0.1.0
{ | ||
"name": "eev", | ||
"main": "eev.js", | ||
"version": "0.0.16", | ||
"version": "0.1.0", | ||
"homepage": "https://github.com/chrisdavies/eev", | ||
@@ -6,0 +6,0 @@ "authors": [ |
100
eev.js
@@ -0,63 +1,6 @@ | ||
var Eev = (function () { | ||
var id = 0; | ||
var splitter = /[^\w\-]+/g; | ||
// The Eev constructor | ||
function PubSub () { | ||
this.events = {}; | ||
} | ||
PubSub.prototype = { | ||
on: function (name, fn) { | ||
this.isRegistered(name, fn) || this.register(name, fn); | ||
}, | ||
off: function (name, fn) { | ||
this.isRegistered(name, fn) && this.unregister(name, fn); | ||
}, | ||
emit: function (name, data) { | ||
var evt = this.events[name]; | ||
evt && evt.head.run(data); | ||
}, | ||
isRegistered: function (name, fn) { | ||
return fn._eev && fn._eev[name]; | ||
}, | ||
register: function (name, fn) { | ||
var link = this.insertLinkInList(name, fn); | ||
this.insertLinkInFn(name, link, fn); | ||
}, | ||
insertLinkInList: function (name, fn) { | ||
var list = this.events[name] || (this.events[name] = new LinkedList()); | ||
return list.insert(fn); | ||
}, | ||
insertLinkInFn: function (name, link, fn) { | ||
var eev = fn._eev || (fn._eev = {}); | ||
eev[name] = link; | ||
}, | ||
unregister: function (name, fn) { | ||
var link = this.removeLinkFromFn(name, fn); | ||
this.removeLinkFromList(name, link); | ||
}, | ||
removeLinkFromFn: function (name, fn) { | ||
var link = fn._eev[name]; | ||
fn._eev[name] = undefined; | ||
return link; | ||
}, | ||
removeLinkFromList: function (name, link) { | ||
var list = this.events[name]; | ||
list && list.remove(link); | ||
} | ||
}; | ||
// A relatively generic LinkedList impl | ||
@@ -69,2 +12,3 @@ function LinkedList(linkConstructor) { | ||
this.linkConstructor = linkConstructor; | ||
this.reg = {}; | ||
} | ||
@@ -100,3 +44,37 @@ | ||
return PubSub; | ||
function Eev () { | ||
this.events = {}; | ||
} | ||
Eev.prototype = { | ||
on: function (names, fn) { | ||
var me = this; | ||
names.split(splitter).forEach(function (name) { | ||
var list = me.events[name] || (me.events[name] = new LinkedList()); | ||
var eev = fn._eev || (fn._eev = (++id)); | ||
list.reg[eev] || (list.reg[eev] = list.insert(fn)); | ||
}); | ||
}, | ||
off: function (names, fn) { | ||
var me = this; | ||
names.split(splitter).forEach(function (name) { | ||
var list = me.events[name]; | ||
var link = list.reg[fn._eev]; | ||
list.reg[fn._eev] = undefined; | ||
list && link && list.remove(link); | ||
}); | ||
}, | ||
emit: function (name, data) { | ||
var evt = this.events[name]; | ||
evt && evt.head.run(data); | ||
} | ||
}; | ||
return Eev; | ||
}()); | ||
@@ -103,0 +81,0 @@ |
@@ -1,2 +0,2 @@ | ||
var Eev=function(){function t(){this.events={}}function e(t){this.head=new n,this.tail=new n(this.head),this.head.next=this.tail,this.linkConstructor=t}function n(t,e,n){this.prev=t,this.next=e,this.fn=n||i}function i(){}return t.prototype={on:function(t,e){this.isRegistered(t,e)||this.register(t,e)},off:function(t,e){this.isRegistered(t,e)&&this.unregister(t,e)},emit:function(t,e){var n=this.events[t];n&&n.head.run(e)},isRegistered:function(t,e){return e._eev&&e._eev[t]},register:function(t,e){var n=this.insertLinkInList(t,e);this.insertLinkInFn(t,n,e)},insertLinkInList:function(t,n){var i=this.events[t]||(this.events[t]=new e);return i.insert(n)},insertLinkInFn:function(t,e,n){var i=n._eev||(n._eev={});i[t]=e},unregister:function(t,e){var n=this.removeLinkFromFn(t,e);this.removeLinkFromList(t,n)},removeLinkFromFn:function(t,e){var n=e._eev[t];return e._eev[t]=void 0,n},removeLinkFromList:function(t,e){var n=this.events[t];n&&n.remove(e)}},e.prototype={insert:function(t){var e=new n(this.tail.prev,this.tail,t);return e.next.prev=e.prev.next=e,e},remove:function(t){t.prev.next=t.next,t.next.prev=t.prev}},n.prototype.run=function(t){this.fn(t),this.next&&this.next.run(t)},t}();!function(t,e){var n=t.define,i=t.module;n&&n.amd?n([],e):i&&i.exports&&(i.exports=e())}(this,function(){return Eev}); | ||
var Eev=function(){function e(e){this.head=new t,this.tail=new t(this.head),this.head.next=this.tail,this.linkConstructor=e,this.reg={}}function t(e,t,i){this.prev=e,this.next=t,this.fn=i||n}function n(){}function i(){this.events={}}var r=0,o=/[^\w\-]+/g;return e.prototype={insert:function(e){var n=new t(this.tail.prev,this.tail,e);return n.next.prev=n.prev.next=n,n},remove:function(e){e.prev.next=e.next,e.next.prev=e.prev}},t.prototype.run=function(e){this.fn(e),this.next&&this.next.run(e)},i.prototype={on:function(t,n){var i=this;t.split(o).forEach(function(t){var o=i.events[t]||(i.events[t]=new e),s=n._eev||(n._eev=++r);o.reg[s]||(o.reg[s]=o.insert(n))})},off:function(e,t){var n=this;e.split(o).forEach(function(e){var i=n.events[e],r=i.reg[t._eev];i.reg[t._eev]=void 0,i&&r&&i.remove(r)})},emit:function(e,t){var n=this.events[e];n&&n.head.run(t)}},i}();!function(e,t){var n=e.define;n&&n.amd?n([],t):"undefined"!=typeof module&&module.exports&&(module.exports=t())}(this,function(){return Eev}); | ||
//# sourceMappingURL=eev.min.js.map |
{ | ||
"name": "eev", | ||
"version": "0.0.16", | ||
"version": "0.1.0", | ||
"description": "A tiny, fast, zero-dependency event emitter", | ||
@@ -28,4 +28,8 @@ "main": "eev.js", | ||
"scripts": { | ||
"make": "uglifyjs eev.js --source-map eev.min.js.map -m -c -o eev.min.js" | ||
"test": "jasmine-node test/*.spec.js", | ||
"min": "uglifyjs eev.js --source-map eev.min.js.map -m -c -o eev.min.js && echo 'Minified size' && cat eev.min.js | gzip -9f | wc -c" | ||
}, | ||
"devDependencies": { | ||
"jasmine-node": "^1.14.5" | ||
} | ||
} |
# Eev | ||
A tiny, fast, zero-dependency event emitter for JavaScript. | ||
A tiny, [fast](http://jsperf.com/jsevents/28), zero-dependency event emitter for JavaScript. | ||
- Less than 500 bytes minified + zipped | ||
- Fast... see [jsperf](http://jsperf.com/jsevents/28) | ||
- Zero dependencies | ||
- Simple | ||
[![Build Status](https://travis-ci.org/chrisdavies/eev.svg?branch=master)](https://travis-ci.org/chrisdavies/eev) | ||
## Usage | ||
@@ -9,35 +16,83 @@ | ||
var e = new Eev(); | ||
```javascript | ||
var e = new Eev(); | ||
``` | ||
Then, add handlers as you see fit. | ||
e.on('some-event', function (data) { | ||
alert('got ' + data); | ||
}); | ||
```javascript | ||
e.on('some-event', function (data) { | ||
alert('got ' + data); | ||
}); | ||
e.on('some-event', function (data) { | ||
console.log('got ' + data); | ||
}); | ||
e.on('some-event', function (data) { | ||
console.log('got ' + data); | ||
}); | ||
``` | ||
Remove handlers using `off`. | ||
function myHandler (data) { console.log(data); } | ||
```javascript | ||
function myHandler (data) { console.log(data); } | ||
e.on('some-event', myHandler); | ||
e.off('some-event', myHandler); | ||
e.on('some-event', myHandler); | ||
e.off('some-event', myHandler); | ||
``` | ||
Trigger events using `emit`. | ||
// The second parameter here is the data you wish to | ||
// pass to the event handlers | ||
e.emit('some-event', { foo: 'Bar' }); | ||
```javascript | ||
// The second parameter here is the data you wish to | ||
// pass to the event handlers | ||
e.emit('some-event', { foo: 'Bar' }); | ||
``` | ||
If you want a handler to only run once, you can do this: | ||
e.on('some-event', function foo () { | ||
e.off('some-event', foo); | ||
```javascript | ||
e.on('some-event', function foo () { | ||
e.off('some-event', foo); | ||
// Do stuff | ||
}); | ||
// Do stuff | ||
}); | ||
``` | ||
You can register for multiple events at once like this: | ||
```javascript | ||
function myHandler (data) { console.log(data); } | ||
e.on('event1 event2 etc', myHandler); | ||
e.off('event1 event2 etc', myHandler); | ||
``` | ||
Stopping propagation isn't build into Eev. If enough people ask for it, I'll bake it in. Meanwile, you can work around this limitation by doing something like this: | ||
```js | ||
;(function () { | ||
var superOn = Eev.prototype.on; | ||
Eev.prototype.on = function (names, fn) { | ||
superOn.call(this, names, function (data) { | ||
if (!data.isCanceled) { | ||
return fn(data); | ||
} | ||
}); | ||
}; | ||
}()); | ||
``` | ||
With the above patch in place, you can do something like this in your event handlers: | ||
```js | ||
e.on('some-event', function foo (data) { | ||
data.isCanceled = true; // Now, no downstream handlers should be invoked | ||
}) | ||
``` | ||
## Installation | ||
@@ -53,8 +108,2 @@ | ||
## Performance | ||
It performs well (compared to jQuery in this case): | ||
http://jsperf.com/jsevents/3 | ||
## License MIT | ||
@@ -61,0 +110,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
13357
10
227
116
1
1