Skip to content

Commit

Permalink
turn config/channel into a module & introduce refs
Browse files Browse the repository at this point in the history
  • Loading branch information
GooseOb committed Jan 6, 2025
1 parent d4eb8c1 commit a939534
Show file tree
Hide file tree
Showing 9 changed files with 15 additions and 19 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "yt-defaulter",
"author": "GooseOb",
"version": "1.11.17",
"version": "1.11.18",
"repository": {
"type": "git",
"url": "git+https://github.com/GooseOb/YT-Defaulter.git"
Expand Down
2 changes: 1 addition & 1 deletion src/compute-settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { speedNormal } from './player';
* 4. Global speed
*/
export const computeSettings = (doUseNormalSpeed: boolean): config.Cfg => {
const channel = config.channel.get();
const channel = config.channel();
const settings = {
...config.value.global,
...channel,
Expand Down
11 changes: 4 additions & 7 deletions src/config/current-channel.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { ref } from '../utils/ref';
import { value } from './value';

export const channel = {
username: '',
get() {
value.channels[this.username] ||= {};
return value.channels[this.username];
},
};
export const username = ref('');

export const channel = () => (value.channels[username.val] ||= {});
2 changes: 1 addition & 1 deletion src/listeners/video-page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import * as menu from '../menu';
export const onVideoPage = async () => {
const aboveTheFold = await untilAppear(get.aboveTheFold);

config.channel.username =
config.username.val =
(await untilAppear(get.channelUsernameElementGetter(aboveTheFold))).href ||
'';

Expand Down
2 changes: 1 addition & 1 deletion src/menu/controls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const flags = {
} satisfies Record<FlagName, HTMLInputElement>;

export const updateThisChannel = () => {
updateValuesIn(sections.thisChannel, config.channel.get());
updateValuesIn(sections.thisChannel, config.channel());
};

export const updateValues = (cfg: ScriptCfg) => {
Expand Down
2 changes: 1 addition & 1 deletion src/menu/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export const init = () => {
const sections = div({ className: PREFIX + 'sections' });
sections.append(
section(SECTION_GLOBAL, text.GLOBAL, config.value.global),
section(SECTION_LOCAL, text.LOCAL, config.channel.get())
section(SECTION_LOCAL, text.LOCAL, config.channel())
);

const controlStatus = div();
Expand Down
4 changes: 2 additions & 2 deletions src/menu/section.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { withControlListeners, withHint } from '../utils/with';
import { getControlCreators } from './get-controls-creators';
import { validateVolume } from './validate-volume';
import { Hint } from '../hint';
import { setFirstFocusable } from './value';
import { firstFocusable } from './value';
import { getElCreator } from '../utils';
import { speedNormal } from '../player';
import * as controls from './controls';
Expand Down Expand Up @@ -52,7 +52,7 @@ export const section = (
values: ['2', '1.75', '1.5', '1.25', speedNormal, '0.75', '0.5', '0.25'],
getText: (val) => val,
});
if (sectionId === SECTION_GLOBAL) setFirstFocusable(speedSelect.elem);
if (sectionId === SECTION_GLOBAL) firstFocusable.val = speedSelect.elem;

const sectionElement = div({ role: 'group' });
sectionElement.setAttribute('aria-labelledby', sectionId);
Expand Down
8 changes: 3 additions & 5 deletions src/menu/value.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { debounce } from '../utils';
import { ref } from '../utils/ref';
import { close, listenForClose } from './close';

export const set = (el: HTMLDivElement, btnEl: HTMLButtonElement) => {
Expand All @@ -16,18 +17,15 @@ export const adjustWidth = () => {
};

type Focusable = { focus(): void };
let firstFocusable = null as Focusable;
export const setFirstFocusable = (el: Focusable) => {
firstFocusable = el;
};
export const firstFocusable = ref<Focusable>(null);

export const toggle = debounce(() => {
isOpen = !isOpen;
if (isOpen) {
fixPosition();
element.style.visibility = 'visible';
listenForClose();
firstFocusable.focus();
firstFocusable.val.focus();
} else {
close();
}
Expand Down
1 change: 1 addition & 0 deletions src/utils/ref.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const ref = <T>(val: T) => ({ val });

0 comments on commit a939534

Please sign in to comment.