Skip to content

Commit

Permalink
feat(project): cache media items from playlists
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristiaanScheermeijer committed May 28, 2022
1 parent 0d70e99 commit 2b8b5ee
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/hooks/useEntitlement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ const useEntitlement: UseEntitlement = (playlistItem) => {
const isPreEntitled = playlistItem && !isLocked(accessModel, !!user, !!subscription, playlistItem);
const mediaOffers = playlistItem?.mediaOffers || [];

// This entitlement query is invalidated by adding all transaction IDs to the queryKey. Perhaps a more optimal way is
// to invalidate the query cache after the payment.
const mediaEntitlementQueries = useQueries(
mediaOffers.map(({ offerId }) => ({
queryKey: ['mediaOffer', offerId, transactions?.map((t) => t.transactionId).join(',')],
Expand Down
11 changes: 9 additions & 2 deletions src/hooks/usePlaylist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import useContentProtection from '#src/hooks/useContentProtection';
import { generatePlaylistPlaceholder } from '#src/utils/collection';
import type { GetPlaylistParams, Playlist } from '#types/playlist';
import { getPlaylistById } from '#src/services/api.service';
import { queryClient } from '#src/providers/QueryProvider';

const placeholderData = generatePlaylistPlaceholder(30);

Expand All @@ -16,12 +17,18 @@ const filterMediaItem = (playlist: Playlist | undefined, mediaId?: string) => {
return playlist;
};

export default function usePlaylist (playlistId: string, params: GetPlaylistParams = {}, enabled: boolean = true, usePlaceholderData: boolean = true) {
export default function usePlaylist(playlistId: string, params: GetPlaylistParams = {}, enabled: boolean = true, usePlaceholderData: boolean = true) {
const callback = async (token?: string, drmPolicyId?: string) => {
const playlist = await getPlaylistById(playlistId, { token, ...params }, drmPolicyId);

// This pre-caches all playlist items and makes navigating a lot faster. This doesn't work when DRM is enabled
// because of the token mechanism.
playlist?.playlist?.forEach((playlistItem) => {
queryClient.setQueryData(['media', playlistItem.mediaid, {}, undefined], playlistItem);
});

return filterMediaItem(playlist);
}
};

return useContentProtection('playlist', playlistId, callback, params, enabled, usePlaceholderData ? placeholderData : undefined);
}
2 changes: 1 addition & 1 deletion src/providers/QueryProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import { QueryClient, QueryClientProvider } from 'react-query';

const queryClient = new QueryClient({
export const queryClient = new QueryClient({
defaultOptions: {
queries: {
staleTime: 60 * 60 * 1000,
Expand Down

0 comments on commit 2b8b5ee

Please sign in to comment.