react-native-tvos
Version:
A framework for building native apps using React
51 lines (44 loc) • 1.48 kB
JavaScript
/**
* 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.
*
* @format
* @flow
*/
;
import type {TVRemoteEvent} from '../../Types/CoreEventTypes';
import NativeTVNavigationEventEmitter from '../../../src/private/specs_DEPRECATED/modules/NativeTVNavigationEventEmitter';
import NativeEventEmitter from '../../EventEmitter/NativeEventEmitter';
import Platform from '../../Utilities/Platform';
import {type EventSubscription} from '../../vendor/emitter/EventEmitter';
let __nativeTVNavigationEventEmitter: ?NativeEventEmitter<TVRemoteEvent> = null;
const TVEventHandler: {
addListener: (
callback: (event: any) => void,
) => EventSubscription | {remove: () => void},
} = {
addListener: callback => {
if (Platform.OS === 'ios' && !NativeTVNavigationEventEmitter) {
return {remove: () => {}};
}
if (!__nativeTVNavigationEventEmitter) {
__nativeTVNavigationEventEmitter = new NativeEventEmitter<TVRemoteEvent>(
NativeTVNavigationEventEmitter,
);
}
const subscription: EventSubscription =
__nativeTVNavigationEventEmitter.addListener(
// $FlowFixMe[prop-missing]
'onHWKeyEvent',
(data: any) => {
if (callback) {
callback(data);
}
},
);
return subscription;
},
};
module.exports = TVEventHandler;