Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor!: replace unnecessary classes with pure functions #468

Merged
merged 21 commits into from
Aug 18, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/parser/classes/GridShow.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { YTNode, type ObservedArray } from '../helpers.js';
import type { RawNode } from '../index.js';
import Parser from '../parser.js';
import * as Parser from '../parser.js';
import Author from './misc/Author.js';
import Text from './misc/Text.js';
import NavigationEndpoint from './NavigationEndpoint.js';
Expand Down
2 changes: 1 addition & 1 deletion src/parser/classes/GuideCollapsibleEntry.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Parser from '../parser.js';
import * as Parser from '../parser.js';
import GuideEntry from './GuideEntry.js';
import type { RawNode } from '../index.js';
import { type ObservedArray, YTNode } from '../helpers.js';
Expand Down
2 changes: 1 addition & 1 deletion src/parser/classes/GuideCollapsibleSectionEntry.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Parser from '../parser.js';
import * as Parser from '../parser.js';
import type { RawNode } from '../index.js';
import { type ObservedArray, YTNode } from '../helpers.js';

Expand Down
2 changes: 1 addition & 1 deletion src/parser/classes/GuideSection.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Text from './misc/Text.js';
import Parser from '../parser.js';
import * as Parser from '../parser.js';
import { type ObservedArray, YTNode } from '../helpers.js';
import type { RawNode } from '../index.js';

Expand Down
2 changes: 1 addition & 1 deletion src/parser/classes/SharedPost.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { YTNode } from '../helpers.js';
import type { RawNode } from '../index.js';
import Parser from '../parser.js';
import * as Parser from '../parser.js';
import BackstagePost from './BackstagePost.js';
import Button from './Button.js';
import Menu from './menus/Menu.js';
Expand Down
7 changes: 4 additions & 3 deletions src/parser/classes/actions/AppendContinuationItemsAction.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import Parser from '../../index.js';
import type { RawNode } from '../../index.js';
import { type SuperParsedResult, YTNode } from '../../helpers.js';
import type { ObservedArray } from '../../helpers.js';
import { YTNode } from '../../helpers.js';

export default class AppendContinuationItemsAction extends YTNode {
static type = 'AppendContinuationItemsAction';

items: SuperParsedResult<YTNode>;
contents: ObservedArray<YTNode> | null;
target: string;

constructor(data: RawNode) {
super();
this.items = Parser.parse(data.continuationItems);
this.contents = Parser.parseArray(data.continuationItems);
this.target = data.target;
}
}
211 changes: 211 additions & 0 deletions src/parser/continuations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
import type { ObservedArray} from './helpers.js';
import { YTNode, observe } from './helpers.js';
import type { RawNode } from './index.js';
import { Thumbnail } from './misc.js';
import { NavigationEndpoint, LiveChatItemList, LiveChatHeader, LiveChatParticipantsList, Message } from './nodes.js';
import * as Parser from './parser.js';

export class ItemSectionContinuation extends YTNode {
static readonly type = 'itemSectionContinuation';

contents: ObservedArray<YTNode> | null;
continuation?: string;

constructor(data: RawNode) {
super();
this.contents = Parser.parseArray(data.contents);
if (Array.isArray(data.continuations)) {
this.continuation = data.continuations?.at(0)?.nextContinuationData?.continuation;
}
}
}

export class NavigateAction extends YTNode {
static readonly type = 'navigateAction';

endpoint: NavigationEndpoint;

constructor(data: RawNode) {
super();
this.endpoint = new NavigationEndpoint(data.endpoint);
}
}

export class ShowMiniplayerCommand extends YTNode {
static readonly type = 'showMiniplayerCommand';

miniplayer_command: NavigationEndpoint;
show_premium_branding: boolean;

constructor(data: RawNode) {
super();
this.miniplayer_command = new NavigationEndpoint(data.miniplayerCommand);
this.show_premium_branding = data.showPremiumBranding;
}
}

export { default as AppendContinuationItemsAction } from './classes/actions/AppendContinuationItemsAction.js';

export class ReloadContinuationItemsCommand extends YTNode {
static readonly type = 'reloadContinuationItemsCommand';

target_id: string;
contents: ObservedArray<YTNode> | null;
slot?: string;

constructor(data: RawNode) {
super();
this.target_id = data.targetId;
this.contents = Parser.parse(data.continuationItems, true);
this.slot = data?.slot;
}
}

