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

Unified Diff: lib/crawler.js

Issue 29338153: Issue 3780 - wait for the loading of filters and only afterwards start to fetch pages (Closed)
Patch Set: Created March 11, 2016, 3:55 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 | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/crawler.js
diff --git a/lib/crawler.js b/lib/crawler.js
index 54df1501957a2a585f608eaca8042c75bc477051..cc28c96ab280ccf51b16ef7b29f9bd57b66211ea 100644
--- a/lib/crawler.js
+++ b/lib/crawler.js
@@ -22,6 +22,8 @@ function abprequire(module)
let {RequestNotifier} = abprequire("requestNotifier");
+let {FilterNotifier} = abprequire("filterNotifier");
+let {FilterStorage} = abprequire("filterStorage");
/**
* Creates a pool of tabs and allocates them to tasks on request.
@@ -219,31 +221,51 @@ function run(window, urls, timeout, maxtabs, targetURL, onDone)
}
};
- for (let url of urls)
+ new Promise(function(resolve, reject)
Wladimir Palant 2016/03/15 09:13:10 Nit: Why not use arrow functions consistently? (re
sergei 2016/03/15 12:16:23 We don't need to capture `this` here, so I have no
Wladimir Palant 2016/03/15 13:44:22 We generally use arrow functions for inline callba
{
- running++;
- Task.spawn(crawl_url.bind(null, url, tabAllocator, loadListener)).then(function(result)
+ if (FilterStorage.subscriptions.length > 0 && !FilterStorage._loading)
Wladimir Palant 2016/03/15 09:13:10 _loading is an internal flag to prevent reentrance
Wladimir Palant 2016/03/15 09:18:11 Actually, I think that this is a bad assumption fr
sergei 2016/03/15 12:16:23 I thought about it. It does happen that "load" ev
Wladimir Palant 2016/03/15 13:44:22 No, subscriptions are added all at once when loadi
sergei 2016/03/15 14:44:25 Acknowledged.
{
- let request = new XMLHttpRequest();
- request.open("POST", targetURL);
- request.addEventListener("load", taskDone, false);
- request.addEventListener("error", taskDone, false);
- request.send(JSON.stringify(result));
- }, function(url, exception)
+ resolve();
+ return;
+ }
+ FilterNotifier.addListener((action, item, newValue, oldValue) =>
+ {
+ if (action === "load")
Wladimir Palant 2016/03/15 09:13:11 Nit: We don't usually use strict equality, and it
sergei 2016/03/15 12:16:23 Fixed.
+ {
+ resolve();
+ }
+ });
+ }).then(_ =>
Wladimir Palant 2016/03/15 09:13:11 No pointless parameter please, () =>
sergei 2016/03/15 12:16:23 Acknowledged.
+ {
+ for (let url of urls)
{
- reportException(exception);
+ running++;
+ Task.spawn(crawl_url.bind(null, url, tabAllocator, loadListener)).then(function(result)
+ {
+ let request = new XMLHttpRequest();
+ request.open("POST", targetURL);
+ request.addEventListener("load", taskDone, false);
+ request.addEventListener("error", taskDone, false);
+ request.send(JSON.stringify(result));
+ }, function(url, exception)
+ {
+ reportException(exception);
- let request = new XMLHttpRequest();
- request.open("POST", targetURL);
- request.addEventListener("load", taskDone, false);
- request.addEventListener("error", taskDone, false);
- request.send(JSON.stringify({
- url: url,
- startTime: Date.now(),
- error: String(exception)
- }));
- }.bind(null, url));
- }
+ let request = new XMLHttpRequest();
+ request.open("POST", targetURL);
+ request.addEventListener("load", taskDone, false);
+ request.addEventListener("error", taskDone, false);
+ request.send(JSON.stringify({
+ url: url,
+ startTime: Date.now(),
+ error: String(exception)
+ }));
+ }.bind(null, url));
+ }
+ // Be careful, `catch` does not catch exeptions from any asynchronous calls
Wladimir Palant 2016/03/15 09:13:11 exeptions => exceptions.
sergei 2016/03/15 12:16:23 Done. Basically, I have removed the comment.
+ // of this `then` handler because the latter one does not return an array of
+ // promises of asynchrounous tasks and does not contain any waiting code.
+ }).catch(reportException);
Wladimir Palant 2016/03/15 09:13:11 I'm not really happy with the way this is structur
sergei 2016/03/15 12:16:23 Done.
}
exports.run = run;
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld