Skip to content

Commit

Permalink
Merge pull request #444 from CloudCannon/fix/debounce-with-options
Browse files Browse the repository at this point in the history
Fix `debouncedSearch()` when passing an options object
  • Loading branch information
bglw authored Sep 19, 2023
2 parents 2b68c84 + 5954094 commit 5eff203
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

## Unreleased

* Fixes a bug where `debouncedSearch` returns `null` if any options object is passed to it.

## v1.0.3 (September 16, 2023)

Hopefully the last hotfix for now — bugfixes only important for sites indexing Japanese or Chinese pages.
Expand Down
33 changes: 33 additions & 0 deletions pagefind/features/debounce.feature
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,36 @@ Feature: Debounced Searches
Then There should be no logs
Then The selector "[data-types]" should contain "null, null, null, null, 1"
Then The selector "[data-last]" should contain "/cat/"

Scenario: Debounce with options
Given I have a "public/cat/index.html" file with the body:
"""
<h1>world</h1>
"""
When I run my program
Then I should see "Running Pagefind" in stdout
Then I should see the file "public/pagefind/pagefind.js"
When I serve the "public" directory
When I load "/"
When I evaluate:
"""
async function() {
let pagefind = await import("/pagefind/pagefind.js");
let results = await Promise.all([
pagefind.debouncedSearch("a", { filters: {} }),
pagefind.debouncedSearch("w", { filters: {} }),
pagefind.debouncedSearch("wo", { filters: {} }),
pagefind.debouncedSearch("wor", { filters: {} }),
pagefind.debouncedSearch("worl", { filters: {} })
]);
document.querySelector('[data-types]').innerText = results.map(r => (r === null ? "null" : r.results.length)).join(', ');
let pages = await Promise.all(results[4].results.map(r => r.data()));
document.querySelector('[data-last]').innerText = pages.map(p => p.url).sort().join(", ");
}
"""
Then There should be no logs
Then The selector "[data-types]" should contain "null, null, null, null, 1"
Then The selector "[data-last]" should contain "/cat/"
6 changes: 4 additions & 2 deletions pagefind_web_js/lib/coupled_search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,8 +346,10 @@ class PagefindInstance {
}

async preload(term: string, options: PagefindSearchOptions = {}) {
options.preload = true;
await this.search(term, options);
await this.search(term, {
...options,
preload: true
});
}

async search(term: string, options: PagefindSearchOptions = {}): Promise<PagefindSearchResults | null> {
Expand Down

0 comments on commit 5eff203

Please sign in to comment.