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

client: refactor volume controls into a composable for simplicity #1561

Merged
merged 1 commit into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions client/src/components/composables/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./copy-from-textbox";
export * from "./media-player";
18 changes: 18 additions & 0 deletions client/src/components/composables/media-player.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { useStore } from "@/store";
import { onMounted, ref, watch } from "vue";

const volume = ref(100);

export function useVolume() {
const store = useStore();

onMounted(() => {
volume.value = store.state.settings.volume;
});

watch(volume, () => {
store.commit("settings/UPDATE", { volume: volume.value });
});

return volume;
}

Check warning on line 18 in client/src/components/composables/media-player.ts

View check run for this annotation

Codecov / codecov/patch

client/src/components/composables/media-player.ts#L7-L18

Added lines #L7 - L18 were not covered by tests
9 changes: 4 additions & 5 deletions client/src/components/controls/VolumeControl.vue
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
</template>

<script lang="ts">
import { defineComponent, inject, ref } from "vue";
import { defineComponent } from "vue";
import VueSlider from "vue-slider-component";
import "vue-slider-component/theme/default.css";
import { VOLUME_KEY } from "./controlkeys";
import { useVolume } from "../composables";

export default defineComponent({
name: "VolumeControl",
Expand All @@ -31,12 +31,11 @@
},
emits: ["update:volume"],
setup(props, { emit }) {
// eslint-disable-next-line @typescript-eslint/no-empty-function, @typescript-eslint/no-unused-vars
const [volume, updateVolume] = inject(VOLUME_KEY, [ref(100), (_: number) => {}]);
const volume = useVolume();

Check warning on line 34 in client/src/components/controls/VolumeControl.vue

View check run for this annotation

Codecov / codecov/patch

client/src/components/controls/VolumeControl.vue#L34

Added line #L34 was not covered by tests

function changed(value: number) {
emit("update:volume", value);
updateVolume(value);
volume.value = value;

Check warning on line 38 in client/src/components/controls/VolumeControl.vue

View check run for this annotation

Codecov / codecov/patch

client/src/components/controls/VolumeControl.vue#L38

Added line #L38 was not covered by tests
}

return { volume, changed };
Expand Down
3 changes: 0 additions & 3 deletions client/src/components/controls/controlkeys.ts

This file was deleted.

21 changes: 15 additions & 6 deletions client/src/components/players/OmniPlayer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@
import { QueueItem } from "ott-common/models/video";
import { calculateCurrentPosition } from "ott-common/timestamp";
import { defineComponent, defineAsyncComponent, PropType, ref, Ref, computed, watch } from "vue";
import { useVolume } from "../composables";

const services = [
"youtube",
Expand Down Expand Up @@ -191,12 +192,6 @@
const store = useStore();

const player: Ref<MediaPlayer | null> = ref(null);
watch(player, () => {
console.debug("Player changed", player.value);
if (player.value) {
player.value.setVolume(store.state.settings.volume);
}
});

function checkForPlayer(p: MediaPlayer | null): p is MediaPlayer {
if (!p) {
Expand Down Expand Up @@ -333,8 +328,22 @@
}
);

const volume = useVolume();
watch(volume, v => {
if (player.value) {
player.value.setVolume(v);
}
});
watch(player, () => {
console.debug("Player changed", player.value);
if (player.value) {
player.value.setVolume(volume.value);
}
});

Check warning on line 343 in client/src/components/players/OmniPlayer.vue

View check run for this annotation

Codecov / codecov/patch

client/src/components/players/OmniPlayer.vue#L331-L343

Added lines #L331 - L343 were not covered by tests
// player events re-emitted or data stored
function onApiReady() {
setVolume(volume.value);

Check warning on line 346 in client/src/components/players/OmniPlayer.vue

View check run for this annotation

Codecov / codecov/patch

client/src/components/players/OmniPlayer.vue#L346

Added line #L346 was not covered by tests
emit("apiready");
}

Expand Down
25 changes: 2 additions & 23 deletions client/src/views/Room.vue
Original file line number Diff line number Diff line change
Expand Up @@ -231,12 +231,12 @@
import { useScreenOrientation, useMouse } from "@vueuse/core";
import { KeyboardShortcuts, RoomKeyboardShortcutsKey } from "@/util/keyboard-shortcuts";
import VideoControls from "@/components/controls/VideoControls.vue";
import { VOLUME_KEY } from "@/components/controls/controlkeys";
import RestoreQueue from "@/components/RestoreQueue.vue";
import VoteSkip from "@/components/VoteSkip.vue";
import { waitForToken } from "@/util/token";
import { useSfx } from "@/plugins/sfx";
import { secondsToTimestamp } from "@/util/timestamp";
import { useVolume } from "@/components/composables";

const VIDEO_CONTROLS_HIDE_TIMEOUT = 3000;

Expand Down Expand Up @@ -432,8 +432,7 @@

// player management
const player = ref<typeof OmniPlayer | null>(null);
const volume = ref(100);
provide(VOLUME_KEY, [volume, updateVolume]);
const volume = useVolume();

Check warning on line 435 in client/src/views/Room.vue

View check run for this annotation

Codecov / codecov/patch

client/src/views/Room.vue#L435

Added line #L435 was not covered by tests

function isPlayerPresent(p: Ref<typeof OmniPlayer | null>): p is Ref<typeof OmniPlayer> {
return !!p.value;
Expand Down Expand Up @@ -484,25 +483,6 @@
applyIsPlaying(store.state.room.isPlaying);
}

function updateVolume(value: number | undefined = undefined) {
if (value !== undefined) {
volume.value = value;
}
if (!isPlayerPresent(player)) {
return;
}
player.value.setVolume(volume.value);
}

onMounted(() => {
volume.value = store.state.settings.volume;
});

watch(volume, () => {
updateVolume();
store.commit("settings/UPDATE", { volume: volume.value });
});

function onPlayerApiReady() {
console.debug("internal player API is now ready");
}
Expand All @@ -514,7 +494,6 @@
} else {
activateVideoControls();
}
updateVolume();
if (changeTo === store.state.room.isPlaying) {
return;
}
Expand Down
Loading