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

Feature request: customizable ontimeupdate frequency #51

Open
gonzaloarca opened this issue Dec 15, 2023 · 3 comments
Open

Feature request: customizable ontimeupdate frequency #51

gonzaloarca opened this issue Dec 15, 2023 · 3 comments

Comments

@gonzaloarca
Copy link

gonzaloarca commented Dec 15, 2023

Trying out the new ontimeupdate callback feature, which is great. However, since it fires so quickly, it ends up leading to excessive amounts of re-renders in React when calling state setters from the callback. I was able to verify this by setting 3 different functions to the ontimeupdate callback:

  1. (...) => {}
  2. (...) => setState(...)
  3. (...) => console.log(...)

Function 2 is the only one that causes the entire UI to act very choppy and laggy, and the other ones do not cause any performance issues. Being able to set a custom value for the callback frequency would definitely help anyone who experiences poor performance from using the ontimeupdate feature.

Thank you!

@regosen
Copy link
Owner

regosen commented Dec 15, 2023

Why not use debounce on the client side? Something like:

import debounce from 'lodash/debounce';
...
[trackPosition, setTrackPosition] = React.useState(0);
const debouncedPositionUpdate = debounce((trackPos) => setTrackPosition(trackPos), CALLBACK_FREQUENCY);
// const debouncedPositionUpdate = debounce(setTrackPosition, CALLBACK_FREQUENCY); might work too
gaplessPlayer.ontimeupdate = (position, _trackIndex) => debouncedPositionUpdate(position);

Doesn't have to be the lodash version, but you get the idea.

@gonzaloarca
Copy link
Author

Interesting. However, if you debounce it to, for example 200 ms, and let's say that the function is called every 27 ms, then debouncing it means it will not run until 200 ms take place between the function calls. That will never happen since it is called every 27 ms. The only way to get that gap is if you pause the player.

@gonzaloarca
Copy link
Author

gonzaloarca commented Dec 15, 2023

I think lodash's throttle might be the right tool for the solution you proposed. Good idea, I think this might just do the trick. Thanks!

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

No branches or pull requests

2 participants