@nordcraft/core 2.0.3 → 2.0.5

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,9 @@
1
1
  const globalScope: any = typeof globalThis !== 'undefined' ? globalThis : window
2
- globalScope.__nc_measure_max_depth = 10
2
+ globalScope.__nc_measure_max_depth =
3
+ typeof sessionStorage !== 'undefined' &&
4
+ sessionStorage.getItem('__nc_measure_max_depth')
5
+ ? parseInt(sessionStorage.getItem('__nc_measure_max_depth')!)
6
+ : 10
3
7
  globalScope.__nc_measure_enabled =
4
8
  typeof sessionStorage !== 'undefined' &&
5
9
  sessionStorage.getItem('__nc_measure') === 'true'
@@ -7,8 +11,10 @@ globalScope.__nc_measure_enabled =
7
11
  globalScope.__nc_enableMeasure = (enabled = true, maxDepth = 10) => {
8
12
  if (enabled) {
9
13
  sessionStorage.setItem('__nc_measure', 'true')
14
+ sessionStorage.setItem('__nc_measure_max_depth', String(maxDepth))
10
15
  } else {
11
16
  sessionStorage.removeItem('__nc_measure')
17
+ sessionStorage.removeItem('__nc_measure_max_depth')
12
18
  }
13
19
  globalScope.__nc_measure_enabled = enabled
14
20
  globalScope.__nc_measure_max_depth = maxDepth
@@ -18,17 +24,25 @@ let measureCount = 0
18
24
  const STACK: string[] = []
19
25
  const NOOP = () => {}
20
26
 
21
- export const measure = (key: string, details: Record<string, unknown>) => {
27
+ export const measure = (
28
+ key: string,
29
+ details: Record<string, unknown>,
30
+ type?: 'component',
31
+ ) => {
22
32
  if (!globalScope.__nc_measure_enabled) {
23
33
  return NOOP
24
34
  }
25
35
 
26
36
  const selfIndex = measureCount++
37
+ const selfStackSize = STACK.length
38
+ if (selfStackSize >= globalScope.__nc_measure_max_depth) {
39
+ return NOOP
40
+ }
27
41
  if (STACK.length >= globalScope.__nc_measure_max_depth) {
28
42
  return NOOP
29
43
  }
30
44
 
31
- const start = performance.now()
45
+ const start = performance.now() + selfStackSize * 0.001
32
46
  STACK.push(key)
33
47
 
34
48
  let _stopped = false
@@ -38,7 +52,7 @@ export const measure = (key: string, details: Record<string, unknown>) => {
38
52
  }
39
53
 
40
54
  _stopped = true
41
- const end = performance.now()
55
+ const end = performance.now() + selfStackSize * 0.001
42
56
  const mergedDetails = extraDetails
43
57
  ? { ...details, ...extraDetails }
44
58
  : details
@@ -50,6 +64,7 @@ export const measure = (key: string, details: Record<string, unknown>) => {
50
64
  devtools: {
51
65
  dataType: 'track-entry',
52
66
  track: 'Nordcraft devtools',
67
+ color: COLOR_MAP[type as keyof typeof COLOR_MAP],
53
68
  properties: [
54
69
  ...Object.entries(mergedDetails).map(([k, v]) => [k, String(v)]),
55
70
  ['Stack', STACK.join(' > ')],
@@ -63,3 +78,7 @@ export const measure = (key: string, details: Record<string, unknown>) => {
63
78
  STACK.pop()
64
79
  }
65
80
  }
81
+
82
+ const COLOR_MAP = {
83
+ component: 'secondary',
84
+ }