-
Notifications
You must be signed in to change notification settings - Fork 0
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
base: master
Are you sure you want to change the base?
Conversation
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.
There was a problem hiding this 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
const targetElement = getTargetElement(); | ||
const preserveOrderId = ( | ||
behaviorElement || currentElement | ||
)?.getAttribute('preserve-order') === 'true' | ||
? String(Math.random()) | ||
: null; | ||
if (preserveOrderId !== null) { | ||
targetElement.setAttribute('_preserve-order-id', preserveOrderId); | ||
} |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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
if (preserveOrderId) { | ||
// Un-set the preserve order id on the target element as the update is now done | ||
targetElement.setAttribute('_preserve-order-id', ""); | ||
} |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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
const preserveOrderId = ( | ||
behaviorElement || currentElement | ||
)?.getAttribute('preserve-order') === 'true' | ||
? String(Math.random()) |
There was a problem hiding this comment.
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
This PR introduce a new behavior boolean attribute,
preserve-order
. This attribute is used by behaviors that "fetch" content, i.e. anytime there's ahref
attribute set, with an update action such asreplace
,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: