From dd1b478e4db84c8611b3705c15615e881a441c01 Mon Sep 17 00:00:00 2001 From: Alex Barstow Date: Mon, 16 May 2022 17:48:01 -0400 Subject: [PATCH] feat: assume native promises, remove promise option and workarounds (#7715) BREAKING CHANGE: Removes support for Promise class option and assumes native Promise is available. Will break in older browsers or devices. --- src/js/player.js | 123 ++++++++++++++------------------ src/js/tech/tech.js | 6 +- test/unit/utils/promise.test.js | 6 -- 3 files changed, 53 insertions(+), 82 deletions(-) diff --git a/src/js/player.js b/src/js/player.js index fbeaf96762..d593930325 100644 --- a/src/js/player.js +++ b/src/js/player.js @@ -1177,8 +1177,7 @@ class Player extends Component { 'playerElIngest': this.playerElIngest_ || false, 'vtt.js': this.options_['vtt.js'], 'canOverridePoster': !!this.options_.techCanOverridePoster, - 'enableSourceset': this.options_.enableSourceset, - 'Promise': this.options_.Promise + 'enableSourceset': this.options_.enableSourceset }; TRACK_TYPES.names.forEach((name) => { @@ -2324,15 +2323,9 @@ class Player extends Component { * the promise from play is fulfilled. */ play() { - const PromiseClass = this.options_.Promise || window.Promise; - - if (PromiseClass) { - return new PromiseClass((resolve) => { - this.play_(resolve); - }); - } - - return this.play_(); + return new Promise((resolve) => { + this.play_(resolve); + }); } /** @@ -2820,38 +2813,32 @@ class Player extends Component { * @fires Player#fullscreenchange */ requestFullscreen(fullscreenOptions) { - const PromiseClass = this.options_.Promise || window.Promise; + const self = this; - if (PromiseClass) { - const self = this; - - return new PromiseClass((resolve, reject) => { - function offHandler() { - self.off('fullscreenerror', errorHandler); - self.off('fullscreenchange', changeHandler); - } - function changeHandler() { - offHandler(); - resolve(); - } - function errorHandler(e, err) { - offHandler(); - reject(err); - } - - self.one('fullscreenchange', changeHandler); - self.one('fullscreenerror', errorHandler); + return new Promise((resolve, reject) => { + function offHandler() { + self.off('fullscreenerror', errorHandler); + self.off('fullscreenchange', changeHandler); + } + function changeHandler() { + offHandler(); + resolve(); + } + function errorHandler(e, err) { + offHandler(); + reject(err); + } - const promise = self.requestFullscreenHelper_(fullscreenOptions); + self.one('fullscreenchange', changeHandler); + self.one('fullscreenerror', errorHandler); - if (promise) { - promise.then(offHandler, offHandler); - promise.then(resolve, reject); - } - }); - } + const promise = self.requestFullscreenHelper_(fullscreenOptions); - return this.requestFullscreenHelper_(); + if (promise) { + promise.then(offHandler, offHandler); + promise.then(resolve, reject); + } + }); } requestFullscreenHelper_(fullscreenOptions) { @@ -2876,6 +2863,7 @@ class Player extends Component { if (this.fsApi_.requestFullscreen) { const promise = this.el_[this.fsApi_.requestFullscreen](fsOptions); + // Even on browsers with promise support this may not return a promise if (promise) { promise.then(() => this.isFullscreen(true), () => this.isFullscreen(false)); } @@ -2898,45 +2886,40 @@ class Player extends Component { * @fires Player#fullscreenchange */ exitFullscreen() { - const PromiseClass = this.options_.Promise || window.Promise; - - if (PromiseClass) { - const self = this; + const self = this; - return new PromiseClass((resolve, reject) => { - function offHandler() { - self.off('fullscreenerror', errorHandler); - self.off('fullscreenchange', changeHandler); - } - function changeHandler() { - offHandler(); - resolve(); - } - function errorHandler(e, err) { - offHandler(); - reject(err); - } - - self.one('fullscreenchange', changeHandler); - self.one('fullscreenerror', errorHandler); + return new Promise((resolve, reject) => { + function offHandler() { + self.off('fullscreenerror', errorHandler); + self.off('fullscreenchange', changeHandler); + } + function changeHandler() { + offHandler(); + resolve(); + } + function errorHandler(e, err) { + offHandler(); + reject(err); + } - const promise = self.exitFullscreenHelper_(); + self.one('fullscreenchange', changeHandler); + self.one('fullscreenerror', errorHandler); - if (promise) { - promise.then(offHandler, offHandler); - // map the promise to our resolve/reject methods - promise.then(resolve, reject); - } - }); - } + const promise = self.exitFullscreenHelper_(); - return this.exitFullscreenHelper_(); + if (promise) { + promise.then(offHandler, offHandler); + // map the promise to our resolve/reject methods + promise.then(resolve, reject); + } + }); } exitFullscreenHelper_() { if (this.fsApi_.requestFullscreen) { const promise = document[this.fsApi_.exitFullscreen](); + // Even on browsers with promise support this may not return a promise if (promise) { // we're splitting the promise here, so, we want to catch the // potential error so that this chain doesn't have unhandled errors @@ -3524,9 +3507,7 @@ class Player extends Component { * and calls `reset` on the `tech`. */ reset() { - const PromiseClass = this.options_.Promise || window.Promise; - - if (this.paused() || !PromiseClass) { + if (this.paused()) { this.doReset_(); } else { const playPromise = this.play(); diff --git a/src/js/tech/tech.js b/src/js/tech/tech.js index 8c237a041b..92e55307dc 100644 --- a/src/js/tech/tech.js +++ b/src/js/tech/tech.js @@ -831,11 +831,7 @@ class Tech extends Component { * @abstract */ requestPictureInPicture() { - const PromiseClass = this.options_.Promise || window.Promise; - - if (PromiseClass) { - return PromiseClass.reject(); - } + return Promise.reject(); } /** diff --git a/test/unit/utils/promise.test.js b/test/unit/utils/promise.test.js index 2171024fc1..bafd76fadb 100644 --- a/test/unit/utils/promise.test.js +++ b/test/unit/utils/promise.test.js @@ -5,12 +5,6 @@ import * as promise from '../../../src/js/utils/promise'; QUnit.module('utils/promise'); QUnit.test('can correctly identify a native Promise (if supported)', function(assert) { - - // If Promises aren't supported, skip this. - if (!window.Promise) { - return assert.expect(0); - } - assert.ok(promise.isPromise(new window.Promise((resolve) => resolve())), 'a native Promise was recognized'); });