Skip to content

Commit

Permalink
abort from DOM
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmywarting committed Sep 6, 2016
1 parent 1108a2c commit f60b23e
Showing 1 changed file with 20 additions and 11 deletions.
31 changes: 20 additions & 11 deletions example.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,18 @@ <h3>What would you like to save?</h3>
<button id="$c">Write some ccc's</button>
<button id="$ipsum">Write a lot of Lorem ipsum</button>
</div>
<button id="$close" hidden>close stream</button>
<button id="$close" hidden title="When you are done writing">Close</button>
<button id="$abort" hidden title="When you want to abort">Abort</button>
<script src="StreamSaver.js"></script>
<script src="https://cdn.jsdelivr.net/webtorrent/latest/webtorrent.min.js"></script>
<script src="https://rawgit.com/jimmywarting/browser-su/master/build/permissions.js"></script>

<!-- 1.2.2 -->
<script src="https://cdn.rawgit.com/creatorrr/web-streams-polyfill/68f93d7240d925d27b175ce39133f57993c3f109/dist/polyfill.min.js"
integrity="sha384-wC6EeO3kVv23JQA/XjoEHMvxOwXZXIRdxvhOt8rOcoGPYQnPbumUJugHHoJAdM0x"
crossorigin
></script>
crossorigin="anonymous">
</script>

<script>
!streamSaver.supported && prompt(
'ReadableStream is not supported, you can enable it in chrome, or wait until v52',
Expand All @@ -72,15 +76,21 @@ <h3>What would you like to save?</h3>
kind = $form.querySelector(':checked').value,
filename = $form.$filename.value,
permission,
writer,
url = "https://d8d913s460fub.cloudfront.net/videoserver/cat-test-video-320x240.mp4",
myFile = streamSaver.createWriteStream(filename)

if(kind !== 'blob')
myFile = myFile.getWriter()
fileStream = streamSaver.createWriteStream(filename)

if(kind == 'blob') {
$abort.onclick = event => fileStream.abort()
} else {
writer = fileStream.getWriter()
$abort.onclick = event => writer.abort()
$close.onclick = event => writer.close()
}

$form.remove()
$abort.hidden = false
$close.hidden = false
$close.onclick = event => myFile.close()

switch (kind) {
case "text":
Expand All @@ -90,7 +100,7 @@ <h3>What would you like to save?</h3>
$a.onclick = $b.onclick = $c.onclick = event => {
let data = event.target.id[1].repeat(1024)
let uint8array = encoder.encode(data + "\n\n")
myFile.write(uint8array)
writer.write(uint8array)
}

$ipsum.onclick = event => {
Expand All @@ -100,8 +110,7 @@ <h3>What would you like to save?</h3>
let que = Promise.resolve()
let pump = () => {
n-- && que.then(() => {
myFile.write(text).then(()=>{setTimeout(pump)})
// myFile.write(text).then(()=>pump()) // This should be possible
writer.write(text).then(()=>{setTimeout(pump)})
})
}
pump()
Expand Down

0 comments on commit f60b23e

Please sign in to comment.