Cleanup update code

This commit is contained in:
Zoe Roux 2023-12-15 22:33:38 +01:00
parent 3a2e5f5eb1
commit a483955763

View File

@ -24,7 +24,6 @@ import {
directories, directories,
DownloadTask, DownloadTask,
checkForExistingDownloads, checkForExistingDownloads,
Network,
ensureDownloadsAreRunning, ensureDownloadsAreRunning,
} from "@kesha-antonov/react-native-background-downloader"; } from "@kesha-antonov/react-native-background-downloader";
import { import {
@ -37,9 +36,9 @@ import {
toQueryKey, toQueryKey,
} from "@kyoo/models"; } from "@kyoo/models";
import { Player } from "../player"; import { Player } from "../player";
import { atom, useSetAtom, useAtom, Atom, PrimitiveAtom, useStore } from "jotai"; import { atom, useSetAtom, PrimitiveAtom, useStore } from "jotai";
import { getCurrentAccount, storage } from "@kyoo/models/src/account-internal"; import { getCurrentAccount, storage } from "@kyoo/models/src/account-internal";
import { useContext, useEffect } from "react"; import { useEffect } from "react";
type State = { type State = {
status: "DOWNLOADING" | "PAUSED" | "DONE" | "FAILED" | "STOPPED"; status: "DOWNLOADING" | "PAUSED" | "DONE" | "FAILED" | "STOPPED";
@ -75,23 +74,19 @@ const query = <T,>(query: QueryIdentifier<T>, info: Account): Promise<T> =>
info.token.access_token, info.token.access_token,
); );
const listenToTask = ( const listenToTask = (task: DownloadTask, update: (f: (old: State) => State) => void) => {
task: DownloadTask,
atom: PrimitiveAtom<State>,
atomStore: ReturnType<typeof useStore>,
) => {
task task
.begin(({ expectedBytes }) => atomStore.set(atom, (x) => ({ ...x, size: expectedBytes }))) .begin(({ expectedBytes }) => update((x) => ({ ...x, size: expectedBytes })))
.progress((percent, availableSize, size) => .progress((percent, availableSize, size) =>
atomStore.set(atom, (x) => ({ ...x, percent, size, availableSize })), update((x) => ({ ...x, percent, size, availableSize })),
) )
.done(() => { .done(() => {
atomStore.set(atom, (x) => ({ ...x, percent: 100, status: "DONE" })); update((x) => ({ ...x, percent: 100, status: "DONE" }));
// apparently this is needed for ios /shrug i'm totaly gona forget this // apparently this is needed for ios /shrug i'm totaly gona forget this
// if i ever implement ios so keeping this here // if i ever implement ios so keeping this here
completeHandler(task.id); completeHandler(task.id);
}) })
.error((error) => atomStore.set(atom, (x) => ({ ...x, status: "FAILED", error }))); .error((error) => update((x) => ({ ...x, status: "FAILED", error })));
}; };
export const useDownloader = () => { export const useDownloader = () => {
@ -138,7 +133,7 @@ export const useDownloader = () => {
// we use the store instead of the onMount because we want to update the state to cache it even if it was not // we use the store instead of the onMount because we want to update the state to cache it even if it was not
// used during this launch of the app. // used during this launch of the app.
listenToTask(task, state, atomStore); listenToTask(task, (f) => atomStore.set(state, f));
setDownloads((x) => [...x, { data, info, path, state }]); setDownloads((x) => [...x, { data, info, path, state }]);
}; };
}; };
@ -151,12 +146,12 @@ export const DownloadProvider = () => {
const tasks = await checkForExistingDownloads(); const tasks = await checkForExistingDownloads();
const downloads = store.get(downloadAtom); const downloads = store.get(downloadAtom);
for (const t of tasks) { for (const t of tasks) {
const downAtom = downloads.find((x) => x.data.id === t.id); const d = downloads.find((x) => x.data.id === t.id);
if (!downAtom) { if (!d) {
t.stop(); t.stop();
continue; continue;
} }
listenToTask(t, downAtom.state, store); listenToTask(t, (f) => store.set(d.state, f));
} }
ensureDownloadsAreRunning(); ensureDownloadsAreRunning();
} }