Skip to content

Commit

Permalink
Fix type inference in useTrackPlayerEvents
Browse files Browse the repository at this point in the history
  • Loading branch information
dcvz committed Sep 21, 2023
1 parent 1333bfb commit e28f0ea
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 37 deletions.
11 changes: 2 additions & 9 deletions src/hooks/useTrackPlayerEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,14 @@ 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 extends Event[]> = T[number];

/**
* Attaches a handler to the given TrackPlayer events and performs cleanup on unmount
* @param events - TrackPlayer events to subscribe to
* @param handler - callback invoked when the event fires
*/
export const useTrackPlayerEvents = <
T extends Event[],
H extends (
data: EventPayloadByEvent[EventTypeFromArray<T>] & {
type: EventTypeFromArray<T>;
}
) => void
H extends (data: EventPayloadByEvent[T[number]]) => void
>(
events: T,
handler: H
Expand Down Expand Up @@ -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);
})
);

Expand Down
82 changes: 54 additions & 28 deletions src/interfaces/events/EventPayloadByEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
}

0 comments on commit e28f0ea

Please sign in to comment.