Skip to content

Commit

Permalink
fix(youtube-player): YT.Player is not a constructor (#20616)
Browse files Browse the repository at this point in the history
* fix(youtube-player): YT.Player is not a constructor

fixes a race condition where window.YT would be truthy
but window.YT.Player would not be ready.

Fixes #20598

* fix(youtube-player): YT.Player is not a constructor test

added a test to prevent reggression, the test mimics a possible state
of
the YT object when loading, when window.YT exists but window.YT.Player
does not.

Fixes #20598
  • Loading branch information
Ronll committed Sep 29, 2020
1 parent 5c42f3c commit b0d0388
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/youtube-player/youtube-player.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {createFakeYtNamespace} from './fake-youtube-player';
import {Subscription} from 'rxjs';

const VIDEO_ID = 'a12345';
const YT_LOADING_STATE_MOCK = {loading: 1, loaded: 0};

describe('YoutubePlayer', () => {
let playerCtorSpy: jasmine.Spy;
Expand Down Expand Up @@ -381,6 +382,8 @@ describe('YoutubePlayer', () => {
});

it('waits until the api is ready before initializing', () => {
(window.YT as any) = YT_LOADING_STATE_MOCK;

fixture = TestBed.createComponent(TestApp);
testComponent = fixture.debugElement.componentInstance;
fixture.detectChanges();
Expand Down
2 changes: 1 addition & 1 deletion src/youtube-player/youtube-player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ export class YouTubePlayer implements AfterViewInit, OnDestroy, OnInit {
}

let iframeApiAvailableObs: Observable<boolean> = observableOf(true);
if (!window.YT) {
if (!window.YT || !window.YT.Player) {
if (this.showBeforeIframeApiLoads && (typeof ngDevMode === 'undefined' || ngDevMode)) {
throw new Error('Namespace YT not found, cannot construct embedded youtube player. ' +
'Please install the YouTube Player API Reference for iframe Embeds: ' +
Expand Down

0 comments on commit b0d0388

Please sign in to comment.