Variable context from an initial non-reactive caller
The whole application cannot be reactive i.e. this method needs to return a result here
public string GetTextOfInterest()
{
var searchQuery = "Single Responsibility";
var titleShouldContain = "SRP";
var uriShouldContain = "pattern";
var pageShouldContain = "arguments";
return this.NonReactive(searchQuery, titleShouldContain, uriShouldContain,pageShouldContain);
}
Non-reactive blocking implementation
This non-reactive implementation works fine
private string NonReactive(string searchQuery, string titleShouldContain, string uriShouldContain, string pageShouldContain)
{
// Googler returns result directly here and does not move on before
var gooResult = this.googler.Search(searchQuery);
if (gooResult.Title.Contains(titleShouldContain) && gooResult.Uri.Contains(uriShouldContain))
{
// Blocking here again to wait for this result
var scrResult = this.scraper.Scrape(gooResult.Uri);
if (scrResult.Body.Contains(pageShouldContain))
{
// Blocking up to this point ensures sequential execution and ability to return this final result
return scrResult.Body;
}
}
return null;
}
How to answer reactively?
This reactive implementation does not work
private string ReactiveButUseless(string searchQuery, string titleShouldContain, string uriShouldContain, string pageShouldContain)
{
googler.Stream.Subscribe(
gooResult =>
{
if (gooResult.Title.Contains(titleShouldContain) && gooResult.Uri.Contains(uriShouldContain))
{
scraper.Stream.Subscribe(
scrResult =>
{
if (scrResult.Body.Contains(pageShouldContain))
{
// Get final result here, but no good as method has already returned (null)
var finalResult = scrResult.Body;
}
});
scraper.Scrape(gooResult.Uri);
}
});
googler.Search(searchQuery);
return null;
}
Is there any way of implementing a working reactive solution?
- without having to change the Googler and Scraper implementation because they need to stay clean and reusable for other purposes (otherwise could pass in delegates)
- and without blocking? Could wait for result of stream and block, but then it is no better than the non-reactive blocking implementation? Or is it just not possible to mix reactive and non-reactive styles without blocking?