@pixldocs/canvas-renderer 0.5.14 → 0.5.15

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
@@ -2637,6 +2637,19 @@ const FONTS_TO_PRELOAD = [
2637
2637
  ];
2638
2638
  let fontsLoaded = false;
2639
2639
  let fontsLoadingPromise = null;
2640
+ const withFontTimeout = async (promise, timeoutMs = 4e3) => {
2641
+ let timeoutId;
2642
+ try {
2643
+ return await Promise.race([
2644
+ promise,
2645
+ new Promise((resolve) => {
2646
+ timeoutId = setTimeout(resolve, timeoutMs);
2647
+ })
2648
+ ]);
2649
+ } finally {
2650
+ if (timeoutId) clearTimeout(timeoutId);
2651
+ }
2652
+ };
2640
2653
  const preloadFont = async (fontFamily) => {
2641
2654
  try {
2642
2655
  if (document.fonts) {
@@ -2657,11 +2670,11 @@ const preloadAllFonts = async () => {
2657
2670
  }
2658
2671
  fontsLoadingPromise = (async () => {
2659
2672
  if (document.fonts) {
2660
- await document.fonts.ready;
2673
+ await withFontTimeout(document.fonts.ready, 2500);
2661
2674
  }
2662
- await Promise.all(FONTS_TO_PRELOAD.map((font) => preloadFont(font)));
2675
+ await withFontTimeout(Promise.all(FONTS_TO_PRELOAD.map((font) => preloadFont(font))).then(() => void 0), 5e3);
2663
2676
  if (document.fonts) {
2664
- await document.fonts.ready;
2677
+ await withFontTimeout(document.fonts.ready, 2500);
2665
2678
  }
2666
2679
  fontsLoaded = true;
2667
2680
  })();
@@ -2669,7 +2682,7 @@ const preloadAllFonts = async () => {
2669
2682
  };
2670
2683
  const waitForFontsReady = async () => {
2671
2684
  if (!document.fonts) return;
2672
- await document.fonts.ready;
2685
+ await withFontTimeout(document.fonts.ready, 2500);
2673
2686
  };
2674
2687
  const DEFAULT_FONT_CHECK_TIMEOUT_MS = 3500;
2675
2688
  const FONT_CHECK_POLL_MS = 60;
@@ -11675,6 +11688,19 @@ function normalizeFontFamily(fontStack) {
11675
11688
  }
11676
11689
  const loadedFonts = /* @__PURE__ */ new Set();
11677
11690
  const loadingPromises = /* @__PURE__ */ new Map();
11691
+ async function withTimeout(promise, timeoutMs = 4e3) {
11692
+ let timeoutId;
11693
+ try {
11694
+ return await Promise.race([
11695
+ promise,
11696
+ new Promise((resolve) => {
11697
+ timeoutId = setTimeout(resolve, timeoutMs);
11698
+ })
11699
+ ]);
11700
+ } finally {
11701
+ if (timeoutId) clearTimeout(timeoutId);
11702
+ }
11703
+ }
11678
11704
  async function loadGoogleFontCSS(rawFontFamily) {
11679
11705
  if (!rawFontFamily || typeof document === "undefined") return;
11680
11706
  const fontFamily = normalizeFontFamily(rawFontFamily);
@@ -11696,8 +11722,8 @@ async function loadGoogleFontCSS(rawFontFamily) {
11696
11722
  document.head.appendChild(link);
11697
11723
  });
11698
11724
  if (document.fonts) {
11699
- await document.fonts.load(`16px "${fontFamily}"`);
11700
- await document.fonts.ready;
11725
+ await withTimeout(document.fonts.load(`16px "${fontFamily}"`), 2500);
11726
+ await withTimeout(document.fonts.ready, 2500);
11701
11727
  }
11702
11728
  loadedFonts.add(fontFamily);
11703
11729
  } catch (e) {
@@ -11810,7 +11836,7 @@ async function ensureFontsForResolvedConfig(config) {
11810
11836
  if (typeof document === "undefined") return;
11811
11837
  const descriptors = collectFontDescriptorsFromConfig(config);
11812
11838
  const families = new Set(descriptors.map((d) => d.family));
11813
- await Promise.all([...families].map((f) => loadGoogleFontCSS(f)));
11839
+ await withTimeout(Promise.all([...families].map((f) => loadGoogleFontCSS(f))), 8e3);
11814
11840
  if (document.fonts) {
11815
11841
  const loadPromises = descriptors.map((d) => {
11816
11842
  const stylePrefix = d.style === "italic" ? "italic " : "";
@@ -11819,8 +11845,8 @@ async function ensureFontsForResolvedConfig(config) {
11819
11845
  return document.fonts.load(spec).catch(() => {
11820
11846
  });
11821
11847
  });
11822
- await Promise.all(loadPromises);
11823
- await document.fonts.ready;
11848
+ await withTimeout(Promise.all(loadPromises), 8e3);
11849
+ await withTimeout(document.fonts.ready, 2500);
11824
11850
  }
11825
11851
  }
11826
11852
  function PixldocsPreview(props) {