-
Notifications
You must be signed in to change notification settings - Fork 427
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
feat(xhr): add request and response hook API #1393
Conversation
Codecov Report
@@ Coverage Diff @@
## main #1393 +/- ##
==========================================
+ Coverage 85.40% 85.45% +0.04%
==========================================
Files 40 40
Lines 9963 10010 +47
Branches 2308 2317 +9
==========================================
+ Hits 8509 8554 +45
- Misses 1454 1456 +2
📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more |
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.
A couple minor points.
I think my main question is whether these new methods should be namespaced on the xhr object itself. The main reason is that I think it'll reduce confusion in the future when VHS gets fetch
support. So, people won't expect these to work when fetch
is enabled.
I thought about this kind of simplification xhr._requestHooksSet = new Set();
xhr._responseHooksSet = new Set();
const addOnRequestHook = (callback) => xhr._requestHooksSet.add(callback);
const addOnResponseHook = (callback) => xhr._responseHooksSet.add(callback);
const removeOnRequestHook = (callback) => xhr._requestHooksSet.delete(callback);
const removeOnResponseHook = (callback) => xhr._responseHooksSet.delete(callback); but then I realized that those 2 XHRs are not the same pointers... LGTM! |
@gkatsev I totally agree regarding namespacing these to the xhr object to separate the hooks from fetch. Update incoming. |
@gkatsev and @misteroneill the namespace changes are pushed. Another quick look would be appreciated before merging. Thanks! |
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.
Description
This PR would add xhr request and response hooks via an
onRequest(callback)
andonResponse(callback)
API. The user can add as many unique hook callbacks as they want, and they will be called in the order they were added before the request and after the response is received respectively. The user can un-register these callbacks via theoffRequest(callback)
andoffResponse(callback)
API. The callback functions can take the following parameters:onRequest
callbacks will pass the xhrrequest
Object to the callback.onResponse
callbacks will pass the xhrrequest, error, and response
Objects to the callback in that order.In addition, the user can use both global
videojs.Vhs
and playerplayer.tech().xhr
scoped API functions to add these hooks, where player hooks will override any global hooks.Note:
onRequest
hooks are called after thebeforeRequest
function, so be aware that these hooks may further modify request options passed through that function.Specific Changes proposed
Adding API functions to both
Vhs
and theVhsHandler
for adding and removing unique xhr request and response hooks, passing those hooks to the xhr module, then calling them in the order they were added before an xhr request and in the response callback respectively.Requirements Checklist