Created
December 10, 2011 17:23
-
-
Save rcastillo/1455661 to your computer and use it in GitHub Desktop.
Crawl Loop: sitecawl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function runCrawler(obs) { | |
var self = this; | |
self.crawling = true; | |
Rx.Observable.While( | |
function() {return self.crawling}, | |
Rx.Observable.If( | |
function(){return self.crawlQueue.length > 0;}, | |
Rx.Observable.Defer(function (){ | |
var nextCrawlStep = self.crawlQueue.pop(); | |
return self.selectForCrawlLinks(nextCrawlStep.Delay(self.delay))}), | |
Rx.Observable.Return('nop').Delay(self.maxCrawlTime)) | |
).Subscribe(function(crawlResult){ | |
if (crawlResult != 'nop') { | |
obs.OnNext(crawlResult); | |
} else { | |
if (self.crawlQueue.length ==0) { | |
// we have waited maxCrawlTime for new pages | |
// and the queue is empty, signal complete | |
obs.OnCompleted(); | |
} | |
} | |
}, | |
function(exn){ | |
obs.OnError(exn); | |
}); | |
}(obs) { | |
var self = this; | |
self.crawling = true; | |
Rx.Observable.While( | |
function() {return self.crawling}, | |
Rx.Observable.If( | |
function(){return self.crawlQueue.length > 0;}, | |
Rx.Observable.Defer(function (){ | |
var nextCrawlStep = self.crawlQueue.pop(); | |
return self.selectForCrawlLinks(nextCrawlStep.Delay(self.delay))}), | |
Rx.Observable.Return('nop').Delay(self.maxCrawlTime)) | |
).Subscribe(function(crawlResult){ | |
if (crawlResult != 'nop') { | |
obs.OnNext(crawlResult); | |
} else { | |
if (self.crawlQueue.length ==0) { | |
// we have waited maxCrawlTime for new pages | |
// and the queue is empty, signal complete | |
obs.OnCompleted(); | |
} | |
} | |
}, | |
function(exn){ | |
obs.OnError(exn); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment