Skip to content

fix(youtube-player): binding some event listeners too late #19429

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

Merged
Merged
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
14 changes: 10 additions & 4 deletions src/youtube-player/youtube-player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ import {
takeUntil,
withLatestFrom,
switchMap,
tap,
} from 'rxjs/operators';

declare global {
Expand Down Expand Up @@ -109,7 +110,7 @@ export class YouTubePlayer implements AfterViewInit, OnDestroy, OnInit {
private _player: Player | undefined;
private _existingApiReadyCallback: (() => void) | undefined;
private _pendingPlayerState: PendingPlayerState | undefined;
private _playerChanges = new BehaviorSubject<Player | undefined>(undefined);
private _playerChanges = new BehaviorSubject<UninitializedPlayer | undefined>(undefined);

/** YouTube Video ID to view */
@Input()
Expand Down Expand Up @@ -225,7 +226,11 @@ export class YouTubePlayer implements AfterViewInit, OnDestroy, OnInit {
this._width,
this._height,
this._ngZone
).pipe(waitUntilReady(player => {
).pipe(tap(player => {
// Emit this before the `waitUntilReady` call so that we can bind to
// events that happen as the player is being initialized (e.g. `onReady`).
this._playerChanges.next(player);
}), waitUntilReady(player => {
// Destroy the player if loading was aborted so that we don't end up leaking memory.
if (!playerIsReady(player)) {
player.destroy();
Expand All @@ -235,7 +240,6 @@ export class YouTubePlayer implements AfterViewInit, OnDestroy, OnInit {
// Set up side effects to bind inputs to the player.
playerObs.subscribe(player => {
this._player = player;
this._playerChanges.next(player);

if (player && this._pendingPlayerState) {
this._initializePlayer(player, this._pendingPlayerState);
Expand Down Expand Up @@ -518,7 +522,9 @@ export class YouTubePlayer implements AfterViewInit, OnDestroy, OnInit {
// expose whether the player has been destroyed so we have to wrap it in a try/catch to
// prevent the entire stream from erroring out.
try {
player.removeEventListener(name, listener);
if ((player as Player).removeEventListener!) {
(player as Player).removeEventListener(name, listener);
}
} catch {}
}) : observableOf<T>();
}),
Expand Down