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

Side by Side 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.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « dependencies ('k') | lib/utils.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-2016 Eyeo GmbH 3 * Copyright (C) 2006-2016 Eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
71 */ 71 */
72 readFromFile: function(/**nsIFile*/ file, /**Object*/ listener, /**Function*/ callback) 72 readFromFile: function(/**nsIFile*/ file, /**Object*/ listener, /**Function*/ callback)
73 { 73 {
74 try 74 try
75 { 75 {
76 let processing = false; 76 let processing = false;
77 let buffer = ""; 77 let buffer = "";
78 let loaded = false; 78 let loaded = false;
79 let error = null; 79 let error = null;
80 80
81 let onProgress = function(data) 81 let onProgress = function*(data)
82 { 82 {
83 let index = (processing ? -1 : Math.max(data.lastIndexOf("\n"), data.las tIndexOf("\r"))); 83 let index = (processing ? -1 : Math.max(data.lastIndexOf("\n"), data.las tIndexOf("\r")));
84 if (index >= 0) 84 if (index >= 0)
85 { 85 {
86 // Protect against reentrance in case the listener processes events. 86 // Protect against reentrance in case the listener processes events.
87 processing = true; 87 processing = true;
88 try 88 try
89 { 89 {
90 let oldBuffer = buffer; 90 let oldBuffer = buffer;
91 buffer = data.substr(index + 1); 91 buffer = data.substr(index + 1);
92 data = data.substr(0, index + 1); 92 data = data.substr(0, index + 1);
93 let lines = data.split(/[\r\n]+/); 93 let lines = data.split(/[\r\n]+/);
94 lines.pop(); 94 lines.pop();
95 lines[0] = oldBuffer + lines[0]; 95 lines[0] = oldBuffer + lines[0];
96 for (let i = 0; i < lines.length; i++) 96 for (let i = 0; i < lines.length; i++)
97 listener.process(lines[i]); 97 yield listener.process(lines[i]);
98 } 98 }
99 finally 99 finally
100 { 100 {
101 processing = false; 101 processing = false;
102 data = buffer; 102 data = buffer;
103 buffer = ""; 103 buffer = "";
104 onProgress(data); 104 yield* onProgress(data);
105 105
106 if (loaded) 106 if (loaded)
107 { 107 {
108 loaded = false; 108 loaded = false;
109 onSuccess(); 109 onSuccess();
110 } 110 }
111 111
112 if (error) 112 if (error)
113 { 113 {
114 let param = error; 114 let param = error;
115 error = null; 115 error = null;
116 onError(param); 116 onError(param);
117 } 117 }
118 } 118 }
119 } 119 }
120 else 120 else
121 buffer += data; 121 buffer += data;
122 }; 122 };
123 123
124 let onSuccess = function() 124 let onSuccess = function()
125 { 125 {
126 if (processing) 126 if (processing)
127 { 127 {
128 // Still processing data, delay processing this event. 128 // Still processing data, delay processing this event.
129 loaded = true; 129 loaded = true;
130 return; 130 return;
131 } 131 }
132 132
133 // We are ignoring return value of listener.process() here because
134 // turning this callback into a generator would be complicated, and
135 // delaying isn't really necessary for the last two calls.
133 if (buffer !== "") 136 if (buffer !== "")
134 listener.process(buffer); 137 listener.process(buffer);
135 listener.process(null); 138 listener.process(null);
136 139
137 callback(null); 140 callback(null);
138 }; 141 };
139 142
140 let onError = function(e) 143 let onError = function(e)
141 { 144 {
142 if (processing) 145 if (processing)
(...skipping 29 matching lines...) Expand all
172 firstRead = false; 175 firstRead = false;
173 176
174 let f = yield OS.File.open(file.path, {read: true}); 177 let f = yield OS.File.open(file.path, {read: true});
175 while (true) 178 while (true)
176 { 179 {
177 let array = yield f.read(BUFFER_SIZE); 180 let array = yield f.read(BUFFER_SIZE);
178 if (!array.length) 181 if (!array.length)
179 break; 182 break;
180 183
181 let data = decoder.decode(array, {stream: true}); 184 let data = decoder.decode(array, {stream: true});
182 onProgress(data); 185 yield* onProgress(data);
183 } 186 }
184 yield f.close(); 187 yield f.close();
185 }.bind(this)).then(onSuccess, onError); 188 }.bind(this)).then(onSuccess, onError);
186 } 189 }
187 catch (e) 190 catch (e)
188 { 191 {
189 callback(e); 192 callback(e);
190 } 193 }
191 }, 194 },
192 195
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after
323 else 326 else
324 callback(e); 327 callback(e);
325 }); 328 });
326 } 329 }
327 catch(e) 330 catch(e)
328 { 331 {
329 callback(e); 332 callback(e);
330 } 333 }
331 } 334 }
332 } 335 }
OLDNEW
« no previous file with comments | « dependencies ('k') | lib/utils.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld