Comparing version 0.0.1 to 0.0.3
{ | ||
"name": "eev", | ||
"main": "eev.js", | ||
"version": "0.0.1", | ||
"version": "0.0.3", | ||
"homepage": "https://github.com/chrisdavies/eev", | ||
@@ -11,3 +11,3 @@ "authors": [ | ||
"keywords": [ | ||
"emitter" | ||
"emitter", | ||
"events", | ||
@@ -14,0 +14,0 @@ "event", |
146
eev.js
var Eev = (function () { | ||
function Eev () { | ||
// The Eev constructor | ||
function PubSub () { | ||
this.events = {}; | ||
}; | ||
} | ||
Eev.prototype = { | ||
on: function (name, handler) { | ||
var prop = evtProp(name); | ||
PubSub.prototype = { | ||
on: function (names, fn) { | ||
var self = this; | ||
this.eachName(names, function (name) { | ||
self.isRegistered(name, fn) || self.register(name, fn); | ||
}); | ||
}, | ||
if (handler[prop]) return; | ||
off: function (names, fn) { | ||
var self = this; | ||
this.eachName(names, function (name) { | ||
self.isRegistered(name, fn) && self.unregister(name, fn); | ||
}); | ||
}, | ||
var evt = this.evt(name), | ||
link = (handler[prop] = new Link(handler)), | ||
tail = evt.tail; | ||
eachName: function (name, fn) { | ||
var names = name.split(/\W+/g); | ||
for (var i = 0; i < names.length; ++i) { | ||
fn(names[i]); | ||
} | ||
}, | ||
link.next = tail; | ||
link.prev = tail.prev; | ||
link.prev.next = link.next.prev = link; | ||
emit: function (name, data) { | ||
var evt = this.events[name]; | ||
evt && evt.head.run(data); | ||
}, | ||
off: function (name, handler) { | ||
var prop = evtProp(name), | ||
link = handler[prop]; | ||
isRegistered: function (name, fn) { | ||
return fn._eev && fn._eev[name]; | ||
}, | ||
if (link) { | ||
link.next.prev = link.prev; | ||
link.prev.next = link.next; | ||
handler[prop] = undefined; | ||
} | ||
register: function (name, fn) { | ||
var link = this.insertLinkInList(name, fn); | ||
this.insertLinkInFn(name, link, fn); | ||
}, | ||
evt: function (name) { | ||
return this.events[name] || (this.events[name] = new LinkedList()); | ||
insertLinkInList: function (name, fn) { | ||
var list = this.events[name] || (this.events[name] = new LinkedList()); | ||
return list.insert(fn); | ||
}, | ||
emit: function (name, data) { | ||
this.evt(name).head.run(data); | ||
} | ||
}; | ||
insertLinkInFn: function (name, link, fn) { | ||
var eev = fn._eev || (fn._eev = {}); | ||
eev[name] = link; | ||
}, | ||
function noop() {} | ||
unregister: function (name, fn) { | ||
var link = this.removeLinkFromFn(name, fn); | ||
function LinkedList() { | ||
var head = new Link(noop), | ||
tail = (head.next = new Link()); | ||
this.removeLinkFromList(name, link); | ||
}, | ||
tail.prev = head; | ||
tail.run = noop; | ||
removeLinkFromFn: function (name, fn) { | ||
var link = fn._eev[name]; | ||
this.head = head; | ||
this.tail = tail; | ||
} | ||
fn._eev[name] = undefined; | ||
return link; | ||
}, | ||
function Link(cb) { | ||
this.cb = cb; | ||
this.prev = undefined; | ||
this.next = undefined; | ||
removeLinkFromList: function (name, link) { | ||
var list = this.events[name]; | ||
list && list.remove(link); | ||
} | ||
}; | ||
// A relatively generic LinkedList impl | ||
function LinkedList(linkConstructor) { | ||
this.head = new RunnableLink(); | ||
this.tail = new RunnableLink(this.head); | ||
this.head.next = this.tail; | ||
this.linkConstructor = linkConstructor; | ||
} | ||
Link.prototype = { | ||
run: function (data) { | ||
this.cb(data); | ||
this.next.run(data); | ||
LinkedList.prototype = { | ||
insert: function (data) { | ||
var link = new RunnableLink(this.tail.prev, this.tail, data); | ||
link.next.prev = link.prev.next = link; | ||
return link; | ||
}, | ||
remove: function (link) { | ||
link.prev.next = link.next; | ||
link.next.prev = link.prev; | ||
} | ||
}; | ||
function evtProp(name) { | ||
return '_evt' + name; | ||
// A link in the linked list which allows | ||
// for efficient execution of the callbacks | ||
function RunnableLink(prev, next, fn) { | ||
this.prev = prev; | ||
this.next = next; | ||
this.fn = fn || noop; | ||
} | ||
return Eev; | ||
})(); | ||
RunnableLink.prototype.run = function (data) { | ||
this.fn(data); | ||
this.next && this.next.run(data); | ||
}; | ||
function noop () { } | ||
return PubSub; | ||
}()); | ||
// AMD/CommonJS support | ||
(function (root, factory) { | ||
var define = root.define; | ||
if (define && define.amd) { | ||
define([], factory); | ||
} else if (typeof module !== 'undefined' && module.exports) { | ||
module.exports = factory(); | ||
} | ||
}(this, function () { return Eev; })); |
@@ -1,2 +0,2 @@ | ||
var Eev=function(){function t(){this.events={}}function n(){}function e(){var t=new i(n),e=t.next=new i;e.prev=t,e.run=n,this.head=t,this.tail=e}function i(t){this.cb=t,this.prev=void 0,this.next=void 0}function r(t){return"_evt"+t}return t.prototype={on:function(t,n){var e=r(t);if(!n[e]){var v=this.evt(t),o=n[e]=new i(n),u=v.tail;o.next=u,o.prev=u.prev,o.prev.next=o.next.prev=o}},off:function(t,n){var e=r(t),i=n[e];i&&(i.next.prev=i.prev,i.prev.next=i.next,n[e]=void 0)},evt:function(t){return this.events[t]||(this.events[t]=new e)},emit:function(t,n){this.evt(t).head.run(n)}},i.prototype={run:function(t){this.cb(t),this.next.run(t)}},t}(); | ||
var Eev=function(){function e(){this.events={}}function n(e){this.head=new t,this.tail=new t(this.head),this.head.next=this.tail,this.linkConstructor=e}function t(e,n,t){this.prev=e,this.next=n,this.fn=t||i}function i(){}return e.prototype={on:function(e,n){var t=this;this.eachName(e,function(e){t.isRegistered(e,n)||t.register(e,n)})},off:function(e,n){var t=this;this.eachName(e,function(e){t.isRegistered(e,n)&&t.unregister(e,n)})},eachName:function(e,n){for(var t=e.split(/\W+/g),i=0;i<t.length;++i)n(t[i])},emit:function(e,n){var t=this.events[e];t&&t.head.run(n)},isRegistered:function(e,n){return n._eev&&n._eev[e]},register:function(e,n){var t=this.insertLinkInList(e,n);this.insertLinkInFn(e,t,n)},insertLinkInList:function(e,t){var i=this.events[e]||(this.events[e]=new n);return i.insert(t)},insertLinkInFn:function(e,n,t){var i=t._eev||(t._eev={});i[e]=n},unregister:function(e,n){var t=this.removeLinkFromFn(e,n);this.removeLinkFromList(e,t)},removeLinkFromFn:function(e,n){var t=n._eev[e];return n._eev[e]=void 0,t},removeLinkFromList:function(e,n){var t=this.events[e];t&&t.remove(n)}},n.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)},e}();!function(e,n){var t=e.define;t&&t.amd?t([],n):"undefined"!=typeof module&&module.exports&&(module.exports=n())}(this,function(){return Eev}); | ||
//# sourceMappingURL=eev.min.js.map |
{ | ||
"name": "eev", | ||
"version": "0.0.1", | ||
"version": "0.0.3", | ||
"description": "A tiny, fast, zero-dependency event emitter", | ||
@@ -26,3 +26,10 @@ "main": "eev.js", | ||
}, | ||
"homepage": "https://github.com/chrisdavies/eev" | ||
"homepage": "https://github.com/chrisdavies/eev", | ||
"scripts": { | ||
"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' && uglifyjs eev.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/3), zero-dependency event emitter for JavaScript. | ||
- Roughly 600 bytes minified + zipped | ||
- Fast... see [jsperf](http://jsperf.com/jsevents/3) | ||
- Zero dependencies | ||
- Simple | ||
[![Build Status](https://travis-ci.org/chrisdavies/eev.svg?branch=master)](https://travis-ci.org/chrisdavies/eev) | ||
## Usage | ||
@@ -6,0 +13,0 @@ |
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
15738
12
294
68
1
1