Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cache disabling for datasets #254

Merged
merged 5 commits into from
Oct 12, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,7 @@ Datasets can be configured using the following options.
* `debounce` – If set, will postpone the source execution until after `debounce` milliseconds
have elapsed since the last time it was invoked.

* `enableCache` - If set to `false`, subsequent identical queries will always execute the source function for suggestions. Defaults to `true`.

## Sources

Expand Down
7 changes: 6 additions & 1 deletion src/autocomplete/dataset.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ function Dataset(o) {

this.debounce = o.debounce;

this.enableCache = o.enableCache !== false;

this.templates = getTemplates(o.templates, this.displayFn);

this.css = _.mixin({}, css, o.appendTo ? css.appendTo : {});
Expand Down Expand Up @@ -235,7 +237,10 @@ _.mixin(Dataset.prototype, EventEmitter, {
},

shouldFetchFromCache: function shouldFetchFromCache(query) {
return this.cachedQuery === query && this.cachedSuggestions && this.cachedSuggestions.length;
return this.enableCache &&
this.cachedQuery === query &&
this.cachedSuggestions &&
this.cachedSuggestions.length;
},

clearCachedSuggestions: function clearCachedSuggestions() {
Expand Down
25 changes: 24 additions & 1 deletion test/unit/dataset_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ describe('Dataset', function() {
expect(this.dataset.cachedRenderExtraArgs).toEqual([42, true, false]);
});

it('should retrieved cached results for subsequent identical queries', function() {
it('should retrieve cached results for subsequent identical queries', function() {
this.source.and.callFake(fakeGetWithSyncResults);

this.dataset.update('woah');
Expand All @@ -308,6 +308,29 @@ describe('Dataset', function() {
expect(this.dataset.getRoot()).toContainText('three');
});

it('should not retrieve cached results for subsequent identical queries if cache is disabled', function() {
this.dataset = new Dataset({
name: 'test',
source: this.source = jasmine.createSpy('source'),
enableCache: false,
});

this.source.and.callFake(fakeGetWithSyncResultsAndExtraParams);

this.dataset.update('woah');
expect(this.source.calls.count()).toBe(1);
expect(this.dataset.getRoot()).toContainText('one');
expect(this.dataset.getRoot()).toContainText('two');
expect(this.dataset.getRoot()).toContainText('three');

this.dataset.clear();
this.dataset.update('woah');
expect(this.source.calls.count()).toBe(2);
expect(this.dataset.getRoot()).toContainText('one');
expect(this.dataset.getRoot()).toContainText('two');
expect(this.dataset.getRoot()).toContainText('three');
});

it('should reuse render function extra params for subsequent identical queries', function() {
var spy = spyOn(this.dataset, '_render');
this.source.and.callFake(fakeGetWithSyncResultsAndExtraParams);
Expand Down