Skip to content

Commit

Permalink
refactor: remove internal Map, Set, and WeakMap shams, assume window.…
Browse files Browse the repository at this point in the history
…performance and requestAnimationFrame support (videojs#7775)
  • Loading branch information
misteroneill authored and edirub committed Jun 8, 2023
1 parent 1151d24 commit 9ff7938
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 342 deletions.
23 changes: 0 additions & 23 deletions src/js/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import * as Fn from './utils/fn.js';
import * as Guid from './utils/guid.js';
import {toTitleCase, toLowerCase} from './utils/str.js';
import {merge} from './utils/obj.js';
import Map from './utils/map.js';
import Set from './utils/set.js';
import keycode from 'keycode';

/**
Expand Down Expand Up @@ -1508,11 +1506,6 @@ class Component {
* @see [Similar to]{@link https://developer.mozilla.org/en-US/docs/Web/API/window/requestAnimationFrame}
*/
requestAnimationFrame(fn) {
// Fall back to using a timer.
if (!this.supportsRaf_) {
return this.setTimeout(fn, 1000 / 60);
}

this.clearTimersOnDispose_();

// declare as variables so they are properly available in rAF function
Expand Down Expand Up @@ -1595,11 +1588,6 @@ class Component {
* @see [Similar to]{@link https://developer.mozilla.org/en-US/docs/Web/API/window/cancelAnimationFrame}
*/
cancelAnimationFrame(id) {
// Fall back to using a timer.
if (!this.supportsRaf_) {
return this.clearTimeout(id);
}

if (this.rafIds_.has(id)) {
this.rafIds_.delete(id);
window.cancelAnimationFrame(id);
Expand Down Expand Up @@ -1732,17 +1720,6 @@ class Component {
}
}

/**
* Whether or not this component supports `requestAnimationFrame`.
*
* This is exposed primarily for testing purposes.
*
* @private
* @type {Boolean}
*/
Component.prototype.supportsRaf_ = typeof window.requestAnimationFrame === 'function' &&
typeof window.cancelAnimationFrame === 'function';

Component.registerComponent('Component', Component);

export default Component;
87 changes: 26 additions & 61 deletions src/js/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -4313,46 +4313,28 @@ class Player extends Component {

this.audioOnlyMode_ = value;

const PromiseClass = this.options_.Promise || window.Promise;

if (PromiseClass) {
// Enable Audio Only Mode
if (value) {
const exitPromises = [];

// Fullscreen and PiP are not supported in audioOnlyMode, so exit if we need to.
if (this.isInPictureInPicture()) {
exitPromises.push(this.exitPictureInPicture());
}

if (this.isFullscreen()) {
exitPromises.push(this.exitFullscreen());
}

if (this.audioPosterMode()) {
exitPromises.push(this.audioPosterMode(false));
}

return PromiseClass.all(exitPromises).then(() => this.enableAudioOnlyUI_());
}

// Disable Audio Only Mode
return PromiseClass.resolve().then(() => this.disableAudioOnlyUI_());
}

// Enable Audio Only Mode
if (value) {
const exitPromises = [];

// Fullscreen and PiP are not supported in audioOnlyMode, so exit if we need to.
if (this.isInPictureInPicture()) {
this.exitPictureInPicture();
exitPromises.push(this.exitPictureInPicture());
}

if (this.isFullscreen()) {
this.exitFullscreen();
exitPromises.push(this.exitFullscreen());
}

this.enableAudioOnlyUI_();
} else {
this.disableAudioOnlyUI_();
if (this.audioPosterMode()) {
exitPromises.push(this.audioPosterMode(false));
}

return Promise.all(exitPromises).then(() => this.enableAudioOnlyUI_());
}

// Disable Audio Only Mode
return Promise.resolve().then(() => this.disableAudioOnlyUI_());
}

enablePosterModeUI_() {
Expand Down Expand Up @@ -4391,44 +4373,27 @@ class Player extends Component {

this.audioPosterMode_ = value;

const PromiseClass = this.options_.Promise || window.Promise;

if (PromiseClass) {

if (value) {

if (this.audioOnlyMode()) {
const audioOnlyModePromise = this.audioOnlyMode(false);
if (value) {

return audioOnlyModePromise.then(() => {
// enable audio poster mode after audio only mode is disabled
this.enablePosterModeUI_();
});
}
if (this.audioOnlyMode()) {
const audioOnlyModePromise = this.audioOnlyMode(false);

return PromiseClass.resolve().then(() => {
// enable audio poster mode
return audioOnlyModePromise.then(() => {
// enable audio poster mode after audio only mode is disabled
this.enablePosterModeUI_();
});
}

return PromiseClass.resolve().then(() => {
// disable audio poster mode
this.disablePosterModeUI_();
return Promise.resolve().then(() => {
// enable audio poster mode
this.enablePosterModeUI_();
});
}

if (value) {

if (this.audioOnlyMode()) {
this.audioOnlyMode(false);
}

this.enablePosterModeUI_();
return;
}

this.disablePosterModeUI_();
return Promise.resolve().then(() => {
// disable audio poster mode
this.disablePosterModeUI_();
});
}

/**
Expand Down
7 changes: 1 addition & 6 deletions src/js/tech/html5.js
Original file line number Diff line number Diff line change
Expand Up @@ -970,13 +970,8 @@ class Html5 extends Tech {
videoPlaybackQuality.totalVideoFrames = this.el().webkitDecodedFrameCount;
}

if (window.performance && typeof window.performance.now === 'function') {
if (window.performance) {
videoPlaybackQuality.creationTime = window.performance.now();
} else if (window.performance &&
window.performance.timing &&
typeof window.performance.timing.navigationStart === 'number') {
videoPlaybackQuality.creationTime =
window.Date.now() - window.performance.timing.navigationStart;
}

return videoPlaybackQuality;
Expand Down
58 changes: 1 addition & 57 deletions src/js/utils/dom-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,62 +3,6 @@
* @module dom-data
*/

import log from './log.js';
import * as Guid from './guid.js';
import window from 'global/window';

let FakeWeakMap;

if (!window.WeakMap) {
FakeWeakMap = class {
constructor() {
this.vdata = 'vdata' + Math.floor(window.performance && window.performance.now() || Date.now());
this.data = {};
}

set(key, value) {
const access = key[this.vdata] || Guid.newGUID();

if (!key[this.vdata]) {
key[this.vdata] = access;
}

this.data[access] = value;

return this;
}

get(key) {
const access = key[this.vdata];

// we have data, return it
if (access) {
return this.data[access];
}

// we don't have data, return nothing.
// return undefined explicitly as that's the contract for this method
log('We have no data for this element', key);
return undefined;
}

has(key) {
const access = key[this.vdata];

return access in this.data;
}

delete(key) {
const access = key[this.vdata];

if (access) {
delete this.data[access];
delete key[this.vdata];
}
}
};
}

/**
* Element Data Store.
*
Expand All @@ -69,4 +13,4 @@ if (!window.WeakMap) {
* @type {Object}
* @private
*/
export default window.WeakMap ? new WeakMap() : new FakeWeakMap();
export default new WeakMap();
28 changes: 0 additions & 28 deletions src/js/utils/map.js

This file was deleted.

28 changes: 0 additions & 28 deletions src/js/utils/set.js

This file was deleted.

18 changes: 11 additions & 7 deletions test/api/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,15 +249,17 @@ QUnit.test('component can be subclassed externally', function(assert) {
const Component = videojs.getComponent('Component');
const ControlBar = videojs.getComponent('ControlBar');

const player = new (videojs.extend(Component, {
reportUserActivity() {},
class TestComponent extends Component {
reportUserActivity() {}
textTracks() {
return {
addEventListener: Function.prototype,
removeEventListener: Function.prototype
};
}
}))({
}

const player = new TestComponent({
id() {},
reportUserActivity() {}
});
Expand All @@ -275,14 +277,15 @@ function testHelperMakeTag() {

QUnit.test('should extend Component', function(assert) {
const Component = videojs.getComponent('Component');
const MyComponent = videojs.extend(Component, {

class MyComponent extends Component {
constructor() {
this.bar = true;
},
}
foo() {
return true;
}
});
}

const myComponent = new MyComponent();

Expand All @@ -291,7 +294,8 @@ QUnit.test('should extend Component', function(assert) {
assert.ok(myComponent.bar, 'the constructor function is used');
assert.ok(myComponent.foo(), 'instance methods are applied');

const NoMethods = videojs.extend(Component);
class NoMethods extends Component {}

const noMethods = new NoMethods({});

assert.ok(noMethods.on, 'should extend component with no methods or constructor');
Expand Down
Loading

0 comments on commit 9ff7938

Please sign in to comment.