You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

59 lines
1.6 KiB

"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.profileMethod = void 0;
function _chalk() {
const data = _interopRequireDefault(require("chalk"));
_chalk = function () {
return data;
};
return data;
}
function _getenv() {
const data = require("getenv");
_getenv = function () {
return data;
};
return data;
}
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
const isProfiling = (0, _getenv().boolish)('EXPO_PROFILE', false);
// eslint-disable-next-line no-console
const consoleTime = isProfiling ? console.time : () => {};
// eslint-disable-next-line no-console
const consoleTimeEnd = isProfiling ? console.timeEnd : () => {};
/**
* Wrap a method and profile the time it takes to execute the method using `EXPO_PROFILE`.
* Works best with named functions (i.e. not arrow functions).
*
* @param fn
* @param functionName
*/
const profileMethod = (fn, functionName) => {
const name = _chalk().default.dim(`⏱ [profile] ${functionName !== null && functionName !== void 0 ? functionName : fn.name || 'unknown'}`);
return (...args) => {
consoleTime(name);
const results = fn(...args);
if (results instanceof Promise) {
// @ts-ignore
return new Promise((resolve, reject) => {
results.then(results => {
resolve(results);
consoleTimeEnd(name);
}).catch(error => {
reject(error);
consoleTimeEnd(name);
});
});
} else {
consoleTimeEnd(name);
}
return results;
};
};
exports.profileMethod = profileMethod;
//# sourceMappingURL=profileMethod.js.map