Tone.js/Tone/core/Emitter.js

118 lines
3.2 KiB
JavaScript
Raw Normal View History

2015-08-17 00:30:41 +00:00
define(["Tone/core/Tone"], function (Tone) {
2015-10-21 16:11:19 +00:00
"use strict";
2015-08-17 00:30:41 +00:00
/**
2015-10-27 21:40:52 +00:00
* @class Tone.Emitter gives classes which extend it
* the ability to listen for and emit events.
2015-08-17 18:57:54 +00:00
* Inspiration and reference from Jerome Etienne's [MicroEvent](https://github.com/jeromeetienne/microevent.js).
* MIT (c) 2011 Jerome Etienne.
2015-08-17 00:30:41 +00:00
*
* @extends {Tone}
*/
2015-10-27 21:40:52 +00:00
Tone.Emitter = function(){
2015-08-17 00:30:41 +00:00
/**
2015-08-17 18:57:54 +00:00
* Contains all of the events.
2015-08-17 00:30:41 +00:00
* @private
* @type {Object}
*/
this._events = {};
};
2015-10-27 21:40:52 +00:00
Tone.extend(Tone.Emitter);
2015-08-17 00:30:41 +00:00
2015-08-17 18:57:54 +00:00
/**
* Bind a callback to a specific event.
* @param {String} event The name of the event to listen for.
* @param {Function} callback The callback to invoke when the
* event is emitted
2015-10-27 21:40:52 +00:00
* @return {Tone.Emitter} this
2015-08-17 18:57:54 +00:00
*/
2015-10-27 21:40:52 +00:00
Tone.Emitter.prototype.on = function(event, callback){
2015-08-17 00:30:41 +00:00
//split the event
var events = event.split(/\W+/);
for (var i = 0; i < events.length; i++){
var eventName = events[i];
if (!this._events.hasOwnProperty(eventName)){
this._events[eventName] = [];
}
this._events[eventName].push(callback);
}
return this;
};
2015-08-17 18:57:54 +00:00
/**
* Remove the event listener.
* @param {String} event The event to stop listening to.
* @param {Function=} callback The callback which was bound to
2015-10-27 21:40:52 +00:00
* the event with Tone.Emitter.on.
* If no callback is given, all callbacks
* events are removed.
2015-10-27 21:40:52 +00:00
* @return {Tone.Emitter} this
2015-08-17 18:57:54 +00:00
*/
2015-10-27 21:40:52 +00:00
Tone.Emitter.prototype.off = function(event, callback){
2015-08-31 19:16:05 +00:00
var events = event.split(/\W+/);
for (var ev = 0; ev < events.length; ev++){
event = events[ev];
2015-08-17 18:57:54 +00:00
if (this._events.hasOwnProperty(event)){
if (Tone.prototype.isUndef(callback)){
this._events[event] = [];
} else {
var eventList = this._events[event];
for (var i = 0; i < eventList.length; i++){
if (eventList[i] === callback){
eventList.splice(i, 1);
}
2015-08-17 18:57:54 +00:00
}
2015-08-17 00:30:41 +00:00
}
}
}
return this;
};
2015-08-17 18:57:54 +00:00
/**
* Invoke all of the callbacks bound to the event
* with any arguments passed in.
* @param {String} event The name of the event.
* @param {*...} args The arguments to pass to the functions listening.
2015-10-27 21:40:52 +00:00
* @return {Tone.Emitter} this
2015-08-17 18:57:54 +00:00
*/
Tone.Emitter.prototype.emit = function(event){
2015-08-17 18:57:54 +00:00
if (this._events){
var args = Array.prototype.slice.call(arguments, 1);
if (this._events.hasOwnProperty(event)){
var eventList = this._events[event];
for (var i = 0, len = eventList.length; i < len; i++){
eventList[i].apply(this, args);
}
2015-08-17 00:30:41 +00:00
}
}
return this;
};
2015-08-17 18:57:54 +00:00
/**
* Add Emitter functions (on/off/emit) to the object
2015-08-17 18:57:54 +00:00
* @param {Object|Function} object The object or class to extend.
*/
2015-10-27 21:40:52 +00:00
Tone.Emitter.mixin = function(object){
var functions = ["on", "off", "emit"];
2015-08-31 19:16:05 +00:00
object._events = {};
2015-08-17 18:57:54 +00:00
for (var i = 0; i < functions.length; i++){
var func = functions[i];
2015-10-27 21:40:52 +00:00
var emitterFunc = Tone.Emitter.prototype[func];
2015-08-31 19:16:05 +00:00
object[func] = emitterFunc;
2015-08-17 18:57:54 +00:00
}
};
/**
* Clean up
2015-10-27 21:40:52 +00:00
* @return {Tone.Emitter} this
2015-08-17 18:57:54 +00:00
*/
2015-10-27 21:40:52 +00:00
Tone.Emitter.prototype.dispose = function(){
2015-08-17 00:30:41 +00:00
Tone.prototype.dispose.call(this);
this._events = null;
return this;
};
2015-10-27 21:40:52 +00:00
return Tone.Emitter;
2015-08-17 00:30:41 +00:00
});