@mottosports/motto-video-player 1.0.1-rc.65 → 1.0.1-rc.66

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1226,7 +1226,7 @@ var supportsWidevinePersistentLicenses = () => {
1226
1226
  var import_mux_data_shakaplayer = __toESM(require("@mux/mux-data-shakaplayer"));
1227
1227
 
1228
1228
  // package.json
1229
- var version = "1.0.1-rc.65";
1229
+ var version = "1.0.1-rc.66";
1230
1230
 
1231
1231
  // src/utils/licenseCache.ts
1232
1232
  var PERSISTENT_LICENSE_PREFIX = "motto_lic_";
@@ -1394,6 +1394,10 @@ var useShakaPlayer = ({
1394
1394
  const playerRef = (0, import_react.useRef)(null);
1395
1395
  const [isRetrying, setIsRetrying] = (0, import_react.useState)(false);
1396
1396
  const videoElementRef = (0, import_react.useRef)(null);
1397
+ const destroyInProgressRef = (0, import_react.useRef)(null);
1398
+ const isDestroyingRef = (0, import_react.useRef)(false);
1399
+ const initSequenceRef = (0, import_react.useRef)(0);
1400
+ const activeInitIdRef = (0, import_react.useRef)(null);
1397
1401
  const waitingForKeyTimerRef = (0, import_react.useRef)(null);
1398
1402
  const waitingForKeyHandlerRef = (0, import_react.useRef)(null);
1399
1403
  const playbackResumedHandlerRef = (0, import_react.useRef)(null);
@@ -1417,11 +1421,22 @@ var useShakaPlayer = ({
1417
1421
  }, [src]);
1418
1422
  const initializePlayerInternal = (0, import_react.useCallback)(async (video) => {
1419
1423
  try {
1424
+ if (destroyInProgressRef.current) {
1425
+ try {
1426
+ await destroyInProgressRef.current;
1427
+ } catch {
1428
+ }
1429
+ }
1430
+ const myInitId = ++initSequenceRef.current;
1431
+ activeInitIdRef.current = myInitId;
1420
1432
  videoElementRef.current = video;
1421
1433
  import_shaka_player.default.polyfill.installAll();
1422
1434
  if (!import_shaka_player.default.Player.isBrowserSupported()) {
1423
1435
  throw new Error("Browser not supported by Shaka Player");
1424
1436
  }
1437
+ if (isDestroyingRef.current) {
1438
+ return;
1439
+ }
1425
1440
  const player = new import_shaka_player.default.Player();
1426
1441
  playerRef.current = player;
1427
1442
  await player.attach(video);
@@ -1432,6 +1447,14 @@ var useShakaPlayer = ({
1432
1447
  let playlistId = src.id;
1433
1448
  const isDRM = Boolean(src.drm);
1434
1449
  storedPersistentThisLoadRef.current = false;
1450
+ if (activeInitIdRef.current !== myInitId || isDestroyingRef.current) {
1451
+ try {
1452
+ await player.destroy();
1453
+ } catch {
1454
+ }
1455
+ if (playerRef.current === player) playerRef.current = null;
1456
+ return;
1457
+ }
1435
1458
  let storedSessionsMetadata = [];
1436
1459
  if (isDRM && playlistId) {
1437
1460
  storedSessionsMetadata = retrievePersistentLicense(playlistId, src.drm.licenseCacheKey ?? "");
@@ -1641,6 +1664,14 @@ var useShakaPlayer = ({
1641
1664
  console.error("Failed to initialize Mux Analytics:", error);
1642
1665
  }
1643
1666
  }
1667
+ if (activeInitIdRef.current !== myInitId || isDestroyingRef.current) {
1668
+ try {
1669
+ await player.destroy();
1670
+ } catch {
1671
+ }
1672
+ if (playerRef.current === player) playerRef.current = null;
1673
+ return;
1674
+ }
1644
1675
  await player.load(manifestUrl);
1645
1676
  onPlayerReady?.(player);
1646
1677
  return player;
@@ -1660,6 +1691,8 @@ var useShakaPlayer = ({
1660
1691
  const playerInstance = playerRef.current;
1661
1692
  if (playerInstance) {
1662
1693
  try {
1694
+ isDestroyingRef.current = true;
1695
+ activeInitIdRef.current = null;
1663
1696
  if (videoElementRef.current) {
1664
1697
  try {
1665
1698
  if (waitingForKeyHandlerRef.current) {
@@ -1687,7 +1720,21 @@ var useShakaPlayer = ({
1687
1720
  } finally {
1688
1721
  drmExpirationHandlerRef.current = null;
1689
1722
  }
1690
- await playerInstance.destroy();
1723
+ const performDestroy = async () => {
1724
+ try {
1725
+ await playerInstance.destroy();
1726
+ } finally {
1727
+ try {
1728
+ if (videoElementRef.current) {
1729
+ videoElementRef.current.removeAttribute("src");
1730
+ videoElementRef.current.load();
1731
+ }
1732
+ } catch {
1733
+ }
1734
+ }
1735
+ };
1736
+ destroyInProgressRef.current = performDestroy();
1737
+ await destroyInProgressRef.current;
1691
1738
  } catch (error) {
1692
1739
  console.warn("Error destroying Shaka Player:", error);
1693
1740
  } finally {
@@ -1695,6 +1742,8 @@ var useShakaPlayer = ({
1695
1742
  playerRef.current = null;
1696
1743
  }
1697
1744
  storedPersistentThisLoadRef.current = false;
1745
+ isDestroyingRef.current = false;
1746
+ destroyInProgressRef.current = null;
1698
1747
  }
1699
1748
  }
1700
1749
  }, [playerRef]);