export class SectionListContinuation extends YTNode {
static readonly type = 'sectionListContinuation';

continuation: string;
contents: ObservedArray<YTNode> | null;

constructor(data: RawNode) {
super();
this.contents = Parser.parse(data.contents, true);
this.continuation =
data.continuations?.[0]?.nextContinuationData?.continuation ||
data.continuations?.[0]?.reloadContinuationData?.continuation || null;
}
}

export class MusicPlaylistShelfContinuation extends YTNode {
static readonly type = 'musicPlaylistShelfContinuation';

continuation: string;
contents: ObservedArray<YTNode> | null;

constructor(data: RawNode) {
super();
this.contents = Parser.parse(data.contents, true);
this.continuation = data.continuations?.[0].nextContinuationData.continuation || null;
}
}

export class MusicShelfContinuation extends YTNode {
static readonly type = 'musicShelfContinuation';

continuation: string;
contents: ObservedArray<YTNode> | null;

constructor(data: RawNode) {
super();
this.contents = Parser.parseArray(data.contents);
this.continuation =
data.continuations?.[0].nextContinuationData?.continuation ||
data.continuations?.[0].reloadContinuationData?.continuation || null;
}
}

export class GridContinuation extends YTNode {
static readonly type = 'gridContinuation';

continuation: string;
items: ObservedArray<YTNode> | null;

constructor(data: RawNode) {
super();
this.items = Parser.parse(data.items, true);
this.continuation = data.continuations?.[0].nextContinuationData.continuation || null;
}

get contents() {
return this.items;
}
}

export class PlaylistPanelContinuation extends YTNode {
static readonly type = 'playlistPanelContinuation';

continuation: string;
contents: ObservedArray<YTNode> | null;

constructor(data: RawNode) {
super();
this.contents = Parser.parseArray(data.contents);
this.continuation = data.continuations?.[0]?.nextContinuationData?.continuation ||
data.continuations?.[0]?.nextRadioContinuationData?.continuation || null;
}
}

export class Continuation extends YTNode {
static readonly type = 'continuation';

continuation_type: string;
timeout_ms?: number;
time_until_last_message_ms?: number;
token: string;

constructor(data: RawNode) {
super();
this.continuation_type = data.type;
this.timeout_ms = data.continuation?.timeoutMs;
this.time_until_last_message_ms = data.continuation?.timeUntilLastMessageMsec;
this.token = data.continuation?.continuation;
}
}

export class LiveChatContinuation extends YTNode {
static readonly type = 'liveChatContinuation';

actions: ObservedArray<YTNode>;
action_panel: YTNode | null;
item_list: LiveChatItemList | null;
header: LiveChatHeader | null;
participants_list: LiveChatParticipantsList | null;
popout_message: Message | null;
emojis: {
emoji_id: string;
shortcuts: string[];
search_terms: string[];
image: Thumbnail[];
}[];
continuation: Continuation;
viewer_name: string;

constructor(data: RawNode) {
super();
this.actions = Parser.parse(data.actions?.map((action: any) => {
delete action.clickTrackingParams;
return action;
}), true) || observe<YTNode>([]);

this.action_panel = Parser.parseItem(data.actionPanel);
this.item_list = Parser.parseItem(data.itemList, LiveChatItemList);
this.header = Parser.parseItem(data.header, LiveChatHeader);
this.participants_list = Parser.parseItem(data.participantsList, LiveChatParticipantsList);
this.popout_message = Parser.parseItem(data.popoutMessage, Message);

this.emojis = data.emojis?.map((emoji: any) => ({
emoji_id: emoji.emojiId,
shortcuts: emoji.shortcuts,
search_terms: emoji.searchTerms,
image: Thumbnail.fromResponse(emoji.image),
is_custom_emoji: emoji.isCustomEmoji
})) || [];

let continuation, type;

if (data.continuations?.[0].timedContinuationData) {
type = 'timed';
continuation = data.continuations?.[0].timedContinuationData;
} else if (data.continuations?.[0].invalidationContinuationData) {
type = 'invalidation';
continuation = data.continuations?.[0].invalidationContinuationData;
} else if (data.continuations?.[0].liveChatReplayContinuationData) {
type = 'replay';
continuation = data.continuations?.[0].liveChatReplayContinuationData;
}

this.continuation = new Continuation({ continuation, type });

this.viewer_name = data.viewerName;
}
}
42 changes: 20 additions & 22 deletions src/parser/generator.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/* eslint-disable no-cond-assign */
import { InnertubeError, Platform } from '../utils/Utils.js';
import { InnertubeError } from '../utils/Utils.js';
import Author from './classes/misc/Author.js';
import Text from './classes/misc/Text.js';
import Thumbnail from './classes/misc/Thumbnail.js';
import NavigationEndpoint from './classes/NavigationEndpoint.js';
import type { YTNodeConstructor } from './helpers.js';
import { YTNode } from './helpers.js';
import Parser from './parser.js';
import * as Parser from './parser.js';

