Skip to content

Commit 890b5e1

Browse files
crisbetoandrewseguin
authored andcommitted
fix(youtube-player): binding some event listeners too late (#19429)
The logic that lazily binds event listeners to the YouTube player was waiting for the player to be done initializing which is problematic if we want to bind the `onReady` listener since the event has already happened by the time we've bound it. These changes move the event binding logic earlier in the process. Fixes #19408.
1 parent 75faddf commit 890b5e1

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

src/youtube-player/youtube-player.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ import {
5656
takeUntil,
5757
withLatestFrom,
5858
switchMap,
59+
tap,
5960
} from 'rxjs/operators';
6061

6162
declare global {
@@ -110,7 +111,7 @@ export class YouTubePlayer implements AfterViewInit, OnDestroy, OnInit {
110111
private _player: Player | undefined;
111112
private _existingApiReadyCallback: (() => void) | undefined;
112113
private _pendingPlayerState: PendingPlayerState | undefined;
113-
private _playerChanges = new BehaviorSubject<Player | undefined>(undefined);
114+
private _playerChanges = new BehaviorSubject<UninitializedPlayer | undefined>(undefined);
114115

115116
/** YouTube Video ID to view */
116117
@Input()
@@ -235,7 +236,11 @@ export class YouTubePlayer implements AfterViewInit, OnDestroy, OnInit {
235236
this._width,
236237
this._height,
237238
this._ngZone
238-
).pipe(waitUntilReady(player => {
239+
).pipe(tap(player => {
240+
// Emit this before the `waitUntilReady` call so that we can bind to
241+
// events that happen as the player is being initialized (e.g. `onReady`).
242+
this._playerChanges.next(player);
243+
}), waitUntilReady(player => {
239244
// Destroy the player if loading was aborted so that we don't end up leaking memory.
240245
if (!playerIsReady(player)) {
241246
player.destroy();
@@ -245,7 +250,6 @@ export class YouTubePlayer implements AfterViewInit, OnDestroy, OnInit {
245250
// Set up side effects to bind inputs to the player.
246251
playerObs.subscribe(player => {
247252
this._player = player;
248-
this._playerChanges.next(player);
249253

250254
if (player && this._pendingPlayerState) {
251255
this._initializePlayer(player, this._pendingPlayerState);
@@ -528,7 +532,9 @@ export class YouTubePlayer implements AfterViewInit, OnDestroy, OnInit {
528532
// expose whether the player has been destroyed so we have to wrap it in a try/catch to
529533
// prevent the entire stream from erroring out.
530534
try {
531-
player.removeEventListener(name, listener);
535+
if ((player as Player).removeEventListener!) {
536+
(player as Player).removeEventListener(name, listener);
537+
}
532538
} catch {}
533539
}) : observableOf<T>();
534540
}),

0 commit comments

Comments
 (0)