@newtonedev/components 0.1.9 → 0.1.10

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@newtonedev/components",
3
- "version": "0.1.9",
3
+ "version": "0.1.10",
4
4
  "description": "React + React Native Web component library for Newtone",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -5,10 +5,19 @@
5
5
  const REF_STRING =
6
6
  'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 ';
7
7
 
8
+ /** Race a promise against a timeout. Resolves with fallback if the promise doesn't settle in time. */
9
+ function withTimeout<T>(promise: Promise<T>, ms: number, fallback: T): Promise<T> {
10
+ return Promise.race([
11
+ promise,
12
+ new Promise<T>((resolve) => setTimeout(() => resolve(fallback), ms)),
13
+ ]);
14
+ }
15
+
8
16
  /**
9
17
  * Measure the average character width ratio for a font using the Canvas API.
10
18
  *
11
- * Waits for the font to load via `document.fonts.load()` before measuring.
19
+ * Waits for the font to load via `document.fonts.load()` before measuring,
20
+ * with a 3-second timeout to prevent hangs when fonts fail to load silently.
12
21
  * Falls back to 0.55 if the browser context is unavailable, the font fails
13
22
  * to load, or canvas is not supported.
14
23
  *
@@ -29,7 +38,11 @@ export async function measureAvgCharWidth(
29
38
  ): Promise<number> {
30
39
  if (typeof document === 'undefined') return 0.55;
31
40
  try {
32
- await document.fonts.load(`${fontWeight} ${fontSize}px "${fontFamily}"`);
41
+ await withTimeout(
42
+ document.fonts.load(`${fontWeight} ${fontSize}px "${fontFamily}"`),
43
+ 3000,
44
+ [] as FontFace[],
45
+ );
33
46
  const canvas = document.createElement('canvas');
34
47
  const ctx = canvas.getContext('2d');
35
48
  if (!ctx) return 0.55;