@pacem/pacem-core 2.0.0-kepler → 2.0.0-lyapunov

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.
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * @pacem/pacem-core v2.0.0-kepler (https://js.pacem.it)
2
+ * @pacem/pacem-core v2.0.0-lyapunov (https://js.pacem.it)
3
3
  * Pacem (https://pacem.it)
4
4
  * Licensed under Apache-2.0
5
5
  */
@@ -1991,8 +1991,17 @@
1991
1991
  }
1992
1992
  /** Theming-related helpers. */
1993
1993
  static { this.Theme = {
1994
- /** Downloads the web fonts referenced by the current theme's `--{@link PCSS}-font-*` CSS custom properties, resolving once at least one font face is ready (or after a short timeout). */
1994
+ /**
1995
+ * Downloads the web fonts referenced by the current theme's `--{@link PCSS}-font-*` CSS custom properties,
1996
+ * resolving once they are loaded and at least one font face is ready. Never rejects, never waits forever:
1997
+ * a font that fails to load is skipped, and the whole wait is capped (fonts refine a page, they must not
1998
+ * keep it hidden).
1999
+ */
1995
2000
  importWebFonts() {
2001
+ /** The longest wait (ms) for the fonts: then it resolves anyway. */
2002
+ const WEB_FONTS_TIMEOUT = 3000;
2003
+ /** The polls (every 250ms) for a first font face, before relying on `document.fonts.ready` anyway. */
2004
+ const FONT_FACE_CHECKS = 4;
1996
2005
  function cleanupQuotes(input) {
1997
2006
  // check if the string is wrapped around quotes to escape ';' chars
1998
2007
  if (/^('|").*\1$/.test(input)) {
@@ -2003,30 +2012,32 @@
2003
2012
  function downloadFont(fontFace) {
2004
2013
  return Utils.isNullOrEmpty(fontFace) ? Promise.resolve() : document.fonts.load('1rem ' + fontFace);
2005
2014
  }
2006
- const promises = ['icons', 'std', 'main', 'tech', 'btn'].map(f => {
2015
+ function importFont(f) {
2007
2016
  const fontFace = Utils.Css.getVariableValue(`--${PCSS}-font-${f}`).split(',')[0];
2008
- const url = cleanupQuotes(Utils.Css.getVariableValue(`--${PCSS}-font-${f}-api-url`));
2017
+ const apiUrl = Utils.Css.getVariableValue(`--${PCSS}-font-${f}-api-url`);
2018
+ const url = cleanupQuotes(apiUrl);
2009
2019
  if (Utils.isNullOrEmpty(url)) {
2010
2020
  return downloadFont(fontFace);
2011
2021
  }
2012
- else {
2013
- return CustomElementUtils.importcss(url).then(_ => downloadFont(fontFace));
2014
- }
2015
- });
2022
+ return CustomElementUtils.importcss(url).then(_ => downloadFont(fontFace));
2023
+ }
2024
+ // (a failing font is skipped: one of them must not hold the others)
2025
+ const promises = ['icons', 'std', 'main', 'tech', 'btn'].map(f => importFont(f).catch(_ => undefined));
2016
2026
  const atLeast1FontFaceReady = new Promise((resolve) => {
2017
- function check(count = 0) {
2027
+ function check(count) {
2018
2028
  // size against the specs: https://developer.mozilla.org/en-US/docs/Web/API/FontFaceSet/size#specifications
2019
- if (count > 4 || !('size' in document.fonts) || typeof document.fonts.size === 'number' && document.fonts.size > 0) {
2029
+ if (count > FONT_FACE_CHECKS || !('size' in document.fonts) || typeof document.fonts.size === 'number' && document.fonts.size > 0) {
2020
2030
  document.fonts.ready.then(resolve);
2031
+ return;
2021
2032
  }
2022
- else {
2023
- setTimeout(check, 250, count++);
2024
- }
2033
+ setTimeout(check, 250, count + 1);
2025
2034
  }
2026
- check();
2035
+ check(0);
2027
2036
  });
2028
2037
  promises.push(atLeast1FontFaceReady);
2029
- return Promise.all(promises);
2038
+ const timeout = new Promise(resolve => setTimeout(resolve, WEB_FONTS_TIMEOUT));
2039
+ const fontsReady = Promise.all(promises);
2040
+ return Promise.race([fontsReady, timeout]);
2030
2041
  }
2031
2042
  }; }
2032
2043
  /**