Skip to content

Openverse is a search engine for openly-licensed media. This monorepo includes all application code.

Fix audio track null duration and add defaultRef (#1129)

sarayourfriend16ac9eee2d2c
diff --git a/.eslintrc.js b/.eslintrc.js
index 666813033..bbdc48eb6 100644
--- a/.eslintrc.js
+++ b/.eslintrc.js
@@ -11,8 +11,8 @@ module.exports = {
parser: '@typescript-eslint/parser',
},
extends: [
- 'plugin:@typescript-eslint/recommended',
'eslint:recommended',
+ 'plugin:@typescript-eslint/recommended',
'plugin:vue/recommended',
'plugin:prettier/recommended',
'plugin:vuejs-accessibility/recommended',
diff --git a/src/components/VAudioTrack/VAudioTrack.vue b/src/components/VAudioTrack/VAudioTrack.vue
index a2b477464..754b9fa88 100644
--- a/src/components/VAudioTrack/VAudioTrack.vue
+++ b/src/components/VAudioTrack/VAudioTrack.vue
@@ -49,6 +49,7 @@ import {
} from '@nuxtjs/composition-api'
import { useActiveAudio } from '~/composables/use-active-audio'
+import { defaultRef } from '~/composables/default-ref'
import { MEDIA } from '~/constants/store-modules'
import { useActiveMediaStore } from '~/stores/active-media'
@@ -126,7 +127,6 @@ export default defineComponent({
const status = ref('paused')
const currentTime = ref(0)
- const audioDuration = ref(null)
const initLocalAudio = () => {
// Preserve existing local audio if we plucked it from the global active audio
@@ -220,8 +220,16 @@ export default defineComponent({
}
}
}
+ const duration = defaultRef(() => {
+ if (localAudio) {
+ return localAudio.duration
+ }
+ if (typeof props.audio?.duration === 'number')
+ return props.audio.duration / 1e3
+ return 0
+ })
const setDuration = () => {
- audioDuration.value = localAudio?.duration
+ if (localAudio) duration.value = localAudio.duration
}
/**
@@ -293,10 +301,6 @@ export default defineComponent({
/* Timekeeping */
- const duration = computed(
- () => audioDuration.value ?? props.audio?.duration / 1e3 ?? 0 // seconds
- )
-
const message = computed(() => activeMediaStore.message)
/* Interface with VPlayPause */
diff --git a/src/components/VAudioTrack/VGlobalAudioTrack.vue b/src/components/VAudioTrack/VGlobalAudioTrack.vue
index 79d9ca7bd..a018f019e 100644
--- a/src/components/VAudioTrack/VGlobalAudioTrack.vue
+++ b/src/components/VAudioTrack/VGlobalAudioTrack.vue
@@ -32,6 +32,7 @@
import { computed, defineComponent, ref, watch } from '@nuxtjs/composition-api'
import { useActiveAudio } from '~/composables/use-active-audio'
+import { defaultRef } from '~/composables/default-ref'
import { useActiveMediaStore } from '~/stores/active-media'
@@ -107,7 +108,11 @@ export default defineComponent({
const status = ref('paused')
const currentTime = ref(0)
- const audioDuration = ref(null)
+ const duration = defaultRef(() => {
+ if (typeof props.audio?.duration === 'number')
+ return props.audio.duration / 1e3
+ return 0
+ })
const setPlaying = () => {
status.value = 'playing'
@@ -129,7 +134,7 @@ export default defineComponent({
}
}
const setDuration = () => {
- audioDuration.value = activeAudio.obj.value?.duration
+ if (activeAudio.obj.value) duration.value = activeAudio.obj.value.duration
}
const updateTimeLoop = () => {
@@ -149,7 +154,7 @@ export default defineComponent({
audio.addEventListener('timeupdate', setTimeWhenPaused)
audio.addEventListener('durationchange', setDuration)
currentTime.value = audio.currentTime
- audioDuration.value = audio.duration
+ duration.value = audio.duration
/**
* By the time the `activeAudio` is updated and a rerender
@@ -189,11 +194,6 @@ export default defineComponent({
const pause = () => activeAudio.obj.value?.pause()
/* Timekeeping */
-
- const duration = computed(
- () => audioDuration.value ?? props.audio?.duration / 1e3 ?? 0
- ) // seconds
-
const message = computed(() => activeMediaStore.message)
/* Interface with VPlayPause */
diff --git a/src/composables/default-ref.ts b/src/composables/default-ref.ts
new file mode 100644
index 000000000..a746a3add
--- /dev/null
+++ b/src/composables/default-ref.ts
@@ -0,0 +1,22 @@
+import { computed, ref, UnwrapRef } from '@nuxtjs/composition-api'
+
+const NotSet = Symbol('NotSet')
+
+/**
+ * Produces a `computed` that returns a default value until
+ * an explicit value is set.
+ *
+ * @param getDefault - A function returning the default value
+ */
+export const defaultRef = <T>(getDefault: () => T) => {
+ const explicitlySet = ref<T | typeof NotSet>(NotSet)
+ return computed<T>({
+ get() {
+ if (explicitlySet.value === NotSet) return getDefault()
+ return explicitlySet.value as T
+ },
+ set(v) {
+ explicitlySet.value = v as UnwrapRef<T>
+ },
+ })
+}
diff --git a/test/unit/specs/composables/default-ref.spec.ts b/test/unit/specs/composables/default-ref.spec.ts
new file mode 100644
index 000000000..037b21b54
--- /dev/null
+++ b/test/unit/specs/composables/default-ref.spec.ts
@@ -0,0 +1,24 @@
+import { defaultRef } from '~/composables/default-ref'
+
+describe('defaultRef', () => {
+ it('should use the default value', () => {
+ const getDefault = () => 100
+ const value = defaultRef(getDefault)
+ expect(value.value).toBe(100)
+ })
+
+ it('should use the set value', () => {
+ const value = defaultRef(() => 'a')
+ value.value = 'b'
+ expect(value.value).toBe('b')
+ })
+
+ it.each([0, '', false, undefined, null] as const)(
+ 'should use the set value even if it is falsy as `%s`',
+ (v) => {
+ const value = defaultRef(() => 'non-null default' as typeof v)
+ value.value = v
+ expect(value.value).toBe(v)
+ }
+ )
+})