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

fix(player): cache_.currentTime is not updated when the current time is set #8285

Merged
merged 2 commits into from
Jun 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 25 additions & 20 deletions src/js/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -2461,29 +2461,34 @@ class Player extends Component {
* - the current time in seconds when getting
*/
currentTime(seconds) {
if (typeof seconds !== 'undefined') {
if (seconds < 0) {
seconds = 0;
}
if (!this.isReady_ || this.changingSrc_ || !this.tech_ || !this.tech_.isReady_) {
this.cache_.initTime = seconds;
this.off('canplay', this.boundApplyInitTime_);
this.one('canplay', this.boundApplyInitTime_);
return;
}
this.techCall_('setCurrentTime', seconds);
this.cache_.initTime = 0;
if (seconds === undefined) {
// cache last currentTime and return. default to 0 seconds
//
// Caching the currentTime is meant to prevent a massive amount of reads on the tech's
// currentTime when scrubbing, but may not provide much performance benefit after all.
// Should be tested. Also something has to read the actual current time or the cache will
// never get updated.
this.cache_.currentTime = (this.techGet_('currentTime') || 0);
return this.cache_.currentTime;
}

if (seconds < 0) {
seconds = 0;
}

if (!this.isReady_ || this.changingSrc_ || !this.tech_ || !this.tech_.isReady_) {
this.cache_.initTime = seconds;
this.off('canplay', this.boundApplyInitTime_);
this.one('canplay', this.boundApplyInitTime_);
return;
}

// cache last currentTime and return. default to 0 seconds
//
// Caching the currentTime is meant to prevent a massive amount of reads on the tech's
// currentTime when scrubbing, but may not provide much performance benefit after all.
// Should be tested. Also something has to read the actual current time or the cache will
// never get updated.
this.cache_.currentTime = (this.techGet_('currentTime') || 0);
return this.cache_.currentTime;
this.techCall_('setCurrentTime', seconds);
this.cache_.initTime = 0;

if (isFinite(seconds)) {
this.cache_.currentTime = Number(seconds);
}
}

/**
Expand Down
14 changes: 14 additions & 0 deletions test/unit/player.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2676,6 +2676,20 @@ QUnit.test('Should accept multiple calls to currentTime after player initializat
assert.equal(player.currentTime(), 800, 'The last value passed is stored as the currentTime value');
});

QUnit.test('Should be able to set the cache currentTime after player initialization as soon the canplay event is fired', function(assert) {
const player = TestHelpers.makePlayer({});

player.src('xyz.mp4');
player.currentTime(500);

assert.strictEqual(player.getCache().currentTime, 0, 'cache currentTime value was not changed');

this.clock.tick(100);
player.trigger('canplay');

assert.strictEqual(player.getCache().currentTime, 500, 'cache currentTime value is the one passed after initialization');
});

QUnit.test('Should fire debugon event when debug mode is enabled', function(assert) {
const player = TestHelpers.makePlayer({});
const debugOnSpy = sinon.spy();
Expand Down