Skip to content

Commit

Permalink
fix: add cache for filteres items
Browse files Browse the repository at this point in the history
  • Loading branch information
fbritoferreira committed Dec 4, 2022
1 parent 9efb61a commit 188d9a1
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {

export class M3U8Parser {
private rawPlaylist: string;
private filteredMap: Map<string, Playlist> = new Map();

private items: Map<number, PlaylistItem> = new Map();
private header: PlaylistHeader = {} as PlaylistHeader;
Expand Down Expand Up @@ -226,12 +227,23 @@ export class M3U8Parser {
};
}
public getPlaylistByGroup(group: string): Playlist {
return {
const key = group.split("").join("-");
const cached = this.filteredMap.get(key);

if (cached) {
return cached;
}

const playlist = {
header: this.header,
items: Array.from(this.items.values()).filter((item) =>
item?.group?.title?.toLowerCase().startsWith(group.toLowerCase())
),
};

this.filteredMap.set(key, playlist);

return playlist;
}

public write(playlist: Playlist): string {
Expand All @@ -241,6 +253,7 @@ export class M3U8Parser {
}

public updateItems(items: Map<number, PlaylistItem>): void {
this.filteredMap = new Map();
this.items = items;
}
}

0 comments on commit 188d9a1

Please sign in to comment.