Skip to content

Commit

Permalink
fix: use xhr instead of the fetch api (#56)
Browse files Browse the repository at this point in the history
* fix: use xhr instead of the fetch api
  • Loading branch information
xiaoyuhen authored Apr 3, 2019
1 parent 110a612 commit 267b56a
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 24 deletions.
1 change: 0 additions & 1 deletion example/fmp4/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/* eslint-disable import/named */
import '@babel/polyfill'
import 'raf/polyfill'
import 'whatwg-fetch'
import React from 'react'
import ReactDOM from 'react-dom'
import App from './App'
Expand Down
1 change: 0 additions & 1 deletion example/hls/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/* eslint-disable import/named */
import '@babel/polyfill'
import 'raf/polyfill'
import 'whatwg-fetch'
import React from 'react'
import ReactDOM from 'react-dom'
import App from './App'
Expand Down
1 change: 0 additions & 1 deletion example/inline/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/* eslint-disable import/named */
import '@babel/polyfill'
import 'raf/polyfill'
import 'whatwg-fetch'
import React from 'react'
import ReactDOM from 'react-dom'
import App from './App'
Expand Down
1 change: 0 additions & 1 deletion example/main/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/* eslint-disable import/named */
import '@babel/polyfill'
import 'raf/polyfill'
import 'whatwg-fetch'
import React from 'react'
import ReactDOM from 'react-dom'
import App from './App'
Expand Down
1 change: 0 additions & 1 deletion example/mp4/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/* eslint-disable import/named */
import '@babel/polyfill'
import 'raf/polyfill'
import 'whatwg-fetch'
import React from 'react'
import ReactDOM from 'react-dom'
import App from './App'
Expand Down
3 changes: 1 addition & 2 deletions example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
"raf": "^3.4.1",
"react": "16.4.2",
"react-dom": "16.4.2",
"react-hot-loader": "^4.3.12",
"whatwg-fetch": "^3.0.0"
"react-hot-loader": "^4.3.12"
},
"devDependencies": {
"@zhihu/babel-preset": "^1.7.0",
Expand Down
38 changes: 22 additions & 16 deletions packages/griffith-mp4/src/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,39 @@ export default class FragmentFetch {

constructor(url, start, end, callback) {
this.start = start
this.controller = new AbortController()
FragmentFetch.queue.push(this)
return fetch(url, {
headers: {
Range: `bytes=${start}-${end}`,
},
signal: this.controller.signal,
})
.then(res => {
callback(res.arrayBuffer())
this.remove()
})
.catch(() => {
this.remove()
})

const xhr = new XMLHttpRequest()
this.xhr = xhr
xhr.open('get', url)
xhr.responseType = 'arraybuffer'
xhr.setRequestHeader('Range', `bytes=${start}-${end}`)
xhr.onload = () => {
if (xhr.status === 200 || xhr.status === 206) {
callback(xhr.response)
}
this.remove()
}
xhr.send()

xhr.onerror = () => {
this.remove()
}
xhr.onabort = () => {
this.remove()
}
}

remove = () => {
FragmentFetch.queue = FragmentFetch.queue.filter(
item => item.start !== this.statr
item => item.start !== this.start
)
}

static clear() {
while (FragmentFetch.queue.length) {
const item = FragmentFetch.queue.shift()
item.controller.abort()
item.xhr.abort()
}
}
}
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10366,7 +10366,7 @@ whatwg-encoding@^1.0.1, whatwg-encoding@^1.0.3:
dependencies:
iconv-lite "0.4.24"

whatwg-fetch@>=0.10.0, whatwg-fetch@^3.0.0:
whatwg-fetch@>=0.10.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/whatwg-fetch/-/whatwg-fetch-3.0.0.tgz#fc804e458cc460009b1a2b966bc8817d2578aefb"
integrity sha512-9GSJUgz1D4MfyKU7KRqwOjXCXTqWdFNvEr7eUBYchQiVc744mqK/MzXPNR2WsPkmkOa4ywfg8C2n8h+13Bey1Q==
Expand Down

0 comments on commit 267b56a

Please sign in to comment.