Permalink
Browse files

initial commit

  • Loading branch information...
0 parents commit d0760a2c822058b2ec37bd8adec3435ff6838d5c @jgallen23 committed Sep 25, 2012
Showing with 248 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. +66 −0 index.js
  3. +22 −0 package.json
  4. +1 −0 test/mocha.opts
  5. +158 −0 test/vents.test.js
@@ -0,0 +1 @@
+node_modules
@@ -0,0 +1,66 @@
+
+var Vents = function(obj) {
+ if (obj) {
+ obj._handlers = {};
+ for (var key in Vents.prototype) {
+ obj[key] = Vents.prototype[key];
+ }
+ return obj;
+ }
+ this._handlers = {};
+}
+
+Vents.prototype.on = function(event, fn) {
+ if (!this._handlers[event]) {
+ this._handlers[event] = [];
+ }
+
+ this._handlers[event].push(fn);
+
+ return fn;
+
+}
+
+Vents.prototype.emit = function(event, data) {
+ if (!this.hasHandlers(event)) {
+ return;
+ }
+
+ for (var i = 0, c = this._handlers[event].length; i < c; i++) {
+ var fn = this._handlers[event][i];
+ fn(data);
+ }
+}
+
+Vents.prototype.off = function(event, handler) {
+ if (!this.hasHandlers(event)) {
+ return;
+ }
+ if (arguments.length == 1) {
+ delete this._handlers[event];
+ return;
+ }
+
+ var index = this._handlers[event].indexOf(handler);
+ if (~index) {
+ this._handlers[event].splice(index, 1);
+ }
+
+}
+
+Vents.prototype.once = function(event, handler) {
+ var self = this;
+ var onceHandler = this.on(event, function(data) {
+ handler(data);
+ self.off(event, onceHandler);
+ });
+}
+
+Vents.prototype.hasHandlers = function(event) {
+ return !! this._handlers[event];
+
+}
+
+if (typeof window === "undefined") {
+ module.exports = Vents;
+}
@@ -0,0 +1,22 @@
+{
+ "name": "vents",
+ "description": "an event lib for node and browser",
+ "homepage": "https://github.com/jgallen23/vents",
+ "author": "Greg Allen <@jgaui> (http://jga.me)",
+ "version": "0.0.1",
+ "repository": {
+ "type": "git",
+ "url": "https://github.com/jgallen23/vents.git"
+ },
+ "dependencies": {
+ },
+ "devDependencies": {
+ "mocha": "*"
+ },
+ "main": "index.js",
+ "scripts": {
+ "test": "mocha"
+ },
+ "bin": {
+ }
+}
@@ -0,0 +1 @@
+--ui tdd
@@ -0,0 +1,158 @@
+var assert = require('assert');
+var Vents = require('../');
+
+suite('vents', function() {
+
+ suite('#on', function() {
+
+ test('should add listeners', function() {
+
+ var vents = new Vents();
+ var calls = 0;
+ vents.on('test', function() {
+ calls++;
+ });
+ vents.on('test', function() {
+ calls++;
+ });
+
+ vents.emit('test');
+ assert.equal(calls, 2);
+
+ });
+
+ test('should pass data from emit', function() {
+
+ var vents = new Vents();
+ var data;
+
+ vents.on('test', function(arg) {
+ data = arg;
+ });
+
+ vents.emit('test', 123);
+
+ assert.equal(data, 123);
+
+ });
+
+ test('should return handler', function() {
+ var vents = new Vents();
+
+ var handler = vents.on('test', function() {
+
+ });
+
+ assert.equal(typeof handler, 'function');
+ });
+
+ });
+
+ suite('#emit', function() {
+ test('no errors if emitting an event that isn\'t bound', function() {
+
+ var vent = new Vents();
+ vent.emit('blah');
+
+ });
+ });
+
+ suite('#off', function() {
+
+ test('should remove listener', function() {
+ var calls = 0;
+ var vents = new Vents();
+
+ var handler = vents.on('test', function() {
+ calls++;
+ });
+
+ vents.emit('test');
+ vents.off('test', handler);
+ vents.emit('test');
+ assert.equal(calls, 1);
+ });
+
+ test('if no handler passed, remove all listeners', function() {
+ var calls = 0;
+ var vents = new Vents();
+
+ var handler = vents.on('test', function() {
+ calls++;
+ });
+ var handler2 = vents.on('test', function() {
+ calls++;
+ });
+
+ vents.emit('test');
+ vents.off('test');
+ vents.emit('test');
+ assert.equal(calls, 2);
+
+ });
+
+ });
+
+ suite('#once', function() {
+
+ test('should only fire event once', function() {
+
+ var calls = 0;
+ var vents = new Vents();
+
+ vents.once('test', function() {
+ calls++;
+ });
+ vents.emit('test');
+ vents.emit('test');
+
+ assert.equal(calls, 1);
+
+ });
+
+ });
+
+ suite('#hasHandlers', function() {
+
+ test('should return true if something is bound', function() {
+
+ var vents = new Vents();
+ vents.on('test', function() {});
+
+ assert.ok(vents.hasHandlers('test'));
+ });
+
+ test('should return false if nothing is bound', function() {
+ var vents = new Vents();
+
+ assert.equal(vents.hasHandlers('test'), false);
+ });
+
+ });
+
+
+ suite('mixin', function(done) {
+ test('should add to object', function(done) {
+ var obj = {};
+ Vents(obj);
+ assert.equal(typeof obj.on, 'function');
+ assert.equal(typeof obj.off, 'function');
+ obj.on('test', done);
+ obj.emit('test');
+ });
+
+ test('should add to prototype', function(done) {
+ var Class = function() {
+ Vents.call(this);
+ }
+
+ Vents(Class.prototype);
+ var cls = new Class();
+ assert.equal(typeof cls.on, 'function');
+ assert.equal(typeof cls.off, 'function');
+ cls.on('test', done);
+ cls.emit('test');
+ });
+ });
+
+});

0 comments on commit d0760a2

Please sign in to comment.