TVEventHandler.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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 Platform = require('../../Utilities/Platform');
  12. const NativeEventEmitter = require('../../EventEmitter/NativeEventEmitter');
  13. import NativeTVNavigationEventEmitter from './NativeTVNavigationEventEmitter';
  14. import type EmitterSubscription from '../../vendor/emitter/EmitterSubscription';
  15. class TVEventHandler {
  16. __nativeTVNavigationEventListener: ?EmitterSubscription = null;
  17. __nativeTVNavigationEventEmitter: ?NativeEventEmitter = null;
  18. enable(component: ?any, callback: Function): void {
  19. if (Platform.OS === 'ios' && !NativeTVNavigationEventEmitter) {
  20. return;
  21. }
  22. this.__nativeTVNavigationEventEmitter = new NativeEventEmitter(
  23. NativeTVNavigationEventEmitter,
  24. );
  25. this.__nativeTVNavigationEventListener = this.__nativeTVNavigationEventEmitter.addListener(
  26. 'onHWKeyEvent',
  27. data => {
  28. if (callback) {
  29. callback(component, data);
  30. }
  31. },
  32. );
  33. }
  34. disable(): void {
  35. if (this.__nativeTVNavigationEventListener) {
  36. this.__nativeTVNavigationEventListener.remove();
  37. delete this.__nativeTVNavigationEventListener;
  38. }
  39. if (this.__nativeTVNavigationEventEmitter) {
  40. delete this.__nativeTVNavigationEventEmitter;
  41. }
  42. }
  43. }
  44. module.exports = TVEventHandler;