Skip to content

Commit

Permalink
Improve IndexManager
Browse files Browse the repository at this point in the history
  • Loading branch information
undyingwraith committed Jan 14, 2025
1 parent f342c63 commit b2be574
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 16 deletions.
26 changes: 14 additions & 12 deletions packages/core/src/Services/IndexManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,20 +49,22 @@ export class IndexManager implements IIndexManager {
private triggerUpdate(): void {
for (const library of this.libraries.values()) {
const lib = library.value;
if (!this.updates.has(lib.name)) {
this.updates.add(lib.name);
this.taskManager.runTask({
task: (onProgress) => this.updateLibrary(lib, onProgress),
title: this.translationService.translate('UpdatingLibrary', { name: lib.name }),
onEnd: () => {
this.updates.delete(lib.name);
},
});
if (this.updates.has(lib.name)) {
this.updates.get(lib.name)!.abort();
}
const controller = new AbortController();
this.updates.set(lib.name, controller);
this.taskManager.runTask({
task: (onProgress) => this.updateLibrary(lib, controller.signal, onProgress),
title: this.translationService.translate('UpdatingLibrary', { name: lib.name }),
onEnd: () => {
this.updates.delete(lib.name);
},
});
}
}

private async updateLibrary(library: ILibrary, onProgress: IOnProgress): Promise<void> {
private async updateLibrary(library: ILibrary, signal: AbortSignal, onProgress: IOnProgress): Promise<void> {
const index = this.indexes.get(library.name);
if (library.upstream != undefined && index != undefined) {
try {
Expand All @@ -73,7 +75,7 @@ export class IndexManager implements IIndexManager {
throw new Error(`Unknown library type [${library.type}]`);
}

const newIndex = await indexer.fetchIndex(cid, onProgress);
const newIndex = await indexer.fetchIndex(cid, signal, onProgress);

index.value = {
cid: cid,
Expand All @@ -91,7 +93,7 @@ export class IndexManager implements IIndexManager {

private libraries = new Map<string, Signal<ILibrary>>();

private updates = new Set<string>();
private updates = new Map<string, AbortController>();

private timer: any;
}
3 changes: 2 additions & 1 deletion packages/core/src/Services/Indexer/IIndexFetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ export interface IIndexFetcher<TIndex> {
/**
* Fetches the index of the specified CID.
* @param cid cid to index.
* @param abortSignal signal to abort the process.
* @param onProgress function to update progress.
*/
fetchIndex(cid: string, onProgress: IOnProgress): Promise<TIndex>;
fetchIndex(cid: string, abortSignal: AbortSignal, onProgress: IOnProgress): Promise<TIndex>;

/**
* Checks wheter the indexer can handler specified {@link ILibrary}.
Expand Down
8 changes: 5 additions & 3 deletions packages/core/src/Services/Indexer/MovieIndexFetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,24 @@ export class MovieIndexFetcher implements IIndexFetcher<IMovieMetaData[]> {

public version = '0';

public async fetchIndex(cid: string, onProgress: IOnProgress): Promise<IMovieMetaData[]> {
public async fetchIndex(cid: string, signal: AbortSignal, onProgress: IOnProgress): Promise<IMovieMetaData[]> {
const files = (await this.node.ls(cid)).filter(f => f.type == 'dir');
signal.throwIfAborted();
const index = [];
for (const [i, file] of files.entries()) {
try {
index.push(await this.extractMovieMetaData(this.node, file));
index.push(await this.extractMovieMetaData(file, signal));
} catch (ex) {
console.error(ex);
}
signal.throwIfAborted();
onProgress(i, files.length);
}

return index;
}

public async extractMovieMetaData(node: IIpfsService, entry: IFileInfo, skeleton?: any): Promise<IMovieMetaData> {
public async extractMovieMetaData(entry: IFileInfo, signal: AbortSignal, skeleton?: any): Promise<IMovieMetaData> {
const files = (await this.node.ls(entry.cid)).filter(f => f.type == 'file');
const videoFile = files.find(f => Regexes.VideoFile('mpd').exec(f.name) != null);

Expand Down
3 changes: 3 additions & 0 deletions packages/core/src/Services/TaskManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ export class TaskManager implements ITaskManager {
task.task((progress, total) => {
this.status.value = this.status.value.map(t => t.id === status.id ? ({ ...t, progress, total }) : t);
})
.then(() => {
return task.onEnd ? task.onEnd() : Promise.resolve();
})
.finally(() => {
this.status.value = this.status.value.filter(s => s.id !== status.id);
});
Expand Down

0 comments on commit b2be574

Please sign in to comment.