fix(589): added 2 sec timeout to /stream request (#234)

keep-around/af30b88d367751c9e05a735e4a0467a96238ef47
Mirlan 5 years ago committed by GitHub
parent b87f9ea622
commit 34424d86b7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 25
      src/features/MatchPage/hooks/useVideoData.tsx
  2. 39
      src/helpers/callApi/index.tsx
  3. 1
      src/helpers/callApi/types.tsx
  4. 6
      src/requests/getLiveVideos.tsx

@ -1,7 +1,9 @@
import { useEffect, useState } from 'react' import {
useCallback,
useEffect,
useState,
} from 'react'
import isNull from 'lodash/isNull'
import isEmpty from 'lodash/isEmpty'
import filter from 'lodash/filter' import filter from 'lodash/filter'
import type { LiveVideos, Videos } from 'requests' import type { LiveVideos, Videos } from 'requests'
@ -17,22 +19,21 @@ export const useVideoData = () => {
const { sportType } = useSportNameParam() const { sportType } = useSportNameParam()
const matchId = usePageId() const matchId = usePageId()
useEffect(() => { const fetchMatchVideos = useCallback(async () => {
const requestVideos = async () => {
const live = await getLiveVideos(sportType, matchId)
if (isNull(live) || isEmpty(live)) {
const videosResponse = await getVideos(sportType, matchId) const videosResponse = await getVideos(sportType, matchId)
const filteredVideosResponseByAbc = filter(videosResponse, (vid) => vid.abc !== 1) const filteredVideosResponseByAbc = filter(videosResponse, (vid) => vid.abc !== 1)
setVideos(filteredVideosResponseByAbc) setVideos(filteredVideosResponseByAbc)
} else { }, [sportType, matchId])
setLiveVideos(live)
} useEffect(() => {
} getLiveVideos(sportType, matchId)
requestVideos() .then(setLiveVideos)
.catch(fetchMatchVideos)
}, },
[ [
sportType, sportType,
matchId, matchId,
fetchMatchVideos,
]) ])
return { return {

@ -21,16 +21,33 @@ export const callApiBase = ({
return fetch(url, requestConfig) return fetch(url, requestConfig)
} }
export const callApi = ({ const callApiWithTimeout = (args: CallApiArgs) => {
abortSignal, const { abortSignal, timeout } = args
config, const abortController = new AbortController()
url, const timeoutId = setTimeout(
}: CallApiArgs) => ( () => abortController.abort(),
callApiBase({ timeout,
abortSignal, )
config,
url, if (abortSignal) {
}).then(checkStatus) abortSignal.addEventListener('abort', () => abortController.abort())
}
return callApiBase({
...args,
abortSignal: abortSignal || abortController.signal,
})
.then(checkStatus)
.then(parseJSON) .then(parseJSON)
.catch(logoutIfUnauthorized) .catch(logoutIfUnauthorized)
) .finally(() => clearTimeout(timeoutId))
}
export const callApi = (args: CallApiArgs) => {
if (args.timeout) return callApiWithTimeout(args)
return callApiBase(args)
.then(checkStatus)
.then(parseJSON)
.catch(logoutIfUnauthorized)
}

@ -7,5 +7,6 @@ export type RequestConfig = {
export type CallApiArgs = { export type CallApiArgs = {
abortSignal?: AbortSignal, abortSignal?: AbortSignal,
config: RequestConfig, config: RequestConfig,
timeout?: number,
url: string, url: string,
} }

@ -1,3 +1,5 @@
import isEmpty from 'lodash/isEmpty'
import { API_ROOT, SportTypes } from 'config' import { API_ROOT, SportTypes } from 'config'
import { callApi } from 'helpers' import { callApi } from 'helpers'
@ -16,6 +18,10 @@ export const getLiveVideos = (
return callApi({ return callApi({
config, config,
timeout: 2000,
url: `${API_ROOT}/video/stream`, url: `${API_ROOT}/video/stream`,
}).then((videos: LiveVideos) => {
if (isEmpty(videos)) return Promise.reject()
return videos
}) })
} }

Loading…
Cancel
Save