UNPKG

1.65 kBJavaScriptView Raw
1/**
2 * Copyright (c) Facebook, Inc. and its affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 *
7 * @format
8 * @flow
9 */
10
11'use strict';
12
13const EventSubscription = require('EventSubscription');
14
15import type EventEmitter from 'EventEmitter';
16import type EventSubscriptionVendor from 'EventSubscriptionVendor';
17
18/**
19 * EmitterSubscription represents a subscription with listener and context data.
20 */
21class EmitterSubscription extends EventSubscription {
22 emitter: EventEmitter;
23 listener: Function;
24 context: ?Object;
25
26 /**
27 * @param {EventEmitter} emitter - The event emitter that registered this
28 * subscription
29 * @param {EventSubscriptionVendor} subscriber - The subscriber that controls
30 * this subscription
31 * @param {function} listener - Function to invoke when the specified event is
32 * emitted
33 * @param {*} context - Optional context object to use when invoking the
34 * listener
35 */
36 constructor(
37 emitter: EventEmitter,
38 subscriber: EventSubscriptionVendor,
39 listener: Function,
40 context: ?Object,
41 ) {
42 super(subscriber);
43 this.emitter = emitter;
44 this.listener = listener;
45 this.context = context;
46 }
47
48 /**
49 * Removes this subscription from the emitter that registered it.
50 * Note: we're overriding the `remove()` method of EventSubscription here
51 * but deliberately not calling `super.remove()` as the responsibility
52 * for removing the subscription lies with the EventEmitter.
53 */
54 remove() {
55 this.emitter.removeSubscription(this);
56 }
57}
58
59module.exports = EmitterSubscription;