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

fix: use initial player ref to avoid redundant callbacks #14

Merged
merged 3 commits into from
Jun 21, 2024
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed

- HMR breaks rendering ([#8](https://github.com/bitmovin/bitmovin-player-react/pull/8))
- Use the initial player ref to avoid redundant callbacks ([#14](https://github.com/bitmovin/bitmovin-player-react/pull/14))
2 changes: 1 addition & 1 deletion src/BitmovinPlayer.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ describe('BitmovinPlayer', () => {
rerender(<BitmovinPlayer config={playerConfig} playerRef={playerRefCallback} />);

expect(playerRefCallback).toHaveBeenCalledWith(expect.any(FakePlayer));
expect(playerRefCallback).toHaveBeenCalledTimes(2);
expect(playerRefCallback).toHaveBeenCalledTimes(1);
expect(FakePlayer.prototype.destroy).not.toHaveBeenCalled();
});
});
Expand Down
36 changes: 26 additions & 10 deletions src/BitmovinPlayer.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
import { Player, PlayerAPI, PlayerConfig, SourceConfig } from 'bitmovin-player';
import { UIContainer, UIFactory, UIManager, UIVariant } from 'bitmovin-player-ui';
import { ForwardedRef, forwardRef, MutableRefObject, RefCallback, useEffect, useRef, useState } from 'react';
import {
ForwardedRef,
forwardRef,
MutableRefObject,
RefCallback,
useCallback,
useEffect,
useRef,
useState,
} from 'react';

export type UiContainerFactory = () => UIContainer;
export type UiVariantsFactory = () => UIVariant[];
Expand Down Expand Up @@ -48,6 +57,20 @@ export const BitmovinPlayer = forwardRef(function BitmovinPlayer(
const [player, setPlayer] = useState<PlayerAPI | undefined>();
const latestPlayerRef = useRef<PlayerAPI | undefined>();

const proxyPlayerRef = useCallback(
(player: PlayerAPI) => {
if (!playerRefProp) {
return;
}

setRef(playerRefProp, player);
},
// Only the first `playerRef` is relevant and should be used to avoid
// unnecessary ref callback invocations (in case the `playerRef` is a ref function).
// eslint-disable-next-line react-hooks/exhaustive-deps
[],
);

// Initialize the player on mount.
useEffect(() => {
const rootContainerElement = rootContainerElementRef.current;
Expand All @@ -70,12 +93,13 @@ export const BitmovinPlayer = forwardRef(function BitmovinPlayer(

latestPlayerRef.current = initializedPlayer;

proxyPlayerRef(initializedPlayer);
setPlayer(initializedPlayer);

return () => {
destroyPlayer(initializedPlayer, rootContainerElement, createdPlayerContainerElement);
};
}, [config, customUi]);
}, [config, customUi, proxyPlayerRef]);

// Load or reload the source.
useEffect(() => {
Expand Down Expand Up @@ -107,14 +131,6 @@ export const BitmovinPlayer = forwardRef(function BitmovinPlayer(
}
}, [source, player]);

useEffect(() => {
if (!player || !playerRefProp) {
return;
}

setRef(playerRefProp, player);
}, [player, playerRefProp]);

return <div className={className} ref={rootContainerElementRefHandler} />;
});

Expand Down