Skip to content
This repository has been archived by the owner on Jun 11, 2023. It is now read-only.

Commit

Permalink
Modified constructor params, added onStart callback
Browse files Browse the repository at this point in the history
  • Loading branch information
chpmnrssll committed Jun 6, 2018
1 parent be5a840 commit b204fad
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 44 deletions.
17 changes: 5 additions & 12 deletions MultiThread.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
class MultiThread {
constructor (options = {}, onProgress = () => {}, onFinish = () => {}) {
if (!this.supported()) {
constructor (options = {onStart: () => {}, onProgress: () => {}, onFinish: () => {}}) {
if (!window.ReadableStream && !window.WritableStream) {
throw Error('Web Streams are not yet supported in this browser.')
}

Object.assign(this, options)
this.onFinish = onFinish.bind(this)
this.onProgress = onProgress.bind(this)
}

supported () {
try {
return !!new window.ReadableStream() && !!new window.WritableStream()
} catch (error) {
return false
}
this.onStart = this.onStart.bind(this)
this.onFinish = this.onFinish.bind(this)
this.onProgress = this.onProgress.bind(this)
}

cancel () {
Expand Down
34 changes: 20 additions & 14 deletions examples/backblaze.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,25 +33,22 @@
progressArea.removeChild(progressArea.firstChild)
}

const url = new URL(`https://f${options.clusterNum}.backblazeb2.com/file/${options.bucketName}/${options.fileName}`)
const multiThread = new MultiThread(options, onProgress, onFinish)
multiThread.fetch(url, options)

downloadButton.setAttribute('disabled', true)
cancelButton.removeAttribute('disabled')

cancelButton.onclick = () => {
cancelButton.setAttribute('disabled', true)
downloadButton.removeAttribute('disabled')
multiThread.cancel()
const info = document.createElement('p')
options.onStart = ({rangeCount, contentLength}) => {
contentLength /= 1048576 // Convert bytes -> mb
info.innerText = `Downloading ${contentLength.toFixed(2)}mb in ${rangeCount} range requests.`
progressArea.appendChild(info)
downloadButton.setAttribute('disabled', true)
cancelButton.removeAttribute('disabled')
}

function onFinish () {
options.onFinish = () => {
info.innerText += `.. Done.`
cancelButton.setAttribute('disabled', true)
downloadButton.removeAttribute('disabled')
}

function onProgress ({id, contentLength, loaded}) {
options.onProgress = ({id, contentLength, loaded}) => {
if (!progressElements[id]) {
progressElements[id] = document.createElement('progress')
progressElements[id].value = 0
Expand All @@ -61,8 +58,17 @@

// handle divide-by-zero edge case when Content-Length=0
const percent = contentLength ? loaded / contentLength : 1

progressElements[id].value = Math.round(percent * 100)
}

const url = new URL(`https://f${options.clusterNum}.backblazeb2.com/file/${options.bucketName}/${options.fileName}`)
const multiThread = new MultiThread(options)
multiThread.fetch(url, options)

cancelButton.onclick = () => {
cancelButton.setAttribute('disabled', true)
downloadButton.removeAttribute('disabled')
multiThread.cancel()
}
}
})()
42 changes: 24 additions & 18 deletions examples/googleDrive.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,36 +110,42 @@

// options.controller = new AbortController()
options.headers = new window.Headers({'Authorization': `Bearer ${accessToken}`})
const url = new URL(`https://www.googleapis.com/drive/v3/files/${options.fileID}?alt=media`)
const multiThread = new MultiThread(options, onProgress, onFinish)
multiThread.fetch(url, options)

downloadButton.setAttribute('disabled', true)
cancelButton.removeAttribute('disabled')

cancelButton.onclick = () => {
cancelButton.setAttribute('disabled', true)
downloadButton.removeAttribute('disabled')
multiThread.cancel()
const info = document.createElement('p')
options.onStart = ({rangeCount, contentLength}) => {
contentLength /= 1048576 // Convert bytes -> mb
info.innerText = `Downloading ${contentLength.toFixed(2)}mb in ${rangeCount} range requests.`
progressArea.appendChild(info)
downloadButton.setAttribute('disabled', true)
cancelButton.removeAttribute('disabled')
}

function onFinish () {
cancelButton.setAttribute('disabled', true)
downloadButton.removeAttribute('disabled')
}

function onProgress ({id, contentLength, loaded}) {
options.onProgress = ({id, contentLength, loaded}) => {
if (!progressElements[id]) {
progressElements[id] = document.createElement('progress')
progressElements[id].value = 0
progressElements[id].max = 100
progressArea.appendChild(progressElements[id])
}

// handle divide-by-zero edge case when Content-Length=0
const percent = contentLength ? loaded / contentLength : 1

progressElements[id].value = Math.round(percent * 100)
}

options.onFinish = () => {
info.innerText += `.. Done.`
cancelButton.setAttribute('disabled', true)
downloadButton.removeAttribute('disabled')
}

const url = new URL(`https://www.googleapis.com/drive/v3/files/${options.fileID}?alt=media`)
const multiThread = new MultiThread(options)
multiThread.fetch(url, options)

cancelButton.onclick = () => {
cancelButton.setAttribute('disabled', true)
downloadButton.removeAttribute('disabled')
multiThread.cancel()
}
}
})()

0 comments on commit b204fad

Please sign in to comment.