Skip to content

Commit

Permalink
fix: instantiate remotehandler only for tv platforms
Browse files Browse the repository at this point in the history
  • Loading branch information
aurimasmi committed Jun 27, 2023
1 parent 34ef59e commit de4113f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 19 deletions.
13 changes: 7 additions & 6 deletions packages/create/src/remoteHandler/index.android.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DeviceEventEmitter, EmitterSubscription } from 'react-native';
import { DeviceEventEmitter, EmitterSubscription, Platform } from 'react-native';
import { useEffect, useCallback } from 'react';
import throttle from 'lodash.throttle';

Expand Down Expand Up @@ -60,16 +60,17 @@ function useTVRemoteHandler(callback: RemoteHandlerCallbackAndroid) {
const handler = useCallback(throttle(callback, 100), []);

useEffect(() => {
const listener: EmitterSubscription = DeviceEventEmitter.addListener(
EVENT_NAME,
({ eventKeyAction, eventType }) => {
let listener: EmitterSubscription;

if (Platform.isTV) {
listener = DeviceEventEmitter.addListener(EVENT_NAME, ({ eventKeyAction, eventType }) => {
return handler({
eventType,
eventKeyAction,
velocity: 0,
});
}
);
});
}

return () => {
if (listener) listener.remove();
Expand Down
29 changes: 16 additions & 13 deletions packages/create/src/remoteHandler/index.ios.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useEffect, useCallback } from 'react';
import { NativeModules, NativeEventEmitter, EmitterSubscription } from 'react-native';
import { NativeModules, NativeEventEmitter, EmitterSubscription, Platform } from 'react-native';
import throttle from 'lodash.throttle';

const EVENT_NAME = 'onTVRemoteKey';
Expand Down Expand Up @@ -54,18 +54,21 @@ const useTVRemoteHandler = (callback: RemoteHandlerCallbackAppleTV) => {
const cb = useCallback(throttle(callback, 100), []);

useEffect(() => {
const { TvRemoteHandler } = NativeModules;
const eventEmitter = new NativeEventEmitter(TvRemoteHandler);
const listener: EmitterSubscription = eventEmitter.addListener(
EVENT_NAME,
(eventData: {
eventType: RemoteHandlerEventTypesAppleTV;
eventKeyAction: RemoteHandlerEventKeyActions;
velocity: number;
}) => {
cb(eventData);
}
);
let listener: EmitterSubscription;
if (Platform.isTV) {
const { TvRemoteHandler } = NativeModules;
const eventEmitter = new NativeEventEmitter(TvRemoteHandler);
listener = eventEmitter.addListener(
EVENT_NAME,
(eventData: {
eventType: RemoteHandlerEventTypesAppleTV;
eventKeyAction: RemoteHandlerEventKeyActions;
velocity: number;
}) => {
cb(eventData);
}
);
}

return () => {
if (listener) {
Expand Down

0 comments on commit de4113f

Please sign in to comment.