From 855ce036ec119330e04b70abf0ebf1bd7eb8ebab Mon Sep 17 00:00:00 2001 From: Matheus Castro Date: Tue, 20 Dec 2022 19:20:56 -0300 Subject: [PATCH] Remove Fibers from meteor-tools: - Adapt Profile to run with both sync and async functions. --- tools/tool-env/profile.ts | 53 +++++++++++++++++++++++++++------------ 1 file changed, 37 insertions(+), 16 deletions(-) diff --git a/tools/tool-env/profile.ts b/tools/tool-env/profile.ts index 1ba2e70d59..4b2d501c3e 100644 --- a/tools/tool-env/profile.ts +++ b/tools/tool-env/profile.ts @@ -282,22 +282,7 @@ export function Profile< export namespace Profile { export let enabled = !! process.env.METEOR_PROFILE; - export function time(bucket: string, f: () => TResult) { - return Profile(bucket, f)(); - } - - export async function run(bucket: string, f: () => TResult) { - if (! Profile.enabled) { - return f(); - } - - if (running) { - // We've kept the calls to Profile.run in the tool disjoint so far, - // and should probably keep doing so, but if we mess up, warn and continue. - console.log("Warning: Nested Profile.run at " + bucket); - return await time(bucket, f); - } - + async function _runAsync(bucket, f) { runningName = bucket; print(`(#${reportNum}) Profiling: ${runningName}`); start(); @@ -309,6 +294,42 @@ export namespace Profile { } } + function _runSync(bucket, f) { + runningName = bucket; + print(`(#${reportNum}) Profiling: ${runningName}`); + start(); + try { + return time(bucket, f); + } finally { + report(); + reportNum++; + } + } + + export function time(bucket: string, f: () => TResult) { + return Profile(bucket, f)(); + } + + export function run(bucket: string, f: () => TResult) { + if (! Profile.enabled) { + return f(); + } + + if (running) { + // We've kept the calls to Profile.run in the tool disjoint so far, + // and should probably keep doing so, but if we mess up, warn and continue. + console.log("Warning: Nested Profile.run at " + bucket); + return time(bucket, f); + } + + const isAsyncFn = f.constructor.name === "AsyncFunction"; + if (!isAsyncFn) { + return _runSync(bucket, f); + } + + return _runAsync(bucket, f); + } + function start() { bucketStats = {}; running = true;