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

Treat empty but idle video state as not loading #141

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions src/DefaultPlayer/DefaultPlayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,17 +122,26 @@ DefaultPlayer.propTypes = {
video: PropTypes.object.isRequired
};

function isLoading(readyState, networkState, userAgent) {
if (readyState == 0 && networkState == 1) {

Choose a reason for hiding this comment

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

Please use strict equality operator ===

Copy link
Author

Choose a reason for hiding this comment

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

Thanks for the review!

Yes, this could use === though the eslint config of this project doesn't include the eqeqeq rule stating it must, and in practice callers are using the browser enums not null or other complicating values.

// If nothing fetched but idle likely preload=none so not loading.
return false;
}
// TODO: This is not pretty. Doing device detection to remove spinner on iOS
// devices for a quick and dirty win. We should see if we can use the same
// readyState check safely across all browsers.
const readyEnough = /iPad|iPhone|iPod/.test(userAgent) ? 1 : 4;
return readyState < readyEnough;
}

const connectedPlayer = videoConnect(
DefaultPlayer,
({ networkState, readyState, error, ...restState }) => ({
video: {
readyState,
networkState,
loading: isLoading(readyState, networkState, navigator.userAgent),
error: error || networkState === 3,
// TODO: This is not pretty. Doing device detection to remove
// spinner on iOS devices for a quick and dirty win. We should see if
// we can use the same readyState check safely across all browsers.
loading: readyState < (/iPad|iPhone|iPod/.test(navigator.userAgent) ? 1 : 4),
percentagePlayed: getPercentagePlayed(restState),
percentageBuffered: getPercentageBuffered(restState),
...restState
Expand All @@ -151,6 +160,7 @@ const connectedPlayer = videoConnect(

export {
connectedPlayer as default,
isLoading,
DefaultPlayer,
Time,
Seek,
Expand Down
31 changes: 30 additions & 1 deletion src/DefaultPlayer/DefaultPlayer.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';
import { shallow } from 'enzyme';
import { DefaultPlayer } from './DefaultPlayer';
import { DefaultPlayer, isLoading } from './DefaultPlayer';
import styles from './DefaultPlayer.css';
import Time from './Time/Time';
import Seek from './Seek/Seek';
Expand Down Expand Up @@ -106,3 +106,32 @@ describe('DefaultPlayer', () => {
.toBeFalsy();
});
});

describe('isLoading', () => {
// https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/readyState
const HAVE_NOTHING = 0;
const HAVE_CURRENT_DATA = 2;
const HAVE_ENOUGH_DATA = 4;
// https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/networkState
const NETWORK_EMPTY = 0;
const NETWORK_IDLE = 1;
const NETWORK_LOADING = 2;

it('is true when empty', () => {
expect(isLoading(HAVE_NOTHING, NETWORK_EMPTY, 'Mozilla')).toBe(true);
});
it('is false when nothing and idle', () => {
expect(isLoading(HAVE_NOTHING, NETWORK_IDLE, 'Mozilla')).toBe(false);
});
it('depends on UA when current', () => {
expect(isLoading(HAVE_CURRENT_DATA, NETWORK_IDLE, 'Mozilla')).toBe(true);
expect(isLoading(HAVE_CURRENT_DATA, NETWORK_LOADING, 'Mozilla')).toBe(true);
expect(isLoading(HAVE_CURRENT_DATA, NETWORK_IDLE, 'iPad')).toBe(false);
expect(isLoading(HAVE_CURRENT_DATA, NETWORK_LOADING, 'iPad')).toBe(false);
});
it('is false when enough', () => {
expect(isLoading(HAVE_ENOUGH_DATA, NETWORK_IDLE, 'Mozilla')).toBe(false);
expect(isLoading(HAVE_ENOUGH_DATA, NETWORK_LOADING, 'Mozilla')).toBe(false);
expect(isLoading(HAVE_ENOUGH_DATA, NETWORK_IDLE, 'iPad')).toBe(false);
});
});