Skip to content

feat: playerresize event in all cases#4864

Merged
gkatsev merged 32 commits into
masterfrom
resizer
Jan 30, 2018
Merged

feat: playerresize event in all cases#4864
gkatsev merged 32 commits into
masterfrom
resizer

Conversation

@gkatsev

@gkatsev gkatsev commented Jan 8, 2018

Copy link
Copy Markdown
Member

Use ResizeObserver when available for better and more performant resizing information, otherwise, fall back to a throttled resize event on an iframe that's the size of the player.
Allows a video.js user to disable this by setting resizeManager: false as an option since the component will not be initialized.
This probably needs to revert #4800 (e0ed0b5) because we end up getting two playerresize events with the dimension methods now.

TODO:

  • Revert feat: player resize event #4800
  • fix current tests
  • make RO polyfill possible
  • allow RO to be disabled
  • debounce calls instead of throttle
  • Write tests
  • documentation
  • make sure things are disposed properly
    • events
    • objects

@gkatsev

gkatsev commented Jan 9, 2018

Copy link
Copy Markdown
Member Author

One thing to consider as well here is that we're going to get multiple playerresize events when changing into fullscreen, depending on what the throttle is, it can be anywhere from 1 to 3 events. Currently, the ResizeObserver isn't throttled but the iframe resize event is throttled to 50ms. It's probable that we want to debounce rather than throttle.

@gkatsev gkatsev changed the title [WIP] feat: playerresize event in all cases feat: playerresize event in all cases Jan 10, 2018
Comment thread src/js/resize-manager.js
let RESIZE_OBSERVER_AVAILABLE = options.ResizeObserver || window.ResizeObserver;

if (options.ResizeObserver === null) {
RESIZE_OBSERVER_AVAILABLE = false;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

wouldn't this set RESIZE_OBSERVER_AVAILABLE to false even when one is available through window.ResizeObserver?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yup, the idea is if you wanted to disable the ResizeObserver for whatever reason you could pass in null.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Gotcha. I'd suggest a comment over this block so it's clear for future readers 😄

Comment thread src/js/player.js
* @event Player#playerresize
* @type {EventTarget~Event}
*/
this.trigger('playerresize');

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I believe the event trigger would have worked in IE8 as well - since we're reverting this for the ResizeObserver and that's only available after IE8, is this a breaking change?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

We are falling back to using an iframe for playerresize events when ResizeObserver isn't available, currently, that's basically everywhere, though, chrome will be shipping ResizeObserver soon.

Comment thread test/unit/resize-manager.test.js Outdated
this.player.on('playerresize', function() {
playerresizeCalled++;
});
rm.resizeObserver_.observer();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

observe?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

actually looks like you don't need this line at all

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I set this in the constructor of MyResizeObserver, calling it calls the observe handler which will trigger playerresize which we can then test. Maybe I should just make it a local variable to be more obvious what is going on.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Ah, I see. I agree that making it a local variable will make it more clear

assert.equal(playerresizeCalled, 1, 'playerresize was triggered');

rm.dispose();
});

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

one good test to add would be a test that makes sure a resizeObserver is created when one is available on the window

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It would be nice but mocking out window is a pain :)

ldayananda
ldayananda previously approved these changes Jan 12, 2018
Comment thread src/js/resize-manager.js
let RESIZE_OBSERVER_AVAILABLE = options.ResizeObserver || window.ResizeObserver;

if (options.ResizeObserver === null) {
RESIZE_OBSERVER_AVAILABLE = false;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Gotcha. I'd suggest a comment over this block so it's clear for future readers 😄

Comment thread test/unit/resize-manager.test.js Outdated
this.player.on('playerresize', function() {
playerresizeCalled++;
});
rm.resizeObserver_.observer();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Ah, I see. I agree that making it a local variable will make it more clear

misteroneill
misteroneill previously approved these changes Jan 16, 2018

@misteroneill misteroneill left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

One minor question that I don't think affects my approval of this PR. 👍

Comment thread src/js/resize-manager.js
return super.createEl('iframe', {
className: 'vjs-resize-manager'
});
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I believe the element will be created even when it's not needed. Should we suppress the creation of it when ResizeObserver is available?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This line in the constructor will make it so no element is created if ResizeObserver is used: https://github.com/videojs/video.js/pull/4864/files#diff-8947c14c969c2d82a892c5fac0d0efd0R19

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Oh, I didn't notice that. Carry on. 👍

@gkatsev gkatsev dismissed stale reviews from misteroneill and ldayananda January 17, 2018 20:13

Updated based on comments and added docs.

@ldayananda ldayananda left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Made a few small typo corrections, but at this point LGTM

@gkatsev gkatsev added confirmed minor This PR can be added to a minor release. It should not be added to a patch release. labels Jan 30, 2018
@pagarwal123

Copy link
Copy Markdown

QA-LGTM

@gkatsev gkatsev merged commit 9ceb4e4 into master Jan 30, 2018
@gkatsev gkatsev deleted the resizer branch January 30, 2018 18:26
@gkatsev gkatsev mentioned this pull request Jul 23, 2018
7 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

confirmed minor This PR can be added to a minor release. It should not be added to a patch release.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants