Skip to content

Commit

Permalink
Fix bug where in-page navigation would not fetch new tags
Browse files Browse the repository at this point in the history
  • Loading branch information
erkie committed Aug 31, 2016
1 parent 54c3791 commit 6cc8ec6
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 12 deletions.
51 changes: 50 additions & 1 deletion spec/TagCacheSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ describe('tag_cache', function() {
// Try to fetch tags when server crashed
runs(function() {
setupGlimrCrashedServer();
delete Glimr.state.loadedTags["keywords_cache_normal"];
clearGlimrState();

Glimr.state.currentURLCacheKey = "6666cd76f9"; // Check the keywords_cache_normal to see why this value was chosen

isDone = false;

Expand Down Expand Up @@ -281,4 +283,51 @@ describe('tag_cache', function() {
expect(tags).toContain("apple");
});
});

it("should make network requests if navigated to different URL", function() {
var isDone = false;
var tags;

// Fetch tags normally
runs(function() {
document.location.hash = "#!/";
Glimr.setTagCacheTimeInSeconds(300);
Glimr.getTags("keywords_cache_normal", function(fetchedTags) {
isDone = true;
tags = fetchedTags;
});
});

waitsFor(function() {
return isDone;
});

runs(function() {
expect(tags.length).toBe(4);
expect(tags).toContain("tag_10");
expect(tags).toContain("tag_12");
expect(tags).toContain("tag_1");
expect(tags).toContain("tag_2");

// Simulate in-page navigation
document.location.hash = "#!/example/path";

isDone = false;
Glimr.getTags("keywords_cache_normal", function(fetchedTags) {
isDone = true;
tags = fetchedTags;
});
});

waitsFor(function() {
return isDone;
});

runs(function() {
expect(tags.length).toBe(3);
expect(tags).toContain("tag_10");
expect(tags).toContain("tag_12");
expect(tags).toContain("/example/path");
});
});
});
1 change: 1 addition & 0 deletions spec/mock_responses/keywords_cache_normal.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
"6666cd76f96956469e7be39d750cc7d9": ["tag_1", "tag_2"],
"d41d8cd98f00b204e9800998ecf8427e": ["tag_3", "tag_4"],
"752efa2ae35bbcaa9fea2f7b27f65de8": ["tag_5", "tag_6"],
"848777223b0405239013218c0f0dc715": ["/example/path"]
}
}
23 changes: 12 additions & 11 deletions src/glimr.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,18 +235,19 @@
};

Gp.getTags = function(pixelId, callback) {
if (this.state.loadedTags[pixelId]) {
var response = this.state.loadedTags[pixelId];
var pageCacheId = pixelId + this.currentURLCacheKey();
if (this.state.loadedTags[pageCacheId]) {
var response = this.state.loadedTags[pageCacheId];
callback(response[0], response[1]);
return;
}

if (typeof this.state.loadingTags[pixelId] !== "undefined") {
this.state.loadingTags[pixelId].push(callback);
if (typeof this.state.loadingTags[pageCacheId] !== "undefined") {
this.state.loadingTags[pageCacheId].push(callback);
return;
}

this._requestTags(pixelId, callback, Library.bindFunction(this, function(data) {
this._requestTags(pixelId, pageCacheId, callback, Library.bindFunction(this, function(data) {
var tags = [];
var tagMappings = {};
var toCache = tags;
Expand Down Expand Up @@ -277,10 +278,10 @@
}
}

this.state.loadedTags[pixelId] = [tags, tagMappings];
this.state.loadedTags[pageCacheId] = [tags, tagMappings];

var callbacks = this.state.loadingTags[pixelId];
delete this.state.loadingTags[pixelId];
var callbacks = this.state.loadingTags[pageCacheId];
delete this.state.loadingTags[pageCacheId];

for (var j = 0; j < callbacks.length; j += 1) {
callbacks[j](tags, tagMappings);
Expand Down Expand Up @@ -308,7 +309,7 @@
}
};

Gp._requestTags = function(pixelId, userCallback, parseCallback) {
Gp._requestTags = function(pixelId, pageCacheId, userCallback, parseCallback) {
this.initGlimrId();

var pixelLastUpdated = this.getPixelLastUpdated(pixelId);
Expand All @@ -335,8 +336,8 @@
}
}

this.state.loadingTags[pixelId] = [];
this.state.loadingTags[pixelId].push(userCallback);
this.state.loadingTags[pageCacheId] = [];
this.state.loadingTags[pageCacheId].push(userCallback);

Library.JSONP(requestUrl, parseCallback);
};
Expand Down

0 comments on commit 6cc8ec6

Please sign in to comment.