Fix EventEmitter to match that expected by Intercom

This commit is contained in:
David Humphrey (:humph) david.humphrey@senecacollege.ca 2014-03-12 16:49:20 -04:00
parent b825772fdd
commit 4a0ffb5f5a
7 changed files with 81 additions and 12 deletions

View File

@ -2,9 +2,6 @@
"name": "filer",
"version": "0.0.4",
"main": "dist/filer.js",
"dependencies": {
"eventemitter2": "~0.4.13"
},
"devDependencies": {
"mocha": "1.17.1",
"chai": "1.9.0"

View File

@ -47,8 +47,7 @@ module.exports = function(grunt) {
options: {
paths: {
"src": "../src",
"build": "../build",
"EventEmitter": "../bower_components/eventemitter2/lib/eventemitter2"
"build": "../build"
},
baseUrl: "lib",
name: "build/almond",

74
lib/eventemitter.js Normal file
View File

@ -0,0 +1,74 @@
define(function(require) {
// Based on https://github.com/diy/intercom.js/blob/master/lib/events.js
// Copyright 2012 DIY Co Apache License, Version 2.0
// http://www.apache.org/licenses/LICENSE-2.0
function removeItem(item, array) {
for (var i = array.length - 1; i >= 0; i--) {
if (array[i] === item) {
array.splice(i, 1);
}
}
return array;
}
var EventEmitter = function() {};
EventEmitter.createInterface = function(space) {
var methods = {};
methods.on = function(name, fn) {
if (typeof this[space] === 'undefined') {
this[space] = {};
}
if (!this[space].hasOwnProperty(name)) {
this[space][name] = [];
}
this[space][name].push(fn);
};
methods.off = function(name, fn) {
if (typeof this[space] === 'undefined') return;
if (this[space].hasOwnProperty(name)) {
removeItem(fn, this[space][name]);
}
};
methods.trigger = function(name) {
if (typeof this[space] !== 'undefined' && this[space].hasOwnProperty(name)) {
var args = Array.prototype.slice.call(arguments, 1);
for (var i = 0; i < this[space][name].length; i++) {
this[space][name][i].apply(this[space][name][i], args);
}
}
};
methods.removeAllListeners = function(name) {
if (typeof this[space] === 'undefined') return;
var self = this;
self[space][name].forEach(function(fn) {
self.off(name, fn);
});
};
return methods;
};
var pvt = EventEmitter.createInterface('_handlers');
EventEmitter.prototype._on = pvt.on;
EventEmitter.prototype._off = pvt.off;
EventEmitter.prototype._trigger = pvt.trigger;
var pub = EventEmitter.createInterface('handlers');
EventEmitter.prototype.on = function() {
pub.on.apply(this, arguments);
Array.prototype.unshift.call(arguments, 'on');
this._trigger.apply(this, arguments);
};
EventEmitter.prototype.off = pub.off;
EventEmitter.prototype.trigger = pub.trigger;
EventEmitter.prototype.removeAllListeners = pub.removeAllListeners;
return EventEmitter;
});

View File

@ -4,7 +4,7 @@ define(function(require) {
// Copyright 2012 DIY Co Apache License, Version 2.0
// http://www.apache.org/licenses/LICENSE-2.0
var EventEmitter = require('EventEmitter');
var EventEmitter = require('eventemitter');
var guid = require('src/shared').guid;
function throttle(delay, fn) {

View File

@ -1671,7 +1671,7 @@ define(function(require) {
}
var intercom = Intercom.getInstance();
changes.forEach(function(change) {
intercom.emit(change.event, change.event, change.path);
intercom.emit(change.event, change.path);
});
}

View File

@ -1,6 +1,6 @@
define(function(require) {
var EventEmitter = require('EventEmitter');
var EventEmitter = require('eventemitter');
var isNullPath = require('src/path').isNull;
var Intercom = require('intercom');
@ -14,10 +14,10 @@ define(function(require) {
var recursive = false;
var filename;
function onchange(event, path) {
function onchange(path) {
// Watch for exact filename, or parent path when recursive is true
if(filename === path || (recursive && path.indexOf(filename + '/') === 0)) {
self.emit('change', 'change', path);
self.trigger('change', 'change', path);
}
}

View File

@ -58,8 +58,7 @@ var config = (function() {
"spec": "../tests/spec",
"bugs": "../tests/bugs",
"util": "../tests/lib/test-utils",
"Filer": "../src/index",
"EventEmitter": "../bower_components/eventemitter2/lib/eventemitter2"
"Filer": "../src/index"
},
baseUrl: "../lib",
optimize: "none",