Skip to content

Commit

Permalink
EventEmitter: Delete once() and removeCurrentListener()
Browse files Browse the repository at this point in the history
Summary:
In an effort to simplify and clean up the `EventEmitter` abstractions in React Native, this removes `once()` and `removeCurrentListener()`. Across the Facebook codebase, there were only two callers of `once()` and no callers of `removeCurrentListener()`.

The same behavior can be achieved using:

```
const subscription = emitter.addListener('event', () => {
  subscription.remove();
  // ...
});
```

Changelog:
[General][Removed] - Removed `once()` and `removeCurrentListener()` fom `DeviceEventEmitter` and `NativeEventEmitter`.

Reviewed By: cpojer

Differential Revision: D22196474

fbshipit-source-id: 06ced186fd812e91d5c57f6580e647c100505807
  • Loading branch information
yungsters authored and facebook-github-bot committed Jun 27, 2020
1 parent e75557b commit 87a2e29
Showing 1 changed file with 0 additions and 53 deletions.
53 changes: 0 additions & 53 deletions Libraries/vendor/emitter/_EventEmitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ const sparseFilterPredicate = () => true;
*/
class EventEmitter {
_subscriber: EventSubscriptionVendor;
_currentSubscription: ?EmitterSubscription;

/**
* @constructor
Expand Down Expand Up @@ -70,27 +69,6 @@ class EventEmitter {
): any);
}

/**
* 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
*/
once(
eventType: string,
listener: Function,
context: ?Object,
): EmitterSubscription {
return this.addListener(eventType, (...args) => {
this.removeCurrentListener();
listener.apply(context, args);
});
}

/**
* Removes all of the registered listeners, including those registered as
* listener maps.
Expand All @@ -102,35 +80,6 @@ class EventEmitter {
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
*/
removeCurrentListener() {
invariant(
!!this._currentSubscription,
'Not in an emitting cycle; there is no current subscription',
);
this.removeSubscription(this._currentSubscription);
}

/**
* Removes a specific subscription. Called by the `remove()` method of the
* subscription itself to ensure any necessary cleanup is performed.
Expand Down Expand Up @@ -185,14 +134,12 @@ class EventEmitter {

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

Expand Down

0 comments on commit 87a2e29

Please sign in to comment.