Skip to content

Commit

Permalink
RN: Implements EventSubscription
Browse files Browse the repository at this point in the history
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
  • Loading branch information
yungsters authored and facebook-github-bot committed Feb 2, 2021
1 parent 70cd569 commit 41b6884
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 11 deletions.
6 changes: 3 additions & 3 deletions Libraries/vendor/emitter/EventEmitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 19 additions & 0 deletions Libraries/vendor/emitter/EventSubscription.js
Original file line number Diff line number Diff line change
@@ -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;
}
12 changes: 6 additions & 6 deletions Libraries/vendor/emitter/_EmitterSubscription.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
*/
Expand Down
6 changes: 4 additions & 2 deletions Libraries/vendor/emitter/_EventSubscription.js
Original file line number Diff line number Diff line change
Expand Up @@ -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>;
Expand All @@ -39,4 +41,4 @@ class EventSubscription<EventDefinitions: {...}, K: $Keys<EventDefinitions>> {
}
}

module.exports = EventSubscription;
module.exports = _EventSubscription;

0 comments on commit 41b6884

Please sign in to comment.