Skip to content
This repository has been archived by the owner on Jul 2, 2021. It is now read-only.

Commit

Permalink
Revert "Sync from internal, prep v3 (#29)"
Browse files Browse the repository at this point in the history
This reverts commit 1276815.
  • Loading branch information
yangshun committed Jan 9, 2021
1 parent e916e97 commit 8113825
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 101 deletions.
13 changes: 5 additions & 8 deletions src/internal/EventEmitter.js → index.js
Expand Up @@ -5,14 +5,11 @@
* 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 EventEmitter
* @flow
*/

const BaseEventEmitter = require('BaseEventEmitter');

class EventEmitter<TEvent: string> extends BaseEventEmitter<TEvent> {
}
var fbemitter = {
EventEmitter: require('./lib/BaseEventEmitter'),
EmitterSubscription : require('./lib/EmitterSubscription')
};

module.exports = EventEmitter;
module.exports = fbemitter;
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -13,7 +13,7 @@
"index.js",
"lib/"
],
"main": "lib/EventEmitter.js",
"main": "index.js",
"repository": "facebook/emitter",
"scripts": {
"build": "gulp build",
Expand Down
75 changes: 31 additions & 44 deletions src/BaseEventEmitter.js
Expand Up @@ -7,17 +7,14 @@
* of patent rights can be found in the PATENTS file in the same directory.
*
* @providesModule BaseEventEmitter
* @flow
* @typechecks
*/

const EmitterSubscription = require('EmitterSubscription');
const ErrorUtils = require('ErrorUtils');
const EventSubscriptionVendor = require('EventSubscriptionVendor');
var EmitterSubscription = require('EmitterSubscription');
var EventSubscriptionVendor = require('EventSubscriptionVendor');

const emptyFunction = require('emptyFunction');
const invariant = require('invariant');

import type EventSubscription from 'EventSubscription';
var emptyFunction = require('emptyFunction');
var invariant = require('invariant');

/**
* @class BaseEventEmitter
Expand All @@ -32,14 +29,11 @@ import type EventSubscription from 'EventSubscription';
* mechanism on top of which extra functionality can be composed. For example, a
* more advanced emitter may use an EventHolder and EventFactory.
*/
class BaseEventEmitter<TEvent: string> {
_currentSubscription: ?EmitterSubscription;
_subscriber: EventSubscriptionVendor;

class BaseEventEmitter {
/**
* @constructor
*/
constructor(): void {
constructor() {
this._subscriber = new EventSubscriptionVendor();
this._currentSubscription = null;
}
Expand All @@ -59,10 +53,7 @@ class BaseEventEmitter<TEvent: string> {
* listener
*/
addListener(
eventType: TEvent,
listener: Function,
context: ?Object,
): EmitterSubscription {
eventType: string, listener, context: ?Object): EmitterSubscription {
return this._subscriber.addSubscription(
eventType,
new EmitterSubscription(this._subscriber, listener, context));
Expand All @@ -78,11 +69,7 @@ class BaseEventEmitter<TEvent: string> {
* @param {*} context - Optional context object to use when invoking the
* listener
*/
once(
eventType: TEvent,
listener: Function,
context: ?Object,
): EmitterSubscription {
once(eventType: string, listener, context: ?Object): EmitterSubscription {
var emitter = this;
return this.addListener(eventType, function() {
emitter.removeCurrentListener();
Expand All @@ -97,21 +84,32 @@ class BaseEventEmitter<TEvent: string> {
* @param {?string} eventType - Optional name of the event whose registered
* listeners to remove
*/
removeAllListeners(eventType: ?TEvent): void {
removeAllListeners(eventType: ?String) {
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 during the invocation.
* 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
*/
removeCurrentListener(): void {
removeCurrentListener() {
invariant(
!!this._currentSubscription,
'Not in an emitting cycle; there is no current subscription'
Expand All @@ -126,7 +124,7 @@ class BaseEventEmitter<TEvent: string> {
* @param {string} eventType - Name of the event to query
* @return {array}
*/
listeners(eventType: ?TEvent): Array<EventSubscription> {
listeners(eventType: string): Array /* TODO: Array<EventSubscription> */ {
var subscriptions = this._subscriber.getSubscriptionsForType(eventType);
return subscriptions
? subscriptions.filter(emptyFunction.thatReturnsTrue).map(
Expand All @@ -150,26 +148,20 @@ class BaseEventEmitter<TEvent: string> {
*
* emitter.emit('someEvent', 'abc'); // logs 'abc'
*/
emit(eventType: TEvent): void {
emit(eventType) {
var subscriptions = this._subscriber.getSubscriptionsForType(eventType);
if (subscriptions) {
var keys = Object.keys(subscriptions);
var args;
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;
if (args == null) {
args = [subscription];
for (var i = 0, len = arguments.length; i < len; i++) {
args[i + 1] = arguments[i];
}
} else {
args[0] = subscription;
}
this.__emitToSubscription.apply(this, args);
this.__emitToSubscription.apply(
this,
[subscription].concat(Array.prototype.slice.call(arguments))
);
}
}
this._currentSubscription = null;
Expand All @@ -185,13 +177,8 @@ class BaseEventEmitter<TEvent: string> {
* @param {string} eventType
* @param {*} Arbitrary arguments to be passed to each registered listener
*/
__emitToSubscription(
subscription: EmitterSubscription,
eventType: TEvent,
...args: Array<mixed>
): void {
// Note: internally we use ErrorUtils.applyWithGuard
// We should add some version of that to fbjs but for now we won't guard.
__emitToSubscription(subscription, eventType) {
var args = Array.prototype.slice.call(arguments, 2);
subscription.listener.apply(subscription.context, args);
}
}
Expand Down
16 changes: 4 additions & 12 deletions src/EmitterSubscription.js
Expand Up @@ -5,23 +5,19 @@
* 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
* @flow
* @typechecks
*/

'use strict';

const EventSubscription = require('EventSubscription');

import type EventSubscriptionVendor from 'EventSubscriptionVendor';
var EventSubscription = require('EventSubscription');

/**
* EmitterSubscription represents a subscription with listener and context data.
*/
class EmitterSubscription extends EventSubscription {
context: ?Object;
listener: Function;

/**
* @param {EventSubscriptionVendor} subscriber - The subscriber that controls
Expand All @@ -31,11 +27,7 @@ class EmitterSubscription extends EventSubscription {
* @param {*} context - Optional context object to use when invoking the
* listener
*/
constructor(
subscriber: EventSubscriptionVendor,
listener: Function,
context: ?Object,
): void {
constructor(subscriber: EventSubscriptionVendor, listener, context: ?Object) {
super(subscriber);
this.listener = listener;
this.context = context;
Expand Down
9 changes: 3 additions & 6 deletions src/EventSubscription.js
Expand Up @@ -7,32 +7,29 @@
* of patent rights can be found in the PATENTS file in the same directory.
*
* @providesModule EventSubscription
* @flow
* @typechecks
*/

'use strict';

import type EventSubscriptionVendor from 'EventSubscriptionVendor';

/**
* EventSubscription represents a subscription to a particular event. It can
* remove its own subscription.
*/
class EventSubscription {
subscriber: EventSubscriptionVendor;

/**
* @param {EventSubscriptionVendor} subscriber the subscriber that controls
* this subscription.
*/
constructor(subscriber: EventSubscriptionVendor): void {
constructor(subscriber: EventSubscriptionVendor) {
this.subscriber = subscriber;
}

/**
* Removes this subscription from the subscriber that controls it.
*/
remove(): void {
remove() {
if (this.subscriber) {
this.subscriber.removeSubscription(this);
this.subscriber = null;
Expand Down
14 changes: 7 additions & 7 deletions src/EventSubscriptionVendor.js
Expand Up @@ -5,15 +5,14 @@
* 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
* @noflow
*/

'use strict';

const invariant = require('invariant');
var invariant = require('invariant');

/**
* EventSubscriptionVendor stores a set of EventSubscriptions that are
Expand All @@ -23,6 +22,7 @@ class EventSubscriptionVendor {

constructor() {
this._subscriptionsForType = {};
this._currentSubscription = null;
}

/**
Expand All @@ -39,7 +39,7 @@ class EventSubscriptionVendor {
if (!this._subscriptionsForType[eventType]) {
this._subscriptionsForType[eventType] = [];
}
const key = this._subscriptionsForType[eventType].length;
var key = this._subscriptionsForType[eventType].length;
this._subscriptionsForType[eventType].push(subscription);
subscription.eventType = eventType;
subscription.key = key;
Expand Down Expand Up @@ -67,10 +67,10 @@ class EventSubscriptionVendor {
* @param {object} subscription
*/
removeSubscription(subscription: Object) {
const eventType = subscription.eventType;
const key = subscription.key;
var eventType = subscription.eventType;
var key = subscription.key;

const subscriptionsForType = this._subscriptionsForType[eventType];
var subscriptionsForType = this._subscriptionsForType[eventType];
if (subscriptionsForType) {
delete subscriptionsForType[key];
}
Expand Down

0 comments on commit 8113825

Please sign in to comment.