Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: stop simultaneous recursive chains #225

Merged
merged 1 commit into from
Apr 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions apps/shopify/src/Pagination.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,13 @@ class Pagination {
this.shopifyClient = await makeShopifyClient(this.sdk);
}

async fetchNext(search) {
async fetchNext(search, recursing = false) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not really a fan of arbitrary flags, but that might be me. What about interrupting recursion before entering the new call? So just before the return of this method (L63 in the new version, L58 of the old one)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure. I think that makes it more likely that we'll be returning data from a dead chain - and I think would make this change a lot more complicated.

const searchHasChanged = search !== this.prevSearch;
const shouldStop = searchHasChanged && recursing;
if (shouldStop) {
return;
}

if (searchHasChanged) {
this.prevSearch = search;
this._resetPagination();
Expand Down Expand Up @@ -55,7 +60,7 @@ class Pagination {
// When there are not enough variants to fill the page, we need to fetch more products,
// extract their variants and then call this method recursively to render the next page.
await this._fetchMoreProducts(search);
return this.fetchNext(search);
return this.fetchNext(search, true);
}

/**
Expand Down
8 changes: 6 additions & 2 deletions packages/ecommerce-app-base/src/SkuPicker/SkuPicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,12 @@ export class SkuPicker extends Component<Props, State> {
search
} = this.state;
const offset = (activePage - 1) * limit;
const { pagination, products } = await this.props.fetchProducts(search, { offset });
this.setState({ pagination, products });
const fetched = await this.props.fetchProducts(search, { offset });
// If the request has been cancelled because a new one has been launched
// then fetchProducts will return null
if (fetched && fetched.pagination && fetched.products) {
this.setState({ pagination: fetched.pagination, products: fetched.products });
}
} catch (error) {
this.props.sdk.notifier.error('There was an error fetching the product list.');
}
Expand Down