Skip to content

Commit 87a2e29

Browse files
yungstersfacebook-github-bot
authored andcommitted
EventEmitter: Delete once() and removeCurrentListener()
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
1 parent e75557b commit 87a2e29

File tree

1 file changed

+0
-53
lines changed

1 file changed

+0
-53
lines changed

Libraries/vendor/emitter/_EventEmitter.js

-53
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ const sparseFilterPredicate = () => true;
3333
*/
3434
class EventEmitter {
3535
_subscriber: EventSubscriptionVendor;
36-
_currentSubscription: ?EmitterSubscription;
3736

3837
/**
3938
* @constructor
@@ -70,27 +69,6 @@ class EventEmitter {
7069
): any);
7170
}
7271

73-
/**
74-
* Similar to addListener, except that the listener is removed after it is
75-
* invoked once.
76-
*
77-
* @param {string} eventType - Name of the event to listen to
78-
* @param {function} listener - Function to invoke only once when the
79-
* specified event is emitted
80-
* @param {*} context - Optional context object to use when invoking the
81-
* listener
82-
*/
83-
once(
84-
eventType: string,
85-
listener: Function,
86-
context: ?Object,
87-
): EmitterSubscription {
88-
return this.addListener(eventType, (...args) => {
89-
this.removeCurrentListener();
90-
listener.apply(context, args);
91-
});
92-
}
93-
9472
/**
9573
* Removes all of the registered listeners, including those registered as
9674
* listener maps.
@@ -102,35 +80,6 @@ class EventEmitter {
10280
this._subscriber.removeAllSubscriptions(eventType);
10381
}
10482

105-
/**
106-
* Provides an API that can be called during an eventing cycle to remove the
107-
* last listener that was invoked. This allows a developer to provide an event
108-
* object that can remove the listener (or listener map) during the
109-
* invocation.
110-
*
111-
* If it is called when not inside of an emitting cycle it will throw.
112-
*
113-
* @throws {Error} When called not during an eventing cycle
114-
*
115-
* @example
116-
* var subscription = emitter.addListenerMap({
117-
* someEvent: function(data, event) {
118-
* console.log(data);
119-
* emitter.removeCurrentListener();
120-
* }
121-
* });
122-
*
123-
* emitter.emit('someEvent', 'abc'); // logs 'abc'
124-
* emitter.emit('someEvent', 'def'); // does not log anything
125-
*/
126-
removeCurrentListener() {
127-
invariant(
128-
!!this._currentSubscription,
129-
'Not in an emitting cycle; there is no current subscription',
130-
);
131-
this.removeSubscription(this._currentSubscription);
132-
}
133-
13483
/**
13584
* Removes a specific subscription. Called by the `remove()` method of the
13685
* subscription itself to ensure any necessary cleanup is performed.
@@ -185,14 +134,12 @@ class EventEmitter {
185134

186135
// The subscription may have been removed during this event loop.
187136
if (subscription && subscription.listener) {
188-
this._currentSubscription = subscription;
189137
subscription.listener.apply(
190138
subscription.context,
191139
Array.prototype.slice.call(arguments, 1),
192140
);
193141
}
194142
}
195-
this._currentSubscription = null;
196143
}
197144
}
198145

0 commit comments

Comments
 (0)