Skip to content

Commit

Permalink
Should be able to abort a download
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmywarting committed Sep 6, 2016
1 parent 4d3d9b8 commit 1108a2c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
2 changes: 1 addition & 1 deletion StreamSaver.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@
console.log('All data successfully read!')
},
abort(e) {
console.error('Something went wrong!', e)
channel.port1.postMessage('abort')
}
}, queuingStrategy)
}
Expand Down
15 changes: 11 additions & 4 deletions sw.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,21 @@ function createStream(resolve, reject, port){
start(controller) {
port.postMessage({debug: 'ReadableStream has been created'})
// When we recive data on the messageChannel, we write
port.onmessage = event => {
port.onmessage = ({data}) => {
// We finaly have a abortable stream =D
if(event.data === 'end'){
if (data === 'end') {
resolve()
return controller.close()
}
controller.enqueue(event.data)
bytesWritten += event.data.byteLength

if (data === 'abort') {
resolve()
controller.error('Aborted the download')
return
}

controller.enqueue(data)
bytesWritten += data.byteLength
port.postMessage({ bytesWritten })
}
},
Expand Down

0 comments on commit 1108a2c

Please sign in to comment.