diff --git a/src/hooks/useTrackPlayerEvents.ts b/src/hooks/useTrackPlayerEvents.ts index 444cef142..e117f3d0e 100644 --- a/src/hooks/useTrackPlayerEvents.ts +++ b/src/hooks/useTrackPlayerEvents.ts @@ -4,9 +4,6 @@ import { addEventListener } from '../trackPlayer'; import { Event } from '../constants'; import type { EventPayloadByEvent } from '../interfaces'; -// Helper that gets the type of the event payload for a given event type -type EventTypeFromArray = T[number]; - /** * Attaches a handler to the given TrackPlayer events and performs cleanup on unmount * @param events - TrackPlayer events to subscribe to @@ -14,11 +11,7 @@ type EventTypeFromArray = T[number]; */ export const useTrackPlayerEvents = < T extends Event[], - H extends ( - data: EventPayloadByEvent[EventTypeFromArray] & { - type: EventTypeFromArray; - } - ) => void + H extends (data: EventPayloadByEvent[T[number]]) => void >( events: T, handler: H @@ -46,7 +39,7 @@ export const useTrackPlayerEvents = < const subs = events.map((type) => addEventListener(type, (payload) => { // @ts-expect-error - we know the type is correct - savedHandler.current({ ...payload, type }); + savedHandler.current(payload); }) ); diff --git a/src/interfaces/events/EventPayloadByEvent.ts b/src/interfaces/events/EventPayloadByEvent.ts index 631fdd687..340d51fbe 100644 --- a/src/interfaces/events/EventPayloadByEvent.ts +++ b/src/interfaces/events/EventPayloadByEvent.ts @@ -20,32 +20,58 @@ import type { RemoteSetRatingEvent } from './RemoteSetRatingEvent'; import type { RemoteSkipEvent } from './RemoteSkipEvent'; export interface EventPayloadByEvent { - [Event.PlayerError]: PlayerErrorEvent; - [Event.PlaybackState]: PlaybackState; - [Event.PlaybackError]: PlaybackErrorEvent; - [Event.PlaybackQueueEnded]: PlaybackQueueEndedEvent; - [Event.PlaybackTrackChanged]: PlaybackTrackChangedEvent; - [Event.PlaybackActiveTrackChanged]: PlaybackActiveTrackChangedEvent; - [Event.PlaybackMetadataReceived]: PlaybackMetadataReceivedEvent; - [Event.PlaybackPlayWhenReadyChanged]: PlaybackPlayWhenReadyChangedEvent; - [Event.PlaybackProgressUpdated]: PlaybackProgressUpdatedEvent; - [Event.RemotePlay]: never; - [Event.RemotePlayId]: RemotePlayIdEvent; - [Event.RemotePlaySearch]: RemotePlaySearchEvent; - [Event.RemotePause]: never; - [Event.RemoteStop]: never; - [Event.RemoteSkip]: RemoteSkipEvent; - [Event.RemoteNext]: never; - [Event.RemotePrevious]: never; - [Event.RemoteJumpForward]: RemoteJumpForwardEvent; - [Event.RemoteJumpBackward]: RemoteJumpBackwardEvent; - [Event.RemoteSeek]: RemoteSeekEvent; - [Event.RemoteSetRating]: RemoteSetRatingEvent; - [Event.RemoteDuck]: RemoteDuckEvent; - [Event.RemoteLike]: never; - [Event.RemoteDislike]: never; - [Event.RemoteBookmark]: never; - [Event.MetadataChapterReceived]: AudioCommonMetadataReceivedEvent[]; - [Event.MetadataTimedReceived]: AudioCommonMetadataReceivedEvent; - [Event.MetadataCommonReceived]: AudioCommonMetadataReceivedEvent[]; + [Event.PlayerError]: PlayerErrorEvent & { type: Event.PlayerError }; + [Event.PlaybackState]: PlaybackState & { type: Event.PlaybackState }; + [Event.PlaybackError]: PlaybackErrorEvent & { type: Event.PlaybackError }; + [Event.PlaybackQueueEnded]: PlaybackQueueEndedEvent & { + type: Event.PlaybackQueueEnded; + }; + [Event.PlaybackTrackChanged]: PlaybackTrackChangedEvent & { + type: Event.PlaybackTrackChanged; + }; + [Event.PlaybackActiveTrackChanged]: PlaybackActiveTrackChangedEvent & { + type: Event.PlaybackActiveTrackChanged; + }; + [Event.PlaybackMetadataReceived]: PlaybackMetadataReceivedEvent & { + type: Event.PlaybackMetadataReceived; + }; + [Event.PlaybackPlayWhenReadyChanged]: PlaybackPlayWhenReadyChangedEvent & { + type: Event.PlaybackPlayWhenReadyChanged; + }; + [Event.PlaybackProgressUpdated]: PlaybackProgressUpdatedEvent & { + type: Event.PlaybackProgressUpdated; + }; + [Event.RemotePlay]: { type: Event.RemotePlay }; + [Event.RemotePlayId]: RemotePlayIdEvent & { type: Event.RemotePlayId }; + [Event.RemotePlaySearch]: RemotePlaySearchEvent & { + type: Event.RemotePlaySearch; + }; + [Event.RemotePause]: { type: Event.RemotePause }; + [Event.RemoteStop]: { type: Event.RemoteStop }; + [Event.RemoteSkip]: RemoteSkipEvent & { type: Event.RemoteSkip }; + [Event.RemoteNext]: { type: Event.RemoteNext }; + [Event.RemotePrevious]: { type: Event.RemotePrevious }; + [Event.RemoteJumpForward]: RemoteJumpForwardEvent & { + type: Event.RemoteJumpForward; + }; + [Event.RemoteJumpBackward]: RemoteJumpBackwardEvent & { + type: Event.RemoteJumpBackward; + }; + [Event.RemoteSeek]: RemoteSeekEvent & { type: Event.RemoteSeek }; + [Event.RemoteSetRating]: RemoteSetRatingEvent & { + type: Event.RemoteSetRating; + }; + [Event.RemoteDuck]: RemoteDuckEvent & { type: Event.RemoteDuck }; + [Event.RemoteLike]: { type: Event.RemoteLike }; + [Event.RemoteDislike]: { type: Event.RemoteDislike }; + [Event.RemoteBookmark]: { type: Event.RemoteBookmark }; + [Event.MetadataChapterReceived]: AudioCommonMetadataReceivedEvent[] & { + type: Event.MetadataChapterReceived; + }; + [Event.MetadataTimedReceived]: AudioCommonMetadataReceivedEvent & { + type: Event.MetadataTimedReceived; + }; + [Event.MetadataCommonReceived]: AudioCommonMetadataReceivedEvent[] & { + type: Event.MetadataCommonReceived; + }; }