[Search] Responsibility change (in progress)

Attempting to move some responsibility from
the search controller to the aggregator.
(Committing for save before possible revert.)
This commit is contained in:
shale 2015-07-27 10:04:09 -07:00
parent 232a648fbd
commit 181fb32a2a
4 changed files with 51 additions and 9 deletions

View File

@ -43,7 +43,8 @@
{ {
"provides": "searchService", "provides": "searchService",
"type": "aggregator", "type": "aggregator",
"implementation": "SearchAggregator.js" "implementation": "SearchAggregator.js",
"depends": [ "$timeout" ]
} }
], ],
"workers": [ "workers": [

View File

@ -21,6 +21,8 @@
--> -->
<div class="search" <div class="search"
ng-controller="SearchController as controller"> ng-controller="SearchController as controller">
<!-- Using search service as 'service' (is in $scope) -->
<!-- Search bar input --> <!-- Search bar input -->
<div> <div>
<input class="search-input" <input class="search-input"
@ -51,7 +53,7 @@
<!-- Results list --> <!-- Results list -->
<div class="results"> <div class="results">
<mct-representation key="'search-item'" <mct-representation key="'search-item'"
ng-repeat="result in results" ng-repeat="result in service.latestResults"
mct-object="result.object" mct-object="result.object"
ng-model="ngModel"> ng-model="ngModel">
</mct-representation> </mct-representation>
@ -59,8 +61,8 @@
<!-- Loading icon --> <!-- Loading icon -->
<div class="load-icon" <div class="load-icon"
ng-class="{loading: controller.isLoading()}" ng-class="{loading: service.isLoading()}"
ng-if="controller.isLoading()"> ng-if="service.isLoading()">
<span class="icon wait-spinner"></span> <span class="icon wait-spinner"></span>
<span class="title-label">Loading...</span> <span class="title-label">Loading...</span>
</div> </div>

View File

@ -40,9 +40,11 @@ define(
* @param {SearchProvider[]} providers the search providers to be * @param {SearchProvider[]} providers the search providers to be
* aggregated * aggregated
*/ */
function SearchAggregator(providers) { function SearchAggregator($timeout, providers) {
var latestMergedResults = [], var latestMergedResults = [],
lastMergeTimestamps = [], lastMergeTimestamps = [],
lastQueryTimestamp = 0,
loading = false,
aggregator = {}; aggregator = {};
// Remove duplicate objects that have the same ID // Remove duplicate objects that have the same ID
@ -120,10 +122,27 @@ define(
newerResults = filterRepeats(newerResults); newerResults = filterRepeats(newerResults);
newerResults = orderByScore(newerResults); newerResults = orderByScore(newerResults);
// After all that is done, now replace latestMergedResults with this // Now replace latestMergedResults
latestMergedResults = newerResults; latestMergedResults = newerResults;
lastMergeTimestamps = providerTimestamps; lastMergeTimestamps = providerTimestamps;
aggregator.latestResults = latestMergedResults; aggregator.latestResults = latestMergedResults;
// Update the loading status, and wait for latest results if needed
function waitForLatest() {
var areOld = lastMergeTimestamps.some(function(c) {return c < lastQueryTimestamp;});
// If any of the timestamps are older than the one we made the query with
if (areOld) {
// Then wait and try to update again
//updateResults();
loading = true;
$timeout(waitForLatest, 50);
} else {
// We got the latest results now (and done loading)
loading = false;
}
}
waitForLatest();
} }
// For documentation, see sendQuery below. // For documentation, see sendQuery below.
@ -134,6 +153,8 @@ define(
timestamp = date.getTime(); timestamp = date.getTime();
} }
lastQueryTimestamp = timestamp;
// Send the query to all the providers // Send the query to all the providers
for (var i = 0; i < providers.length; i += 1) { for (var i = 0; i < providers.length; i += 1) {
providers[i].query(inputText, timestamp, DEFAULT_MAX_RESULTS, DEFUALT_TIMEOUT); providers[i].query(inputText, timestamp, DEFAULT_MAX_RESULTS, DEFUALT_TIMEOUT);
@ -201,6 +222,14 @@ define(
*/ */
getNumResults: function () { getNumResults: function () {
return latestMergedResults.length; return latestMergedResults.length;
},
/**
* Checks to see if we are still waiting for the results to be
* fully updated.
*/
isLoading: function () {
return loading;
} }
}; };

View File

@ -40,10 +40,11 @@ define(function () {
// This allows us to directly access the search aggregator's members. // This allows us to directly access the search aggregator's members.
// Most important is latestResults, which is continuously updated. This // Most important is latestResults, which is continuously updated. This
// means that this controller does not have to poll for results any more. // means that this controller does not have to poll for results any more.
//$scope.searchService = searchService; $scope.service = searchService;
// TODO: Modify search aggregator to have a search result array which // TODO: Modify search aggregator to have a search result array which
// is of a size that can be chosen and modified by this controller. // is of a size that can be chosen and modified by this controller.
/*
function update(timestamp) { function update(timestamp) {
// We are loading results // We are loading results
loading = true; loading = true;
@ -69,6 +70,7 @@ define(function () {
} }
waitForLatest(); waitForLatest();
} }
*/
function search() { function search() {
var date = new Date(), var date = new Date(),
@ -88,9 +90,15 @@ define(function () {
// Send the query // Send the query
searchService.sendQuery(inputText, timestamp); searchService.sendQuery(inputText, timestamp);
update(timestamp); //update(timestamp);
} }
$scope.$watch("searchService.latestResults", function () {
console.log('watcher saw')
// Update the displayed result list
$scope.service = searchService;
});
return { return {
/** /**
* Search the filetree. * Search the filetree.
@ -113,9 +121,11 @@ define(function () {
* Checks to see if we are still waiting for the results to be * Checks to see if we are still waiting for the results to be
* fully updated. * fully updated.
*/ */
/*
isLoading: function () { isLoading: function () {
return loading; return loading;
}, },
*/
/** /**
* Checks to see if there are more search results to display. * Checks to see if there are more search results to display.