Skip to content

Commit

Permalink
Fix: avoid re-render in novel screen
Browse files Browse the repository at this point in the history
  • Loading branch information
nyagami committed Jul 7, 2024
1 parent 867b63a commit b45a30f
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/hooks/persisted/useDownload.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import { ChapterInfo, NovelInfo } from '@database/types';
import ServiceManager, { BackgroundTask } from '@services/ServiceManager';
import { useMemo } from 'react';
import { useMMKVObject } from 'react-native-mmkv';

export const DOWNLOAD_QUEUE = 'DOWNLOAD';
export const CHAPTER_DOWNLOADING = 'CHAPTER_DOWNLOADING';

const defaultQueue: BackgroundTask[] = [];

export default function useDownload() {
const [queue = defaultQueue] = useMMKVObject<BackgroundTask[]>(
const [queue] = useMMKVObject<BackgroundTask[]>(
ServiceManager.manager.STORE_KEY,
);
const downloadQueue = useMemo(
() => queue?.filter(t => t.name === 'DOWNLOAD_CHAPTER') || [],
[queue],
);

const downloadChapter = (novel: NovelInfo, chapter: ChapterInfo) =>
ServiceManager.manager.addTask({
Expand Down Expand Up @@ -40,7 +43,7 @@ export default function useDownload() {
ServiceManager.manager.removeTasksByName('DOWNLOAD_CHAPTER');

return {
downloadQueue: queue.filter(t => t.name === 'DOWNLOAD_CHAPTER'),
downloadQueue,
resumeDowndload,
downloadChapter,
downloadChapters,
Expand Down

0 comments on commit b45a30f

Please sign in to comment.