RN: Implements EventSubscription

Summary:
Changes the to-be-legacy `_EventSubscription` and `_EmitterSubscription` classes to implement the event-agnostic `EventSubscription` interface.

This interface is valuable because it abstracts away the event definitions.

Changelog:
[Internal]

Reviewed By: lunaleaps

Differential Revision: D26155911

fbshipit-source-id: f94280976d4f9219f4780ac8fae0d5fbc1865386
This commit is contained in:
Tim Yung
2021-02-01 17:47:09 -08:00
committed by Facebook GitHub Bot
parent 70cd569e7e
commit 41b6884a1b
4 changed files with 32 additions and 11 deletions
+3 -3
View File
@@ -12,8 +12,8 @@
const EventEmitter = require('./_EventEmitter');
import type {EventSubscription} from './EventSubscription';
export default EventEmitter;
export interface EventSubscription {
remove(): void;
}
export type {EventSubscription};
+19
View File
@@ -0,0 +1,19 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict
* @format
*/
'use strict';
// This exists as a separate file only to avoid circular dependencies from
// using this in `_EmitterSubscription`. Combine this back into `EventEmitter`
// after migration and cleanup is done.
export interface EventSubscription {
remove(): void;
}
+6 -6
View File
@@ -11,16 +11,16 @@
'use strict';
import type EventEmitter from './EventEmitter';
import EventSubscription from './_EventSubscription';
import _EventSubscription from './_EventSubscription';
import type EventSubscriptionVendor from './_EventSubscriptionVendor';
import {type EventSubscription} from './EventSubscription';
/**
* EmitterSubscription represents a subscription with listener and context data.
*/
class EmitterSubscription<
EventDefinitions: {...},
K: $Keys<EventDefinitions>,
> extends EventSubscription<EventDefinitions, K> {
class EmitterSubscription<EventDefinitions: {...}, K: $Keys<EventDefinitions>>
extends _EventSubscription<EventDefinitions, K>
implements EventSubscription {
emitter: EventEmitter<EventDefinitions>;
listener: ?(...$ElementType<EventDefinitions, K>) => mixed;
context: ?$FlowFixMe;
@@ -49,7 +49,7 @@ class EmitterSubscription<
/**
* Removes this subscription from the emitter that registered it.
* Note: we're overriding the `remove()` method of EventSubscription here
* 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.
*/
+4 -2
View File
@@ -10,13 +10,15 @@
'use strict';
import {type EventSubscription} from './EventSubscription';
import type EventSubscriptionVendor from './_EventSubscriptionVendor';
/**
* EventSubscription represents a subscription to a particular event. It can
* remove its own subscription.
*/
class EventSubscription<EventDefinitions: {...}, K: $Keys<EventDefinitions>> {
class _EventSubscription<EventDefinitions: {...}, K: $Keys<EventDefinitions>>
implements EventSubscription {
eventType: K;
key: number;
subscriber: EventSubscriptionVendor<EventDefinitions>;
@@ -39,4 +41,4 @@ class EventSubscription<EventDefinitions: {...}, K: $Keys<EventDefinitions>> {
}
}
module.exports = EventSubscription;
module.exports = _EventSubscription;