EmitterSubscription.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. 'use strict';
  11. const EventSubscription = require('./EventSubscription');
  12. import type EventEmitter from './EventEmitter';
  13. import type EventSubscriptionVendor from './EventSubscriptionVendor';
  14. /**
  15. * EmitterSubscription represents a subscription with listener and context data.
  16. */
  17. class EmitterSubscription extends EventSubscription {
  18. emitter: EventEmitter;
  19. listener: Function;
  20. context: ?Object;
  21. /**
  22. * @param {EventEmitter} emitter - The event emitter that registered this
  23. * subscription
  24. * @param {EventSubscriptionVendor} subscriber - The subscriber that controls
  25. * this subscription
  26. * @param {function} listener - Function to invoke when the specified event is
  27. * emitted
  28. * @param {*} context - Optional context object to use when invoking the
  29. * listener
  30. */
  31. constructor(
  32. emitter: EventEmitter,
  33. subscriber: EventSubscriptionVendor,
  34. listener: Function,
  35. context: ?Object,
  36. ) {
  37. super(subscriber);
  38. this.emitter = emitter;
  39. this.listener = listener;
  40. this.context = context;
  41. }
  42. /**
  43. * Removes this subscription from the emitter that registered it.
  44. * Note: we're overriding the `remove()` method of EventSubscription here
  45. * but deliberately not calling `super.remove()` as the responsibility
  46. * for removing the subscription lies with the EventEmitter.
  47. */
  48. remove() {
  49. this.emitter.removeSubscription(this);
  50. }
  51. }
  52. module.exports = EmitterSubscription;