diff --git a/lib/audio/audio.ts b/lib/audio/audio.ts deleted file mode 100644 index 0d4137c..0000000 --- a/lib/audio/audio.ts +++ /dev/null @@ -1,100 +0,0 @@ -import { createPubSub } from '../pubsub'; -import { AudioState } from './types'; - -export const createAudio = () => { - const pubsub = createPubSub(); - const element = document.createElement('video'); - let currentTime = 0; - - let state: AudioState = { - duration: 0, - playing: false, - volume: 0, - }; - - const setState = (value: Partial) => { - state = { ...state, ...value }; - - pubsub.publish('change', state); - }; - - const setup = () => { - element.addEventListener('durationchange', () => - setState({ duration: element.duration }), - ); - - element.addEventListener('playing', () => setState({ playing: true })); - - element.addEventListener('pause', () => setState({ playing: false })); - - element.addEventListener('timeupdate', () => { - const newCurrentTime = Math.round(element.currentTime); - - if (currentTime !== newCurrentTime) { - currentTime = newCurrentTime; - - pubsub.publish('change-current-time', currentTime); - } - }); - - element.addEventListener('volumechange', () => - setState({ volume: element.volume }), - ); - - setState({ volume: element.volume }); - }; - - setup(); - - return { - seek(seconds: number) { - element.currentTime = seconds; - currentTime = seconds; - - pubsub.publish('change-current-time', currentTime); - }, - - getElement() { - return element; - }, - - getState() { - return state; - }, - - getCurrentTime() { - return currentTime; - }, - - play() { - element.play(); - }, - - pause() { - element.pause(); - }, - - volume(value: number) { - element.volume = value; - }, - - setUrl(url: string) { - element.setAttribute('src', url); - setState({ playing: false }); - }, - - subscribe(listener: (newState: AudioState) => void) { - return pubsub.subscribe('change', listener); - }, - - onChangeCurrentTime(listener: (newCurrentTime: number) => void) { - return pubsub.subscribe('change-current-time', listener); - }, - - onEnded(listener: () => void) { - element.addEventListener('ended', listener); - - return () => element.removeEventListener('ended', listener); - }, - }; -}; diff --git a/lib/audio/hooks.ts b/lib/audio/hooks.ts deleted file mode 100644 index 9704390..0000000 --- a/lib/audio/hooks.ts +++ /dev/null @@ -1,27 +0,0 @@ -import { useState, useEffect } from 'react'; - -import player from './player'; - -export const usePlayerState = () => { - const [state, setState] = useState(player.getState()); - - useEffect(() => { - const unsubscribe = player.subscribe(setState); - - return unsubscribe; - }, []); - - return state; -}; - -export const useCurrentTime = () => { - const [currentTime, setCurrentTime] = useState(player.getCurrentTime()); - - useEffect(() => { - const unsubscribe = player.onChangeCurrentTime(setCurrentTime); - - return unsubscribe; - }, []); - - return currentTime; -}; diff --git a/lib/audio/player.ts b/lib/audio/player.ts deleted file mode 100644 index bb00fef..0000000 --- a/lib/audio/player.ts +++ /dev/null @@ -1,106 +0,0 @@ -import { createPubSub } from '../pubsub'; -import { createAudio } from './audio'; -import { State, Track } from './types'; - -const createPlayer = () => { - const pubsub = createPubSub(); - const audio = createAudio(); - - let state: State = { - ...audio.getState(), - tracks: [], - currentTrackIndex: null, - currentTrack: null, - }; - - const setState = (value: Partial) => { - state = { ...state, ...value }; - - pubsub.publish('change', state); - }; - - audio.subscribe(setState); - - const changeTrack = () => { - const track = state.currentTrack; - - if (track) { - audio.setUrl(track.url); - audio.play(); - } - }; - - const next = () => { - if (state.currentTrackIndex === null) { - return; - } - - const lastIndex = state.tracks.length - 1; - const newIndex = state.currentTrackIndex + 1; - - if (newIndex <= lastIndex) { - setState({ - currentTrackIndex: newIndex, - currentTrack: state.tracks[newIndex], - }); - - changeTrack(); - } - }; - - audio.onEnded(next); - - return { - play: audio.play, - pause: audio.pause, - seek: audio.seek, - volume: audio.volume, - getCurrentTime: audio.getCurrentTime, - getElement: audio.getElement, - onChangeCurrentTime: audio.onChangeCurrentTime, - - getState() { - return state; - }, - - setQueue(tracks: Track[]) { - setState({ tracks }); - }, - - playTrack(trackIndex: number) { - setState({ - currentTrackIndex: trackIndex, - currentTrack: state.tracks[trackIndex], - }); - - changeTrack(); - }, - - next, - - prev() { - if (state.currentTrackIndex === null) { - return; - } - - const newIndex = state.currentTrackIndex - 1; - - if (newIndex >= 0) { - setState({ - currentTrack: state.tracks[newIndex], - currentTrackIndex: newIndex, - }); - - changeTrack(); - } - }, - - subscribe(listener: (newState: State) => void) { - return pubsub.subscribe('change', listener); - }, - }; -}; - -const player = createPlayer(); - -export default player; diff --git a/lib/audio/types.ts b/lib/audio/types.ts deleted file mode 100644 index 138f9d3..0000000 --- a/lib/audio/types.ts +++ /dev/null @@ -1,17 +0,0 @@ -export type AudioState = { - duration: number; - playing: boolean; - volume: number; - }; - - export type Track = { - url: string; - title: string; - }; - - export type State = AudioState & { - tracks: Track[]; - currentTrack: Track | null; - currentTrackIndex: number | null; - }; - \ No newline at end of file