Skip to content

Commit

Permalink
[skip ci] reduce global types
Browse files Browse the repository at this point in the history
  • Loading branch information
GooseOb committed Dec 12, 2024
1 parent 28d7ac4 commit dfaa9e2
Show file tree
Hide file tree
Showing 17 changed files with 65 additions and 47 deletions.
2 changes: 1 addition & 1 deletion src/compute-settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { plr } from './player';
* 3. Global custom speed
* 4. Global speed
*/
export const computeSettings = (doUseNormalSpeed: boolean): Cfg => {
export const computeSettings = (doUseNormalSpeed: boolean): config.Cfg => {
const channel = config.channel.get();
const settings = {
...config.value.global,
Expand Down
1 change: 1 addition & 0 deletions src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from './save';
export * from './prune';
export * from './value';
export * from './current-channel';
export type * from './types';
1 change: 1 addition & 0 deletions src/config/save.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { ScriptCfg } from './types';
import { update } from './update';
import { value } from './value';

Expand Down
26 changes: 26 additions & 0 deletions src/config/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
export type FlagName =
| 'shortsToUsual'
| 'newTab'
| 'copySubs'
| 'standardMusicSpeed'
| 'enhancedBitrate';

type YtCfg = {
speed?: string;
quality?: string;
volume?: string;
subtitles?: boolean;
};

export type Cfg = YtCfg & {
customSpeed?: string;
};

export type Setting = keyof Cfg;

export type ScriptCfg = {
_v: number;
global: Cfg;
channels: Record<string, Cfg>;
flags: Record<FlagName, boolean>;
};
2 changes: 2 additions & 0 deletions src/config/update.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { ScriptCfg } from './types';

/**
* @returns if the cfg was updated
*/
Expand Down
2 changes: 2 additions & 0 deletions src/config/value.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { ScriptCfg } from './types';

const cfgLocalStorage = localStorage[STORAGE_NAME];

export const value: ScriptCfg = cfgLocalStorage
Expand Down
2 changes: 2 additions & 0 deletions src/menu/controls.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { text } from '../text';
import type { SettingControls } from './types';
import type { Cfg, FlagName, ScriptCfg } from '../config';

const updateValuesIn = (controls: SettingControls, cfgPart: Readonly<Cfg>) => {
controls[SPEED].value = cfgPart[SPEED] || text.DEFAULT;
Expand Down
2 changes: 2 additions & 0 deletions src/menu/get-controls-creators.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { checkbox, input, option, selectEl } from '../utils/element-creators';
import { text } from '../text';
import type { SettingControls } from './types';
import type { Setting } from '../config';

type GetControlCreator = <
TSetting extends Setting,
Expand Down
2 changes: 1 addition & 1 deletion src/menu/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import * as get from '../element-getters';

const controlCheckboxDiv = (
id: string,
flagName: FlagName,
flagName: config.FlagName,
textContent: string
): HTMLDivElement => {
const cont = div({ className: 'check-cont' });
Expand Down
1 change: 1 addition & 0 deletions src/menu/section.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { value } from './value';
import { getElCreator } from '../utils';
import { plr } from '../player';
import { controls } from './controls';
import type { Cfg } from '../config';

export const section = (
sectionId: typeof SECTION_GLOBAL | typeof SECTION_LOCAL,
Expand Down
4 changes: 4 additions & 0 deletions src/menu/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import type { Setting } from '../config';

type Control = HTMLSelectElement | HTMLInputElement;
export type SettingControls = Record<Setting, Control>;
1 change: 1 addition & 0 deletions src/player/apply-settings.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { Cfg, Setting } from '../config';
import { restoreFocusAfter } from '../utils';
import { plr } from './plr';
import { valueSetters } from './value-setters';
Expand Down
1 change: 1 addition & 0 deletions src/player/plr.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as get from '../element-getters';
import { delay, until, restoreFocusAfter, findInNodeList } from '../utils';
import type { YtSettingItem, YtSettingName } from './types';

export const plr = {
async set(el: HTMLElement) {
Expand Down
2 changes: 2 additions & 0 deletions src/player/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export type YtSettingName = typeof SPEED | typeof QUALITY;
export type YtSettingItem = HTMLDivElement & { role: 'menuitem' };
7 changes: 4 additions & 3 deletions src/player/value-setters.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import * as config from '../config';
import * as logger from '../logger';
import { plr } from '../player';

type YtSettingName = typeof SPEED | typeof QUALITY;
import type { YtSettingName } from './types';

type Comparator = (target: string, current: string) => boolean;
const comparators: { readonly [P in YtSettingName]: Comparator } = {
Expand All @@ -16,7 +15,9 @@ const comparators: { readonly [P in YtSettingName]: Comparator } = {
type ValueSetterHelpers = {
_ytSettingItem(settingName: YtSettingName, value: string): void;
};
type ValueSetters = { [P in Setting]: (value: Required<Cfg>[P]) => void };
type ValueSetters = {
[P in config.Setting]: (value: Required<config.Cfg>[P]) => void;
};

export const valueSetters: ValueSetters & ValueSetterHelpers = {
_ytSettingItem(settingName, value) {
Expand Down
14 changes: 14 additions & 0 deletions src/types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
type DeepReadonly<T> = T extends (infer R)[]
? DeepReadonlyArray<R>
: T extends Function
? T
: T extends object
? DeepReadonlyObject<T>
: T;
type DeepReadonlyArray<T> = ReadonlyArray<DeepReadonly<T>>;
type DeepReadonlyObject<T> = {
readonly [P in keyof T]: DeepReadonly<T[P]>;
};

type Props<T extends HTMLElement> = Partial<T> & object;
type ControlItem<T extends HTMLElement> = { item: HTMLDivElement; elem: T };
42 changes: 0 additions & 42 deletions src/types.ts

This file was deleted.

0 comments on commit dfaa9e2

Please sign in to comment.