how to wrap FileSystemFileEntry.file into a function that returns Observable?











up vote
2
down vote

favorite












I'm trying to read files recursively from the dropped folder.



onDrop(event) {
event.preventDefault();
this.folderData = ;
this.filesData = ;

const items = event.dataTransfer.items;
for (let i = 0; i < items.length; i++) {
const item = items[i].webkitGetAsEntry();
if (item) {
this.scanFiles(item, this.folderData);
}
}
// send out data
// this.dropped.emit({ folderData: this.folderData, filesData: this.filesData });
}

private scanFiles(item, container: Array<any>) {
const nodeData = {
name: item.name,
isDirectory: item.isDirectory,
item: item,
children:
};

container.push(nodeData);
if (item.isDirectory) {
const directoryReader = item.createReader();
directoryReader.readEntries(entries => {
if (entries) {
entries.forEach(entry => this.scanFiles(entry, nodeData.children));
}
});
} else if (item.isFile) {
// How to return an Observable array here?
item.file(file => {
file.fullPath = item.fullPath;
this.filesData.push(file);
});
}


}



According to MDN, FileSystemFileEntry.file returns the result in its callback.
https://developer.mozilla.org/en-US/docs/Web/API/FileSystemFileEntry/file



So before onDrop sends out the result, it has to wait, until all the FileSystemFileEntry.file callbacks complete.



I want to use Observable.forkJoin to achieve this. But before that, how to wrap FileSystemFileEntry.file into a function that returns Observable?










