Skip to content

Commit

Permalink
Improve debugging console logging with traces
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinVandy committed Dec 27, 2024
1 parent 89f551b commit 1d385bc
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 19 deletions.
2 changes: 1 addition & 1 deletion packages/match-sorter-utils/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"extends": "../../tsconfig.json",
"include": ["src", "vite.config.ts"]
"include": ["src", "vite.config.ts", "tests"]
}
2 changes: 1 addition & 1 deletion packages/react-table/eslint.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// @ts-check

import pluginReact from '@eslint-react/eslint-plugin'
//// @ts-expect-error
// @ts-expect-error
// import pluginReactCompiler from 'eslint-plugin-react-compiler'
import pluginReactHooks from 'eslint-plugin-react-hooks'
import rootConfig from '../../eslint.config.js'
Expand Down
2 changes: 1 addition & 1 deletion packages/react-table/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
"compilerOptions": {
"jsx": "react"
},
"include": ["src"]
"include": ["src", "eslint.config.js", "vite.config.ts", "tests"]
}
2 changes: 1 addition & 1 deletion packages/react-table/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const config = defineConfig({
dir: './tests',
watch: false,
environment: 'jsdom',
setupFiles: ['./tests/test-setup.ts'],
// setupFiles: ['./tests/test-setup.ts'],
globals: true,
},
})
Expand Down
29 changes: 16 additions & 13 deletions packages/table-core/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,29 +132,32 @@ export function tableMemo<TDeps extends ReadonlyArray<any>, TDepArgs, TResult>({
let afterCompareTime: number
let startCalcTime: number
let endCalcTime: number
let isFirstRun = true
let runCount = 0

function logTime(time: number, depsChanged: boolean) {
if (isDev) {
const runType = isFirstRun
? '(1st run)'
: depsChanged
? '(rerun)'
: '(cache)'
isFirstRun = false
const runType =
runCount === 0
? '(1st run)'
: depsChanged
? '(rerun #' + runCount + ')'
: '(cache)'
runCount++

console.info(
`%c⏱ ${pad(`${time.toFixed(time < 1 ? 2 : 1)} ms`, 12)} %c${runType}%c ${fnName}%c ${objectId ?? ''}`,
console.groupCollapsed(
`%c⏱ ${pad(`${time.toFixed(1)} ms`, 12)} %c${runType}%c ${fnName}%c ${objectId ? `(${fnName.split('.')[0]}Id: ${objectId})` : ''}`,
`font-size: .6rem; font-weight: bold; ${
depsChanged
? `color: hsl(
${Math.max(0, Math.min(120 - Math.log10(time) * 60, 120))}deg 100% 31%);`
: ''
} `,
'color: #FF69B4',
'color: gray',
`color: ${runCount < 2 ? '#FF00FF' : '#FF1493'}`,
'color: #666',
'color: #87CEEB',
)
console.trace()
console.groupEnd()
}
}

Expand All @@ -169,7 +172,7 @@ export function tableMemo<TDeps extends ReadonlyArray<any>, TDepArgs, TResult>({
if (debugCache) {
afterCompareTime = performance.now()
const compareTime =
Math.round((afterCompareTime - beforeCompareTime) * 1000) / 1000
Math.round((afterCompareTime - beforeCompareTime) * 100) / 100
if (!depsChanged) {
logTime(compareTime, depsChanged)
}
Expand All @@ -184,7 +187,7 @@ export function tableMemo<TDeps extends ReadonlyArray<any>, TDepArgs, TResult>({
if (debug) {
endCalcTime = performance.now()
const executionTime =
Math.round((endCalcTime - startCalcTime) * 1000) / 1000
Math.round((endCalcTime - startCalcTime) * 100) / 100
logTime(executionTime, true)
}
queueMicrotask(() => onAfterUpdate?.())
Expand Down
1 change: 0 additions & 1 deletion packages/vue-table/eslint.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// @ts-check

//@ts-expect-error
import pluginVue from 'eslint-plugin-vue'
import rootConfig from '../../eslint.config.js'

Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"target": "ES2020"
},
"include": [
"prettier.config.cjs",
"prettier.config.js",
"scripts",
"eslint.config.js",
"vitest.workspace.js"
Expand Down

0 comments on commit 1d385bc

Please sign in to comment.