Skip to content

Commit

Permalink
add debug message
Browse files Browse the repository at this point in the history
  • Loading branch information
uenoB committed Mar 28, 2024
1 parent 6f2154c commit b5d971c
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 1 deletion.
6 changes: 6 additions & 0 deletions packages/page/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,13 @@
"keywords": [
"vite-plugin-minissg"
],
"dependencies": {
"debug": "^4.3.4"
},
"peerDependencies": {
"vite-plugin-minissg": "^3.0.0"
},
"devDependencies": {
"@types/debug": "^4.1.12"
}
}
38 changes: 38 additions & 0 deletions packages/page/src/debug.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import createDebug from 'debug'
import type { Awaitable } from '../../vite-plugin-minissg/src/util'

const d = createDebug('minissg:page')
export const debug = d.enabled ? d : undefined

const sleep = (msec: number): PromiseLike<undefined> =>
new Promise(resolve => {
setTimeout(() => {
resolve(undefined)
}, msec)
})

const debugTimerPeriod = 3000

const debugTimerImpl = <X>(
task: () => PromiseLike<X>,
callback: (
debug: typeof d,
dt: number,
when: 'start' | 'middle' | 'end'
) => Awaitable<void>
): PromiseLike<X> => {
const promise = task().then(x => ({ done: x }))
const t1 = performance.now()
return Promise.resolve(callback(d, 0, 'start')).then(async () => {
for (;;) {
const result = await Promise.race([promise, sleep(debugTimerPeriod)])
const t2 = performance.now()
await callback(d, t2 - t1, result != null ? 'end' : 'middle')
if (result != null) return result.done
}
})
}

export const debugTimer = d.enabled
? debugTimerImpl
: <X>(f: () => PromiseLike<X>) => f()
13 changes: 12 additions & 1 deletion packages/page/src/tree.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { AsyncLocalStorage } from 'node:async_hooks'
import type * as minissg from '../../vite-plugin-minissg/src/module'
import { type Awaitable, raise } from '../../vite-plugin-minissg/src/util'
import { debugTimer } from './debug'
import { isAbsURL, defineProperty, createObject, objectAssign } from './util'
import { type Delay, delay } from './delay'
import { Memo } from './memo'
Expand Down Expand Up @@ -329,7 +330,17 @@ class TreeNodeImpl<Base, This extends Base, Impl> {
const render1 = async (): R => await this.module.render(mod)
const render2 = async (): R => await currentNode.run(this, render1)
const render3 = async (): R => await this.memo.run(render2)
return { default: delay(render3) }
const render = debugTimer(render3, (debug, dt, when) => {
const path = `/${c.moduleName.path}`
if (when === 'start') {
debug('start rendering %s', path)
} else if (when === 'middle') {
debug('now rendering %s (+%s sec)', path, (dt / 1000).toFixed(3))
} else {
debug('rendering %s finished (%s sec)', path, (dt / 1000).toFixed(3))
}
})
return { default: delay(render) }
}
}

Expand Down
7 changes: 7 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b5d971c

Please sign in to comment.