Skip to content

Commit

Permalink
create uncompressed dist
Browse files Browse the repository at this point in the history
  • Loading branch information
harttle committed Oct 9, 2016
1 parent c709228 commit 825b6ef
Showing 1 changed file with 100 additions and 0 deletions.
100 changes: 100 additions & 0 deletions dist/micro-event.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
'use strict';

(function () {
function Emitter() {
var e = Object.create(emitter);
e.events = {};
return e;
}

function Event(type) {
this.type = type;
this.timeStamp = new Date();
}

var emitter = {};

emitter.on = function (type, handler) {
if (this.events.hasOwnProperty(type)) {
this.events[type].push(handler);
} else {
this.events[type] = [handler];
}
return this;
};

emitter.off = function (type, handler) {
if (arguments.length === 0) {
return this._offAll();
}
if (handler === undefined) {
return this._offByType(type);
}
return this._offByHandler(type, handler);
};

emitter.trigger = function (event, args) {
if (!(event instanceof Event)) {
event = new Event(event);
}
return this._dispatch(event, args);
};

emitter._dispatch = function (event, args) {
if (!this.events.hasOwnProperty(event.type)) return;
args = args || [];
args.unshift(event);

var handlers = this.events[event.type] || [];
handlers.forEach(function (handler) {
return handler.apply(null, args);
});
return this;
};

emitter._offByHandler = function (type, handler) {
if (!this.events.hasOwnProperty(type)) return;
var i = this.events[type].indexOf(handler);
if (i > -1) {
this.events[type].splice(i, 1);
}
return this;
};

emitter._offByType = function (type) {
if (this.events.hasOwnProperty(type)) {
delete this.events[type];
}
return this;
};

emitter._offAll = function () {
this.events = {};
return this;
};

Emitter.Event = Event;

Emitter.mixin = function (obj, arr) {
var emitter = new Emitter();
arr.map(function (name) {
obj[name] = function () {
return emitter[name].apply(emitter, arguments);
};
});
};

// CommonJS
if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
module.exports = Emitter;
}
// Browser
else if (typeof define === 'function' && define.amd) {
define('Emitter', [], function () {
return Emitter;
});
} else {
window.Emitter = Emitter;
}
})();

0 comments on commit 825b6ef

Please sign in to comment.