按顺序处理文件内容

时间:2016-10-07 13:32:12

标签: javascript asynchronous filereader synchronous

您好我有几个文件需要按特定顺序阅读。

为此,我尝试实现从前一个文件的onloadend函数中生成的文件读取。

我的MWE在这里:https://jsfiddle.net/q3h49dL2/

使用以下测试代码:

class Chain {
  // File Chain ops
  addReadJob(job) {
    console.log("Queuing:", job.file.name);

    if (this.firstjob === undefined) {
      this.firstjob = 0;
    }

    // Store previous job 
    var lastjob = this.firstjob;

    // Create new job that passes the previous job
    // as a readbeforefunc argument to new job.
    this.firstjob = function() {
      Task.readFile(job.file, job.task, lastjob);
    }
  }

  constructor() {
    //Let's create a bunch of jobs
    var job1 = {
      file: {name: "File1",text: "File1 contents"},
      task: Task.taskDoer1
    };
    var job2 = {
      file: {name: "File2",text: "File2 contents"},
      task: Task.taskDoer2
    };
    var job3 = {
      file: {name: "File3",text: "File3 contents"},
      task: Task.taskDoer1
    };

    // Now queue them
    this.addReadJob(job1);
    this.addReadJob(job2);
    this.addReadJob(job3);

    // And process them all from the first chain
    this.firstjob();
  }
}


class Task {
  static taskDoer1(text) {console.log("randomtask:", text);}
  static taskDoer2(text) {console.log("anotherrandomtask", text);}

  // !!!HERE PROBLEMS OCCUR!!!
  static readFile(file, callback, runbeforefunc = 0) {
    var fr = new FileReadPretend();

    fr.onloadend = function(text) {
      // Run previous job in chain first
      if (runbeforefunc !== 0) {
          runbeforefunc();
      }
      callback(text);
    }
    fr.readAsText(file);
  }
}


class FileReadPretend {
  constructor(){
     this.onloadend = null;
  }
  readAsText(file) {  
    var that = this;

    setTimeout(function() {
      that.onloadend(file.text);
      console.log("--read", file.name);
    }, 1000);
  }
}


new Chain();

一般的想法是我将一堆文件及其文件处理程序任务函数排队到链接队列中。

然后,每个队列将队列中的上一个作业挂钩到runBeforeFunc中的Task.readFilerunbeforeFunc在处理当前文件之前执行。

但是,这似乎不起作用,无论我在callback(text)函数中Task.readFile语句之前还是之后移动//setup watch for FB API to be ready //note that since you use $window, you need to inject it into your controller //angular.module('myApp').controller('appController', function ($scope, $window, ...) { $scope.FBListener = $scope.$watch(function () { return $window.FB; }, function (newVal, oldVal) { // FB API loaded, make calls console.log("FB is ready"); //functions that do FB API calls $scope.getFBEvents(); $scope.getFBPosts(); }); ,它仍然执行错误的作业顺序。

我做错了什么?

2 个答案:

答案 0 :(得分:1)

也许有点琐碎的答案,但为什么不改变顺序,例如;

 // Now queue them
    this.addReadJob(job3);
    this.addReadJob(job2);
    this.addReadJob(job1);

https://jsfiddle.net/4Lv10zk1/

答案 1 :(得分:0)

解决它,实际上是微不足道的:

addReadJob(job) {
   console.log("Queuing:", job.file.name);

   if (this.firstjob === undefined) {
       this.firstjob = function(){}; // dummy func
   }

   // Store previous job 
   var lastjob = this.firstjob;

   // Create new job that passes the previous job
   this.firstjob = function() {
        lastjob(); // <--- run lastjob first, duh!
        Task.readFile(job.file, job.task);
   }
}