@thumbmarkjs/thumbmarkjs 1.7.0 → 1.7.2

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/src/utils/log.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import { componentInterface } from '../factory';
2
2
  import { optionsInterface, DEFAULT_API_ENDPOINT } from '../options';
3
3
  import { getVersion } from './version';
4
+ import type { ThumbmarkError } from '../functions';
4
5
 
5
6
  // ===================== Logging (Internal) =====================
6
7
 
@@ -9,7 +10,7 @@ import { getVersion } from './version';
9
10
  * You can disable this by setting options.logging to false.
10
11
  * @internal
11
12
  */
12
- export async function logThumbmarkData(thisHash: string, thumbmarkData: componentInterface, options: optionsInterface, experimentalData: componentInterface = {}): Promise<void> {
13
+ export async function logThumbmarkData(thisHash: string, thumbmarkData: componentInterface, options: optionsInterface, experimentalData: componentInterface = {}, errors: ThumbmarkError[] = []): Promise<void> {
13
14
  const apiEndpoint = DEFAULT_API_ENDPOINT;
14
15
  const url = `${apiEndpoint}/log`;
15
16
  const payload = {
@@ -19,6 +20,7 @@ export async function logThumbmarkData(thisHash: string, thumbmarkData: componen
19
20
  version: getVersion(),
20
21
  options,
21
22
  path: window?.location?.pathname,
23
+ ...(errors.length > 0 && { errors }),
22
24
  };
23
25
 
24
26
  sessionStorage.setItem("_tmjs_l", "1");
@@ -0,0 +1,86 @@
1
+ import { raceAllPerformance, raceAll } from './raceAll';
2
+
3
+ describe('raceAll', () => {
4
+ test('returns resolved values when promises fulfill', async () => {
5
+ const results = await raceAll(
6
+ [Promise.resolve('ok')],
7
+ 50,
8
+ 'timeout'
9
+ );
10
+
11
+ expect(results).toHaveLength(1);
12
+ expect(results[0]).toBe('ok');
13
+ });
14
+
15
+ test('returns timeout fallback when promise does not settle in time', async () => {
16
+ const neverResolves = new Promise<string>(() => { });
17
+ const results = await raceAll(
18
+ [neverResolves],
19
+ 10,
20
+ 'timeout'
21
+ );
22
+
23
+ expect(results).toHaveLength(1);
24
+ expect(results[0]).toBe('timeout');
25
+ });
26
+
27
+ test('does not reject the whole batch when one promise rejects', async () => {
28
+ const rejects = Promise.reject(new Error('component failed'));
29
+ const resolves = Promise.resolve('ok');
30
+
31
+ const results = await raceAll(
32
+ [rejects, resolves],
33
+ 50,
34
+ 'timeout'
35
+ );
36
+
37
+ expect(results).toHaveLength(2);
38
+ expect(results[0]).toBe('timeout');
39
+ expect(results[1]).toBe('ok');
40
+ });
41
+ });
42
+
43
+ describe('raceAllPerformance', () => {
44
+ test('returns resolved values when promises fulfill', async () => {
45
+ const results = await raceAllPerformance(
46
+ [Promise.resolve('ok')],
47
+ 50,
48
+ 'timeout'
49
+ );
50
+
51
+ expect(results).toHaveLength(1);
52
+ expect(results[0].value).toBe('ok');
53
+ expect(typeof results[0].elapsed).toBe('number');
54
+ expect(results[0].error).toBeUndefined();
55
+ });
56
+
57
+ test('returns timeout fallback when promise does not settle in time', async () => {
58
+ const neverResolves = new Promise<string>(() => { });
59
+ const results = await raceAllPerformance(
60
+ [neverResolves],
61
+ 10,
62
+ 'timeout'
63
+ );
64
+
65
+ expect(results).toHaveLength(1);
66
+ expect(results[0].value).toBe('timeout');
67
+ expect(results[0].error).toBe('timeout');
68
+ });
69
+
70
+ test('does not reject the whole batch when one promise rejects', async () => {
71
+ const rejects = Promise.reject(new Error('component failed'));
72
+ const resolves = Promise.resolve('ok');
73
+
74
+ const results = await raceAllPerformance(
75
+ [rejects, resolves],
76
+ 50,
77
+ 'timeout'
78
+ );
79
+
80
+ expect(results).toHaveLength(2);
81
+ expect(results[0].value).toBe('timeout');
82
+ expect(results[0].error).toBe('component failed');
83
+ expect(results[1].value).toBe('ok');
84
+ expect(results[1].error).toBeUndefined();
85
+ });
86
+ });
@@ -10,6 +10,7 @@ export function delay<T>(t: number, val: T): DelayedPromise<T> {
10
10
  export interface RaceResult<T> {
11
11
  value: T;
12
12
  elapsed?: number;
13
+ error?: string;
13
14
  }
14
15
 
15
16
  export function raceAllPerformance<T>(
@@ -24,10 +25,15 @@ export function raceAllPerformance<T>(
24
25
  p.then((value) => ({
25
26
  value,
26
27
  elapsed: performance.now() - startTime,
28
+ })).catch((err: unknown) => ({
29
+ value: timeoutVal,
30
+ elapsed: performance.now() - startTime,
31
+ error: err instanceof Error ? err.message : String(err),
27
32
  })),
28
33
  delay(timeoutTime, timeoutVal).then((value) => ({
29
34
  value,
30
35
  elapsed: performance.now() - startTime,
36
+ error: 'timeout' as string,
31
37
  })),
32
38
  ]);
33
39
  })
@@ -38,6 +44,6 @@ export function raceAllPerformance<T>(
38
44
 
39
45
  export function raceAll<T>(promises: Promise<T>[], timeoutTime: number, timeoutVal: T): Promise<(T | undefined)[]> {
40
46
  return Promise.all(promises.map((p) => {
41
- return Promise.race([p, delay(timeoutTime, timeoutVal)]);
47
+ return Promise.race([p.catch(() => timeoutVal), delay(timeoutTime, timeoutVal)]);
42
48
  }));
43
- }
49
+ }