Files
bitvid/js/utils/cardSourceVisibility.js
T
thePR0M3TH3ANandClaude Opus 4.8 ccbe8d9bb2 feat(liveness): config-driven card visibility policy + probe prefetch (#5)
Makes the card liveness-visibility behavior switchable so it can be A/B
feel-tested, and probes ahead of the scroll for a snappier result.

- CARD_LIVENESS_POLICY (config/instance-config.js): "show-pending" (default,
  unchanged) | "hide-foreign" (foreign/ingested cards hidden until a source
  verifies; native cards stay show-pending) | "hide-all". Owner's own cards are
  always visible. Flip live for testing without a rebuild:
  window.__BITVID_CARD_LIVENESS_POLICY__ = "hide-foreign" then refresh/scroll.
  Implemented via a data-foreign provenance flag on the card +
  getCardLivenessPolicy() in cardSourceVisibility.js.
- LIVENESS_PROBE_PREFETCH_MARGIN (default 600px): both liveness probes were
  already viewport-gated (IntersectionObserver, on-screen-first, WebTorrent
  priority-queued + concurrency-capped); this expands their rootMargin so cards
  just below the fold verify before the user reaches them.
- Tests: three policy scenarios in tests/video-card-source-visibility.test.mjs;
  also un-broke 3 pre-existing VideoCard tests by adding the missing
  IntersectionObserver JSDOM stub.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
2026-06-25 09:13:52 -04:00

104 lines
3.1 KiB
JavaScript

import { getCardLivenessPolicy } from "../constants.js";
function resolveCardElement(cardLike) {
if (!cardLike) {
return null;
}
const isElement =
typeof cardLike === "object" && cardLike !== null && cardLike.nodeType === 1;
if (isElement) {
const element = cardLike;
if (typeof element.closest === "function") {
if (element.classList?.contains("card")) {
return element;
}
return element.closest(".card");
}
return element.classList?.contains("card") ? element : null;
}
if (
typeof HTMLElement !== "undefined" &&
cardLike instanceof HTMLElement &&
typeof cardLike.closest === "function"
) {
if (cardLike.classList.contains("card")) {
return cardLike;
}
return cardLike.closest(".card");
}
if (typeof cardLike === "object" && cardLike !== null) {
if (typeof cardLike.card === "object" && cardLike.card?.nodeType === 1) {
return resolveCardElement(cardLike.card);
}
if (typeof cardLike.root === "object" && cardLike.root?.nodeType === 1) {
return resolveCardElement(cardLike.root);
}
}
return null;
}
export function updateVideoCardSourceVisibility(cardLike) {
const card = resolveCardElement(cardLike);
if (!card) {
return;
}
if (card.dataset.ownerIsViewer === "true") {
if (card.hidden) {
card.hidden = false;
}
if (card.dataset.sourceVisibility !== "visible") {
card.dataset.sourceVisibility = "visible";
}
return;
}
const cdnState = (card.dataset.urlHealthState || "").toLowerCase();
const streamState = (card.dataset.streamHealthState || "").toLowerCase();
const cdnPending = !cdnState || cdnState === "checking";
const streamPending = !streamState || streamState === "checking";
const cdnHealthy = cdnState === "healthy";
const streamHealthy = streamState === "healthy";
// Liveness visibility policy (config/instance-config.js → CARD_LIVENESS_POLICY):
// show-pending : show now, hide only once every source is confirmed dead.
// hide-foreign : foreign/ingested cards stay hidden until a source is proven
// playable; native cards keep show-pending.
// hide-all : every non-owner card stays hidden until proven playable.
const policy = getCardLivenessPolicy();
const isForeign = card.dataset.foreign === "true";
const hideUntilVerified =
policy === "hide-all" || (policy === "hide-foreign" && isForeign);
const shouldHide = hideUntilVerified
? // Hidden until at least one source proves playable (covers the pending
// window too, so a dead foreign stranger never flashes in).
!cdnHealthy && !streamHealthy
: // Default: only hide once every source has come back dead.
!cdnHealthy && !streamHealthy && !cdnPending && !streamPending;
if (shouldHide) {
if (!card.hidden) {
card.hidden = true;
}
if (card.dataset.sourceVisibility !== "hidden") {
card.dataset.sourceVisibility = "hidden";
}
return;
}
if (card.hidden) {
card.hidden = false;
}
if (card.dataset.sourceVisibility !== "visible") {
card.dataset.sourceVisibility = "visible";
}
}