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: Created April 26, 2016, 6:50 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
« dependencies ('K') | « 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;
(...skipping 10 matching lines...) Expand all
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 if (buffer !== "") 133 if (buffer !== "")
134 listener.process(buffer); 134 listener.process(buffer);
135 listener.process(null); 135 listener.process(null);
Wladimir Palant 2016/04/26 18:52:56 Note that return value is ignored here - turning o
Erik 2016/04/28 00:51:48 worth adding a comment for I think, which says wha
Wladimir Palant 2016/04/28 12:18:34 Done.
136 136
137 callback(null); 137 callback(null);
138 }; 138 };
139 139
140 let onError = function(e) 140 let onError = function(e)
141 { 141 {
142 if (processing) 142 if (processing)
143 { 143 {
144 // Still processing data, delay processing this event. 144 // Still processing data, delay processing this event.
145 error = e; 145 error = e;
(...skipping 26 matching lines...) Expand all
172 firstRead = false; 172 firstRead = false;
173 173
174 let f = yield OS.File.open(file.path, {read: true}); 174 let f = yield OS.File.open(file.path, {read: true});
175 while (true) 175 while (true)
176 { 176 {
177 let array = yield f.read(BUFFER_SIZE); 177 let array = yield f.read(BUFFER_SIZE);
178 if (!array.length) 178 if (!array.length)
179 break; 179 break;
180 180
181 let data = decoder.decode(array, {stream: true}); 181 let data = decoder.decode(array, {stream: true});
182 onProgress(data); 182 yield* onProgress(data);
183 } 183 }
184 yield f.close(); 184 yield f.close();
185 }.bind(this)).then(onSuccess, onError); 185 }.bind(this)).then(onSuccess, onError);
186 } 186 }
187 catch (e) 187 catch (e)
188 { 188 {
189 callback(e); 189 callback(e);
190 } 190 }
191 }, 191 },
192 192
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after
323 else 323 else
324 callback(e); 324 callback(e);
325 }); 325 });
326 } 326 }
327 catch(e) 327 catch(e)
328 { 328 {
329 callback(e); 329 callback(e);
330 } 330 }
331 } 331 }
332 } 332 }
OLDNEW
« dependencies ('K') | « dependencies ('k') | lib/utils.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld