refactor(Library): use memo to get target YTNodes

This commit is contained in:
LuanRT
2022-11-11 19:00:12 -03:00
parent 0f8adfd9b8
commit 95ff1e6c5e

View File

@@ -5,16 +5,10 @@ import { InnertubeError } from '../../utils/Utils';
import Feed from '../../core/Feed';
import History from './History';
import Playlist from './Playlist';
import Tab from '../classes/Tab';
import Menu from '../classes/menus/Menu';
import Shelf from '../classes/Shelf';
import Button from '../classes/Button';
import SectionList from '../classes/SectionList';
import ItemSection from '../classes/ItemSection';
import TwoColumnBrowseResults from '../classes/TwoColumnBrowseResults';
import ProfileColumn from '../classes/ProfileColumn';
import ProfileColumnStats from '../classes/ProfileColumnStats';
import ProfileColumnUserInfo from '../classes/ProfileColumnUserInfo';
@@ -29,30 +23,17 @@ class Library {
this.#actions = actions;
this.#page = Parser.parseResponse(response);
const two_col = this.#page.contents.item().as(TwoColumnBrowseResults);
if (!two_col)
throw new InnertubeError('Response did not have a TwoColumnBrowseResults.');
const tab = two_col.tabs.array().as(Tab).get({ selected: true });
if (!tab)
throw new InnertubeError('Could not find target tab.');
const stats = two_col.secondary_contents.item().as(ProfileColumn).items.array().get({ type: 'ProfileColumnStats' })?.as(ProfileColumnStats) || null;
const user_info = two_col.secondary_contents.item().as(ProfileColumn).items.array().get({ type: 'ProfileColumnUserInfo' })?.as(ProfileColumnUserInfo) || null;
const stats = this.#page.contents_memo.getType(ProfileColumnStats)?.[0];
const user_info = this.#page.contents_memo.getType(ProfileColumnUserInfo)?.[0];
this.profile = { stats, user_info };
if (!tab.content)
throw new InnertubeError('Target tab did not have any content.');
const shelves = tab.content.as(SectionList).contents.array().as(ItemSection).map((is: ItemSection) => is.contents?.firstOfType(Shelf));
const shelves = this.#page.contents_memo.getType(Shelf);
this.sections = shelves.map((shelf: any) => ({
type: shelf.icon_type,
title: shelf.title,
contents: shelf.content?.item().items.array() || [],
contents: shelf.content?.item().items || [],
getAll: () => this.#getAll(shelf)
}));
}
@@ -61,7 +42,7 @@ class Library {
if (!shelf.menu?.item().as(Menu).hasKey('top_level_buttons'))
throw new InnertubeError(`The ${shelf.title.text} shelf doesn't have more items`);
const button = await shelf.menu.item().as(Menu).top_level_buttons.get({ text: 'See all' });
const button = shelf.menu.item().as(Menu).top_level_buttons.get({ text: 'See all' });
if (!button)
throw new InnertubeError('Did not find target button.');