Search code examples
javascriptmultithreadinghtml5-filesystem

is file system is running on diffrent thread in javascript


I hvae used file system of javascript, and what i noticed is that the file system creates the file after the read is been performed, but physically call to readFile is after the call to writeFile.

readFile

var readFile = function () {
console.log("readfile" + new Date());
var that = this;
var result = "";
var successCallback = function (fs) {
console.log("Inside readFile()");
fs.root.getFile('rosters.txt', {create: false, exclusive: false},
  function (fileEntry) {
    fileEntry.file(function (file) {
      var reader = new FileReader();
      reader.onloadend = function(e) {
        console.log(this.result);
        result = this.result;
        that.showRosters(this.result);
      }; //onloadend() ENDS HERE
      reader.readAsText(file);
    },  //fileEntry success callback ENDS HERE
    function () { console.log("Error occurred during reading file");
    result = "Error";  } //error callback for fileEntry.file
    );  //fileEntry.file ENDS HERE  
  }, //fs.roor.getFile success callback ENDS HERE
  function () { console.log("File Not Found"); } ); //error callback fs.root
 }
 var errorCallback = function (e) {
 console.log(e.toString());
 }
 this.EMarshal.reqFs.call(window,this.EMarshal.persistent, 5*1024*1024 /*5MB*/,       successCallback, errorCallback);
return result
}

writeFile

var writefile = function (rosters) {
console.log("writeFile" + new Date());
var that = this;
console.log("Rosters" + rosters);
var successCallback = function (fs) {
console.log('Opened file system: ' + fs.name);
fs.root.getFile('rosters.txt', {create: true, exclusive: false},
  function(fileEntry) {
    console.log("File Created");
    fileEntry.createWriter( function(writer) { 
      that.gotFileWriter(writer,rosters);
      console.log("write");
      }
      ,function() { console.log("Failed"); 
        this.EMarshal.fail() }
    );
  },
  function (e) {
    console.log(e.toString()); 
  } );
}
var errorCallback = function (e) {
   console.log(e.toString);
};
console.log('Error: ' + msg);
}
this.EMarshal.reqFs.call(window,this.EMarshal.persistent, 5*1024*1024 /*5MB*/, successCallback, errorCallback);

}

for gotFileWriter

 var gotFileWriter = function (writer,roster) {
 writer.seek(writer.length);
 writer.truncate(0);
 roster = "tripid:" + this.tripid + roster;
 writer.write(roster);
}

I tried to get the time, then also readFile is running before writeFile. Is javascript is running 2 threads here ? If anybody can explain why this happening it will be helpfull.


Solution

  • Try to check for the name property in the js-module. I have this problem while using the same plugin, it should me same as the Plugin name.

    If it doesn't work switch to other one PhoneGap-SMS Plugin. This will be useful.

    If you will read this document about the plugins in cordova you will get the idea, how these plugins are built. Cordova Plugin Documentation