From fef005de2cc8247f27d8a90f63b18c3c195c8d0b Mon Sep 17 00:00:00 2001 From: Misode Date: Fri, 6 May 2022 17:31:40 +0200 Subject: [PATCH] Code-split howler --- src/app/components/sounds/SoundConfig.tsx | 7 ++++--- src/app/pages/Sounds.tsx | 13 ++++++++++++- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/app/components/sounds/SoundConfig.tsx b/src/app/components/sounds/SoundConfig.tsx index 216e4847..f13623f7 100644 --- a/src/app/components/sounds/SoundConfig.tsx +++ b/src/app/components/sounds/SoundConfig.tsx @@ -1,4 +1,4 @@ -import { Howl } from 'howler' +import type { Howl, HowlOptions } from 'howler' import { useEffect, useRef, useState } from 'preact/hooks' import { Btn, NumberInput, RangeInput, TextInput } from '..' import { useLocale, useVersion } from '../../contexts' @@ -13,12 +13,13 @@ export interface SoundConfig { volume: number, } type SoundConfigProps = SoundConfig & { + howler: (options: HowlOptions) => Howl, sounds: SoundEvents, onEdit: (changes: Partial) => unknown, onDelete: () => unknown, delayedPlay?: number, } -export function SoundConfig({ sounds, sound, delay, pitch, volume, onEdit, onDelete, delayedPlay }: SoundConfigProps) { +export function SoundConfig({ howler, sounds, sound, delay, pitch, volume, onEdit, onDelete, delayedPlay }: SoundConfigProps) { const { locale } = useLocale() const { version } = useVersion() const [loading, setLoading] = useState(true) @@ -34,7 +35,7 @@ export function SoundConfig({ sounds, sound, delay, pitch, volume, onEdit, onDel howls.current = (soundEvent?.sounds ?? []).map(entry => { const soundPath = typeof entry === 'string' ? entry : entry.name const url = getSoundUrl(version, soundPath) - const howl = new Howl({ + const howl = howler({ src: [url], format: ['ogg'], volume, diff --git a/src/app/pages/Sounds.tsx b/src/app/pages/Sounds.tsx index 513d049b..665549fe 100644 --- a/src/app/pages/Sounds.tsx +++ b/src/app/pages/Sounds.tsx @@ -1,3 +1,4 @@ +import type { Howl, HowlOptions } from 'howler' import { useEffect, useRef, useState } from 'preact/hooks' import config from '../../config.json' import { Btn, BtnMenu, ErrorPanel, SoundConfig, TextInput } from '../components' @@ -15,6 +16,14 @@ export function Sounds({}: Props) { const [error, setError] = useState(null) useTitle(locale('title.sounds')) + const [howler, setHowler] = useState Howl)>(undefined) + useEffect(() => { + (async () => { + const howler = await import('howler') + setHowler(() => (options: HowlOptions) => new howler.Howl(options)) + })() + }, []) + const [sounds, setSounds] = useState({}) const soundKeys = Object.keys(sounds ?? {}) useEffect(() => { @@ -71,7 +80,9 @@ export function Sounds({}: Props) {
- {configs.map(c => )} + {howler && configs.map(c => + + )}
}