[whatwg/streams] Pause stream and continue stops readable-stream from reading source. (#1037)

Hi All 
Am working on finding the best solution to stream larger files from the client-side.

So I choose streamsaver.js and it works perfectly for my use case.

But I want to pause the downloads based on user click event. So I came up with the below solution but when I pause the readable stream actually pauses. But the writable stream stops writing to the download file (Means when resume again it won't start writing back to the same file. Any idea on how to achieve this functionality I will be sharing y code here for reference.


      $pause.onclick = () => {
        if(!pause){
          pause = true;

        }else{
          pause = false;
          pauseRes();
        }

       }

       $start.onclick = () => {
          const url = 'http://localhost:8081/downloadFile'
          const fileStream = streamSaver.createWriteStream('aka.pdf')
          fetch(url)
              .then(resp => {
                        for (var pair of resp.headers.entries()) {
              console.log(pair[0]+ ': '+ pair[1]);
            }
               total = Number(resp.headers.get('X-zowe-filesize'));
                return resp.body;
              })
              .then(res => {

                  updateProgress(res)
              })
         
        }

        function pauseRes(){
          if(!pause){
            Promise.resolve(true);
          }
        }

        async function updateProgress(res){
          const fileStream = streamSaver.createWriteStream('aka.pdf')

                var progress = new TransformStream({
                   async transform(chunk, controller) {
                      if(!pause){
                          await pauseRes();
                          loaded += chunk.length;
                          controller.enqueue(chunk);

                        elem.style.width = (loaded/total)*100 + '%'; 
                        elem.innerHTML = (loaded/total)*100  + '%';
                      }
                    
                  }
                })

                const readableStream = res;
                rstream = readableStream;
              // more optimized
                if(window.WritableStream && readableStream.pipeTo){
                    return readableStream
                    .pipeThrough(progress)
                    .pipeTo(fileStream,{signal:abortSignal})
                      .then(() => console.log('done writing'))
                }
      
               
        }



-- 
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/whatwg/streams/issues/1037

Received on Wednesday, 20 May 2020 18:40:28 UTC