Skip to content

Commit

Permalink
feat(source): add cache disabling for datasets (#254)
Browse files Browse the repository at this point in the history
* Add cache disabling for datasets

* Invert cache disable flag

* Rename `enableCache` flag to `cache`
  • Loading branch information
cdtinney authored and Haroenv committed Oct 12, 2018
1 parent d343bee commit 0e65fee
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
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.

* `cache` - 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.cache = o.cache !== 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.cache &&
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'),
cache: 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

0 comments on commit 0e65fee

Please sign in to comment.