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

Unified Diff: lib/io.js

Issue 29340852: Issue 3993 - Implement Utils.yield() in a better way (ABP/Firefox part) (Closed)
Patch Set: Finalized revisions and added comment Created April 28, 2016, 12:16 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
« no previous file with comments | « dependencies ('k') | lib/utils.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/io.js
===================================================================
--- a/lib/io.js
+++ b/lib/io.js
@@ -73,40 +73,40 @@ let IO = exports.IO =
{
try
{
let processing = false;
let buffer = "";
let loaded = false;
let error = null;
- let onProgress = function(data)
+ let onProgress = function*(data)
{
let index = (processing ? -1 : Math.max(data.lastIndexOf("\n"), data.lastIndexOf("\r")));
if (index >= 0)
{
// Protect against reentrance in case the listener processes events.
processing = true;
try
{
let oldBuffer = buffer;
buffer = data.substr(index + 1);
data = data.substr(0, index + 1);
let lines = data.split(/[\r\n]+/);
lines.pop();
lines[0] = oldBuffer + lines[0];
for (let i = 0; i < lines.length; i++)
- listener.process(lines[i]);
+ yield listener.process(lines[i]);
}
finally
{
processing = false;
data = buffer;
buffer = "";
- onProgress(data);
+ yield* onProgress(data);
if (loaded)
{
loaded = false;
onSuccess();
}
if (error)
@@ -125,16 +125,19 @@ let IO = exports.IO =
{
if (processing)
{
// Still processing data, delay processing this event.
loaded = true;
return;
}
+ // We are ignoring return value of listener.process() here because
+ // turning this callback into a generator would be complicated, and
+ // delaying isn't really necessary for the last two calls.
if (buffer !== "")
listener.process(buffer);
listener.process(null);
callback(null);
};
let onError = function(e)
@@ -174,17 +177,17 @@ let IO = exports.IO =
let f = yield OS.File.open(file.path, {read: true});
while (true)
{
let array = yield f.read(BUFFER_SIZE);
if (!array.length)
break;
let data = decoder.decode(array, {stream: true});
- onProgress(data);
+ yield* onProgress(data);
}
yield f.close();
}.bind(this)).then(onSuccess, onError);
}
catch (e)
{
callback(e);
}
« no previous file with comments | « dependencies ('k') | lib/utils.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld