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

feat(xhr): add request and response hook API #1393

Merged
merged 13 commits into from
May 3, 2023
Merged

feat(xhr): add request and response hook API #1393

merged 13 commits into from
May 3, 2023

Conversation

adrums86
Copy link
Contributor

@adrums86 adrums86 commented Apr 27, 2023

Description

This PR would add xhr request and response hooks via an onRequest(callback) and onResponse(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 the offRequest(callback) and offResponse(callback) API. The callback functions can take the following parameters:

  • onRequest callbacks will pass the xhr request Object to the callback.
  • onResponse callbacks will pass the xhr request, error, and response Objects to the callback in that order.

In addition, the user can use both global videojs.Vhs and player player.tech().xhr scoped API functions to add these hooks, where player hooks will override any global hooks.

Note: onRequest hooks are called after the beforeRequest 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 the VhsHandler 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

  • Feature implemented / Bug fixed
  • If necessary, more likely in a feature request than a bug fix
  • Reviewed by Two Core Contributors

@codecov
Copy link

codecov bot commented Apr 27, 2023

Codecov Report

Merging #1393 (e56d103) into main (bdd842a) will increase coverage by 0.04%.
The diff coverage is 95.74%.

@@            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     
Impacted Files Coverage Δ
src/videojs-http-streaming.js 90.96% <94.73%> (+0.32%) ⬆️
src/xhr.js 100.00% <100.00%> (ø)

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

src/videojs-http-streaming.js Outdated Show resolved Hide resolved
src/xhr.js Outdated Show resolved Hide resolved
src/xhr.js Outdated Show resolved Hide resolved
src/xhr.js Outdated Show resolved Hide resolved
@adrums86 adrums86 marked this pull request as ready for review April 28, 2023 17:54
@adrums86 adrums86 self-assigned this Apr 28, 2023
README.md Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
Copy link
Member

@gkatsev gkatsev left a 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.

README.md Show resolved Hide resolved
src/videojs-http-streaming.js Outdated Show resolved Hide resolved
src/videojs-http-streaming.js Outdated Show resolved Hide resolved
@dzianis-dashkevich
Copy link
Contributor

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!

@adrums86
Copy link
Contributor Author

adrums86 commented May 1, 2023

@gkatsev I totally agree regarding namespacing these to the xhr object to separate the hooks from fetch. Update incoming.

@adrums86
Copy link
Contributor Author

adrums86 commented May 2, 2023

@gkatsev and @misteroneill the namespace changes are pushed. Another quick look would be appreciated before merging. Thanks!

Copy link
Member

@misteroneill misteroneill left a comment

Choose a reason for hiding this comment

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

:shipit:

README.md Outdated Show resolved Hide resolved
@adrums86 adrums86 merged commit 2356c34 into main May 3, 2023
@adrums86 adrums86 deleted the xhr-hooks branch May 3, 2023 02:37
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.

5 participants