Skip to content

Commit 6fec1b4

Browse files
Merge pull request #245 from canjs/244-wait-for-searchMap-before-clearing-localstorage
#244 - wait until we have a new searchMap before clearing the old one
2 parents 8213d23 + 0a9b8a5 commit 6fec1b4

File tree

1 file changed

+51
-22
lines changed

1 file changed

+51
-22
lines changed

static/search.js

Lines changed: 51 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ var Search = Control.extend({
1616
//renderer stuff
1717
resultsRenderer: searchResultsRenderer,
1818
pathPrefix: window.pathPrefix,
19-
docMapHashUrl: window.pathPrefix + '/searchMapHash.json',
19+
searchMapHashUrl: window.pathPrefix + '/searchMapHash.json',
2020
searchMapUrl: window.pathPrefix + '/searchMap.json',
2121

2222
//callbacks
@@ -33,7 +33,7 @@ var Search = Control.extend({
3333
minSearchLength: 3,
3434
searchTimeout: 400,
3535

36-
localStorageKeyPrefix: "bit-docs"
36+
localStorageKeyPrefix: "bit-docs-search"
3737
}
3838
}, {
3939

@@ -69,8 +69,8 @@ var Search = Control.extend({
6969
//hide the input until the search engine is ready
7070
this.$inputWrap.hide();
7171

72-
this.checkDocMapHash(this.options.docMapHashUrl).then(function(localStorageCleared){
73-
self.getSearchMap(self.options.searchMapUrl, localStorageCleared).then(function(searchMap){
72+
this.checkSearchMapHash(this.options.searchMapHashUrl).then(function(searchMapHashChangedObject){
73+
self.getSearchMap(self.options.searchMapUrl, searchMapHashChangedObject).then(function(searchMap){
7474
self.initSearchEngine(searchMap);
7575

7676
//show the search input when the search engine is ready
@@ -83,7 +83,11 @@ var Search = Control.extend({
8383
}
8484

8585
self.bindResultsEvents();
86+
}, function(error){
87+
console.error("getSearchMap error", error);
8688
});
89+
}, function(error){
90+
console.error("checkSearchMapHash error", error);
8791
});
8892
},
8993
destroy: function(){
@@ -154,42 +158,69 @@ var Search = Control.extend({
154158
// or the specified url
155159
//
156160
// @param dataUrl the url of the searchMap.json file
157-
// @param localStorageCleared whether or not the localStorage was cleared
161+
// @param searchMapHashChangedObject {localStorageKey, data} if we should clear localStorage
162+
// false otherwise
158163
//
159164
// @returns thenable
160-
getSearchMap: function(dataUrl, localStorageCleared) {
165+
getSearchMap: function(dataUrl, searchMapHashChangedObject) {
161166
var self = this,
162167
returnDeferred = $.Deferred(),
163168
localStorageKey = this.formatLocalStorageKey(this.searchMapLocalStorageKey);
164169

165170
this.searchMap = this.getLocalStorageItem(localStorageKey);
166-
if(this.searchMap){
171+
if(this.searchMap && !searchMapHashChangedObject){
167172
returnDeferred.resolve(this.searchMap);
168173
}else{
174+
169175
$.ajax({
170176
url: dataUrl,
171177
dataType: "json",
172178
cache: true
173179
}).then(function(data){
180+
if(!data){
181+
if(self.searchMap){
182+
returnDeferred.resolve(self.searchMap);
183+
}else{
184+
returnDeferred.reject({
185+
error: "No searchMap data"
186+
});
187+
}
188+
189+
return false;
190+
}
191+
192+
//wait until after we have a new searchMap before clearing (if necessary)
193+
if(searchMapHashChangedObject){
194+
localStorage.clear();
195+
//set the searchMapHash item
196+
self.setLocalStorageItem(searchMapHashChangedObject.localStorageKey, searchMapHashChangedObject.data);
197+
}
198+
174199
//save search map
175200
self.searchMap = data;
176201
self.setLocalStorageItem(localStorageKey, data);
177202
returnDeferred.resolve(data);
178-
}, returnDeferred.reject);
203+
}, function(error){
204+
if(self.searchMap){
205+
returnDeferred.resolve(self.searchMap);
206+
}else{
207+
returnDeferred.reject(error);
208+
}
209+
});
179210
}
180211
return returnDeferred;
181212
},
182213

183-
docMapHashLocalStorageKey: "docMapHash",
184-
// function getDocMapHash
185-
// retrieves the docMapHash localStorage (if present)
214+
searchMapHashLocalStorageKey: "searchMapHash",
215+
// function checkSearchMapHash
216+
// retrieves the searchMapHash localStorage (if present)
186217
// and from the specified url
187218
// then compares the two. If they're different, localStorage is cleared
188219
//
189-
// @param dataUrl the url of the docMapHash.json file
220+
// @param dataUrl the url of the searchMapHash.json file
190221
//
191222
// @returns thenable that resolves to true if localStorage was cleared and false otherwise
192-
checkDocMapHash(dataUrl) {
223+
checkSearchMapHash(dataUrl) {
193224
var self = this,
194225
returnDeferred = $.Deferred();
195226

@@ -204,9 +235,9 @@ var Search = Control.extend({
204235
dataType: "json",
205236
cache: false
206237
}).then(function(data){
207-
var localStorageKey = self.formatLocalStorageKey(self.docMapHashLocalStorageKey),
208-
docMapHashLocalStorage = self.getLocalStorageItem(localStorageKey),
209-
lsHash = docMapHashLocalStorage && docMapHashLocalStorage.hash,
238+
var localStorageKey = self.formatLocalStorageKey(self.searchMapHashLocalStorageKey),
239+
searchMapHashLocalStorage = self.getLocalStorageItem(localStorageKey),
240+
lsHash = searchMapHashLocalStorage && searchMapHashLocalStorage.hash,
210241
dataHash = data && data.hash;
211242

212243
//no lsHash && no dataHash => resolve
@@ -219,12 +250,10 @@ var Search = Control.extend({
219250
//no lsHash && dataHash => save && resolve
220251
//lsHash && dataHash => check if same
221252
if(lsHash !== dataHash){
222-
223-
//TODO: wait until after we've attempted to get a new
224-
//searchMap before clearing?
225-
localStorage.clear();
226-
self.setLocalStorageItem(localStorageKey, data);
227-
returnDeferred.resolve(true);
253+
returnDeferred.resolve({
254+
localStorageKey: localStorageKey,
255+
data: data
256+
});
228257
return;
229258
}
230259

0 commit comments

Comments
 (0)