export type MiscInferenceType = {
type: 'misc',
Expand Down Expand Up @@ -59,7 +59,7 @@ const IGNORED_KEYS = new Set([

const RENDERER_EXAMPLES: Record<string, unknown> = {};

function camelToSnake(str: string) {
export function camelToSnake(str: string) {
return str.replace(/[A-Z]/g, (letter) => `_${letter.toLowerCase()}`);
}

Expand Down Expand Up @@ -301,9 +301,13 @@ export function isIgnoredKey(key: string | symbol) {
* @param key_info - The resolved key info
* @returns Class based on the key info extending YTNode
*/
export function createRuntimeClass(classname: string, key_info: KeyInfo, suppress_logs = false): YTNodeConstructor {
if (!suppress_logs)
logNewClass(classname, key_info);
export function createRuntimeClass(classname: string, key_info: KeyInfo, logger: Parser.ParserErrorHandler): YTNodeConstructor {
logger({
error_type: 'class_not_found',
classname,
key_info
});

const node = class extends YTNode {
static type = classname;
static #key_info = new Map<string, InferenceType>();
Expand All @@ -329,12 +333,16 @@ export function createRuntimeClass(classname: string, key_info: KeyInfo, suppres

if (did_change) {
node.key_info = resolved_key_info;
if (!suppress_logs)
logChangedKeys(classname, node.key_info, changed_keys);
logger({
error_type: 'class_changed',
classname,
key_info: node.key_info,
changed_keys
});
}

for (const [ name, data ] of unimplemented_dependencies)
generateRuntimeClass(name, data, suppress_logs);
generateRuntimeClass(name, data, logger);

for (const [ key, value ] of key_info) {
let snake_key = camelToSnake(key);
Expand All @@ -355,17 +363,17 @@ export function createRuntimeClass(classname: string, key_info: KeyInfo, suppres
* @param classdata - The example of the class
* @returns Class based on the example classdata extending YTNode
*/
export function generateRuntimeClass(classname: string, classdata: unknown, suppress_logs = false) {
export function generateRuntimeClass(classname: string, classdata: unknown, logger: Parser.ParserErrorHandler) {
const {
key_info,
unimplemented_dependencies
} = introspect(classdata);

const JITNode = createRuntimeClass(classname, key_info, suppress_logs);
const JITNode = createRuntimeClass(classname, key_info, logger);
Parser.addRuntimeParser(classname, JITNode);

for (const [ name, data ] of unimplemented_dependencies)
generateRuntimeClass(name, data, suppress_logs);
generateRuntimeClass(name, data, logger);

return JITNode;
}
Expand Down Expand Up @@ -710,13 +718,3 @@ export function mergeKeyInfo(key_info: KeyInfo, new_key_info: KeyInfo) {
changed_keys: [ ...changed_keys.entries() ]
};
}

function logNewClass(classname: string, key_info: KeyInfo) {
console.warn(
new InnertubeError(`${classname} not found!\nThis is a bug, want to help us fix it? Follow the instructions at ${Platform.shim.info.repo_url}/blob/main/docs/updating-the-parser.md or report it at ${Platform.shim.info.bugs_url}!\nIntrospected and JIT generated this class in the meantime:\n${generateTypescriptClass(classname, key_info)}`)
);
}

function logChangedKeys(classname: string, key_info: KeyInfo, changed_keys: KeyInfo) {
console.warn(`${classname} changed!\nThe following keys where altered: ${changed_keys.map(([ key ]) => camelToSnake(key)).join(', ')}\nThe class has changed to:\n${generateTypescriptClass(classname, key_info)}`);
}
6 changes: 3 additions & 3 deletions src/parser/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export { default as Parser } from './parser.js';
export * from './parser.js';
export * as Parser from './parser.js';
export * from './continuations.js';
export * from './types/index.js';
export * as Misc from './misc.js';
export * as YTNodes from './nodes.js';
Expand All @@ -8,5 +8,5 @@ export * as YTMusic from './ytmusic/index.js';
export * as YTKids from './ytkids/index.js';
export * as Helpers from './helpers.js';
export * as Generator from './generator.js';
import Parser from './parser.js';
import * as Parser from './parser.js';
export default Parser;
Loading