Skip to content

Commit d85732c

Browse files
authored
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 c865136 commit d85732c

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
@@ -55,6 +55,7 @@ import {
5555
takeUntil,
5656
withLatestFrom,
5757
switchMap,
58+
tap,
5859
} from 'rxjs/operators';
5960

6061
declare global {
@@ -109,7 +110,7 @@ export class YouTubePlayer implements AfterViewInit, OnDestroy, OnInit {
109110
private _player: Player | undefined;
110111
private _existingApiReadyCallback: (() => void) | undefined;
111112
private _pendingPlayerState: PendingPlayerState | undefined;
112-
private _playerChanges = new BehaviorSubject<Player | undefined>(undefined);
113+
private _playerChanges = new BehaviorSubject<UninitializedPlayer | undefined>(undefined);
113114

114115
/** YouTube Video ID to view */
115116
@Input()
@@ -225,7 +226,11 @@ export class YouTubePlayer implements AfterViewInit, OnDestroy, OnInit {
225226
this._width,
226227
this._height,
227228
this._ngZone
228-
).pipe(waitUntilReady(player => {
229+
).pipe(tap(player => {
230+
// Emit this before the `waitUntilReady` call so that we can bind to
231+
// events that happen as the player is being initialized (e.g. `onReady`).
232+
this._playerChanges.next(player);
233+
}), waitUntilReady(player => {
229234
// Destroy the player if loading was aborted so that we don't end up leaking memory.
230235
if (!playerIsReady(player)) {
231236
player.destroy();
@@ -235,7 +240,6 @@ export class YouTubePlayer implements AfterViewInit, OnDestroy, OnInit {
235240
// Set up side effects to bind inputs to the player.
236241
playerObs.subscribe(player => {
237242
this._player = player;
238-
this._playerChanges.next(player);
239243

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

0 commit comments

Comments
 (0)