Skip to content

Commit

Permalink
chore: update types
Browse files Browse the repository at this point in the history
  • Loading branch information
ambar committed Sep 17, 2021
1 parent 5c396fc commit 02f5c05
Show file tree
Hide file tree
Showing 23 changed files with 234 additions and 148 deletions.
5 changes: 2 additions & 3 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
**/dist
**/esm
**/cjs
dist
coverage
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ type VideoProps = NativeVideoProps & {
onRef(el: HTMLVideoElement | null): void
}

export default class Video extends Component<VideoProps> {
export default class VideoComponent extends Component<VideoProps> {
hls: any
src: any
video: any
src!: string
video: HTMLVideoElement | null = null
manuallyBuildAdaptiveM3U8Blob = false
hasLoadStarted = false

Expand All @@ -35,7 +35,7 @@ export default class Video extends Component<VideoProps> {
this.src = URL.createObjectURL(master)
this.manuallyBuildAdaptiveM3U8Blob = true
} else {
this.src = src
this.src = src!
}

this.hls.loadSource(this.src)
Expand All @@ -57,15 +57,15 @@ export default class Video extends Component<VideoProps> {
} else {
// TODO: 没有在 hls 的 API 内部找到顺畅切换 source 的方法
// 因此这里比较直接和生硬
const currentTime = this.video.currentTime
const currentTime = this.video!.currentTime
this.hls.destroy()
this.hls = new Hls({autoStartLoad: false})
this.hls.attachMedia(this.video)
this.hls.loadSource(source.source)
this.video.currentTime = currentTime
this.video!.currentTime = currentTime
this.hls.startLoad()
if (!paused) {
this.video.play()
void this.video!.play()
}
}
} else {
Expand Down
2 changes: 1 addition & 1 deletion packages/griffith-hls/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import VideoComponent from './Video'
import VideoComponent from './VideoComponent'

export default {
pluginName: 'griffith-hls',
Expand Down
19 changes: 14 additions & 5 deletions packages/griffith-hls/src/utils/createMasterM3U8.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
export default function createMasterM3U8(list: any) {
type Item = {
bandwidth: number
resolution: {
height: number
width: number
}
source: string
}

export default function createMasterM3U8(list: Item[]) {
const result = []
result.push('#EXTM3U')

list.forEach((item: any) => {
const meta = {
list.forEach((item) => {
const meta: Record<string, unknown> = {
'PROGRAM-ID': '1',
BANDWIDTH: String(item.bandwidth),
}
if (item.resolution) {
const {width, height} = item.resolution
;(meta as any).RESOLUTION = `${width}x${height}`
meta.RESOLUTION = `${width}x${height}`
}
result.push(
`#EXT-X-STREAM-INF:${Object.entries(meta)
.map(([key, value]) => `${key}=${value}`)
.map(([key, value]) => `${key}=${value as string}`)
.join(',')}`
)

Expand Down
11 changes: 9 additions & 2 deletions packages/griffith-hls/src/utils/getMasterM3U8Blob.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import createMasterM3U8 from './createMasterM3U8'

export default (sources: any) => {
const list = sources.map((item: any) => ({
type Source = {
source: string
bitrate: number
height: number
width: number
}

export default (sources: Source[]): Blob => {
const list = sources.map((item) => ({
source: item.source,
bandwidth: item.bitrate * 1024,

Expand Down
1 change: 1 addition & 0 deletions packages/griffith/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
},
"dependencies": {
"@types/lodash": "^4.14.172",
"@types/react": "^17.0.21",
"aphrodite": "^2.4.0",
"eventemitter3": "^3.1.0",
"griffith-hls": "^1.18.0",
Expand Down
46 changes: 26 additions & 20 deletions packages/griffith/src/components/Controller.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,26 @@ import PipButtonItem from './items/PipButtonItem'
import styles from './Controller.styles'
import PlaybackRateMenuItem from './items/PlaybackRateMenuItem'

type OwnProps = {
export type ToggleType = 'button' | 'keyCode' | 'video' | null

type ControllerProps = {
standalone?: boolean
isPlaying?: boolean
duration?: number
currentTime?: number
volume?: number
currentTime: number
volume: number
buffered?: number
isFullScreen?: boolean
isPageFullScreen: boolean
isPip: boolean
onDragStart?: (...args: any[]) => any
onDragEnd?: (...args: any[]) => any
onPlay?: (...args: any[]) => any
onPause?: (...args: any[]) => any
onSeek?: (...args: any[]) => any
onDragStart?: () => void
onDragEnd?: () => void
onPlay?: (type: ToggleType) => void
onPause?: (type: ToggleType) => void
onSeek?: (currentTime: number) => void
onQualityChange?: (...args: any[]) => any
onVolumeChange?: (...args: any[]) => any
onToggleFullScreen?: (...args: void[]) => void
onVolumeChange?: (volume: number) => void
onToggleFullScreen?: () => void
onTogglePageFullScreen: (...args: void[]) => void
onTogglePip?: (...args: void[]) => void
onProgressDotHover?: (...args: any[]) => any
Expand All @@ -49,11 +51,14 @@ type OwnProps = {
hiddenPlaybackRateItem?: boolean
}

type State = any

type Props = OwnProps & typeof Controller.defaultProps
type State = {
slideTime?: number | null
isVolumeHovered: boolean
isVolumeDragging: boolean
isVolumeKeyboard: boolean
}

class Controller extends Component<Props, State> {
class Controller extends Component<ControllerProps, State> {
static defaultProps = {
show: false,
standalone: false,
Expand Down Expand Up @@ -91,7 +96,8 @@ class Controller extends Component<Props, State> {
}
}

shouldComponentUpdate(nextProps: Props) {
shouldComponentUpdate(nextProps: ControllerProps) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return this.props.show || nextProps.show
}

Expand All @@ -102,13 +108,13 @@ class Controller extends Component<Props, State> {
}
}

onDragMove = (slideTime: any) => {
onDragMove = (slideTime: number) => {
const {duration} = this.props
slideTime = clamp(slideTime, 0, duration)
this.setState({slideTime})
}

handleToggle = (type: any) => {
handleToggle = (type: ToggleType) => {
const {isPlaying, onPlay, onPause} = this.props
if (!isPlaying && onPlay) {
onPlay(type)
Expand All @@ -118,7 +124,7 @@ class Controller extends Component<Props, State> {
}
}

handleSeek = (currentTime: any) => {
handleSeek = (currentTime: number) => {
const {duration, onSeek} = this.props
currentTime = clamp(currentTime, 0, duration)
if (onSeek) {
Expand All @@ -127,7 +133,7 @@ class Controller extends Component<Props, State> {
}
}

handleVolumeChange = (volume: any) => {
handleVolumeChange = (volume: number) => {
volume = clamp(volume, 0, 1)
const {onVolumeChange} = this.props
if (onVolumeChange) {
Expand Down Expand Up @@ -174,7 +180,7 @@ class Controller extends Component<Props, State> {
}
}

handleKeyDown = (event: any) => {
handleKeyDown = (event: KeyboardEvent) => {
const {duration, currentTime, volume, show, onToggleFullScreen} = this.props
let handled = true
switch (event.keyCode) {
Expand Down
3 changes: 2 additions & 1 deletion packages/griffith/src/components/MinimalTimeline.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ class MinimalTimeline extends Component<Props> {
progressDots: [] as ProgressDot[],
}

shouldComponentUpdate(nextProps: Props) {
shouldComponentUpdate(nextProps: Props): boolean {
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return this.props.show || nextProps.show
}

Expand Down
48 changes: 35 additions & 13 deletions packages/griffith/src/components/Player.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import Icon from './Icon'
import * as icons from './icons/display/index'
import Loader from './Loader'
import Video from './Video'
import Controller from './Controller'
import Controller, {ToggleType} from './Controller'
import VolumeItem from './items/VolumeItem'
import MinimalTimeline from './MinimalTimeline'
import getBufferedTime from '../utils/getBufferedTime'
Expand Down Expand Up @@ -97,7 +97,25 @@ type OuterPlayerProps = {
export type PlayerProps = Omit<InnerPlayerProps, keyof ProviderOnlyProps> &
OuterPlayerProps

type State = any
type State = {
isPlaybackStarted: boolean
isNeverPlayed: boolean
lastAction?: 'play' | 'pause' | null
isDataLoaded: boolean
isPlaying: boolean
isLoading: boolean
duration: number
currentTime: number
volume: number
buffered: {start: number; end: number}[][]
isControllerShown: boolean
isControllerHovered: boolean
isControllerDragging: boolean
type: ToggleType
hovered: boolean
pressed: boolean
isEnterPageFullScreen: boolean
}

class InnerPlayer extends Component<InnerPlayerProps, State> {
static contextType = VideoSourceContext
Expand Down Expand Up @@ -136,9 +154,12 @@ class InnerPlayer extends Component<InnerPlayerProps, State> {

// refs
playerRef = React.createRef<HTMLDivElement>()
videoRef = React.createRef()
videoRef = React.createRef<{
root: HTMLVideoElement
seek(currentTime: number): void
}>()

static getDerivedStateFromProps = (props: any, state: any) => {
static getDerivedStateFromProps = (props: InnerPlayerProps, state: any) => {
const {duration} = props

const shouldUpdateDuration = duration && !state.duration
Expand All @@ -159,7 +180,7 @@ class InnerPlayer extends Component<InnerPlayerProps, State> {

const historyVolume = storage.get('@griffith/history-volume')
if (historyVolume) {
this.setState({volume: historyVolume})
this.setState({volume: historyVolume as number})
}

this.actionSubscriptions_ = [
Expand All @@ -177,7 +198,7 @@ class InnerPlayer extends Component<InnerPlayerProps, State> {
),
]

if ((this as any).videoRef.current.root) {
if (this.videoRef.current!.root) {
if (this.props.muted) {
this.handleVideoVolumeChange(0)
}
Expand Down Expand Up @@ -210,7 +231,7 @@ class InnerPlayer extends Component<InnerPlayerProps, State> {
isControllerHovered,
isControllerDragging,
currentTime,
}: any) => {
}: Partial<State>) => {
// 播放中:暂停 或 Controller shown/hovered/dragging 时展示 Controller
if (isPlaybackStarted) {
return (
Expand All @@ -235,11 +256,11 @@ class InnerPlayer extends Component<InnerPlayerProps, State> {
initPip = () => {
if (
!this.props.disablePictureInPicture &&
(this as any).videoRef.current.root &&
this.videoRef.current!.root &&
!Pip.inited
) {
Pip.init(
(this as any).videoRef.current.root,
this.videoRef.current!.root,
() => this.props.onEvent(EVENTS.ENTER_PIP),
() => this.props.onEvent(EVENTS.EXIT_PIP)
)
Expand All @@ -266,7 +287,7 @@ class InnerPlayer extends Component<InnerPlayerProps, State> {
}
}

handlePlay = (type: 'video' | null = null) => {
handlePlay = (type: ToggleType = null) => {
const {onEvent, onBeforePlay} = this.props
const {currentSrc} = this.context
onEvent(EVENTS.REQUEST_PLAY)
Expand All @@ -293,7 +314,7 @@ class InnerPlayer extends Component<InnerPlayerProps, State> {
})
}

handlePause = (type: 'video' | 'button' | null = null) => {
handlePause = (type: ToggleType = null) => {
this.props.onEvent(EVENTS.REQUEST_PAUSE)
const {isLoading} = this.state

Expand Down Expand Up @@ -368,7 +389,7 @@ class InnerPlayer extends Component<InnerPlayerProps, State> {
!isPlaybackStarted && !isNeverPlayed && stateCurrentTime !== 0 // 播放结束,显示「重新播放」状态
this.setState({currentTime})
// TODO 想办法去掉这个实例方法调用
;(this as any).videoRef.current.seek(currentTime)
this.videoRef.current!.seek(currentTime)
if (isPlayEnded) {
this.handlePlay()
}
Expand Down Expand Up @@ -814,7 +835,8 @@ const Player: React.FC<PlayerProps> = ({
<InternalMessageContext.Consumer>
{({emitEvent, subscribeAction}) => (
<VideoSourceProvider
onEvent={emitEvent}
// TODO:改名 emitEvent
onEvent={emitEvent as any}
sources={sources}
id={id}
defaultQuality={defaultQuality}
Expand Down
Loading

0 comments on commit 02f5c05

Please sign in to comment.