Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(523)

Unified Diff: client/lib/search.dart

Issue 22862004: added a timeout of 500 milliseconds. (Closed) Base URL: git@github.com:dart-lang/dartdoc-viewer.git@master
Patch Set: Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | client/web/search.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: client/lib/search.dart
diff --git a/client/lib/search.dart b/client/lib/search.dart
index dd300dde9a7802f24826c3d49ec955d2c6ba1246..08bf642fbd4f753689ed6e4fda01490d50cb7fe5 100644
--- a/client/lib/search.dart
+++ b/client/lib/search.dart
@@ -10,9 +10,15 @@
*/
library search;
+import 'dart:async';
+import 'package:web_ui/web_ui.dart';
+
/** Search Index */
Map<String, String> index = {};
+/** Search query. */
+@observable String searchQuery = "";
+
class SearchResult implements Comparable {
/** Qualified name of this search result references. */
@@ -55,15 +61,22 @@ Map<String, int> value = {
* A score is given to each potential search result based off how likely it is
* the appropriate qualified name to return for the search query.
*/
-List<SearchResult> lookupSearchResults(String searchQuery, int maxResults) {
-
+List<SearchResult> lookupSearchResults(String query, int maxResults) {
+
+ var stopwatch = new Stopwatch()..start();
+
var scoredResults = <SearchResult>[];
var resultSet = new Set<String>();
- var queryList = searchQuery.trim().toLowerCase().split(' ');
+ var queryList = query.trim().toLowerCase().split(' ');
queryList.forEach((q) => resultSet.addAll(index.keys.where((e) =>
e.toLowerCase().contains(q))));
- for (var r in resultSet) {
+ for (var r in resultSet) {
+ /// If it is taking too long to compute the search results, time out and
+ /// return an empty list of results.
+ if (stopwatch.elapsedMilliseconds > 500) {
+ return [];
+ }
int score = 0;
var lowerCaseResult = r.toLowerCase();
var type = index[r];
@@ -123,6 +136,11 @@ List<SearchResult> lookupSearchResults(String searchQuery, int maxResults) {
scoredResults.add(new SearchResult(r, type, score));
}
+ /// If it is taking too long to compute the search results, time out and
+ /// return an empty list of results.
+ if (stopwatch.elapsedMilliseconds > 500) {
+ return [];
+ }
scoredResults.sort();
updatePositions(scoredResults);
if (scoredResults.length > maxResults) {
@@ -136,4 +154,4 @@ void updatePositions(List<SearchResult> list) {
for(int i = 0; i < list.length; i++) {
list[i].position = i;
}
-}
+}
« no previous file with comments | « no previous file | client/web/search.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698