-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
125 additions
and
171 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,3 +35,4 @@ jspm_packages | |
|
||
# Optional REPL history | ||
.node_repl_history | ||
.*.swp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,83 +0,0 @@ | ||
'use strict'; | ||
|
||
(function () { | ||
var events = {}; | ||
|
||
function Event(type) { | ||
this.type = type; | ||
this.timeStamp = new Date(); | ||
} | ||
|
||
Event.on = function (type, handler) { | ||
if (events.hasOwnProperty(type)) { | ||
events[type].push(handler); | ||
} else { | ||
events[type] = [handler]; | ||
} | ||
return Event; | ||
}; | ||
|
||
Event.off = function (type, handler) { | ||
if (arguments.length === 0) { | ||
return _offAll(); | ||
} | ||
if (handler === undefined) { | ||
return _offByType(type); | ||
} | ||
return _offByHandler(type, handler); | ||
}; | ||
|
||
Event.trigger = function (event, args) { | ||
if (!(event instanceof Event)) { | ||
event = new Event(event); | ||
} | ||
return _dispatch(event, args); | ||
}; | ||
|
||
function _dispatch(event, args) { | ||
if (!events.hasOwnProperty(event.type)) return; | ||
args = args || []; | ||
args.unshift(event); | ||
|
||
var handlers = events[event.type] || []; | ||
handlers.forEach(function (handler) { | ||
return handler.apply(null, args); | ||
}); | ||
return Event; | ||
} | ||
|
||
function _offByHandler(type, handler) { | ||
if (!events.hasOwnProperty(type)) return; | ||
var i = events[type].indexOf(handler); | ||
if (i > -1) { | ||
events[type].splice(i, 1); | ||
} | ||
return Event; | ||
} | ||
|
||
function _offByType(type) { | ||
if (events.hasOwnProperty(type)) { | ||
delete events[type]; | ||
} | ||
return Event; | ||
} | ||
|
||
function _offAll() { | ||
events = {}; | ||
return Event; | ||
} | ||
|
||
// CommonJS | ||
if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') { | ||
module.exports = Event; | ||
} | ||
// Browser | ||
else if (typeof define === 'function' && define.amd) { | ||
define('MicroEvent', [], function () { | ||
return Event; | ||
}); | ||
} else { | ||
window.MicroEvent = Event; | ||
} | ||
})(); | ||
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,79 +1,95 @@ | ||
(function() { | ||
var events = {}; | ||
function Emitter() { | ||
var e = Object.create(emitter); | ||
e.events = {}; | ||
return e; | ||
} | ||
|
||
function Event(type) { | ||
this.type = type; | ||
this.timeStamp = new Date(); | ||
} | ||
|
||
Event.on = function(type, handler) { | ||
if (events.hasOwnProperty(type)) { | ||
events[type].push(handler); | ||
var emitter = {}; | ||
|
||
emitter.on = function(type, handler) { | ||
if (this.events.hasOwnProperty(type)) { | ||
this.events[type].push(handler); | ||
} else { | ||
events[type] = [handler]; | ||
this.events[type] = [handler]; | ||
} | ||
return Event; | ||
return this; | ||
}; | ||
|
||
Event.off = function(type, handler) { | ||
emitter.off = function(type, handler) { | ||
if (arguments.length === 0) { | ||
return _offAll(); | ||
return this._offAll(); | ||
} | ||
if (handler === undefined) { | ||
return _offByType(type); | ||
return this._offByType(type); | ||
} | ||
return _offByHandler(type, handler); | ||
return this._offByHandler(type, handler); | ||
}; | ||
|
||
Event.trigger = function(event, args) { | ||
emitter.trigger = function(event, args) { | ||
if (!(event instanceof Event)) { | ||
event = new Event(event); | ||
} | ||
return _dispatch(event, args); | ||
return this._dispatch(event, args); | ||
}; | ||
|
||
function _dispatch(event, args) { | ||
if (!events.hasOwnProperty(event.type)) return; | ||
emitter._dispatch = function(event, args) { | ||
if (!this.events.hasOwnProperty(event.type)) return; | ||
args = args || []; | ||
args.unshift(event); | ||
|
||
var handlers = events[event.type] || []; | ||
var handlers = this.events[event.type] || []; | ||
handlers.forEach(handler => handler.apply(null, args)); | ||
return Event; | ||
} | ||
return this; | ||
}; | ||
|
||
function _offByHandler(type, handler) { | ||
if (!events.hasOwnProperty(type)) return; | ||
var i = events[type].indexOf(handler); | ||
emitter._offByHandler = function(type, handler) { | ||
if (!this.events.hasOwnProperty(type)) return; | ||
var i = this.events[type].indexOf(handler); | ||
if (i > -1) { | ||
events[type].splice(i, 1); | ||
this.events[type].splice(i, 1); | ||
} | ||
return Event; | ||
} | ||
return this; | ||
}; | ||
|
||
function _offByType(type) { | ||
if (events.hasOwnProperty(type)) { | ||
delete events[type]; | ||
emitter._offByType = function(type) { | ||
if (this.events.hasOwnProperty(type)) { | ||
delete this.events[type]; | ||
} | ||
return Event; | ||
} | ||
return this; | ||
}; | ||
|
||
function _offAll() { | ||
events = {}; | ||
return Event; | ||
} | ||
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 = Event; | ||
module.exports = Emitter; | ||
} | ||
// Browser | ||
else if (typeof define === 'function' && define.amd) { | ||
define('MicroEvent', [], function() { | ||
return Event; | ||
define('Emitter', [], function() { | ||
return Emitter; | ||
}); | ||
} else { | ||
window.MicroEvent = Event; | ||
window.Emitter = Emitter; | ||
} | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.