-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a7b6c97
commit b91302f
Showing
28 changed files
with
221 additions
and
298 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
import { IKeyValueStoreSymbol, IObjectStoreSymbol } from 'ipmc-interfaces'; | ||
import { IIndexManagerSymbol, IKeyValueStoreSymbol, IObjectStoreSymbol } from 'ipmc-interfaces'; | ||
import { MemoryKeyValueStore } from '../Services/MemoryKeyValueStore'; | ||
import { ObjectStore } from '../Services/ObjectStore'; | ||
import { IModule } from './IModule'; | ||
import { IndexManager } from '../Services/IndexManager'; | ||
|
||
export const CoreModule: IModule = (app) => { | ||
app.register(MemoryKeyValueStore, IKeyValueStoreSymbol); | ||
app.register(ObjectStore, IObjectStoreSymbol); | ||
app.register(IndexManager, IIndexManagerSymbol); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import { Signal } from '@preact/signals-core'; | ||
import { inject, injectable, postConstruct, preDestroy } from 'inversify'; | ||
import { IIndexManager, IIpfsService, IIpfsServiceSymbol, ILibrary, ILibraryIndex, IObjectStore, IObjectStoreSymbol, IProfile, IProfileSymbol, ITask, isMovieLibrary, isSeriesLibrary } from 'ipmc-interfaces'; | ||
import { MovieIndexFetcher, SeriesIndexFetcher } from './Indexer'; | ||
|
||
@injectable() | ||
export class IndexManager implements IIndexManager { | ||
public constructor( | ||
@inject(IProfileSymbol) private readonly profile: IProfile, | ||
@inject(IIpfsServiceSymbol) private readonly ipfs: IIpfsService, | ||
@inject(IObjectStoreSymbol) private readonly objectStore: IObjectStore, | ||
) { | ||
for (const lib of this.profile.libraries) { | ||
this.libraries.set(lib.name, new Signal<ILibrary>(lib)); | ||
const indexSignal = new Signal<ILibraryIndex<any> | undefined>(this.objectStore.get(this.getIndexStorageKey(lib.name))); | ||
this.indexes.set(lib.name, indexSignal); | ||
indexSignal.subscribe((newState) => { | ||
if (newState !== undefined) { | ||
this.objectStore.set(this.getIndexStorageKey(lib.name), newState); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
@postConstruct() | ||
public start(): void { | ||
this.triggerUpdate(); | ||
this.timer = setInterval(() => { | ||
this.triggerUpdate(); | ||
}, 15 * 60 * 1000); | ||
} | ||
|
||
@preDestroy() | ||
public stop(): void { | ||
clearInterval(this.timer); | ||
} | ||
|
||
public indexes = new Map<string, Signal<ILibraryIndex<any> | undefined>>(); | ||
|
||
public tasks = new Signal<ITask[]>([]); | ||
|
||
private getIndexStorageKey(name: string) { | ||
return `${this.profile.id}_index_${name}`; | ||
} | ||
|
||
private triggerUpdate(): void { | ||
for (const library of this.libraries.values()) { | ||
const lib = library.value; | ||
if (!this.updates.has(lib.name)) { | ||
const task = { | ||
title: 'Updating library ' + lib.name, | ||
}; | ||
this.tasks.value = [...this.tasks.value, task]; | ||
this.updates.set(lib.name, this.updateLibrary(lib).finally(() => { | ||
this.updates.delete(lib.name); | ||
this.tasks.value = this.tasks.value.filter(t => t != task); | ||
})); | ||
} | ||
} | ||
} | ||
|
||
private async updateLibrary(library: ILibrary): Promise<void> { | ||
const index = this.indexes.get(library.name); | ||
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) { | ||
if (indexer == undefined) { | ||
throw new Error(`Unknown library type [${library.type}]`); | ||
} | ||
|
||
const newIndex = await indexer.fetchIndex(); | ||
|
||
index.value = { | ||
cid: cid, | ||
indexer: indexer.version, | ||
index: newIndex, | ||
}; | ||
} | ||
} catch (ex) { | ||
console.error(ex); | ||
} | ||
} | ||
} | ||
|
||
private libraries = new Map<string, Signal<ILibrary>>(); | ||
|
||
private updates = new Map<string, Promise<void>>(); | ||
|
||
private timer: any; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export interface IIndexFetcher<TIndex> { | ||
fetchIndex(): Promise<TIndex>; | ||
version: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export { type IIndexFetcher } from './IIndexFetcher'; | ||
export { MovieIndexFetcher } from './MovieIndexFetcher'; | ||
export { SeriesIndexFetcher } from './SeriesIndexFetcher'; |
103 changes: 0 additions & 103 deletions
103
packages/core/src/Services/ProfileManager/BaseProfileManager.ts
This file was deleted.
Oops, something went wrong.
70 changes: 0 additions & 70 deletions
70
packages/core/src/Services/ProfileManager/RemoteProfileManager.ts
This file was deleted.
Oops, something went wrong.
17 changes: 0 additions & 17 deletions
17
packages/core/src/Services/ProfileManager/SimpleProfileManager.ts
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.