Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Fix/pagination of sources which require pages to be fetched in order #451

Merged
merged 2 commits into from
Nov 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/components/SourceCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,11 @@ export const SourceCard: React.FC<IProps> = (props: IProps) => {
margin: '10px',
}}
>
<CardActionArea component={Link} to={`/sources/${id}`} state={{ contentType: SourceContentType.POPULAR }}>
<CardActionArea
component={Link}
to={`/sources/${id}`}
state={{ contentType: SourceContentType.POPULAR, clearCache: true }}
>
<CardContent
sx={{
display: 'flex',
Expand Down Expand Up @@ -105,7 +109,7 @@ export const SourceCard: React.FC<IProps> = (props: IProps) => {
variant="outlined"
component={Link}
to={`/sources/${id}`}
state={{ contentType: SourceContentType.LATEST }}
state={{ contentType: SourceContentType.LATEST, clearCache: true }}
>
{t('global.button.latest')}
</Button>
Expand All @@ -117,7 +121,7 @@ export const SourceCard: React.FC<IProps> = (props: IProps) => {
variant="outlined"
component={Link}
to={`/sources/${id}`}
state={{ contentType: SourceContentType.LATEST }}
state={{ contentType: SourceContentType.LATEST, clearCache: true }}
>
{t('global.button.latest')}
</Button>
Expand All @@ -126,7 +130,7 @@ export const SourceCard: React.FC<IProps> = (props: IProps) => {
variant="outlined"
component={Link}
to={`/sources/${id}`}
state={{ contentType: SourceContentType.POPULAR }}
state={{ contentType: SourceContentType.POPULAR, clearCache: true }}
>
{t('global.button.popular')}
</Button>
Expand Down
15 changes: 15 additions & 0 deletions src/lib/requests/CustomCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,21 @@ export class CustomCache {
return this.keyToResponseMap.get(key) as Response;
}

public getAllKeys(): string[] {
return [...this.keyToResponseMap.keys()];
}

public getMatchingKeys(regex: RegExp): string[] {
return this.getAllKeys().filter((key) => !!regex.exec(key));
}

public clearFor(...keys: string[]) {
keys.forEach((key) => {
this.keyToResponseMap.delete(key);
this.keyToFetchTimestampMap.delete(key);
});
}

public clear(): void {
this.keyToResponseMap.clear();
this.keyToFetchTimestampMap.clear();
Expand Down
29 changes: 25 additions & 4 deletions src/lib/requests/RequestManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,16 @@ export type AbortableApolloMutationResponse<Data = any> = { response: Promise<Fe

const EXTENSION_LIST_CACHE_KEY = 'useExtensionListFetch';

const CACHE_INITIAL_PAGES_FETCHING_KEY = 'GET_SOURCE_MANGAS_FETCH_FETCHING_INITIAL_PAGES';
const CACHE_PAGES_KEY = 'GET_SOURCE_MANGAS_FETCH_PAGES';
const CACHE_RESULTS_KEY = 'GET_SOURCE_MANGAS_FETCH';

export const SPECIAL_ED_SOURCES = {
REVALIDATION: [
'57122881048805941', // e-hentai
],
};

// TODO - correctly update cache after all mutations instead of refetching queries
export class RequestManager {
public static readonly API_VERSION = '/api/v1/';
Expand Down Expand Up @@ -320,6 +330,14 @@ export class RequestManager {
return `${this.getBaseUrl()}${apiVersion}${endpoint}`;
}

public clearBrowseCacheFor(sourceId: string) {
const cacheKeys = this.cache.getMatchingKeys(
new RegExp(`${CACHE_INITIAL_PAGES_FETCHING_KEY}|${CACHE_PAGES_KEY}|${CACHE_RESULTS_KEY}.*${sourceId}`),
);

this.cache.clearFor(...cacheKeys);
}

private createAbortController(): { signal: AbortSignal } & AbortableRequest {
const abortController = new AbortController();
const abortRequest = (reason?: any): void => {
Expand Down Expand Up @@ -354,6 +372,7 @@ export class RequestManager {
}

private async revalidatePage<Data = any, Variables extends OperationVariables = OperationVariables>(
sourceId: string,
cacheResultsKey: string,
cachePagesKey: string,
getVariablesFor: (page: number) => Variables,
Expand All @@ -367,6 +386,10 @@ export class RequestManager {
maxPage: number,
signal: AbortSignal,
): Promise<void> {
if (SPECIAL_ED_SOURCES.REVALIDATION.includes(sourceId)) {
return;
}

const { response: revalidationRequest } = this.doRequest(
GQLMethod.MUTATION,
GET_SOURCE_MANGAS_FETCH,
Expand Down Expand Up @@ -404,6 +427,7 @@ export class RequestManager {

if (isCachedPageInvalid && pageToRevalidate < maxPage) {
await this.revalidatePage(
sourceId,
cacheResultsKey,
cachePagesKey,
getVariablesFor,
Expand Down Expand Up @@ -956,10 +980,6 @@ export class RequestManager {
},
});

const CACHE_INITIAL_PAGES_FETCHING_KEY = 'GET_SOURCE_MANGAS_FETCH_FETCHING_INITIAL_PAGES';
const CACHE_PAGES_KEY = 'GET_SOURCE_MANGAS_FETCH_PAGES';
const CACHE_RESULTS_KEY = 'GET_SOURCE_MANGAS_FETCH';

const isRevalidationDoneRef = useRef(false);
const activeRevalidationRef = useRef<
| [
Expand Down Expand Up @@ -1010,6 +1030,7 @@ export class RequestManager {

const revalidatePage = async (pageToRevalidate: number, maxPage: number, signal: AbortSignal) =>
this.revalidatePage(
input.source,
CACHE_RESULTS_KEY,
CACHE_PAGES_KEY,
getVariablesFor,
Expand Down
34 changes: 28 additions & 6 deletions src/screens/SourceMangas.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ import FavoriteIcon from '@mui/icons-material/Favorite';
import NewReleasesIcon from '@mui/icons-material/NewReleases';
import FilterListIcon from '@mui/icons-material/FilterList';
import { TPartialManga, TranslationKey } from '@/typings';
import { requestManager, AbortableApolloUseMutationPaginatedResponse } from '@/lib/requests/RequestManager.ts';
import {
requestManager,
AbortableApolloUseMutationPaginatedResponse,
SPECIAL_ED_SOURCES,
} from '@/lib/requests/RequestManager.ts';
import { useDebounce } from '@/components/manga/hooks';
import { useLibraryOptionsContext } from '@/components/context/LibraryOptionsContext';
import { SourceGridLayout } from '@/components/source/GridLayouts';
Expand Down Expand Up @@ -203,11 +207,12 @@ export function SourceMangas() {
const {
contentType: currentLocationContentType = SourceContentType.POPULAR,
filtersToApply: currentLocationFiltersToApply = [],
} =
useLocation<{
contentType: SourceContentType;
filtersToApply: IPos[];
}>().state ?? {};
clearCache = false,
} = useLocation<{
contentType: SourceContentType;
filtersToApply: IPos[];
clearCache: boolean;
}>().state ?? {};

const { options } = useLibraryOptionsContext();
const [query] = useQueryParam('query', StringParam);
Expand Down Expand Up @@ -288,6 +293,23 @@ export function SourceMangas() {
setResetScrollPosition(true);
}, [sourceId, contentType]);

useEffect(() => {
if (!clearCache) {
return;
}

const requiresClear = SPECIAL_ED_SOURCES.REVALIDATION.includes(sourceId);
if (!requiresClear) {
return;
}

requestManager.clearBrowseCacheFor(sourceId);
navigate('', {
replace: true,
state: { contentType: currentLocationContentType, filters: currentLocationFiltersToApply },
});
}, [clearCache]);

useEffect(
() => () => {
if (contentType !== SourceContentType.SEARCH) {
Expand Down