Skip to content

Commit 2435e55

Browse files
authored
WEBDEV-7150 Ensure cleared query propagates to data source & URL (#412)
* Ensure cleared query propagates to data source & URL * Fix race condition in initial search promise
1 parent beed37a commit 2435e55

File tree

3 files changed

+14
-21
lines changed

3 files changed

+14
-21
lines changed

src/collection-browser.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1717,15 +1717,14 @@ export class CollectionBrowser
17171717
if (
17181718
!this.searchService ||
17191719
this.dataSource.pageFetchQueryKey === this.previousQueryKey
1720-
)
1720+
) {
17211721
return;
1722+
}
17221723

17231724
// If the new state prevents us from updating the search results, don't reset
1724-
if (
1725-
!this.dataSource.canPerformSearch &&
1726-
!(this.clearResultsOnEmptyQuery && this.baseQuery === '')
1727-
)
1725+
if (this.baseQuery && !this.dataSource.canPerformSearch) {
17281726
return;
1727+
}
17291728

17301729
this.previousQueryKey = this.dataSource.pageFetchQueryKey;
17311730

src/data-source/collection-browser-data-source.ts

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -149,18 +149,11 @@ export class CollectionBrowserDataSource
149149
*/
150150
queryErrorMessage?: string;
151151

152-
/**
153-
* Internal property to store the `resolve` function for the most recent
154-
* `initialSearchComplete` promise, allowing us to resolve it at the appropriate time.
155-
*/
156-
private _initialSearchCompleteResolver!: (val: boolean) => void;
157-
158152
/**
159153
* Internal property to store the private value backing the `initialSearchComplete` getter.
160154
*/
161-
private _initialSearchCompletePromise: Promise<boolean> = new Promise(res => {
162-
this._initialSearchCompleteResolver = res;
163-
});
155+
private _initialSearchCompletePromise: Promise<boolean> =
156+
Promise.resolve(true);
164157

165158
/**
166159
* @inheritdoc
@@ -205,10 +198,9 @@ export class CollectionBrowserDataSource
205198

206199
// We should only reset if either:
207200
// (a) our state permits a valid search, or
208-
// (b) we have a blank query that we want to show empty results for
209-
const shouldShowEmptyQueryResults =
210-
this.host.clearResultsOnEmptyQuery && this.host.baseQuery === '';
211-
if (!(this.canPerformSearch || shouldShowEmptyQueryResults)) return;
201+
// (b) we have a blank query that we're showing a placeholder/message for
202+
const queryIsEmpty = !this.host.baseQuery;
203+
if (!(this.canPerformSearch || queryIsEmpty)) return;
212204

213205
if (this.activeOnHost) this.host.emitQueryStateChanged();
214206
this.handleQueryChange();
@@ -238,6 +230,7 @@ export class CollectionBrowserDataSource
238230
this.yearHistogramAggregation = undefined;
239231
this.pageElements = undefined;
240232
this.parentCollections = [];
233+
this.previousQueryKey = '';
241234
this.queryErrorMessage = undefined;
242235

243236
this.offset = 0;
@@ -382,8 +375,9 @@ export class CollectionBrowserDataSource
382375
this.reset();
383376

384377
// Reset the `initialSearchComplete` promise with a new value for the imminent search
378+
let initialSearchCompleteResolver: (value: boolean) => void;
385379
this._initialSearchCompletePromise = new Promise(res => {
386-
this._initialSearchCompleteResolver = res;
380+
initialSearchCompleteResolver = res;
387381
});
388382

389383
// Fire the initial page & facet requests
@@ -394,7 +388,7 @@ export class CollectionBrowserDataSource
394388
]);
395389

396390
// Resolve the `initialSearchComplete` promise for this search
397-
this._initialSearchCompleteResolver(true);
391+
initialSearchCompleteResolver!(true);
398392
}
399393

400394
/**

test/collection-browser.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1010,11 +1010,11 @@ describe('Collection Browser', () => {
10101010
const el = await fixture<CollectionBrowser>(
10111011
html`<collection-browser
10121012
.searchService=${searchService}
1013+
@searchResultsLoadingChanged=${spy}
10131014
></collection-browser>`
10141015
);
10151016

10161017
el.baseQuery = 'collection:foo';
1017-
el.addEventListener('searchResultsLoadingChanged', spy);
10181018
await el.updateComplete;
10191019
await el.initialSearchComplete;
10201020

0 commit comments

Comments
 (0)