yeet
This commit is contained in:
191
node_modules/fbemitter/lib/BaseEventEmitter.js
generated
vendored
Normal file
191
node_modules/fbemitter/lib/BaseEventEmitter.js
generated
vendored
Normal file
@ -0,0 +1,191 @@
|
||||
/**
|
||||
* Copyright (c) 2014-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.
|
||||
*
|
||||
* @providesModule BaseEventEmitter
|
||||
* @typechecks
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
|
||||
|
||||
var EmitterSubscription = require('./EmitterSubscription');
|
||||
var EventSubscriptionVendor = require('./EventSubscriptionVendor');
|
||||
|
||||
var emptyFunction = require('fbjs/lib/emptyFunction');
|
||||
var invariant = require('fbjs/lib/invariant');
|
||||
|
||||
/**
|
||||
* @class BaseEventEmitter
|
||||
* @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 BaseEventEmitter = (function () {
|
||||
/**
|
||||
* @constructor
|
||||
*/
|
||||
|
||||
function BaseEventEmitter() {
|
||||
_classCallCheck(this, BaseEventEmitter);
|
||||
|
||||
this._subscriber = new EventSubscriptionVendor();
|
||||
this._currentSubscription = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
|
||||
BaseEventEmitter.prototype.addListener = function addListener(eventType, listener, context) {
|
||||
return this._subscriber.addSubscription(eventType, new EmitterSubscription(this._subscriber, listener, context));
|
||||
};
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
|
||||
BaseEventEmitter.prototype.once = function once(eventType, listener, context) {
|
||||
var emitter = this;
|
||||
return this.addListener(eventType, function () {
|
||||
emitter.removeCurrentListener();
|
||||
listener.apply(context, arguments);
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
|
||||
BaseEventEmitter.prototype.removeAllListeners = function removeAllListeners(eventType) {
|
||||
this._subscriber.removeAllSubscriptions(eventType);
|
||||
};
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
|
||||
BaseEventEmitter.prototype.removeCurrentListener = function removeCurrentListener() {
|
||||
!!!this._currentSubscription ? process.env.NODE_ENV !== 'production' ? invariant(false, 'Not in an emitting cycle; there is no current subscription') : invariant(false) : undefined;
|
||||
this._subscriber.removeSubscription(this._currentSubscription);
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns an array of listeners that are currently registered for the given
|
||||
* event.
|
||||
*
|
||||
* @param {string} eventType - Name of the event to query
|
||||
* @return {array}
|
||||
*/
|
||||
|
||||
BaseEventEmitter.prototype.listeners = function listeners(eventType) /* TODO: Array<EventSubscription> */{
|
||||
var subscriptions = this._subscriber.getSubscriptionsForType(eventType);
|
||||
return subscriptions ? subscriptions.filter(emptyFunction.thatReturnsTrue).map(function (subscription) {
|
||||
return subscription.listener;
|
||||
}) : [];
|
||||
};
|
||||
|
||||
/**
|
||||
* 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'
|
||||
*/
|
||||
|
||||
BaseEventEmitter.prototype.emit = function emit(eventType) {
|
||||
var subscriptions = this._subscriber.getSubscriptionsForType(eventType);
|
||||
if (subscriptions) {
|
||||
var keys = Object.keys(subscriptions);
|
||||
for (var ii = 0; ii < keys.length; ii++) {
|
||||
var key = keys[ii];
|
||||
var subscription = subscriptions[key];
|
||||
// The subscription may have been removed during this event loop.
|
||||
if (subscription) {
|
||||
this._currentSubscription = subscription;
|
||||
this.__emitToSubscription.apply(this, [subscription].concat(Array.prototype.slice.call(arguments)));
|
||||
}
|
||||
}
|
||||
this._currentSubscription = null;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Provides a hook to override how the emitter emits an event to a specific
|
||||
* subscription. This allows you to set up logging and error boundaries
|
||||
* specific to your environment.
|
||||
*
|
||||
* @param {EmitterSubscription} subscription
|
||||
* @param {string} eventType
|
||||
* @param {*} Arbitrary arguments to be passed to each registered listener
|
||||
*/
|
||||
|
||||
BaseEventEmitter.prototype.__emitToSubscription = function __emitToSubscription(subscription, eventType) {
|
||||
var args = Array.prototype.slice.call(arguments, 2);
|
||||
subscription.listener.apply(subscription.context, args);
|
||||
};
|
||||
|
||||
return BaseEventEmitter;
|
||||
})();
|
||||
|
||||
module.exports = BaseEventEmitter;
|
48
node_modules/fbemitter/lib/EmitterSubscription.js
generated
vendored
Normal file
48
node_modules/fbemitter/lib/EmitterSubscription.js
generated
vendored
Normal file
@ -0,0 +1,48 @@
|
||||
/**
|
||||
* Copyright (c) 2014-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.
|
||||
*
|
||||
* @providesModule EmitterSubscription
|
||||
* @typechecks
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
|
||||
|
||||
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; }
|
||||
|
||||
var EventSubscription = require('./EventSubscription');
|
||||
|
||||
/**
|
||||
* EmitterSubscription represents a subscription with listener and context data.
|
||||
*/
|
||||
|
||||
var EmitterSubscription = (function (_EventSubscription) {
|
||||
_inherits(EmitterSubscription, _EventSubscription);
|
||||
|
||||
/**
|
||||
* @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(subscriber, listener, context) {
|
||||
_classCallCheck(this, EmitterSubscription);
|
||||
|
||||
_EventSubscription.call(this, subscriber);
|
||||
this.listener = listener;
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
return EmitterSubscription;
|
||||
})(EventSubscription);
|
||||
|
||||
module.exports = EmitterSubscription;
|
49
node_modules/fbemitter/lib/EventSubscription.js
generated
vendored
Normal file
49
node_modules/fbemitter/lib/EventSubscription.js
generated
vendored
Normal file
@ -0,0 +1,49 @@
|
||||
/**
|
||||
* Copyright (c) 2014-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.
|
||||
*
|
||||
* @providesModule EventSubscription
|
||||
* @typechecks
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* EventSubscription represents a subscription to a particular event. It can
|
||||
* remove its own subscription.
|
||||
*/
|
||||
|
||||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
|
||||
|
||||
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.
|
||||
*/
|
||||
|
||||
EventSubscription.prototype.remove = function remove() {
|
||||
if (this.subscriber) {
|
||||
this.subscriber.removeSubscription(this);
|
||||
this.subscriber = null;
|
||||
}
|
||||
};
|
||||
|
||||
return EventSubscription;
|
||||
})();
|
||||
|
||||
module.exports = EventSubscription;
|
103
node_modules/fbemitter/lib/EventSubscriptionVendor.js
generated
vendored
Normal file
103
node_modules/fbemitter/lib/EventSubscriptionVendor.js
generated
vendored
Normal file
@ -0,0 +1,103 @@
|
||||
/**
|
||||
* Copyright (c) 2014-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.
|
||||
*
|
||||
* @providesModule EventSubscriptionVendor
|
||||
* @typechecks
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
|
||||
|
||||
var invariant = require('fbjs/lib/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
|
||||
*/
|
||||
|
||||
EventSubscriptionVendor.prototype.addSubscription = function addSubscription(eventType, subscription) {
|
||||
!(subscription.subscriber === this) ? process.env.NODE_ENV !== 'production' ? invariant(false, 'The subscriber of the subscription is incorrectly set.') : invariant(false) : undefined;
|
||||
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;
|
||||
};
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
|
||||
EventSubscriptionVendor.prototype.removeAllSubscriptions = function removeAllSubscriptions(eventType) {
|
||||
if (eventType === undefined) {
|
||||
this._subscriptionsForType = {};
|
||||
} else {
|
||||
delete this._subscriptionsForType[eventType];
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Removes a specific subscription. Instead of calling this function, call
|
||||
* `subscription.remove()` directly.
|
||||
*
|
||||
* @param {object} subscription
|
||||
*/
|
||||
|
||||
EventSubscriptionVendor.prototype.removeSubscription = function removeSubscription(subscription) {
|
||||
var eventType = subscription.eventType;
|
||||
var key = subscription.key;
|
||||
|
||||
var subscriptionsForType = this._subscriptionsForType[eventType];
|
||||
if (subscriptionsForType) {
|
||||
delete subscriptionsForType[key];
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* 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
|
||||
* @return {?array}
|
||||
*/
|
||||
|
||||
EventSubscriptionVendor.prototype.getSubscriptionsForType = function getSubscriptionsForType(eventType) {
|
||||
return this._subscriptionsForType[eventType];
|
||||
};
|
||||
|
||||
return EventSubscriptionVendor;
|
||||
})();
|
||||
|
||||
module.exports = EventSubscriptionVendor;
|
Reference in New Issue
Block a user