Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: lib/io.js

Issue 29512648: Issue 5475 - Update adblockpluscore dependency to revision hg:b935a0402215 (Closed) Base URL: https://github.com/adblockplus/libadblockplus.git
Patch Set: Created Aug. 11, 2017, 12:36 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: lib/io.js
diff --git a/lib/io.js b/lib/io.js
index 012529f64e1511bc433ad2cc63540781146bd3be..2b7026142b112ccea13bacc0fb0735451596d4cf 100644
--- a/lib/io.js
+++ b/lib/io.js
@@ -15,72 +15,93 @@
* along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
*/
-var IO = exports.IO =
+function readFileAsync(fileName)
{
- lineBreak: "\n",
+ return new Promise((resolve, reject) =>
+ {
+ _fileSystem.read(fileName, (result) =>
+ {
+ if (result.error)
+ return reject(result.error);
+ resolve(result);
+ });
+ });
+};
- resolveFilePath: function(path)
+function writeFileAsync(fileName, content)
+{
+ return new Promise((resolve, reject) =>
{
- return new FakeFile(_fileSystem.resolve(path));
- },
+ _fileSystem.write(fileName, content, (error) =>
+ {
+ if (error)
+ return reject(error);
+ resolve();
+ });
+ });
+};
- readFromFile: function(file, listener, callback, timeLineID)
+exports.IO =
+{
+ lineBreak: "\n",
+
+ readFromFile(fileName, listener)
{
- _fileSystem.read(file.path, function(result)
+ return readFileAsync(fileName).then((result) =>
{
- if (result.error)
- callback(result.error);
- else
- {
- var lines = result.content.split(/[\r\n]+/);
- for (var i = 0; i < lines.length; i++)
- listener.process(lines[i]);
- listener.process(null);
- callback(null);
- }
+ let lines = result.content.split(/[\r\n]+/);
+ for (let line of lines)
+ listener(line);
});
},
- writeToFile: function(file, data, callback, timeLineID)
+ writeToFile(fileName, generator)
{
- let content = Array.from(data).join(this.lineBreak) + this.lineBreak;
- _fileSystem.write(file.path, content, callback);
+ let content = Array.from(generator).join(this.lineBreak) + this.lineBreak;
+ return writeFileAsync(fileName, content);
},
- copyFile: function(fromFile, toFile, callback)
+ copyFile(fromFileName, toFileName)
{
- // Simply combine read and write operations
- var data = [];
- this.readFromFile(fromFile, {
- process: function(line)
- {
- if (line !== null)
- data.push(line);
- }
- }, function(e)
- {
- if (e)
- callback(e);
- else
- this.writeToFile(toFile, data, callback);
- }.bind(this));
+ return readFileAsync(fromFileName).then(content => writeFileAsync(toFileName, content));
},
- renameFile: function(fromFile, newName, callback)
+ renameFile(fromFileName, newNameFile)
{
- _fileSystem.move(fromFile.path, newName, callback);
+ return new Promise((resolve, reject) =>
+ {
+ _fileSystem.move(fromFileName, newNameFile, (error) =>
+ {
+ if (error)
+ return reject(error);
+ resolve();
+ });
+ });
},
- removeFile: function(file, callback)
+ removeFile(fileName)
{
- _fileSystem.remove(file.path, callback);
+ return new Promise((resolve, reject) =>
+ {
+ _fileSystem.remove(fileName, (error) =>
+ {
+ if (error)
+ return reject(error);
+ resolve();
+ });
+ });
},
- statFile: function(file, callback)
+ statFile(fileName, callback)
{
- _fileSystem.stat(file.path, function(result)
+ return new Promise((resolve, reject) =>
{
- callback(result.error, result);
+ _fileSystem.stat(fileName, (result) =>
+ {
+ if (result.error)
+ return reject(result.error);
+ resolve(result);
+ });
});
}
};

Powered by Google App Engine
This is Rietveld