Skip to content

Commit

Permalink
Improve unobserve
Browse files Browse the repository at this point in the history
  • Loading branch information
Shi Su committed Feb 24, 2025
1 parent 5bc9828 commit 52141ee
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/videotile/DefaultVideoElementResolutionMonitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,15 @@ export default class DefaultVideoElementResolutionMonitor implements VideoElemen
}

bindVideoElement(newElement: HTMLVideoElement | null): void {
if (!newElement) {
if (this.element) {
this.resizeObserver.unobserve(this.element);
}
this.element = newElement;
if (this.element === newElement) {
return;
}
if (this.element) {
this.resizeObserver.unobserve(this.element);
}
this.element = newElement;
this.resizeObserver.observe(this.element);
if (this.element) {
this.resizeObserver.observe(this.element);
}
}
}
15 changes: 15 additions & 0 deletions test/videotile/DefaultVideoElementResolutionMonitor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,22 @@ describe('DefaultVideoElementResolutionMonitor', () => {
const videoElement = videoElementFactory.create();
expect(() => monitor.bindVideoElement(videoElement)).to.not.throw();
expect(observeCalled).to.be.true;
expect(unobserveCalled).to.be.false;
observeCalled = false;
unobserveCalled = false;
expect(() => monitor.bindVideoElement(videoElement)).to.not.throw();
expect(observeCalled).to.be.false;
expect(unobserveCalled).to.be.false;
observeCalled = false;
unobserveCalled = false;
const newVideoElement = videoElementFactory.create();
expect(() => monitor.bindVideoElement(newVideoElement)).to.not.throw();
expect(observeCalled).to.be.true;
expect(unobserveCalled).to.be.true;
observeCalled = false;
unobserveCalled = false;
expect(() => monitor.bindVideoElement(null)).to.not.throw();
expect(observeCalled).to.be.false;
expect(unobserveCalled).to.be.true;
});

Expand Down

0 comments on commit 52141ee

Please sign in to comment.