@f8team/reel-core 1.0.0 → 1.0.3

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
@@ -111,16 +111,16 @@ function createPluginBus({ player, host }) {
111
111
  if (findIndex(plugin.name) >= 0) {
112
112
  throw new Error(`[@f8team/reel-core] plugin "${plugin.name}" is already registered`);
113
113
  }
114
- let teardown = null;
114
+ let setupResult;
115
115
  try {
116
- const result = plugin.setup(player, host);
117
- teardown = typeof result === "function" ? result : null;
116
+ setupResult = plugin.setup(player, host);
118
117
  } catch (err) {
119
118
  throw new Error(
120
119
  `[@f8team/reel-core] plugin "${plugin.name}" setup threw: ${err.message}`,
121
120
  { cause: err }
122
121
  );
123
122
  }
123
+ const teardown = typeof setupResult === "function" ? setupResult : null;
124
124
  entries.push({ plugin, teardown });
125
125
  }
126
126
  function unregister(name) {
@@ -1208,6 +1208,10 @@ function createPlayer(options = {}, internal = {}) {
1208
1208
  let disposed = false;
1209
1209
  let initialOptionsApplied = false;
1210
1210
  let pendingPlay = false;
1211
+ const YT_PLAYER_STATE_PLAYING = 1;
1212
+ function getYouTubePlayer() {
1213
+ return activeLoader?._instance?.() ?? null;
1214
+ }
1211
1215
  const registry = createSourceRegistry();
1212
1216
  function emitUnauthorized(payload) {
1213
1217
  bus.emit("unauthorized", payload);
@@ -1261,6 +1265,20 @@ function createPlayer(options = {}, internal = {}) {
1261
1265
  onDuration: (duration) => {
1262
1266
  store.setState({ duration });
1263
1267
  bus.emit("durationchange", { duration });
1268
+ },
1269
+ onStateChange: (ytState) => {
1270
+ if (ytState === "playing") {
1271
+ bus.emit("play");
1272
+ if (status !== "playing" && status !== "loading") dispatch({ type: "play" });
1273
+ } else if (ytState === "paused") {
1274
+ bus.emit("pause");
1275
+ if (status === "playing") dispatch({ type: "pause" });
1276
+ } else if (ytState === "ended") {
1277
+ bus.emit("ended");
1278
+ if (status !== "ended") dispatch({ type: "ended" });
1279
+ } else if (ytState === "buffering") {
1280
+ bus.emit("buffering", { isBuffering: true });
1281
+ }
1264
1282
  }
1265
1283
  })
1266
1284
  );
@@ -1386,6 +1404,14 @@ function createPlayer(options = {}, internal = {}) {
1386
1404
  }
1387
1405
  function runMediaPlay() {
1388
1406
  if (!video) return;
1407
+ const yt = getYouTubePlayer();
1408
+ if (yt) {
1409
+ try {
1410
+ yt.playVideo();
1411
+ } catch {
1412
+ }
1413
+ return;
1414
+ }
1389
1415
  const result = video.play();
1390
1416
  if (result && typeof result.then === "function") {
1391
1417
  result.catch((err) => {
@@ -1398,6 +1424,14 @@ function createPlayer(options = {}, internal = {}) {
1398
1424
  }
1399
1425
  function runMediaPause() {
1400
1426
  if (!video) return;
1427
+ const yt = getYouTubePlayer();
1428
+ if (yt) {
1429
+ try {
1430
+ yt.pauseVideo();
1431
+ } catch {
1432
+ }
1433
+ return;
1434
+ }
1401
1435
  try {
1402
1436
  video.pause();
1403
1437
  } catch {
@@ -1405,6 +1439,20 @@ function createPlayer(options = {}, internal = {}) {
1405
1439
  }
1406
1440
  function runMediaSeek(seconds) {
1407
1441
  if (!video) return;
1442
+ const yt = getYouTubePlayer();
1443
+ if (yt) {
1444
+ try {
1445
+ yt.seekTo(seconds, true);
1446
+ store.setState({ currentTime: seconds });
1447
+ bus.emit("timeupdate", {
1448
+ currentTime: seconds,
1449
+ playedSeconds: seconds,
1450
+ duration: store.getState().duration
1451
+ });
1452
+ } catch {
1453
+ }
1454
+ return;
1455
+ }
1408
1456
  try {
1409
1457
  video.currentTime = seconds;
1410
1458
  } catch {
@@ -1434,6 +1482,7 @@ function createPlayer(options = {}, internal = {}) {
1434
1482
  bus.emit("durationchange", { duration });
1435
1483
  });
1436
1484
  on2("timeupdate", () => {
1485
+ if (getYouTubePlayer()) return;
1437
1486
  const currentTime = target.currentTime;
1438
1487
  store.setState({ currentTime, buffered: bufferedToRanges(target.buffered) });
1439
1488
  bus.emit("timeupdate", {
@@ -1650,6 +1699,14 @@ function createPlayer(options = {}, internal = {}) {
1650
1699
  }
1651
1700
  function paused() {
1652
1701
  if (!video) return true;
1702
+ const yt = getYouTubePlayer();
1703
+ if (yt) {
1704
+ try {
1705
+ return yt.getPlayerState() !== YT_PLAYER_STATE_PLAYING;
1706
+ } catch {
1707
+ return true;
1708
+ }
1709
+ }
1653
1710
  return video.paused;
1654
1711
  }
1655
1712
  function seekTo(seconds) {
@@ -1657,6 +1714,20 @@ function createPlayer(options = {}, internal = {}) {
1657
1714
  if (!video) return;
1658
1715
  const duration = Number.isFinite(video.duration) ? video.duration : Number.POSITIVE_INFINITY;
1659
1716
  const clamped = Math.max(0, Math.min(seconds, duration));
1717
+ const yt = getYouTubePlayer();
1718
+ if (yt) {
1719
+ try {
1720
+ yt.seekTo(clamped, true);
1721
+ store.setState({ currentTime: clamped });
1722
+ bus.emit("timeupdate", {
1723
+ currentTime: clamped,
1724
+ playedSeconds: clamped,
1725
+ duration: store.getState().duration
1726
+ });
1727
+ } catch {
1728
+ }
1729
+ return;
1730
+ }
1660
1731
  video.currentTime = clamped;
1661
1732
  }
1662
1733
  function syncVolumeMutedStoreFromVideo() {