From f901938997b91ca0b10935aa9e8cb8bc3e72e9b6 Mon Sep 17 00:00:00 2001 From: UndyingWraith Date: Tue, 18 Jun 2024 12:08:29 +0200 Subject: [PATCH] Fix library switching --- packages/core/src/Services/IndexManager.ts | 6 +++--- packages/core/src/Services/Indexer/IIndexFetcher.ts | 2 +- .../core/src/Services/Indexer/MovieIndexFetcher.ts | 10 +++++----- .../core/src/Services/Indexer/SeriesIndexFetcher.ts | 10 +++++----- packages/ui/src/components/organisms/Library.tsx | 4 +++- 5 files changed, 17 insertions(+), 15 deletions(-) diff --git a/packages/core/src/Services/IndexManager.ts b/packages/core/src/Services/IndexManager.ts index bfe3f01..3f6f9e4 100644 --- a/packages/core/src/Services/IndexManager.ts +++ b/packages/core/src/Services/IndexManager.ts @@ -64,13 +64,13 @@ export class IndexManager implements IIndexManager { if (library.upstream != undefined && index != undefined) { try { const cid = await this.ipfs.resolve(library.upstream); - const indexer = isMovieLibrary(library) ? new MovieIndexFetcher(this.ipfs, library) : isSeriesLibrary(library) ? new SeriesIndexFetcher(this.ipfs, library) : undefined; - if (index.value?.cid != cid || indexer?.version !== index.value?.indexer) { + const indexer = isMovieLibrary(library) ? new MovieIndexFetcher(this.ipfs) : isSeriesLibrary(library) ? new SeriesIndexFetcher(this.ipfs) : undefined; + if (index.value?.cid != cid || indexer?.version !== index.value.indexer) { if (indexer == undefined) { throw new Error(`Unknown library type [${library.type}]`); } - const newIndex = await indexer.fetchIndex(); + const newIndex = await indexer.fetchIndex(cid); index.value = { cid: cid, diff --git a/packages/core/src/Services/Indexer/IIndexFetcher.ts b/packages/core/src/Services/Indexer/IIndexFetcher.ts index fb1b1ad..65a5ae0 100644 --- a/packages/core/src/Services/Indexer/IIndexFetcher.ts +++ b/packages/core/src/Services/Indexer/IIndexFetcher.ts @@ -1,4 +1,4 @@ export interface IIndexFetcher { - fetchIndex(): Promise; + fetchIndex(cid: string): Promise; version: string; } diff --git a/packages/core/src/Services/Indexer/MovieIndexFetcher.ts b/packages/core/src/Services/Indexer/MovieIndexFetcher.ts index 2f201e0..2ea4f83 100644 --- a/packages/core/src/Services/Indexer/MovieIndexFetcher.ts +++ b/packages/core/src/Services/Indexer/MovieIndexFetcher.ts @@ -1,15 +1,15 @@ -import { IIpfsService, IGenericLibrary, IMovieMetaData, IFileInfo } from 'ipmc-interfaces'; -import { IIndexFetcher } from './IIndexFetcher'; +import { IFileInfo, IIpfsService, IMovieMetaData } from 'ipmc-interfaces'; import { Regexes } from '../../Regexes'; +import { IIndexFetcher } from './IIndexFetcher'; export class MovieIndexFetcher implements IIndexFetcher { - constructor(private readonly node: IIpfsService, private readonly lib: IGenericLibrary) { + constructor(private readonly node: IIpfsService) { } public version = '0'; - public async fetchIndex(): Promise { - const files = (await this.node.ls(this.lib.root.toString())).filter(f => f.type == 'dir'); + public async fetchIndex(cid: string): Promise { + const files = (await this.node.ls(cid)).filter(f => f.type == 'dir'); const index = []; for (const file of files) { index.push(await this.extractMovieMetaData(this.node, file)); diff --git a/packages/core/src/Services/Indexer/SeriesIndexFetcher.ts b/packages/core/src/Services/Indexer/SeriesIndexFetcher.ts index 62f0607..0eaa983 100644 --- a/packages/core/src/Services/Indexer/SeriesIndexFetcher.ts +++ b/packages/core/src/Services/Indexer/SeriesIndexFetcher.ts @@ -1,15 +1,15 @@ -import { IIpfsService, IEpisodeMetaData, IGenericLibrary, ISeasonMetaData, ISeriesMetaData, IFileInfo } from 'ipmc-interfaces'; -import { IIndexFetcher } from './IIndexFetcher'; +import { IEpisodeMetaData, IFileInfo, IIpfsService, ISeasonMetaData, ISeriesMetaData } from 'ipmc-interfaces'; import { Regexes } from '../../Regexes'; +import { IIndexFetcher } from './IIndexFetcher'; export class SeriesIndexFetcher implements IIndexFetcher { - constructor(private readonly node: IIpfsService, private readonly lib: IGenericLibrary) { + constructor(private readonly node: IIpfsService) { } public version = '0'; - public async fetchIndex(): Promise { - const files = (await this.node.ls(this.lib.root.toString())).filter(f => f.type == 'dir'); + public async fetchIndex(cid: string): Promise { + const files = (await this.node.ls(cid)).filter(f => f.type == 'dir'); const index = []; for (const file of files) { index.push(await this.extractSeriesMetaData(this.node, file)); diff --git a/packages/ui/src/components/organisms/Library.tsx b/packages/ui/src/components/organisms/Library.tsx index d1401cf..31cf622 100644 --- a/packages/ui/src/components/organisms/Library.tsx +++ b/packages/ui/src/components/organisms/Library.tsx @@ -8,6 +8,7 @@ import { IFileInfo, IIndexManager, IIndexManagerSymbol } from 'ipmc-interfaces'; import { Display } from '../pages/LibraryManager'; import { ErrorBoundary } from '../atoms/ErrorBoundary'; import { useService } from '../../context/AppContext'; +import { useWatcher } from '../../hooks'; export function Library(props: { display: ReadonlySignal; @@ -15,7 +16,8 @@ export function Library(props: { library: string; }) { const { display, library } = props; - const index = useService(IIndexManagerSymbol).indexes.get(library)!; + const indexManager = useService(IIndexManagerSymbol); + const index = useWatcher(indexManager.indexes.get(library)!.value); const selected = useSignal(undefined);