Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,4 @@ node_modules
.npm

# Optional REPL history
.node_repl_history

/build
.node_repl_history
45 changes: 45 additions & 0 deletions build/Libraries/EventEmitter/EmitterSubscription.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
var _createClass=function(){function defineProperties(target,props){for(var i=0;i<props.length;i++){var descriptor=props[i];descriptor.enumerable=descriptor.enumerable||false;descriptor.configurable=true;if("value"in descriptor)descriptor.writable=true;Object.defineProperty(target,descriptor.key,descriptor);}}return function(Constructor,protoProps,staticProps){if(protoProps)defineProperties(Constructor.prototype,protoProps);if(staticProps)defineProperties(Constructor,staticProps);return Constructor;};}();function _classCallCheck(instance,Constructor){if(!(instance instanceof Constructor)){throw new TypeError("Cannot call a class as a function");}}function _possibleConstructorReturn(self,call){if(!self){throw new ReferenceError("this hasn't been initialised - super() hasn't been called");}return call&&(typeof call==="object"||typeof call==="function")?call:self;}function _inherits(subClass,superClass){if(typeof superClass!=="function"&&superClass!==null){throw new TypeError("Super expression must either be null or a function, not "+typeof superClass);}subClass.prototype=Object.create(superClass&&superClass.prototype,{constructor:{value:subClass,enumerable:false,writable:true,configurable:true}});if(superClass)Object.setPrototypeOf?Object.setPrototypeOf(subClass,superClass):subClass.__proto__=superClass;}/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/

var EventSubscription=require('./EventSubscription');

/**
* EmitterSubscription represents a subscription with listener and context data.
*/var
EmitterSubscription=function(_EventSubscription){_inherits(EmitterSubscription,_EventSubscription);

/**
* @param {EventEmitter} emitter - The event emitter that registered this
* subscription
* @param {EventSubscriptionVendor} subscriber - The subscriber that controls
* this subscription
* @param {function} listener - Function to invoke when the specified event is
* emitted
* @param {*} context - Optional context object to use when invoking the
* listener
*/
function EmitterSubscription(emitter,subscriber,listener,context){_classCallCheck(this,EmitterSubscription);var _this=_possibleConstructorReturn(this,(EmitterSubscription.__proto__||Object.getPrototypeOf(EmitterSubscription)).call(this,
subscriber));
_this.emitter=emitter;
_this.listener=listener;
_this.context=context;return _this;
}

/**
* Removes this subscription from the emitter that registered it.
* Note: we're overriding the `remove()` method of EventSubscription here
* but deliberately not calling `super.remove()` as the responsibility
* for removing the subscription lies with the EventEmitter.
*/_createClass(EmitterSubscription,[{key:'remove',value:function(){function remove()
{
this.emitter.removeSubscription(this);
}return remove;}()}]);return EmitterSubscription;}(EventSubscription);


module.exports=EmitterSubscription;
202 changes: 202 additions & 0 deletions build/Libraries/EventEmitter/EventEmitter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
var _createClass=function(){function defineProperties(target,props){for(var i=0;i<props.length;i++){var descriptor=props[i];descriptor.enumerable=descriptor.enumerable||false;descriptor.configurable=true;if("value"in descriptor)descriptor.writable=true;Object.defineProperty(target,descriptor.key,descriptor);}}return function(Constructor,protoProps,staticProps){if(protoProps)defineProperties(Constructor.prototype,protoProps);if(staticProps)defineProperties(Constructor,staticProps);return Constructor;};}();function _classCallCheck(instance,Constructor){if(!(instance instanceof Constructor)){throw new TypeError("Cannot call a class as a function");}}/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/

var EmitterSubscription=require('./EmitterSubscription');
var EventSubscriptionVendor=require('./EventSubscriptionVendor');
var invariant=require('invariant');

