1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
|
// Copyright 2013 The Closure Library Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/**
* @fileoverview Matcher which maintains a client-side cache on top of some
* other matcher.
* @author reinerp@google.com (Reiner Pope)
*/
goog.provide('goog.ui.ac.CachingMatcher');
goog.require('goog.array');
goog.require('goog.async.Throttle');
goog.require('goog.ui.ac.ArrayMatcher');
goog.require('goog.ui.ac.RenderOptions');
/**
* A matcher which wraps another (typically slow) matcher and
* keeps a client-side cache of the results. For instance, you can use this to
* wrap a RemoteArrayMatcher to hide the latency of the underlying matcher
* having to make ajax request.
*
* Objects in the cache are deduped on their stringified forms.
*
* Note - when the user types a character, they will instantly get a set of
* local results, and then some time later, the results from the server will
* show up.
*
* @constructor
* @param {!Object} baseMatcher The underlying matcher to use. Must implement
* requestMatchingRows.
* @final
*/
goog.ui.ac.CachingMatcher = function(baseMatcher) {
/** @private {!Array<!Object>}} The cache. */
this.rows_ = [];
/**
* Set of stringified rows, for fast deduping. Each element of this.rows_
* is stored in rowStrings_ as (' ' + row) to ensure we avoid builtin
* properties like 'toString'.
* @private {Object<string, boolean>}
*/
this.rowStrings_ = {};
/**
* Maximum number of rows in the cache. If the cache grows larger than this,
* the entire cache will be emptied.
* @private {number}
*/
this.maxCacheSize_ = 1000;
/** @private {!Object} The underlying matcher to use. */
this.baseMatcher_ = baseMatcher;
/**
* Local matching function.
* @private {function(string, number, !Array<!Object>): !Array<!Object>}
*/
this.getMatchesForRows_ = goog.ui.ac.ArrayMatcher.getMatchesForRows;
/** @private {number} Number of matches to request from the base matcher. */
this.baseMatcherMaxMatches_ = 100;
/** @private {goog.async.Throttle} */
this.throttledTriggerBaseMatch_ =
new goog.async.Throttle(this.triggerBaseMatch_, 150, this);
/** @private {string} */
this.mostRecentToken_ = '';
/** @private {Function} */
this.mostRecentMatchHandler_ = null;
/** @private {number} */
this.mostRecentMaxMatches_ = 10;
/**
* The set of rows which we last displayed.
*
* NOTE(reinerp): The need for this is subtle. When a server result comes
* back, we don't want to suddenly change the list of results without the user
* doing anything. So we make sure to add the new server results to the end of
* the currently displayed list.
*
* We need to keep track of the last rows we displayed, because the "similar
* matcher" we use locally might otherwise reorder results.
*
* @private {Array<!Object>}
*/
this.mostRecentMatches_ = [];
};
/**
* Sets the number of milliseconds with which to throttle the match requests
* on the underlying matcher.
*
* Default value: 150.
*
* @param {number} throttleTime .
*/
goog.ui.ac.CachingMatcher.prototype.setThrottleTime = function(throttleTime) {
this.throttledTriggerBaseMatch_ =
new goog.async.Throttle(this.triggerBaseMatch_, throttleTime, this);
};
/**
* Sets the maxMatches to use for the base matcher. If the base matcher makes
* AJAX requests, it may help to make this a large number so that the local
* cache gets populated quickly.
*
* Default value: 100.
*
* @param {number} maxMatches The value to set.
*/
goog.ui.ac.CachingMatcher.prototype.setBaseMatcherMaxMatches = function(
maxMatches) {
this.baseMatcherMaxMatches_ = maxMatches;
};
/**
* Sets the maximum size of the local cache. If the local cache grows larger
* than this size, it will be emptied.
*
* Default value: 1000.
*
* @param {number} maxCacheSize .
*/
goog.ui.ac.CachingMatcher.prototype.setMaxCacheSize = function(maxCacheSize) {
this.maxCacheSize_ = maxCacheSize;
};
/**
* Sets the local matcher to use.
*
* The local matcher should be a function with the same signature as
* {@link goog.ui.ac.ArrayMatcher.getMatchesForRows}, i.e. its arguments are
* searchToken, maxMatches, rowsToSearch; and it returns a list of matching
* rows.
*
* Default value: {@link goog.ui.ac.ArrayMatcher.getMatchesForRows}.
*
* @param {function(string, number, !Array<!Object>): !Array<!Object>}
* localMatcher
*/
goog.ui.ac.CachingMatcher.prototype.setLocalMatcher = function(localMatcher) {
this.getMatchesForRows_ = localMatcher;
};
/**
* Function used to pass matches to the autocomplete.
* @param {string} token Token to match.
* @param {number} maxMatches Max number of matches to return.
* @param {Function} matchHandler callback to execute after matching.
*/
goog.ui.ac.CachingMatcher.prototype.requestMatchingRows = function(
token, maxMatches, matchHandler) {
this.mostRecentMaxMatches_ = maxMatches;
this.mostRecentToken_ = token;
this.mostRecentMatchHandler_ = matchHandler;
this.throttledTriggerBaseMatch_.fire();
var matches = this.getMatchesForRows_(token, maxMatches, this.rows_);
matchHandler(token, matches);
this.mostRecentMatches_ = matches;
};
/** Clears the cache. */
goog.ui.ac.CachingMatcher.prototype.clearCache = function() {
this.rows_ = [];
this.rowStrings_ = {};
};
/**
* Adds the specified rows to the cache.
* @param {!Array<!Object>} rows .
* @private
*/
goog.ui.ac.CachingMatcher.prototype.addRows_ = function(rows) {
goog.array.forEach(rows, function(row) {
// The ' ' prefix is to avoid colliding with builtins like toString.
if (!this.rowStrings_[' ' + row]) {
this.rows_.push(row);
this.rowStrings_[' ' + row] = true;
}
}, this);
};
/**
* Checks if the cache is larger than the maximum cache size. If so clears it.
* @private
*/
goog.ui.ac.CachingMatcher.prototype.clearCacheIfTooLarge_ = function() {
if (this.rows_.length > this.maxCacheSize_) {
this.clearCache();
}
};
/**
* Triggers a match request against the base matcher. This function is
* unthrottled, so don't call it directly; instead use
* this.throttledTriggerBaseMatch_.
* @private
*/
goog.ui.ac.CachingMatcher.prototype.triggerBaseMatch_ = function() {
this.baseMatcher_.requestMatchingRows(
this.mostRecentToken_, this.baseMatcherMaxMatches_,
goog.bind(this.onBaseMatch_, this));
};
/**
* Handles a match response from the base matcher.
* @param {string} token The token against which the base match was called.
* @param {!Array<!Object>} matches The matches returned by the base matcher.
* @private
*/
goog.ui.ac.CachingMatcher.prototype.onBaseMatch_ = function(token, matches) {
// NOTE(reinerp): The user might have typed some more characters since the
// base matcher request was sent out, which manifests in that token might be
// older than this.mostRecentToken_. We make sure to do our local matches
// using this.mostRecentToken_ rather than token so that we display results
// relevant to what the user is seeing right now.
// NOTE(reinerp): We compute a diff between the currently displayed results
// and the new results we would get now that the server results have come
// back. Using this diff, we make sure the new results are only added to the
// end of the list of results. See the documentation on
// this.mostRecentMatches_ for details
this.addRows_(matches);
var oldMatchesSet = {};
goog.array.forEach(this.mostRecentMatches_, function(match) {
// The ' ' prefix is to avoid colliding with builtins like toString.
oldMatchesSet[' ' + match] = true;
});
var newMatches = this.getMatchesForRows_(
this.mostRecentToken_, this.mostRecentMaxMatches_, this.rows_);
newMatches = goog.array.filter(
newMatches, function(match) { return !(oldMatchesSet[' ' + match]); });
newMatches = this.mostRecentMatches_.concat(newMatches)
.slice(0, this.mostRecentMaxMatches_);
this.mostRecentMatches_ = newMatches;
// We've gone to the effort of keeping the existing rows as before, so let's
// make sure to keep them highlighted.
var options = new goog.ui.ac.RenderOptions();
options.setPreserveHilited(true);
this.mostRecentMatchHandler_(this.mostRecentToken_, newMatches, options);
// We clear the cache *after* running the local match, so we don't
// suddenly remove results just because the remote match came back.
this.clearCacheIfTooLarge_();
};
|