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: always return a promise from play, if supported #5227

Merged
merged 3 commits into from
Aug 10, 2018

Conversation

brandonocasey
Copy link
Contributor

@brandonocasey brandonocasey commented Jun 4, 2018

Description

Always return a Promise from play when we have a Promise object.

Fixes #3927

@@ -1148,7 +1149,18 @@ QUnit.test('play promise should resolve to native value if returned', function(a
player.tech_.play = () => 'foo';
const p = player.play();

assert.equal(p, 'foo', 'play returns foo');
const finish = (v) => {
Copy link
Contributor Author

@brandonocasey brandonocasey Jun 4, 2018

Choose a reason for hiding this comment

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

now that play will always return a promise, if it is supported, we have to use then to check the resolved value.

});
}

return this.play_();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

ie11 does not support promises, so that should be the only platform this happens on.

@brandonocasey brandonocasey force-pushed the feat/always-return-promise branch from ea148de to 9789bf4 Compare June 11, 2018 15:39
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.

This is probably a good first step, we should make sure that this is set up to adding rejecting the promise properly as well. We might want to do that in a separate PR though.

src/js/player.js Outdated

if (promise) {
return new Promise((resolve) => {
Copy link
Member

Choose a reason for hiding this comment

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

I wonder if it's possible to have play_ return a promise itself or use a promise we give it. Maybe a bit tricky since our play_ method has some event listeners.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

whats really tricky is that IE 11 does not have promise support, so we can't technically return a promise all the time.

Copy link
Member

Choose a reason for hiding this comment

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

yeah, IE11 aside, I mean.
We can require a polyfill for the best behavior here, as long as we don't fail if one doesn't provided.

@brandonocasey
Copy link
Contributor Author

Two questions for you @gkatsev.

  1. By " sure that this is set up to adding rejecting the promise properly as well" Do you mean that we should make sure to silence the play promise? Or do you mean that we should just make sure that the promise we return will be rejected on a rejected play promise? If its number two I think we already will do that because we resolve our promise on the play promise. Then that play promise will actually determine if we are rejected/resolved.
  2. Why do we want to return a promise from play_? I added play_ because returning a promise from play was really messy when their wasn't always a PromiseClass to use. We could return a fake promise in those cases which would solve it, but I thought it would be better to not do that, so I opted for play_ to take a callback which will be the resolve function when working with promises, or a noop when there are no promises.

@gkatsev
Copy link
Member

gkatsev commented Aug 8, 2018

I guess I never responded here.

  1. The idea is that we want to shim the play promise behavior for when the play() doesn't return a promise natively. Like on IE11. So, any change we do now, hopefully shouldn't be too complex that would require us to rewrite a lot of it as part of that work.
    I think that what we have now works well if there is a native play promise and a step towards our goals. Though, maybe we should even check if just window.Promise exists right now because otherwise IE11 will return a promise that never resolves. Or maybe even do an IE11-specific check as well.
  2. Looking at the code more, my suggestion is probably not helpful/doesn't make much sense based on the current implementation. I'm mostly trying to reduce usage of Promise constructor and passing around the inner resolve/reject methods.

@brandonocasey
Copy link
Contributor Author

So if we want to try and shim the play promise behavior on IE 11. Does that mean we want to:

  1. Create a shim Promise that is returned from Player#play() when we do not have a real promise to use.
  2. Check if the tech returns a Promise from it's play method and if it does not return, resolve the Player#play promise on the play event.

@gkatsev
Copy link
Member

gkatsev commented Aug 8, 2018

Yeah, more or less. It gets a bit trickier because you'd want to reject the promise if you get a pause before play and stuff like that but essentially if a native play promise isn't provided we want to make our own promise.

@gkatsev
Copy link
Member

gkatsev commented Aug 8, 2018

My main worry was that on IE11 if a Promise Polyfill was on the page, we'd end up returning a forever-pending promise, but I think that we'd get a promise that gets resolved once play_ does its work, it would just not be rejected and that's probably good enough for this PR.

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.

I want to take this for a quick spin but otherwise, it's good to go. I'll work on making a full play promise shim afterwards.

@brandonocasey
Copy link
Contributor Author

brandonocasey commented Aug 8, 2018

awesome, also make sure in the next pr to add something here: https://github.com/videojs/video.js/pull/5227/files#diff-3b0266ff1c037b289ec624ab25b0272eR1928

that will check to see if there is a play promise and if not it will fallback to using play/pause events

@gkatsev gkatsev merged commit 58405fd into master Aug 10, 2018
@gkatsev gkatsev deleted the feat/always-return-promise branch August 10, 2018 20:27
gkatsev pushed a commit that referenced this pull request Aug 13, 2018
If Promise is available or if Promise is polyfilled, then we'll return a new Promise from `play()` that will get fulfilled with the value returned from the `play()` method. This means that on IE11, this promise will get resolved when we call `play()` even if play doesn't succeed. We will have a follow-on PR to polyfill this behavior and resolve or reject the promise for browsers like IE11 that don't return a promise from `play()`.
gkatsev pushed a commit that referenced this pull request Aug 14, 2018
If Promise is available or if Promise is polyfilled, then we'll return a new Promise from `play()` that will get fulfilled with the value returned from the `play()` method. This means that on IE11, this promise will get resolved when we call `play()` even if play doesn't succeed. We will have a follow-on PR to polyfill this behavior and resolve or reject the promise for browsers like IE11 that don't return a promise from `play()`.
gkatsev pushed a commit that referenced this pull request Aug 14, 2018
If Promise is available or if Promise is polyfilled, then we'll return a new Promise from `play()` that will get fulfilled with the value returned from the `play()` method. This means that on IE11, this promise will get resolved when we call `play()` even if play doesn't succeed. We will have a follow-on PR to polyfill this behavior and resolve or reject the promise for browsers like IE11 that don't return a promise from `play()`.
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.

2 participants