Add Player
This commit is contained in:
parent
c5ae8033a5
commit
004b2002f4
@ -4,10 +4,12 @@ import { TextInput } from './components/TextInput'
|
|||||||
import { TextOutput } from './components/TextOutput'
|
import { TextOutput } from './components/TextOutput'
|
||||||
import { RsvpReader } from './components/RsvpReader'
|
import { RsvpReader } from './components/RsvpReader'
|
||||||
|
|
||||||
|
import { IconContext } from 'react-icons'
|
||||||
|
|
||||||
export function App() {
|
export function App() {
|
||||||
return (
|
return (
|
||||||
<div>
|
<IconContext.Provider value={{ color: 'grey', size: '1em' }}>
|
||||||
<RsvpReader></RsvpReader>
|
<RsvpReader></RsvpReader>
|
||||||
</div>
|
</IconContext.Provider>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -1,26 +1,74 @@
|
|||||||
import React, { useEffect, useState, useCallback } from 'react'
|
import React, { useState, useEffect, useRef } from 'react'
|
||||||
|
|
||||||
export const PlayerControl = ({ interval, onTick, onStart, onStop }) => {
|
import { FaPlay, FaPause, FaStop } from 'react-icons/fa'
|
||||||
const dispatch = useDispatch()
|
|
||||||
const [isPlaying, setPlaying] = useState(false)
|
|
||||||
|
|
||||||
const [handle, setHandle] = useState(null)
|
function safeCall(fn) {
|
||||||
|
typeof fn === 'function' && fn()
|
||||||
|
}
|
||||||
|
|
||||||
useEffect(() => {
|
export const Player = ({ onTick, onStart, onStop, onPause, delay }) => {
|
||||||
console.log('effect fired', isPlaying)
|
const [isRunning, start, stop] = usePlayer(stop => onTick(stop), delay)
|
||||||
clearInterval(handle)
|
|
||||||
if (isPlaying) {
|
const handleStart = () => {
|
||||||
setHandle(setInterval(() => dispatch(incrementSegment()), 500))
|
safeCall(onStart)
|
||||||
|
start()
|
||||||
|
}
|
||||||
|
|
||||||
|
const handleStop = () => {
|
||||||
|
safeCall(onStop)
|
||||||
|
stop()
|
||||||
|
}
|
||||||
|
|
||||||
|
const handlePause = () => {
|
||||||
|
safeCall(onPause)
|
||||||
|
stop()
|
||||||
}
|
}
|
||||||
}, [handle, isPlaying])
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
<button onClick={() => dispatch(stop())}>{'stop'}</button>
|
<button onClick={handleStop}>
|
||||||
<button onClick={() => dispatch(pause())}>{'pause'}</button>
|
<FaStop />
|
||||||
<button onClick={() => dispatch(start())}>{'play'}</button>
|
</button>
|
||||||
<div>playing: {isPlaying.toString()}</div>
|
{isRunning && (
|
||||||
<div>interval: {handle}</div>
|
<button onClick={handlePause}>
|
||||||
|
<FaPause />
|
||||||
|
</button>
|
||||||
|
)}{' '}
|
||||||
|
{!isRunning && (
|
||||||
|
<button onClick={handleStart}>
|
||||||
|
<FaPlay />
|
||||||
|
</button>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function usePlayer(onTick, delay) {
|
||||||
|
const [isRunning, setRunning] = useState(false)
|
||||||
|
const start = () => setRunning(true)
|
||||||
|
const stop = () => setRunning(false)
|
||||||
|
|
||||||
|
useInterval(() => onTick(stop), isRunning ? delay : null)
|
||||||
|
return [isRunning, start, stop]
|
||||||
|
}
|
||||||
|
|
||||||
|
// from: https://overreacted.io/making-setinterval-declarative-with-react-hooks/
|
||||||
|
function useInterval(callback, delay) {
|
||||||
|
const savedCallback = useRef()
|
||||||
|
|
||||||
|
// Remember the latest callback.
|
||||||
|
useEffect(() => {
|
||||||
|
savedCallback.current = callback
|
||||||
|
}, [callback])
|
||||||
|
|
||||||
|
// Set up the interval.
|
||||||
|
useEffect(() => {
|
||||||
|
function tick() {
|
||||||
|
savedCallback.current()
|
||||||
|
}
|
||||||
|
if (delay !== null) {
|
||||||
|
let id = setInterval(tick, delay)
|
||||||
|
return () => clearInterval(id)
|
||||||
|
}
|
||||||
|
}, [delay])
|
||||||
|
}
|
||||||
|
@ -1,40 +0,0 @@
|
|||||||
import React, { useEffect, useState, useCallback } from 'react'
|
|
||||||
|
|
||||||
import { useSelector, useDispatch } from 'react-redux'
|
|
||||||
import { selectPlaying, hasNextSegment } from '../store/selectors'
|
|
||||||
import { start, stop, pause, incrementSegment } from '../store/actions'
|
|
||||||
|
|
||||||
export const PlayerControl = ({ onTick }) => {
|
|
||||||
const dispatch = useDispatch()
|
|
||||||
const isPlaying = useSelector(selectPlaying)
|
|
||||||
const hasNext = useSelector(hasNextSegment)
|
|
||||||
const wpm = useSelector(state => state.wpm)
|
|
||||||
|
|
||||||
const [handle, setHandle] = useState(null)
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
clearInterval(handle)
|
|
||||||
if (isPlaying) {
|
|
||||||
setHandle(setInterval(() => dispatch(incrementSegment()), 60000 / wpm))
|
|
||||||
}
|
|
||||||
}, [isPlaying, wpm])
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
if (!hasNext) dispatch(pause())
|
|
||||||
}, [hasNext, dispatch])
|
|
||||||
|
|
||||||
return (
|
|
||||||
<div>
|
|
||||||
<button onClick={() => dispatch(stop())}>{'stop'}</button>
|
|
||||||
{isPlaying && (
|
|
||||||
<button onClick={() => dispatch(pause())}>{'pause'}</button>
|
|
||||||
)}{' '}
|
|
||||||
{!isPlaying && (
|
|
||||||
<button onClick={() => dispatch(start())}>{'play'}</button>
|
|
||||||
)}
|
|
||||||
<div>playing: {isPlaying.toString()}</div>
|
|
||||||
<div>interval: {handle}</div>
|
|
||||||
<div>has next: {hasNext.toString()}</div>
|
|
||||||
</div>
|
|
||||||
)
|
|
||||||
}
|
|
27
src/components/RsvpPlayer.js
Normal file
27
src/components/RsvpPlayer.js
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
import React from 'react'
|
||||||
|
|
||||||
|
import { useSelector, useDispatch } from 'react-redux'
|
||||||
|
import { hasNextSegment } from '../store/selectors'
|
||||||
|
import { incrementSegment, resetSegment } from '../store/actions'
|
||||||
|
import { Player } from './Player'
|
||||||
|
|
||||||
|
export const RsvpPlayer = () => {
|
||||||
|
const dispatch = useDispatch()
|
||||||
|
const hasNext = useSelector(hasNextSegment)
|
||||||
|
const wpm = useSelector(state => state.wpm)
|
||||||
|
|
||||||
|
const onTick = stop => {
|
||||||
|
if (!hasNext) return stop()
|
||||||
|
dispatch(incrementSegment())
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div>
|
||||||
|
<Player
|
||||||
|
delay={60000 / wpm}
|
||||||
|
onTick={onTick}
|
||||||
|
onStop={() => dispatch(resetSegment())}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
@ -5,7 +5,7 @@ import { TextOutput } from './TextOutput'
|
|||||||
import { MainControl } from './MainControl'
|
import { MainControl } from './MainControl'
|
||||||
import { RsvpSegment } from './RsvpSegment'
|
import { RsvpSegment } from './RsvpSegment'
|
||||||
import { RsvpOptions } from './RsvpOptions'
|
import { RsvpOptions } from './RsvpOptions'
|
||||||
import { PlayerControl } from './PlayerControl'
|
import { RsvpPlayer } from './RsvpPlayer'
|
||||||
|
|
||||||
const FlexRow = styled.div`
|
const FlexRow = styled.div`
|
||||||
display: flex;
|
display: flex;
|
||||||
@ -26,7 +26,7 @@ export const RsvpReader = () => {
|
|||||||
<RsvpSegment />
|
<RsvpSegment />
|
||||||
<MainControl></MainControl>
|
<MainControl></MainControl>
|
||||||
<RsvpOptions></RsvpOptions>
|
<RsvpOptions></RsvpOptions>
|
||||||
<PlayerControl />
|
<RsvpPlayer />
|
||||||
</FlexItem>
|
</FlexItem>
|
||||||
<FlexItem>
|
<FlexItem>
|
||||||
<TextOutput />
|
<TextOutput />
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
export const setText = text => ({ type: 'SET_TEXT', payload: text })
|
export const setText = text => ({ type: 'SET_TEXT', payload: text })
|
||||||
|
export const resetSegment = () => ({ type: 'SET_CURRENT_SEGMENT', payload: 0 })
|
||||||
export const incrementSegment = () => ({ type: 'INC_SEGMENT' })
|
export const incrementSegment = () => ({ type: 'INC_SEGMENT' })
|
||||||
export const decrementSegment = () => ({ type: 'DEC_SEGMENT' })
|
export const decrementSegment = () => ({ type: 'DEC_SEGMENT' })
|
||||||
export const incrementWord = () => ({ type: 'INC_WORD' })
|
export const incrementWord = () => ({ type: 'INC_WORD' })
|
||||||
@ -10,6 +11,3 @@ export const setMaxLength = length => ({
|
|||||||
payload: length
|
payload: length
|
||||||
})
|
})
|
||||||
export const setWpm = wpm => ({ type: 'SET_WPM', payload: wpm })
|
export const setWpm = wpm => ({ type: 'SET_WPM', payload: wpm })
|
||||||
export const start = () => ({ type: 'START' })
|
|
||||||
export const pause = () => ({ type: 'PAUSE' })
|
|
||||||
export const stop = () => ({ type: 'STOP' })
|
|
||||||
|
@ -27,10 +27,7 @@ const reducer = {
|
|||||||
INC_SENTENCE: state => ({ ...state, curIdx: selectNextSentence(state) }),
|
INC_SENTENCE: state => ({ ...state, curIdx: selectNextSentence(state) }),
|
||||||
DEC_SENTENCE: state => ({ ...state, curIdx: selectPrevSentence(state) }),
|
DEC_SENTENCE: state => ({ ...state, curIdx: selectPrevSentence(state) }),
|
||||||
SET_MAX_LENGTH: (state, payload) => ({ ...state, maxLength: payload }),
|
SET_MAX_LENGTH: (state, payload) => ({ ...state, maxLength: payload }),
|
||||||
SET_WPM: (state, payload) => ({ ...state, wpm: payload }),
|
SET_WPM: (state, payload) => ({ ...state, wpm: payload })
|
||||||
START: state => ({ ...state, isPlaying: true }),
|
|
||||||
PAUSE: state => ({ ...state, isPlaying: false }),
|
|
||||||
STOP: state => ({ ...state, isPlaying: false, curIdx: 0 })
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export const reducerFn = (state, { type, payload }) => {
|
export const reducerFn = (state, { type, payload }) => {
|
||||||
|
@ -77,7 +77,3 @@ export const selectPrevSentence = createSelector(
|
|||||||
|
|
||||||
export const selectHasNextSentence = createSelector(selectNextSentence, Boolean)
|
export const selectHasNextSentence = createSelector(selectNextSentence, Boolean)
|
||||||
export const selectHasPrevSentence = createSelector(selectPrevSentence, Boolean)
|
export const selectHasPrevSentence = createSelector(selectPrevSentence, Boolean)
|
||||||
|
|
||||||
// player
|
|
||||||
|
|
||||||
export const selectPlaying = state => state.isPlaying
|
|
||||||
|
Loading…
Reference in New Issue
Block a user