Skip to content

fix: improve compatibility of loadstart/load/loadend events #181

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

ryo-fujinone
Copy link

@ryo-fujinone ryo-fujinone commented Jun 1, 2025

  • moved dispatch of loadstart event to send()
  • included ProgressEvent-specific properties in loadstart/load/loadend

moved dispatch of loadstart event to send()

Native XHR fires the loadstart event right after xhr.send(), so xhr.addEventListener(“loadstart”, listener) can be placed either before or after xhr.open().
xhook currently dispatches loadstart inside open(). If xhr.open() is called before xhr.addEventListener(“loadstart”, listener), that listener will not work.
This issue is fixed by moving the loadstart dispatch to send().

I noticed an interesting comment just before the loadstart dispatch, but personally, I think we should imitate the behavior of native XHR.

// make fake events for libraries that actually check the type on
// the event object

included ProgressEvent-specific properties in loadstart/load/loadend

In native XHR, the loadstart/load/loadend events are ProgressEvent and have specific properties (lengthComputable, loaded, total).
These were missing from the xhook events, so I added them.

  • loadstart
    • sets initial values.
  • progress
    • caches the latest { lengthComputable, loaded, total } in a private variable (lastProgress).
  • load/loadend
    • uses the cached values when available.

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