0

Basically I'm trying to read all the image files from a directory and get the meta data for each file to create a thumbnail name. Then put the name, path and thumbnail name of each file into an array. Once all the meta data has been grabbed I want to run another function. The catch is the function requires all the observables for file operations and metadata be complete.

My way of getting around this problem was to check if this was the last item in the array to be processed for meta data and assuming that each observable will be finishing in the exact order they are called. Which doesn't seem 100% fool proof to me. Is there a better way to do this?

Snippet of the code below:

this.fileAccess.listDir(this.OSRootPath, this.directoryPath).then(
        (files)=>{

          for(let i = 0; i < files.length; i++){

            let fileName:string =  files[i].name.toLowerCase();

            // check if folder, if so copy its path and name
            if (files[i].isDirectory){

              this.directorySubDirectories.push({'path': files[i].nativeURL, "name": files[i].name});

            } // check if file is a image type, by checking the extensions
            else if (fileName.includes(this.jpgExtension) || fileName.includes(this.gifExtension) ||
                      fileName.includes(this.pngExtension) || fileName.includes(this.tifExtension)) {

                files[i].getMetadata((metaData)=>{

                let thumbnailName:string = this.generateThumbnailName(files[i].name, 
                  metaData);

                this.directoryImages.push({ 'name': files[i].name, 
                'path': files[i].nativeURL, 
                'thumbnailName': thumbnailName,
                'thumbnailPath': ''});

                // Here I'm checking to see if this is the last file in the array that needs to have it's meta data proccessed,
                // if so I run a fuction that will check if the thumbnails already exists or it will create a new one
                if (i == (files.length - 1)){

                  // run next function
                  this.processAllThumbnailsImages(); 
                }
              });
            }
          }
        }
      ).catch(
        (error) =>{
          console.log("ERROR Reading the directory, code below:");
          console.log(error);
        }
      );
    });
 }
niroice
  • 163
  • 1
  • 14
  • Isn't what you want chaining observables? If so this could help: https://stackoverflow.com/questions/34701304/rxjs-promise-composition-passing-data/34701912#34701912 – Juxture Oct 09 '17 at 09:45
  • I'm not sure that will work as my observales are called in a loop. My guess is the meta data observables need to be stored in a array then observed to see when its finished? Im just not sure exactly how to set this out in angular 2/4; the documentation available for angular is pretty sparse. – niroice Oct 10 '17 at 00:19

0 Answers0