[Pkg-javascript-commits] [node-component-emitter] 01/02: Imported Upstream version 1.1.2
Leo Iannacone
l3on-guest at moszumanska.debian.org
Mon Jun 2 11:14:19 UTC 2014
This is an automated email from the git hooks/post-receive script.
l3on-guest pushed a commit to branch master
in repository node-component-emitter.
commit 70ed2057266f3dad58e7c14bd666e29f8663a434
Author: Leo Iannacone <l3on at ubuntu.com>
Date: Mon Jun 2 12:30:15 2014 +0200
Imported Upstream version 1.1.2
---
.npmignore | 2 +
.travis.yml | 4 ++
History.md | 52 +++++++++++++++
Makefile | 7 ++
Readme.md | 74 +++++++++++++++++++++
bower.json | 21 ++++++
component.json | 14 ++++
index.js | 164 +++++++++++++++++++++++++++++++++++++++++++++++
package.json | 22 +++++++
test/emitter.js | 195 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
10 files changed, 555 insertions(+)
diff --git a/.npmignore b/.npmignore
new file mode 100644
index 0000000..f05b1f2
--- /dev/null
+++ b/.npmignore
@@ -0,0 +1,2 @@
+node_modules
+test
diff --git a/.travis.yml b/.travis.yml
new file mode 100644
index 0000000..8750e3b
--- /dev/null
+++ b/.travis.yml
@@ -0,0 +1,4 @@
+node_js:
+- "0.8"
+- "0.10"
+language: node_js
\ No newline at end of file
diff --git a/History.md b/History.md
new file mode 100644
index 0000000..b898ca6
--- /dev/null
+++ b/History.md
@@ -0,0 +1,52 @@
+
+1.1.2 / 2014-02-10
+==================
+
+ * package: rename to "component-emitter"
+ * package: update "main" and "component" fields
+ * Add license to Readme (same format as the other components)
+ * created .npmignore
+ * travis stuff
+
+1.1.1 / 2013-12-01
+==================
+
+ * fix .once adding .on to the listener
+ * docs: Emitter#off()
+ * component: add `.repo` prop
+
+1.1.0 / 2013-10-20
+==================
+
+ * add `.addEventListener()` and `.removeEventListener()` aliases
+
+1.0.1 / 2013-06-27
+==================
+
+ * add support for legacy ie
+
+1.0.0 / 2013-02-26
+==================
+
+ * add `.off()` support for removing all listeners
+
+0.0.6 / 2012-10-08
+==================
+
+ * add `this._callbacks` initialization to prevent funky gotcha
+
+0.0.5 / 2012-09-07
+==================
+
+ * fix `Emitter.call(this)` usage
+
+0.0.3 / 2012-07-11
+==================
+
+ * add `.listeners()`
+ * rename `.has()` to `.hasListeners()`
+
+0.0.2 / 2012-06-28
+==================
+
+ * fix `.off()` with `.once()`-registered callbacks
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..4e9c8d3
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,7 @@
+
+test:
+ @./node_modules/.bin/mocha \
+ --require should \
+ --reporter spec
+
+.PHONY: test
\ No newline at end of file
diff --git a/Readme.md b/Readme.md
new file mode 100644
index 0000000..0f3f9b9
--- /dev/null
+++ b/Readme.md
@@ -0,0 +1,74 @@
+# Emitter [![Build Status](https://travis-ci.org/component/emitter.png)](https://travis-ci.org/component/emitter)
+
+ Event emitter component.
+
+## Installation
+
+```
+$ component install component/emitter
+```
+
+## API
+
+### Emitter(obj)
+
+ The `Emitter` may also be used as a mixin. For example
+ a "plain" object may become an emitter, or you may
+ extend an existing prototype.
+
+ As an `Emitter` instance:
+
+```js
+var Emitter = require('emitter');
+var emitter = new Emitter;
+emitter.emit('something');
+```
+
+ As a mixin:
+
+```js
+var Emitter = require('emitter');
+var user = { name: 'tobi' };
+Emitter(user);
+
+user.emit('im a user');
+```
+
+ As a prototype mixin:
+
+```js
+var Emitter = require('emitter');
+Emitter(User.prototype);
+```
+
+### Emitter#on(event, fn)
+
+ Register an `event` handler `fn`.
+
+### Emitter#once(event, fn)
+
+ Register a single-shot `event` handler `fn`,
+ removed immediately after it is invoked the
+ first time.
+
+### Emitter#off(event, fn)
+
+ * Pass `event` and `fn` to remove a listener.
+ * Pass `event` to remove all listeners on that event.
+ * Pass nothing to remove all listeners on all events.
+
+### Emitter#emit(event, ...)
+
+ Emit an `event` with variable option args.
+
+### Emitter#listeners(event)
+
+ Return an array of callbacks, or an empty array.
+
+### Emitter#hasListeners(event)
+
+ Check if this emitter has `event` handlers.
+
+## License
+
+MIT
diff --git a/bower.json b/bower.json
new file mode 100644
index 0000000..c618d41
--- /dev/null
+++ b/bower.json
@@ -0,0 +1,21 @@
+{
+ "name": "emitter",
+ "description": "Event emitter",
+ "keywords": [
+ "emitter",
+ "events"
+ ],
+ "version": "1.1.2",
+ "license": "MIT",
+ "main": "index.js",
+ "homepage": "https://github.com/component/emitter",
+ "ignore": [
+ "**/.*",
+ "node_modules",
+ "bower_components",
+ "test",
+ "Makefile",
+ "package.json",
+ "component.json"
+ ]
+}
diff --git a/component.json b/component.json
new file mode 100644
index 0000000..68ba0b1
--- /dev/null
+++ b/component.json
@@ -0,0 +1,14 @@
+{
+ "name": "emitter",
+ "repo": "component/emitter",
+ "description": "Event emitter",
+ "keywords": [
+ "emitter",
+ "events"
+ ],
+ "version": "1.1.2",
+ "scripts": [
+ "index.js"
+ ],
+ "license": "MIT"
+}
diff --git a/index.js b/index.js
new file mode 100644
index 0000000..ad71163
--- /dev/null
+++ b/index.js
@@ -0,0 +1,164 @@
+
+/**
+ * Expose `Emitter`.
+ */
+
+module.exports = Emitter;
+
+/**
+ * Initialize a new `Emitter`.
+ *
+ * @api public
+ */
+
+function Emitter(obj) {
+ if (obj) return mixin(obj);
+};
+
+/**
+ * Mixin the emitter properties.
+ *
+ * @param {Object} obj
+ * @return {Object}
+ * @api private
+ */
+
+function mixin(obj) {
+ for (var key in Emitter.prototype) {
+ obj[key] = Emitter.prototype[key];
+ }
+ return obj;
+}
+
+/**
+ * Listen on the given `event` with `fn`.
+ *
+ * @param {String} event
+ * @param {Function} fn
+ * @return {Emitter}
+ * @api public
+ */
+
+Emitter.prototype.on =
+Emitter.prototype.addEventListener = function(event, fn){
+ this._callbacks = this._callbacks || {};
+ (this._callbacks[event] = this._callbacks[event] || [])
+ .push(fn);
+ return this;
+};
+
+/**
+ * Adds an `event` listener that will be invoked a single
+ * time then automatically removed.
+ *
+ * @param {String} event
+ * @param {Function} fn
+ * @return {Emitter}
+ * @api public
+ */
+
+Emitter.prototype.once = function(event, fn){
+ var self = this;
+ this._callbacks = this._callbacks || {};
+
+ function on() {
+ self.off(event, on);
+ fn.apply(this, arguments);
+ }
+
+ on.fn = fn;
+ this.on(event, on);
+ return this;
+};
+
+/**
+ * Remove the given callback for `event` or all
+ * registered callbacks.
+ *
+ * @param {String} event
+ * @param {Function} fn
+ * @return {Emitter}
+ * @api public
+ */
+
+Emitter.prototype.off =
+Emitter.prototype.removeListener =
+Emitter.prototype.removeAllListeners =
+Emitter.prototype.removeEventListener = function(event, fn){
+ this._callbacks = this._callbacks || {};
+
+ // all
+ if (0 == arguments.length) {
+ this._callbacks = {};
+ return this;
+ }
+
+ // specific event
+ var callbacks = this._callbacks[event];
+ if (!callbacks) return this;
+
+ // remove all handlers
+ if (1 == arguments.length) {
+ delete this._callbacks[event];
+ return this;
+ }
+
+ // remove specific handler
+ var cb;
+ for (var i = 0; i < callbacks.length; i++) {
+ cb = callbacks[i];
+ if (cb === fn || cb.fn === fn) {
+ callbacks.splice(i, 1);
+ break;
+ }
+ }
+ return this;
+};
+
+/**
+ * Emit `event` with the given args.
+ *
+ * @param {String} event
+ * @param {Mixed} ...
+ * @return {Emitter}
+ */
+
+Emitter.prototype.emit = function(event){
+ this._callbacks = this._callbacks || {};
+ var args = [].slice.call(arguments, 1)
+ , callbacks = this._callbacks[event];
+
+ if (callbacks) {
+ callbacks = callbacks.slice(0);
+ for (var i = 0, len = callbacks.length; i < len; ++i) {
+ callbacks[i].apply(this, args);
+ }
+ }
+
+ return this;
+};
+
+/**
+ * Return array of callbacks for `event`.
+ *
+ * @param {String} event
+ * @return {Array}
+ * @api public
+ */
+
+Emitter.prototype.listeners = function(event){
+ this._callbacks = this._callbacks || {};
+ return this._callbacks[event] || [];
+};
+
+/**
+ * Check if this emitter has `event` handlers.
+ *
+ * @param {String} event
+ * @return {Boolean}
+ * @api public
+ */
+
+Emitter.prototype.hasListeners = function(event){
+ return !! this.listeners(event).length;
+};
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..4f096ab
--- /dev/null
+++ b/package.json
@@ -0,0 +1,22 @@
+{
+ "name": "component-emitter",
+ "description": "Event emitter",
+ "version": "1.1.2",
+ "devDependencies": {
+ "mocha": "*",
+ "should": "*"
+ },
+ "component": {
+ "scripts": {
+ "emitter/index.js": "index.js"
+ }
+ },
+ "main": "index.js",
+ "repository": {
+ "type": "git",
+ "url": "https://github.com/component/emitter.git"
+ },
+ "scripts": {
+ "test": "make test"
+ }
+}
diff --git a/test/emitter.js b/test/emitter.js
new file mode 100644
index 0000000..91ea483
--- /dev/null
+++ b/test/emitter.js
@@ -0,0 +1,195 @@
+
+var Emitter = require('..');
+
+function Custom() {
+ Emitter.call(this)
+}
+
+Custom.prototype.__proto__ = Emitter.prototype;
+
+describe('Custom', function(){
+ describe('with Emitter.call(this)', function(){
+ it('should work', function(done){
+ var emitter = new Custom;
+ emitter.on('foo', done);
+ emitter.emit('foo');
+ })
+ })
+})
+
+describe('Emitter', function(){
+ describe('.on(event, fn)', function(){
+ it('should add listeners', function(){
+ var emitter = new Emitter;
+ var calls = [];
+
+ emitter.on('foo', function(val){
+ calls.push('one', val);
+ });
+
+ emitter.on('foo', function(val){
+ calls.push('two', val);
+ });
+
+ emitter.emit('foo', 1);
+ emitter.emit('bar', 1);
+ emitter.emit('foo', 2);
+
+ calls.should.eql([ 'one', 1, 'two', 1, 'one', 2, 'two', 2 ]);
+ })
+ })
+
+ describe('.once(event, fn)', function(){
+ it('should add a single-shot listener', function(){
+ var emitter = new Emitter;
+ var calls = [];
+
+ emitter.once('foo', function(val){
+ calls.push('one', val);
+ });
+
+ emitter.emit('foo', 1);
+ emitter.emit('foo', 2);
+ emitter.emit('foo', 3);
+ emitter.emit('bar', 1);
+
+ calls.should.eql([ 'one', 1 ]);
+ })
+ })
+
+ describe('.off(event, fn)', function(){
+ it('should remove a listener', function(){
+ var emitter = new Emitter;
+ var calls = [];
+
+ function one() { calls.push('one'); }
+ function two() { calls.push('two'); }
+
+ emitter.on('foo', one);
+ emitter.on('foo', two);
+ emitter.off('foo', two);
+
+ emitter.emit('foo');
+
+ calls.should.eql([ 'one' ]);
+ })
+
+ it('should work with .once()', function(){
+ var emitter = new Emitter;
+ var calls = [];
+
+ function one() { calls.push('one'); }
+
+ emitter.once('foo', one);
+ emitter.once('fee', one);
+ emitter.off('foo', one);
+
+ emitter.emit('foo');
+
+ calls.should.eql([]);
+ })
+
+ it('should work when called from an event', function(){
+ var emitter = new Emitter
+ , called
+ function b () {
+ called = true;
+ }
+ emitter.on('tobi', function () {
+ emitter.off('tobi', b);
+ });
+ emitter.on('tobi', b);
+ emitter.emit('tobi');
+ called.should.be.true;
+ called = false;
+ emitter.emit('tobi');
+ called.should.be.false;
+ });
+ })
+
+ describe('.off(event)', function(){
+ it('should remove all listeners for an event', function(){
+ var emitter = new Emitter;
+ var calls = [];
+
+ function one() { calls.push('one'); }
+ function two() { calls.push('two'); }
+
+ emitter.on('foo', one);
+ emitter.on('foo', two);
+ emitter.off('foo');
+
+ emitter.emit('foo');
+ emitter.emit('foo');
+
+ calls.should.eql([]);
+ })
+ })
+
+ describe('.off()', function(){
+ it('should remove all listeners', function(){
+ var emitter = new Emitter;
+ var calls = [];
+
+ function one() { calls.push('one'); }
+ function two() { calls.push('two'); }
+
+ emitter.on('foo', one);
+ emitter.on('bar', two);
+
+ emitter.emit('foo');
+ emitter.emit('bar');
+
+ emitter.off();
+
+ emitter.emit('foo');
+ emitter.emit('bar');
+
+ calls.should.eql(['one', 'two']);
+ })
+ })
+
+ describe('.listeners(event)', function(){
+ describe('when handlers are present', function(){
+ it('should return an array of callbacks', function(){
+ var emitter = new Emitter;
+ function foo(){}
+ emitter.on('foo', foo);
+ emitter.listeners('foo').should.eql([foo]);
+ })
+ })
+
+ describe('when no handlers are present', function(){
+ it('should return an empty array', function(){
+ var emitter = new Emitter;
+ emitter.listeners('foo').should.eql([]);
+ })
+ })
+ })
+
+ describe('.hasListeners(event)', function(){
+ describe('when handlers are present', function(){
+ it('should return true', function(){
+ var emitter = new Emitter;
+ emitter.on('foo', function(){});
+ emitter.hasListeners('foo').should.be.true;
+ })
+ })
+
+ describe('when no handlers are present', function(){
+ it('should return false', function(){
+ var emitter = new Emitter;
+ emitter.hasListeners('foo').should.be.false;
+ })
+ })
+ })
+})
+
+describe('Emitter(obj)', function(){
+ it('should mixin', function(done){
+ var proto = {};
+ Emitter(proto);
+ proto.on('something', done);
+ proto.emit('something');
+ })
+})
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-component-emitter.git
More information about the Pkg-javascript-commits
mailing list