@metamask/multichain-account-service
Version:
Service to manage multichain accounts
65 lines • 2.22 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.withLocalPerfTrace = exports.tick = exports.isPerfEnabled = exports.log = void 0;
const logger_1 = require("../logger.cjs");
const timer_1 = require("./timer.cjs");
exports.log = (0, logger_1.createModuleLogger)(logger_1.projectLogger, 'perf');
/**
* Returns true when DEBUG=metamask:multichain-account-service, DEBUG=metamask:multichain-account-service:perf
* or a matching glob is set.
* Re-uses the same enable/disable logic as the rest of the package loggers.
*
* @returns True if performance logging is enabled, false otherwise.
*/
function isPerfEnabled() {
return logger_1.projectLogger.enabled || exports.log.enabled;
}
exports.isPerfEnabled = isPerfEnabled;
/**
* Starts a local performance timer. Returns a `tock` function that, when called,
* logs the elapsed time for `label`.
*
* @example
* ```ts
* const tock = tick(request);
* await createAccounts(...);
* tock(); // logs: "${request.name}: 123.45ms"
* ```
*
* @param request - A trace request object containing the name and optional data.
* @returns A function that, when called, logs the elapsed time since `tick` was called.
*/
function tick(request) {
if (!isPerfEnabled()) {
return () => undefined;
}
const start = (0, timer_1.now)();
return function tock() {
const duration = (0, timer_1.now)() - start;
const context = request.data ? ` (${JSON.stringify(request.data)})` : '';
(0, exports.log)(`${request.name}${context}: ${duration.toFixed(2)}ms`);
};
}
exports.tick = tick;
/**
* Wraps a trace callback with local performance logging.
*
* @param trace - The original trace callback to wrap.
* @returns A new trace callback that logs the duration of the traced operation.
*/
function withLocalPerfTrace(trace) {
return async (request, fn) => {
if (!isPerfEnabled()) {
return await trace(request, fn);
}
const tock = tick(request);
try {
return await trace(request, fn);
}
finally {
tock();
}
};
}
exports.withLocalPerfTrace = withLocalPerfTrace;
//# sourceMappingURL=perf.cjs.map