/**
* @class EventEmitter
* @description
* An EventEmitter is responsible for managing a set of listeners and publishing
* events to them when it is told that such events happened. In addition to the
* data for the given event it also sends a event control object which allows
* the listeners/handlers to prevent the default behavior of the given event.
*
* The emitter is designed to be generic enough to support all the different
* contexts in which one might want to emit events. It is a simple multicast
* mechanism on top of which extra functionality can be composed. For example, a
* more advanced emitter may use an EventHolder and EventFactory.
*/var
EventEmitter=function(){
/**
* @constructor
*
* @param {EventSubscriptionVendor} subscriber - Optional subscriber instance
* to use. If omitted, a new subscriber will be created for the emitter.
*/
function EventEmitter(subscriber){_classCallCheck(this,EventEmitter);
this._subscriber=subscriber||new EventSubscriptionVendor();
}

/**
* Adds a listener to be invoked when events of the specified type are
* emitted. An optional calling context may be provided. The data arguments
* emitted will be passed to the listener function.
*
* TODO: Annotate the listener arg's type. This is tricky because listeners
* can be invoked with varargs.
*
* @param {string} eventType - Name of the event to listen to
* @param {function} listener - Function to invoke when the specified event is
* emitted
* @param {*} context - Optional context object to use when invoking the
* listener
*/_createClass(EventEmitter,[{key:'addListener',value:function(){function addListener(
eventType,listener,context){
return this._subscriber.addSubscription(
eventType,
new EmitterSubscription(this,this._subscriber,listener,context));

}return addListener;}()

/**
* Similar to addListener, except that the listener is removed after it is
* invoked once.
*
* @param {string} eventType - Name of the event to listen to
* @param {function} listener - Function to invoke only once when the
* specified event is emitted
* @param {*} context - Optional context object to use when invoking the
* listener
*/},{key:'once',value:function(){function once(
eventType,listener,context){var _this=this;
return this.addListener(eventType,function(){for(var _len=arguments.length,args=Array(_len),_key=0;_key<_len;_key++){args[_key]=arguments[_key];}
_this.removeCurrentListener();
listener.apply(context,args);
});
}return once;}()

/**
* Removes all of the registered listeners, including those registered as
* listener maps.
*
* @param {?string} eventType - Optional name of the event whose registered
* listeners to remove
*/},{key:'removeAllListeners',value:function(){function removeAllListeners(
eventType){
this._subscriber.removeAllSubscriptions(eventType);
}return removeAllListeners;}()

/**
* Provides an API that can be called during an eventing cycle to remove the
* last listener that was invoked. This allows a developer to provide an event
* object that can remove the listener (or listener map) during the
* invocation.
*
* If it is called when not inside of an emitting cycle it will throw.
*
* @throws {Error} When called not during an eventing cycle
*
* @example
* var subscription = emitter.addListenerMap({
* someEvent: function(data, event) {
* console.log(data);
* emitter.removeCurrentListener();
* }
* });
*
* emitter.emit('someEvent', 'abc'); // logs 'abc'
* emitter.emit('someEvent', 'def'); // does not log anything
*/},{key:'removeCurrentListener',value:function(){function removeCurrentListener()
{
invariant(
!!this._currentSubscription,
'Not in an emitting cycle; there is no current subscription');

this.removeSubscription(this._currentSubscription);
}return removeCurrentListener;}()

/**
* Removes a specific subscription. Called by the `remove()` method of the
* subscription itself to ensure any necessary cleanup is performed.
*/},{key:'removeSubscription',value:function(){function removeSubscription(
subscription){
invariant(
subscription.emitter===this,
'Subscription does not belong to this emitter.');

this._subscriber.removeSubscription(subscription);
}return removeSubscription;}()

/**
* Returns an array of listeners that are currently registered for the given
* event.
*
* @param {string} eventType - Name of the event to query
* @returns {array}
*/},{key:'listeners',value:function(){function listeners(
eventType){
var subscriptions=this._subscriber.getSubscriptionsForType(eventType);
return subscriptions?subscriptions.map(function(subscription){return subscription.listener;}):[];
}return listeners;}()

/**
* Emits an event of the given type with the given data. All handlers of that
* particular type will be notified.
*
* @param {string} eventType - Name of the event to emit
* @param {...*} Arbitrary arguments to be passed to each registered listener
*
* @example
* emitter.addListener('someEvent', function(message) {
* console.log(message);
* });
*
* emitter.emit('someEvent', 'abc'); // logs 'abc'
*/},{key:'emit',value:function(){function emit(
eventType){
var subscriptions=this._subscriber.getSubscriptionsForType(eventType);
if(subscriptions){
for(var i=0,l=subscriptions.length;i<l;i++){
var subscription=subscriptions[i];

// The subscription may have been removed during this event loop.
if(subscription){
this._currentSubscription=subscription;
subscription.listener.apply(
subscription.context,
Array.prototype.slice.call(arguments,1));

}
}
this._currentSubscription=null;
}
}return emit;}()

/**
* Removes the given listener for event of specific type.
*
* @param {string} eventType - Name of the event to emit
* @param {function} listener - Function to invoke when the specified event is
* emitted
*
* @example
* emitter.removeListener('someEvent', function(message) {
* console.log(message);
* }); // removes the listener if already registered
*
*/},{key:'removeListener',value:function(){function removeListener(
eventType,listener){
var subscriptions=this._subscriber.getSubscriptionsForType(eventType);
if(subscriptions){
for(var i=0,l=subscriptions.length;i<l;i++){
var subscription=subscriptions[i];

// The subscription may have been removed during this event loop.
// its listener matches the listener in method parameters
if(subscription&&subscription.listener===listener){
subscription.remove();
}
}
}
}return removeListener;}()}]);return EventEmitter;}();


