mirror of
https://github.com/jlfwong/speedscope.git
synced 2024-11-22 22:14:25 +03:00
25 lines
750 B
TypeScript
25 lines
750 B
TypeScript
export function atMostOnceAFrame<F extends Function>(fn: F) {
|
|
let frameRequest: number | null = null
|
|
function ret(...args: any[]) {
|
|
if (frameRequest == null) {
|
|
frameRequest = requestAnimationFrame(function () {
|
|
fn(...args)
|
|
frameRequest = null
|
|
})
|
|
}
|
|
}
|
|
return ret as any as F
|
|
}
|
|
|
|
export function lastOf<T>(ts: T[]): T | null {
|
|
return ts[ts.length-1] || null
|
|
}
|
|
|
|
// NOTE: This blindly assumes the same result across contexts.
|
|
const measureTextCache = new Map<string, number>()
|
|
export function cachedMeasureTextWidth(ctx: CanvasRenderingContext2D, text: string): number {
|
|
if (!measureTextCache.has(text)) {
|
|
measureTextCache.set(text, ctx.measureText(text).width)
|
|
}
|
|
return measureTextCache.get(text)!
|
|
} |