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 3 commits
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 package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
"license": "MIT",
"dependencies": {
"jintr": "^1.1.0",
"linkedom": "^0.14.12",
"linkedom": "^0.15.1",
"tslib": "^2.5.0",
"undici": "^5.19.1"
},
Expand Down
2 changes: 1 addition & 1 deletion src/parser/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ const example_data = {

// The first argument is the name of the class, the second is the data you have for the node.
// It will return a class that extends YTNode.
const Example = Generator.YTNodeGenerator.generateRuntimeClass('Example', example_data);
const Example = Generator.generateRuntimeClass('Example', example_data);

// You may now use this class as you would any other node.
const example = new Example(example_data);
Expand Down
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;
}
}
Loading