module.exports=EventEmitter;
31 changes: 31 additions & 0 deletions build/Libraries/EventEmitter/EventSubscription.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
var _createClass=function(){function defineProperties(target,props){for(var i=0;i<props.length;i++){var descriptor=props[i];descriptor.enumerable=descriptor.enumerable||false;descriptor.configurable=true;if("value"in descriptor)descriptor.writable=true;Object.defineProperty(target,descriptor.key,descriptor);}}return function(Constructor,protoProps,staticProps){if(protoProps)defineProperties(Constructor.prototype,protoProps);if(staticProps)defineProperties(Constructor,staticProps);return Constructor;};}();function _classCallCheck(instance,Constructor){if(!(instance instanceof Constructor)){throw new TypeError("Cannot call a class as a function");}}/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/

/**
* EventSubscription represents a subscription to a particular event. It can
* remove its own subscription.
*/var
EventSubscription=function(){
/**
* @param {EventSubscriptionVendor} subscriber the subscriber that controls
* this subscription.
*/
function EventSubscription(subscriber){_classCallCheck(this,EventSubscription);
this.subscriber=subscriber;
}

/**
* Removes this subscription from the subscriber that controls it.
*/_createClass(EventSubscription,[{key:"remove",value:function(){function remove()
{
this.subscriber.removeSubscription(this);
}return remove;}()}]);return EventSubscription;}();


module.exports=EventSubscription;
91 changes: 91 additions & 0 deletions build/Libraries/EventEmitter/EventSubscriptionVendor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
var _createClass=function(){function defineProperties(target,props){for(var i=0;i<props.length;i++){var descriptor=props[i];descriptor.enumerable=descriptor.enumerable||false;descriptor.configurable=true;if("value"in descriptor)descriptor.writable=true;Object.defineProperty(target,descriptor.key,descriptor);}}return function(Constructor,protoProps,staticProps){if(protoProps)defineProperties(Constructor.prototype,protoProps);if(staticProps)defineProperties(Constructor,staticProps);return Constructor;};}();function _classCallCheck(instance,Constructor){if(!(instance instanceof Constructor)){throw new TypeError("Cannot call a class as a function");}}/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/

var invariant=require('invariant');

/**
* EventSubscriptionVendor stores a set of EventSubscriptions that are
* subscribed to a particular event type.
*/var
EventSubscriptionVendor=function(){

function EventSubscriptionVendor(){_classCallCheck(this,EventSubscriptionVendor);
this._subscriptionsForType={};
this._currentSubscription=null;
}

/**
* Adds a subscription keyed by an event type.
*
* @param {string} eventType
* @param {EventSubscription} subscription
*/_createClass(EventSubscriptionVendor,[{key:'addSubscription',value:function(){function addSubscription(
eventType,subscription){
/* eslint-disable no-param-reassign */
invariant(
subscription.subscriber===this,
'The subscriber of the subscription is incorrectly set.');
if(!this._subscriptionsForType[eventType]){
this._subscriptionsForType[eventType]=[];
}
var key=this._subscriptionsForType[eventType].length;
this._subscriptionsForType[eventType].push(subscription);
subscription.eventType=eventType;
subscription.key=key;
return subscription;
}return addSubscription;}()

/**
* Removes a bulk set of the subscriptions.
*
* @param {?string} eventType - Optional name of the event type whose
* registered supscriptions to remove, if null remove all subscriptions.
*/},{key:'removeAllSubscriptions',value:function(){function removeAllSubscriptions(
eventType){
if(eventType===undefined){
this._subscriptionsForType={};
}else{
delete this._subscriptionsForType[eventType];
}
}return removeAllSubscriptions;}()

/**
* Removes a specific subscription. Instead of calling this function, call
* `subscription.remove()` directly.
*
* @param {object} subscription
*/},{key:'removeSubscription',value:function(){function removeSubscription(
subscription){
var eventType=subscription.eventType;
var key=subscription.key;

var subscriptionsForType=this._subscriptionsForType[eventType];
if(subscriptionsForType){
delete subscriptionsForType[key];
}
}return removeSubscription;}()

/**
* Returns the array of subscriptions that are currently registered for the
* given event type.
*
* Note: This array can be potentially sparse as subscriptions are deleted
* from it when they are removed.
*
* TODO: This returns a nullable array. wat?
*
* @param {string} eventType
* @returns {?array}
*/},{key:'getSubscriptionsForType',value:function(){function getSubscriptionsForType(
eventType){
return this._subscriptionsForType[eventType];
}return getSubscriptionsForType;}()}]);return EventSubscriptionVendor;}();


module.exports=EventSubscriptionVendor;
Loading