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

proposal: preserve fetching order #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

lizard-boy
Copy link

This PR introduce a new behavior boolean attribute, preserve-order. This attribute is used by behaviors that "fetch" content, i.e. anytime there's a href attribute set, with an update action such as replace, replace-inner, etc. When present, we compute a unique id before the request is emitted, and store it on an attribute of the target element. When the response arrives, we read the attribute of the target element and compare it to the one we generated before the request was sent. If the attribute is set with a value that's different from the one set before the request, we assume this means another request/response cycle took place, and the response we're presently dealing with is "outdated", so we discard it.

To be discussed:

  • naming: is "preserve-order" descriptive enough? should we use a boolean or an enum?
  • visibility: should we make this feature public / documented or keep it "dark" for now?
  • scope: should we think of this capability to be applied to other, non-fetching behaviors?

This PR introduce a new behavior boolean attribute, `preserve-order`.
This attribute is used by behaviors that "fetch" content, i.e. anytime there's a `href` attribute set, with an update action such as `replace`, `replace-inner`, etc. When present, we compute an unique id before the request is emitted, and store it on an attribute of the target element. When the response arrives, we read the attribute of the target element and compare it to the one we generated before the request was sent. If the attribute is set with a value that's different from the one set before the request, we assume this means another request/response cycle took place, and the response we're presently dealing with is "outdated", so we discard it.
Copy link

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

PR Summary

This PR introduces a new 'preserve-order' attribute to handle sequenced requests in Hyperview, addressing potential race conditions in asynchronous content fetching.

  • Added 'preserve-order' attribute to the search behavior in examples/case_studies/contacts/list.xml.njk
  • Implemented logic in src/core/components/hv-screen/index.js to track and validate request order using unique IDs
  • Modified examples/case_studies/contacts/index.xml.js to include an additional condition for query parameter checks
  • Refactored onUpdateFragment method in HvScreen component for improved code organization

3 file(s) reviewed, 5 comment(s)
Edit PR Review Bot Settings

Comment on lines +522 to +530
const targetElement = getTargetElement();
const preserveOrderId = (
behaviorElement || currentElement
)?.getAttribute('preserve-order') === 'true'
? String(Math.random())
: null;
if (preserveOrderId !== null) {
targetElement.setAttribute('_preserve-order-id', preserveOrderId);
}
Copy link

Choose a reason for hiding this comment

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

style: Consider using a more descriptive name for preserveOrderId, such as requestSequenceId

// When fetch fails, make sure to get the latest version of the doc to avoid any race conditions
newRoot = this.doc;
const targetElement = getTargetElement();
const shouldSkipUpdate = preserveOrderId !== null && targetElement.getAttribute('_preserve-order-id') !== preserveOrderId;
Copy link

Choose a reason for hiding this comment

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

style: Add a comment explaining the purpose of shouldSkipUpdate for better code readability

Comment on lines +540 to +543
if (preserveOrderId) {
// Un-set the preserve order id on the target element as the update is now done
targetElement.setAttribute('_preserve-order-id', "");
}
Copy link

Choose a reason for hiding this comment

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

style: Consider moving this logic to a separate method for better organization

? String(Math.random())
: null;
if (preserveOrderId !== null) {
targetElement.setAttribute('_preserve-order-id', preserveOrderId);
Copy link

Choose a reason for hiding this comment

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

style: Use a constant for '_preserve-order-id' to avoid magic strings

Comment on lines +523 to +526
const preserveOrderId = (
behaviorElement || currentElement
)?.getAttribute('preserve-order') === 'true'
? String(Math.random())
Copy link

Choose a reason for hiding this comment

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

style: Use optional chaining (?.) consistently here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant