Remove Fibers from meteor-tools:

- Adapt Profile to run with both sync and async functions.
This commit is contained in:
Matheus Castro
2022-12-20 19:20:56 -03:00
parent c88b4f76d9
commit 855ce036ec

View File

@@ -282,22 +282,7 @@ export function Profile<
export namespace Profile {
export let enabled = !! process.env.METEOR_PROFILE;
export function time<TResult>(bucket: string, f: () => TResult) {
return Profile(bucket, f)();
}
export async function run<TResult>(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<TResult>(bucket: string, f: () => TResult) {
return Profile(bucket, f)();
}
export function run<TResult>(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;