From fae0c37c48a9a5d3510b02b301be197ca91e3d0a Mon Sep 17 00:00:00 2001 From: ctinney Date: Sat, 6 Oct 2018 15:02:53 -0400 Subject: [PATCH 1/3] Add cache disabling for datasets --- README.md | 2 ++ src/autocomplete/dataset.js | 6 ++++++ test/unit/dataset_spec.js | 25 ++++++++++++++++++++++++- 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index c7813abc7..0e26ee54f 100644 --- a/README.md +++ b/README.md @@ -482,6 +482,8 @@ 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. +* `disableCache` - If set, subsequent identical queries will always execute the source +function for suggestions. Defaults to `false`. ## Sources diff --git a/src/autocomplete/dataset.js b/src/autocomplete/dataset.js index b01501fb6..7081bbfa2 100644 --- a/src/autocomplete/dataset.js +++ b/src/autocomplete/dataset.js @@ -37,6 +37,8 @@ function Dataset(o) { this.debounce = o.debounce; + this.disableCache = !!o.disableCache; + this.templates = getTemplates(o.templates, this.displayFn); this.css = _.mixin({}, css, o.appendTo ? css.appendTo : {}); @@ -235,6 +237,10 @@ _.mixin(Dataset.prototype, EventEmitter, { }, shouldFetchFromCache: function shouldFetchFromCache(query) { + if (this.disableCache) { + return false; + } + return this.cachedQuery === query && this.cachedSuggestions && this.cachedSuggestions.length; }, diff --git a/test/unit/dataset_spec.js b/test/unit/dataset_spec.js index db01eb107..1e25c45ba 100644 --- a/test/unit/dataset_spec.js +++ b/test/unit/dataset_spec.js @@ -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'); @@ -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'), + disableCache: true, + }); + + 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); From ce0b4c49b1ceef47b9d3ebd6fcef8411d0d90a39 Mon Sep 17 00:00:00 2001 From: ctinney Date: Mon, 8 Oct 2018 14:56:24 -0400 Subject: [PATCH 2/3] Invert cache disable flag --- README.md | 3 +-- src/autocomplete/dataset.js | 11 +++++------ test/unit/dataset_spec.js | 2 +- 3 files changed, 7 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 0e26ee54f..02a5a9c78 100644 --- a/README.md +++ b/README.md @@ -482,8 +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. -* `disableCache` - If set, subsequent identical queries will always execute the source -function for suggestions. Defaults to `false`. +* `enableCache` - If set to `false`, subsequent identical queries will always execute the source function for suggestions. Defaults to `true`. ## Sources diff --git a/src/autocomplete/dataset.js b/src/autocomplete/dataset.js index 7081bbfa2..b3194bc16 100644 --- a/src/autocomplete/dataset.js +++ b/src/autocomplete/dataset.js @@ -37,7 +37,7 @@ function Dataset(o) { this.debounce = o.debounce; - this.disableCache = !!o.disableCache; + this.enableCache = o.enableCache !== false; this.templates = getTemplates(o.templates, this.displayFn); @@ -237,11 +237,10 @@ _.mixin(Dataset.prototype, EventEmitter, { }, shouldFetchFromCache: function shouldFetchFromCache(query) { - if (this.disableCache) { - return false; - } - - return this.cachedQuery === query && this.cachedSuggestions && this.cachedSuggestions.length; + return this.enableCache && + this.cachedQuery === query && + this.cachedSuggestions && + this.cachedSuggestions.length; }, clearCachedSuggestions: function clearCachedSuggestions() { diff --git a/test/unit/dataset_spec.js b/test/unit/dataset_spec.js index 1e25c45ba..05cfae760 100644 --- a/test/unit/dataset_spec.js +++ b/test/unit/dataset_spec.js @@ -312,7 +312,7 @@ describe('Dataset', function() { this.dataset = new Dataset({ name: 'test', source: this.source = jasmine.createSpy('source'), - disableCache: true, + enableCache: false, }); this.source.and.callFake(fakeGetWithSyncResultsAndExtraParams); From 4989978b1ba18e48758d9925b167492632b44c8a Mon Sep 17 00:00:00 2001 From: ctinney Date: Fri, 12 Oct 2018 09:25:58 -0400 Subject: [PATCH 3/3] Rename `enableCache` flag to `cache` --- README.md | 2 +- src/autocomplete/dataset.js | 4 ++-- test/unit/dataset_spec.js | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 02a5a9c78..b71f1f34b 100644 --- a/README.md +++ b/README.md @@ -482,7 +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`. +* `cache` - If set to `false`, subsequent identical queries will always execute the source function for suggestions. Defaults to `true`. ## Sources diff --git a/src/autocomplete/dataset.js b/src/autocomplete/dataset.js index b3194bc16..bd1f56f3f 100644 --- a/src/autocomplete/dataset.js +++ b/src/autocomplete/dataset.js @@ -37,7 +37,7 @@ function Dataset(o) { this.debounce = o.debounce; - this.enableCache = o.enableCache !== false; + this.cache = o.cache !== false; this.templates = getTemplates(o.templates, this.displayFn); @@ -237,7 +237,7 @@ _.mixin(Dataset.prototype, EventEmitter, { }, shouldFetchFromCache: function shouldFetchFromCache(query) { - return this.enableCache && + return this.cache && this.cachedQuery === query && this.cachedSuggestions && this.cachedSuggestions.length; diff --git a/test/unit/dataset_spec.js b/test/unit/dataset_spec.js index 05cfae760..1a4cb0ae3 100644 --- a/test/unit/dataset_spec.js +++ b/test/unit/dataset_spec.js @@ -312,7 +312,7 @@ describe('Dataset', function() { this.dataset = new Dataset({ name: 'test', source: this.source = jasmine.createSpy('source'), - enableCache: false, + cache: false, }); this.source.and.callFake(fakeGetWithSyncResultsAndExtraParams);