From 1cf2cab3fdc25301f6a5276ebb40fec719808fc4 Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Sat, 20 Dec 2025 20:26:44 -0400 Subject: quickstat (mayhaps) --- src/quickstat.js | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 src/quickstat.js (limited to 'src/quickstat.js') diff --git a/src/quickstat.js b/src/quickstat.js new file mode 100644 index 00000000..e8714b2a --- /dev/null +++ b/src/quickstat.js @@ -0,0 +1,52 @@ +import * as fs from 'node:fs/promises'; +import * as path from 'node:path'; + +export const requested = {}; +export const recorded = {}; + +export function track(pathArg, { + readdir = false, + stat = false, +}) { + pathArg = path.resolve(pathArg); + requested[pathArg] = {readdir, stat}; +} + +export function tracking(pathArg, resolved = false) { + if (!resolved) pathArg = path.resolve(pathArg); + for (const key of Object.keys(requested)) { + if (pathArg.startsWith(key + '/')) { + return requested[key]; + } + } + + return null; +} + +async function go(fn, key, pathArg) { + pathArg = path.resolve(pathArg); + + const info = tracking(pathArg, true); + if (!info?.[key]) return fn(pathArg); + + const record = recorded[pathArg]?.[key]; + if (record) return record; + + const result = await fn(pathArg); + recorded[pathArg] ??= {}; + recorded[pathArg][key] = result; + return result; +} + +export function stat(pathArg) { + return go(fs.stat, 'stat', pathArg); +} + +export function readdir(pathArg) { + return go(fs.readdir, 'readdir', pathArg); +} + +export function reset() { + for (const key in requested) delete requested[key]; + for (const key in recorded) delete recorded[key]; +} \ No newline at end of file -- cgit 1.3.0-6-gf8a5