share|improve this question




























    up vote
    2
    down vote

    favorite












    I'm trying to read files recursively from the dropped folder.



    onDrop(event) {
    event.preventDefault();
    this.folderData = ;
    this.filesData = ;

    const items = event.dataTransfer.items;
    for (let i = 0; i < items.length; i++) {
    const item = items[i].webkitGetAsEntry();
    if (item) {
    this.scanFiles(item, this.folderData);
    }
    }
    // send out data
    // this.dropped.emit({ folderData: this.folderData, filesData: this.filesData });
    }

    private scanFiles(item, container: Array<any>) {
    const nodeData = {
    name: item.name,
    isDirectory: item.isDirectory,
    item: item,
    children:
    };

    container.push(nodeData);
    if (item.isDirectory) {
    const directoryReader = item.createReader();
    directoryReader.readEntries(entries => {
    if (entries) {
    entries.forEach(entry => this.scanFiles(entry, nodeData.children));
    }
    });
    } else if (item.isFile) {
    // How to return an Observable array here?
    item.file(file => {
    file.fullPath = item.fullPath;
    this.filesData.push(file);
    });
    }


    }



    According to MDN, FileSystemFileEntry.file returns the result in its callback.
    https://developer.mozilla.org/en-US/docs/Web/API/FileSystemFileEntry/file



    So before onDrop sends out the result, it has to wait, until all the FileSystemFileEntry.file callbacks complete.



    I want to use Observable.forkJoin to achieve this. But before that, how to wrap FileSystemFileEntry.file into a function that returns Observable?










    share|improve this question


























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      I'm trying to read files recursively from the dropped folder.



      onDrop(event) {
      event.preventDefault();
      this.folderData = ;
      this.filesData = ;

      const items = event.dataTransfer.items;
      for (let i = 0; i < items.length; i++) {
      const item = items[i].webkitGetAsEntry();
      if (item) {
      this.scanFiles(item, this.folderData);
      }
      }
      // send out data
      // this.dropped.emit({ folderData: this.folderData, filesData: this.filesData });
      }

      private scanFiles(item, container: Array<any>) {
      const nodeData = {
      name: item.name,
      isDirectory: item.isDirectory,
      item: item,
      children:
      };

      container.push(nodeData);
      if (item.isDirectory) {
      const directoryReader = item.createReader();
      directoryReader.readEntries(entries => {
      if (entries) {
      entries.forEach(entry => this.scanFiles(entry, nodeData.children));
      }
      });
      } else if (item.isFile) {
      // How to return an Observable array here?
      item.file(file => {
      file.fullPath = item.fullPath;
      this.filesData.push(file);
      });
      }


      }



      According to MDN, FileSystemFileEntry.file returns the result in its callback.
      https://developer.mozilla.org/en-US/docs/Web/API/FileSystemFileEntry/file



      So before onDrop sends out the result, it has to wait, until all the FileSystemFileEntry.file callbacks complete.



      I want to use Observable.forkJoin to achieve this. But before that, how to wrap FileSystemFileEntry.file into a function that returns Observable?










      share|improve this question















      I'm trying to read files recursively from the dropped folder.



      onDrop(event) {
      event.preventDefault();
      this.folderData = ;
      this.filesData = ;

      const items = event.dataTransfer.items;
      for (let i = 0; i < items.length; i++) {
      const item = items[i].webkitGetAsEntry();
      if (item) {
      this.scanFiles(item, this.folderData);
      }
      }
      // send out data
      // this.dropped.emit({ folderData: this.folderData, filesData: this.filesData });
      }

      private scanFiles(item, container: Array<any>) {
      const nodeData = {
      name: item.name,
      isDirectory: item.isDirectory,
      item: item,
      children:
      };

      container.push(nodeData);
      if (item.isDirectory) {
      const directoryReader = item.createReader();
      directoryReader.readEntries(entries => {
      if (entries) {
      entries.forEach(entry => this.scanFiles(entry, nodeData.children));
      }
      });
      } else if (item.isFile) {
      // How to return an Observable array here?
      item.file(file => {
      file.fullPath = item.fullPath;
      this.filesData.push(file);
      });
      }


      }



      According to MDN, FileSystemFileEntry.file returns the result in its callback.
      https://developer.mozilla.org/en-US/docs/Web/API/FileSystemFileEntry/file



      So before onDrop sends out the result, it has to wait, until all the FileSystemFileEntry.file callbacks complete.



      I want to use Observable.forkJoin to achieve this. But before that, how to wrap FileSystemFileEntry.file into a function that returns Observable?







      javascript angular observable






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 7 at 6:10

























      asked Nov 7 at 4:26









      Newton Zou

      311311




      311311
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          It turns out that a single forkJoin can't resolve the problem. I finally complete this by recursive forkJoin.



          onDrop(event) {
          event.preventDefault();
          this.folderData = ;
          this.filesData = ;

          const items = event.dataTransfer.items;
          const obs = ;
          for (let i = 0; i < items.length; i++) {
          const item = items[i].webkitGetAsEntry();
          if (item) {
          obs.push(new Observable<any>(observer => this.scanFiles(item, this.folderData, observer)));
          }
          }

          forkJoin(obs).subscribe(e => {
          this.dropped.emit({ folderData: this.folderData, filesData: this.filesData });
          });
          }

          private scanFiles(item, container: Array<any>, observer: Subscriber<any>) {
          const nodeData = {
          name: item.name,
          isDirectory: item.isDirectory,
          item: item,
          children:
          };

          container.push(nodeData);
          if (item.isDirectory) {
          const directoryReader = item.createReader();
          directoryReader.readEntries(entries => {
          if (entries) {
          if (entries.length === 0) {
          observer.next();
          observer.complete();
          } else {
          const subObs = entries.map(entry => new Observable<any>(innerObserver =>
          this.scanFiles(entry, nodeData.children, innerObserver)));
          forkJoin(subObs).subscribe(e => {
          observer.next();
          observer.complete();
          });
          }
          } else {
          observer.next();
          observer.complete();
          }
          });
          } else if (item.isFile) {
          item.file(file => {
          file.fullPath = item.fullPath;
          this.filesData.push(file);
          observer.next();
          observer.complete();
          });
          }


          }






          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














             

            draft saved


            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53183557%2fhow-to-wrap-filesystemfileentry-file-into-a-function-that-returns-observable%23new-answer', 'question_page');
            }
            );

            Post as a guest
































            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote













            It turns out that a single forkJoin can't resolve the problem. I finally complete this by recursive forkJoin.



            onDrop(event) {
            event.preventDefault();
            this.folderData = ;
            this.filesData = ;

            const items = event.dataTransfer.items;
            const obs = ;
            for (let i = 0; i < items.length; i++) {
            const item = items[i].webkitGetAsEntry();
            if (item) {
            obs.push(new Observable<any>(observer => this.scanFiles(item, this.folderData, observer)));
            }
            }

            forkJoin(obs).subscribe(e => {
            this.dropped.emit({ folderData: this.folderData, filesData: this.filesData });
            });
            }

            private scanFiles(item, container: Array<any>, observer: Subscriber<any>) {
            const nodeData = {
            name: item.name,
            isDirectory: item.isDirectory,
            item: item,
            children:
            };

            container.push(nodeData);
            if (item.isDirectory) {
            const directoryReader = item.createReader();
            directoryReader.readEntries(entries => {
            if (entries) {
            if (entries.length === 0) {
            observer.next();
            observer.complete();
            } else {
            const subObs = entries.map(entry => new Observable<any>(innerObserver =>
            this.scanFiles(entry, nodeData.children, innerObserver)));
            forkJoin(subObs).subscribe(e => {
            observer.next();
            observer.complete();
            });
            }
            } else {
            observer.next();
            observer.complete();
            }
            });
            } else if (item.isFile) {
            item.file(file => {
            file.fullPath = item.fullPath;
            this.filesData.push(file);
            observer.next();
            observer.complete();
            });
            }


            }






            share|improve this answer

























              up vote
              0
              down vote













              It turns out that a single forkJoin can't resolve the problem. I finally complete this by recursive forkJoin.



              onDrop(event) {
              event.preventDefault();
              this.folderData = ;
              this.filesData = ;

              const items = event.dataTransfer.items;
              const obs = ;
              for (let i = 0; i < items.length; i++) {
              const item = items[i].webkitGetAsEntry();
              if (item) {
              obs.push(new Observable<any>(observer => this.scanFiles(item, this.folderData, observer)));
              }
              }

              forkJoin(obs).subscribe(e => {
              this.dropped.emit({ folderData: this.folderData, filesData: this.filesData });
              });
              }

              private scanFiles(item, container: Array<any>, observer: Subscriber<any>) {
              const nodeData = {
              name: item.name,
              isDirectory: item.isDirectory,
              item: item,
              children:
              };

              container.push(nodeData);
              if (item.isDirectory) {
              const directoryReader = item.createReader();
              directoryReader.readEntries(entries => {
              if (entries) {
              if (entries.length === 0) {
              observer.next();
              observer.complete();
              } else {
              const subObs = entries.map(entry => new Observable<any>(innerObserver =>
              this.scanFiles(entry, nodeData.children, innerObserver)));
              forkJoin(subObs).subscribe(e => {
              observer.next();
              observer.complete();
              });
              }
              } else {
              observer.next();
              observer.complete();
              }
              });
              } else if (item.isFile) {
              item.file(file => {
              file.fullPath = item.fullPath;
              this.filesData.push(file);
              observer.next();
              observer.complete();
              });
              }


              }






              share|improve this answer























                up vote
                0
                down vote










                up vote
                0
                down vote









                It turns out that a single forkJoin can't resolve the problem. I finally complete this by recursive forkJoin.



                onDrop(event) {
                event.preventDefault();
                this.folderData = ;
                this.filesData = ;

                const items = event.dataTransfer.items;
                const obs = ;
                for (let i = 0; i < items.length; i++) {
                const item = items[i].webkitGetAsEntry();
                if (item) {
                obs.push(new Observable<any>(observer => this.scanFiles(item, this.folderData, observer)));
                }
                }

                forkJoin(obs).subscribe(e => {
                this.dropped.emit({ folderData: this.folderData, filesData: this.filesData });
                });
                }

                private scanFiles(item, container: Array<any>, observer: Subscriber<any>) {
                const nodeData = {
                name: item.name,
                isDirectory: item.isDirectory,
                item: item,
                children:
                };

                container.push(nodeData);
                if (item.isDirectory) {
                const directoryReader = item.createReader();
                directoryReader.readEntries(entries => {
                if (entries) {
                if (entries.length === 0) {
                observer.next();
                observer.complete();
                } else {
                const subObs = entries.map(entry => new Observable<any>(innerObserver =>
                this.scanFiles(entry, nodeData.children, innerObserver)));
                forkJoin(subObs).subscribe(e => {
                observer.next();
                observer.complete();
                });
                }
                } else {
                observer.next();
                observer.complete();
                }
                });
                } else if (item.isFile) {
                item.file(file => {
                file.fullPath = item.fullPath;
                this.filesData.push(file);
                observer.next();
                observer.complete();
                });
                }


                }






                share|improve this answer












                It turns out that a single forkJoin can't resolve the problem. I finally complete this by recursive forkJoin.



                onDrop(event) {
                event.preventDefault();
                this.folderData = ;
                this.filesData = ;

                const items = event.dataTransfer.items;
                const obs = ;
                for (let i = 0; i < items.length; i++) {
                const item = items[i].webkitGetAsEntry();
                if (item) {
                obs.push(new Observable<any>(observer => this.scanFiles(item, this.folderData, observer)));
                }
                }

                forkJoin(obs).subscribe(e => {
                this.dropped.emit({ folderData: this.folderData, filesData: this.filesData });
                });
                }

                private scanFiles(item, container: Array<any>, observer: Subscriber<any>) {
                const nodeData = {
                name: item.name,
                isDirectory: item.isDirectory,
                item: item,
                children:
                };

                container.push(nodeData);
                if (item.isDirectory) {
                const directoryReader = item.createReader();
                directoryReader.readEntries(entries => {
                if (entries) {
                if (entries.length === 0) {
                observer.next();
                observer.complete();
                } else {
                const subObs = entries.map(entry => new Observable<any>(innerObserver =>
                this.scanFiles(entry, nodeData.children, innerObserver)));
                forkJoin(subObs).subscribe(e => {
                observer.next();
                observer.complete();
                });
                }
                } else {
                observer.next();
                observer.complete();
                }
                });
                } else if (item.isFile) {
                item.file(file => {
                file.fullPath = item.fullPath;
                this.filesData.push(file);
                observer.next();
                observer.complete();
                });
                }


                }







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 7 at 8:27









                Newton Zou

                311311




                311311






























                     

                    draft saved


                    draft discarded



















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53183557%2fhow-to-wrap-filesystemfileentry-file-into-a-function-that-returns-observable%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest




















































































                    Popular posts from this blog

                    横浜市

                    Rostock

                    Europa