Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use xhr instead of the fetch api #56

Merged
merged 2 commits into from
Apr 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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