【前端】智能库房综合管理系统前端项目
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

80 lines
1.6 KiB

'use strict'
export default class FetchSource {
constructor(url, options) {
this.url = url
this.destination = null
this.request = null
this.streaming = true
this.completed = false
this.established = false
this.progress = 0
this.aborted = false
this.onEstablishedCallback = options.onSourceEstablished
this.onCompletedCallback = options.onSourceCompleted
}
connect(destination) {
this.destination = destination
}
start() {
const params = {
method: 'GET',
headers: new Headers(),
keepAlive: 'default'
}
self
.fetch(this.url, params)
.then(
function(res) {
if (res.ok && res.status >= 200 && res.status <= 299) {
this.progress = 1
this.established = true
return this.pump(res.body.getReader())
} else {
// error
}
}.bind(this)
)
.catch(function(err) {
throw err
})
}
pump(reader) {
return reader
.read()
.then(
function(result) {
if (result.done) {
this.completed = true
} else {
if (this.aborted) {
return reader.cancel()
}
if (this.destination) {
this.destination.write(result.value.buffer)
}
return this.pump(reader)
}
}.bind(this)
)
.catch(function(err) {
throw err
})
}
resume(secondsHeadroom) {
// Nothing to do here
}
abort() {
this.aborted = true
}
}