Skip to content

Commit

Permalink
Merge pull request #685 from searchspring/backfill-meta-prefetch
Browse files Browse the repository at this point in the history
Backfill meta prefetch
  • Loading branch information
korgon authored Nov 16, 2022
2 parents 44482f0 + 17fa02a commit 550f7f5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
12 changes: 10 additions & 2 deletions packages/snap-controller/src/Search/SearchController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,9 +272,15 @@ export class SearchController extends AbstractController {
let previousResults = this.previousResults;
const backfills = [];

let pageSize = params.pagination?.pageSize || this.store.pagination.pageSize || this.store.pagination.defaultPageSize;
if (this.config.settings?.infinite.backfill && !previousResults.length) {
// figure out how many pages of results to backfill and wait on all responses
const pageSize = params.pagination?.pageSize || this.store.pagination.pageSize || this.store.pagination.defaultPageSize;
if (!pageSize) {
//unfortunatly we need to fetch meta to know the default pagesize before we can continue.
const meta = await this.client.meta();
pageSize = meta.pagination?.defaultPageSize!;
}

let pagesNeeded1 =
params.pagination?.page && params.pagination?.page > this.config.settings?.infinite.backfill
? this.config.settings?.infinite.backfill
Expand Down Expand Up @@ -327,7 +333,8 @@ export class SearchController extends AbstractController {
}

//we need to overwrite the pagination params so the ui doesnt get confused.
response.pagination.pageSize = params.pagination?.pageSize || this.store.pagination.pageSize || this.store.pagination.defaultPageSize;
response.pagination.pageSize = pageSize;
response.pagination.totalPages = Math.ceil(response.pagination.totalResults / response.pagination.pageSize);
response.pagination.page = params.pagination?.page;

//set the response results after all backfill promises are resolved.
Expand Down Expand Up @@ -386,6 +393,7 @@ export class SearchController extends AbstractController {
afterSearchProfile.stop();
this.log.profile(afterSearchProfile);

// store previous results for infinite usage
if (this.config.settings?.infinite) {
this.previousResults = JSON.parse(JSON.stringify(response.results));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class SearchPaginationStore {
this.page = paginationData.page!;
this.pageSize = paginationData.pageSize!;
this.totalResults = paginationData.totalResults!;
this.defaultPageSize = meta?.pagination?.defaultPageSize! || 24;
this.defaultPageSize = meta?.pagination?.defaultPageSize!;
this.totalPages = paginationData.totalPages!;

this.pageSizeOptions = [
Expand Down

0 comments on commit 550f7f5

Please sign in to comment.